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.
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"
27 #include <sys/ioctl.h>
28 #include <sys/socket.h>
31 #include "standard-headers/linux/vhost_types.h"
34 #include <linux/userfaultfd.h>
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_INFLIGHT_SHMFD
= 12,
60 VHOST_USER_PROTOCOL_F_MAX
63 #define VHOST_USER_PROTOCOL_FEATURE_MASK ((1 << VHOST_USER_PROTOCOL_F_MAX) - 1)
65 typedef enum VhostUserRequest
{
67 VHOST_USER_GET_FEATURES
= 1,
68 VHOST_USER_SET_FEATURES
= 2,
69 VHOST_USER_SET_OWNER
= 3,
70 VHOST_USER_RESET_OWNER
= 4,
71 VHOST_USER_SET_MEM_TABLE
= 5,
72 VHOST_USER_SET_LOG_BASE
= 6,
73 VHOST_USER_SET_LOG_FD
= 7,
74 VHOST_USER_SET_VRING_NUM
= 8,
75 VHOST_USER_SET_VRING_ADDR
= 9,
76 VHOST_USER_SET_VRING_BASE
= 10,
77 VHOST_USER_GET_VRING_BASE
= 11,
78 VHOST_USER_SET_VRING_KICK
= 12,
79 VHOST_USER_SET_VRING_CALL
= 13,
80 VHOST_USER_SET_VRING_ERR
= 14,
81 VHOST_USER_GET_PROTOCOL_FEATURES
= 15,
82 VHOST_USER_SET_PROTOCOL_FEATURES
= 16,
83 VHOST_USER_GET_QUEUE_NUM
= 17,
84 VHOST_USER_SET_VRING_ENABLE
= 18,
85 VHOST_USER_SEND_RARP
= 19,
86 VHOST_USER_NET_SET_MTU
= 20,
87 VHOST_USER_SET_SLAVE_REQ_FD
= 21,
88 VHOST_USER_IOTLB_MSG
= 22,
89 VHOST_USER_SET_VRING_ENDIAN
= 23,
90 VHOST_USER_GET_CONFIG
= 24,
91 VHOST_USER_SET_CONFIG
= 25,
92 VHOST_USER_CREATE_CRYPTO_SESSION
= 26,
93 VHOST_USER_CLOSE_CRYPTO_SESSION
= 27,
94 VHOST_USER_POSTCOPY_ADVISE
= 28,
95 VHOST_USER_POSTCOPY_LISTEN
= 29,
96 VHOST_USER_POSTCOPY_END
= 30,
97 VHOST_USER_GET_INFLIGHT_FD
= 31,
98 VHOST_USER_SET_INFLIGHT_FD
= 32,
99 VHOST_USER_GPU_SET_SOCKET
= 33,
103 typedef enum VhostUserSlaveRequest
{
104 VHOST_USER_SLAVE_NONE
= 0,
105 VHOST_USER_SLAVE_IOTLB_MSG
= 1,
106 VHOST_USER_SLAVE_CONFIG_CHANGE_MSG
= 2,
107 VHOST_USER_SLAVE_VRING_HOST_NOTIFIER_MSG
= 3,
109 } VhostUserSlaveRequest
;
111 typedef struct VhostUserMemoryRegion
{
112 uint64_t guest_phys_addr
;
113 uint64_t memory_size
;
114 uint64_t userspace_addr
;
115 uint64_t mmap_offset
;
116 } VhostUserMemoryRegion
;
118 typedef struct VhostUserMemory
{
121 VhostUserMemoryRegion regions
[VHOST_MEMORY_MAX_NREGIONS
];
124 typedef struct VhostUserLog
{
126 uint64_t mmap_offset
;
129 typedef struct VhostUserConfig
{
133 uint8_t region
[VHOST_USER_MAX_CONFIG_SIZE
];
136 #define VHOST_CRYPTO_SYM_HMAC_MAX_KEY_LEN 512
137 #define VHOST_CRYPTO_SYM_CIPHER_MAX_KEY_LEN 64
139 typedef struct VhostUserCryptoSession
{
140 /* session id for success, -1 on errors */
142 CryptoDevBackendSymSessionInfo session_setup_data
;
143 uint8_t key
[VHOST_CRYPTO_SYM_CIPHER_MAX_KEY_LEN
];
144 uint8_t auth_key
[VHOST_CRYPTO_SYM_HMAC_MAX_KEY_LEN
];
145 } VhostUserCryptoSession
;
147 static VhostUserConfig c
__attribute__ ((unused
));
148 #define VHOST_USER_CONFIG_HDR_SIZE (sizeof(c.offset) \
152 typedef struct VhostUserVringArea
{
156 } VhostUserVringArea
;
158 typedef struct VhostUserInflight
{
160 uint64_t mmap_offset
;
166 VhostUserRequest request
;
168 #define VHOST_USER_VERSION_MASK (0x3)
169 #define VHOST_USER_REPLY_MASK (0x1<<2)
170 #define VHOST_USER_NEED_REPLY_MASK (0x1 << 3)
172 uint32_t size
; /* the following payload size */
173 } QEMU_PACKED VhostUserHeader
;
176 #define VHOST_USER_VRING_IDX_MASK (0xff)
177 #define VHOST_USER_VRING_NOFD_MASK (0x1<<8)
179 struct vhost_vring_state state
;
180 struct vhost_vring_addr addr
;
181 VhostUserMemory memory
;
183 struct vhost_iotlb_msg iotlb
;
184 VhostUserConfig config
;
185 VhostUserCryptoSession session
;
186 VhostUserVringArea area
;
187 VhostUserInflight inflight
;
190 typedef struct VhostUserMsg
{
192 VhostUserPayload payload
;
193 } QEMU_PACKED VhostUserMsg
;
195 static VhostUserMsg m
__attribute__ ((unused
));
196 #define VHOST_USER_HDR_SIZE (sizeof(VhostUserHeader))
198 #define VHOST_USER_PAYLOAD_SIZE (sizeof(VhostUserPayload))
200 /* The version of the protocol we support */
201 #define VHOST_USER_VERSION (0x1)
204 struct vhost_dev
*dev
;
205 /* Shared between vhost devs of the same virtio device */
206 VhostUserState
*user
;
208 NotifierWithReturn postcopy_notifier
;
209 struct PostCopyFD postcopy_fd
;
210 uint64_t postcopy_client_bases
[VHOST_MEMORY_MAX_NREGIONS
];
211 /* Length of the region_rb and region_rb_offset arrays */
212 size_t region_rb_len
;
213 /* RAMBlock associated with a given region */
214 RAMBlock
**region_rb
;
215 /* The offset from the start of the RAMBlock to the start of the
218 ram_addr_t
*region_rb_offset
;
220 /* True once we've entered postcopy_listen */
221 bool postcopy_listen
;
224 static bool ioeventfd_enabled(void)
226 return !kvm_enabled() || kvm_eventfds_enabled();
229 static int vhost_user_read_header(struct vhost_dev
*dev
, VhostUserMsg
*msg
)
231 struct vhost_user
*u
= dev
->opaque
;
232 CharBackend
*chr
= u
->user
->chr
;
233 uint8_t *p
= (uint8_t *) msg
;
234 int r
, size
= VHOST_USER_HDR_SIZE
;
236 r
= qemu_chr_fe_read_all(chr
, p
, size
);
238 error_report("Failed to read msg header. Read %d instead of %d."
239 " Original request %d.", r
, size
, msg
->hdr
.request
);
243 /* validate received flags */
244 if (msg
->hdr
.flags
!= (VHOST_USER_REPLY_MASK
| VHOST_USER_VERSION
)) {
245 error_report("Failed to read msg header."
246 " Flags 0x%x instead of 0x%x.", msg
->hdr
.flags
,
247 VHOST_USER_REPLY_MASK
| VHOST_USER_VERSION
);
254 static int vhost_user_read(struct vhost_dev
*dev
, VhostUserMsg
*msg
)
256 struct vhost_user
*u
= dev
->opaque
;
257 CharBackend
*chr
= u
->user
->chr
;
258 uint8_t *p
= (uint8_t *) msg
;
261 if (vhost_user_read_header(dev
, msg
) < 0) {
265 /* validate message size is sane */
266 if (msg
->hdr
.size
> VHOST_USER_PAYLOAD_SIZE
) {
267 error_report("Failed to read msg header."
268 " Size %d exceeds the maximum %zu.", msg
->hdr
.size
,
269 VHOST_USER_PAYLOAD_SIZE
);
274 p
+= VHOST_USER_HDR_SIZE
;
275 size
= msg
->hdr
.size
;
276 r
= qemu_chr_fe_read_all(chr
, p
, size
);
278 error_report("Failed to read msg payload."
279 " Read %d instead of %d.", r
, msg
->hdr
.size
);
287 static int process_message_reply(struct vhost_dev
*dev
,
288 const VhostUserMsg
*msg
)
290 VhostUserMsg msg_reply
;
292 if ((msg
->hdr
.flags
& VHOST_USER_NEED_REPLY_MASK
) == 0) {
296 if (vhost_user_read(dev
, &msg_reply
) < 0) {
300 if (msg_reply
.hdr
.request
!= msg
->hdr
.request
) {
301 error_report("Received unexpected msg type."
302 "Expected %d received %d",
303 msg
->hdr
.request
, msg_reply
.hdr
.request
);
307 return msg_reply
.payload
.u64
? -1 : 0;
310 static bool vhost_user_one_time_request(VhostUserRequest request
)
313 case VHOST_USER_SET_OWNER
:
314 case VHOST_USER_RESET_OWNER
:
315 case VHOST_USER_SET_MEM_TABLE
:
316 case VHOST_USER_GET_QUEUE_NUM
:
317 case VHOST_USER_NET_SET_MTU
:
324 /* most non-init callers ignore the error */
325 static int vhost_user_write(struct vhost_dev
*dev
, VhostUserMsg
*msg
,
326 int *fds
, int fd_num
)
328 struct vhost_user
*u
= dev
->opaque
;
329 CharBackend
*chr
= u
->user
->chr
;
330 int ret
, size
= VHOST_USER_HDR_SIZE
+ msg
->hdr
.size
;
333 * For non-vring specific requests, like VHOST_USER_SET_MEM_TABLE,
334 * we just need send it once in the first time. For later such
335 * request, we just ignore it.
337 if (vhost_user_one_time_request(msg
->hdr
.request
) && dev
->vq_index
!= 0) {
338 msg
->hdr
.flags
&= ~VHOST_USER_NEED_REPLY_MASK
;
342 if (qemu_chr_fe_set_msgfds(chr
, fds
, fd_num
) < 0) {
343 error_report("Failed to set msg fds.");
347 ret
= qemu_chr_fe_write_all(chr
, (const uint8_t *) msg
, size
);
349 error_report("Failed to write msg."
350 " Wrote %d instead of %d.", ret
, size
);
357 int vhost_user_gpu_set_socket(struct vhost_dev
*dev
, int fd
)
360 .hdr
.request
= VHOST_USER_GPU_SET_SOCKET
,
361 .hdr
.flags
= VHOST_USER_VERSION
,
364 return vhost_user_write(dev
, &msg
, &fd
, 1);
367 static int vhost_user_set_log_base(struct vhost_dev
*dev
, uint64_t base
,
368 struct vhost_log
*log
)
370 int fds
[VHOST_MEMORY_MAX_NREGIONS
];
372 bool shmfd
= virtio_has_feature(dev
->protocol_features
,
373 VHOST_USER_PROTOCOL_F_LOG_SHMFD
);
375 .hdr
.request
= VHOST_USER_SET_LOG_BASE
,
376 .hdr
.flags
= VHOST_USER_VERSION
,
377 .payload
.log
.mmap_size
= log
->size
* sizeof(*(log
->log
)),
378 .payload
.log
.mmap_offset
= 0,
379 .hdr
.size
= sizeof(msg
.payload
.log
),
382 if (shmfd
&& log
->fd
!= -1) {
383 fds
[fd_num
++] = log
->fd
;
386 if (vhost_user_write(dev
, &msg
, fds
, fd_num
) < 0) {
392 if (vhost_user_read(dev
, &msg
) < 0) {
396 if (msg
.hdr
.request
!= VHOST_USER_SET_LOG_BASE
) {
397 error_report("Received unexpected msg type. "
398 "Expected %d received %d",
399 VHOST_USER_SET_LOG_BASE
, msg
.hdr
.request
);
407 static int vhost_user_set_mem_table_postcopy(struct vhost_dev
*dev
,
408 struct vhost_memory
*mem
)
410 struct vhost_user
*u
= dev
->opaque
;
411 int fds
[VHOST_MEMORY_MAX_NREGIONS
];
414 VhostUserMsg msg_reply
;
418 .hdr
.request
= VHOST_USER_SET_MEM_TABLE
,
419 .hdr
.flags
= VHOST_USER_VERSION
,
422 if (u
->region_rb_len
< dev
->mem
->nregions
) {
423 u
->region_rb
= g_renew(RAMBlock
*, u
->region_rb
, dev
->mem
->nregions
);
424 u
->region_rb_offset
= g_renew(ram_addr_t
, u
->region_rb_offset
,
426 memset(&(u
->region_rb
[u
->region_rb_len
]), '\0',
427 sizeof(RAMBlock
*) * (dev
->mem
->nregions
- u
->region_rb_len
));
428 memset(&(u
->region_rb_offset
[u
->region_rb_len
]), '\0',
429 sizeof(ram_addr_t
) * (dev
->mem
->nregions
- u
->region_rb_len
));
430 u
->region_rb_len
= dev
->mem
->nregions
;
433 for (i
= 0; i
< dev
->mem
->nregions
; ++i
) {
434 struct vhost_memory_region
*reg
= dev
->mem
->regions
+ i
;
438 assert((uintptr_t)reg
->userspace_addr
== reg
->userspace_addr
);
439 mr
= memory_region_from_host((void *)(uintptr_t)reg
->userspace_addr
,
441 fd
= memory_region_get_fd(mr
);
443 trace_vhost_user_set_mem_table_withfd(fd_num
, mr
->name
,
445 reg
->guest_phys_addr
,
446 reg
->userspace_addr
, offset
);
447 u
->region_rb_offset
[i
] = offset
;
448 u
->region_rb
[i
] = mr
->ram_block
;
449 msg
.payload
.memory
.regions
[fd_num
].userspace_addr
=
451 msg
.payload
.memory
.regions
[fd_num
].memory_size
= reg
->memory_size
;
452 msg
.payload
.memory
.regions
[fd_num
].guest_phys_addr
=
453 reg
->guest_phys_addr
;
454 msg
.payload
.memory
.regions
[fd_num
].mmap_offset
= offset
;
455 assert(fd_num
< VHOST_MEMORY_MAX_NREGIONS
);
458 u
->region_rb_offset
[i
] = 0;
459 u
->region_rb
[i
] = NULL
;
463 msg
.payload
.memory
.nregions
= fd_num
;
466 error_report("Failed initializing vhost-user memory map, "
467 "consider using -object memory-backend-file share=on");
471 msg
.hdr
.size
= sizeof(msg
.payload
.memory
.nregions
);
472 msg
.hdr
.size
+= sizeof(msg
.payload
.memory
.padding
);
473 msg
.hdr
.size
+= fd_num
* sizeof(VhostUserMemoryRegion
);
475 if (vhost_user_write(dev
, &msg
, fds
, fd_num
) < 0) {
479 if (vhost_user_read(dev
, &msg_reply
) < 0) {
483 if (msg_reply
.hdr
.request
!= VHOST_USER_SET_MEM_TABLE
) {
484 error_report("%s: Received unexpected msg type."
485 "Expected %d received %d", __func__
,
486 VHOST_USER_SET_MEM_TABLE
, msg_reply
.hdr
.request
);
489 /* We're using the same structure, just reusing one of the
490 * fields, so it should be the same size.
492 if (msg_reply
.hdr
.size
!= msg
.hdr
.size
) {
493 error_report("%s: Unexpected size for postcopy reply "
494 "%d vs %d", __func__
, msg_reply
.hdr
.size
, msg
.hdr
.size
);
498 memset(u
->postcopy_client_bases
, 0,
499 sizeof(uint64_t) * VHOST_MEMORY_MAX_NREGIONS
);
501 /* They're in the same order as the regions that were sent
502 * but some of the regions were skipped (above) if they
505 for (msg_i
= 0, region_i
= 0;
506 region_i
< dev
->mem
->nregions
;
508 if (msg_i
< fd_num
&&
509 msg_reply
.payload
.memory
.regions
[msg_i
].guest_phys_addr
==
510 dev
->mem
->regions
[region_i
].guest_phys_addr
) {
511 u
->postcopy_client_bases
[region_i
] =
512 msg_reply
.payload
.memory
.regions
[msg_i
].userspace_addr
;
513 trace_vhost_user_set_mem_table_postcopy(
514 msg_reply
.payload
.memory
.regions
[msg_i
].userspace_addr
,
515 msg
.payload
.memory
.regions
[msg_i
].userspace_addr
,
520 if (msg_i
!= fd_num
) {
521 error_report("%s: postcopy reply not fully consumed "
523 __func__
, msg_i
, fd_num
);
526 /* Now we've registered this with the postcopy code, we ack to the client,
527 * because now we're in the position to be able to deal with any faults
530 /* TODO: Use this for failure cases as well with a bad value */
531 msg
.hdr
.size
= sizeof(msg
.payload
.u64
);
532 msg
.payload
.u64
= 0; /* OK */
533 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
540 static int vhost_user_set_mem_table(struct vhost_dev
*dev
,
541 struct vhost_memory
*mem
)
543 struct vhost_user
*u
= dev
->opaque
;
544 int fds
[VHOST_MEMORY_MAX_NREGIONS
];
547 bool do_postcopy
= u
->postcopy_listen
&& u
->postcopy_fd
.handler
;
548 bool reply_supported
= virtio_has_feature(dev
->protocol_features
,
549 VHOST_USER_PROTOCOL_F_REPLY_ACK
);
552 /* Postcopy has enough differences that it's best done in it's own
555 return vhost_user_set_mem_table_postcopy(dev
, mem
);
559 .hdr
.request
= VHOST_USER_SET_MEM_TABLE
,
560 .hdr
.flags
= VHOST_USER_VERSION
,
563 if (reply_supported
) {
564 msg
.hdr
.flags
|= VHOST_USER_NEED_REPLY_MASK
;
567 for (i
= 0; i
< dev
->mem
->nregions
; ++i
) {
568 struct vhost_memory_region
*reg
= dev
->mem
->regions
+ i
;
572 assert((uintptr_t)reg
->userspace_addr
== reg
->userspace_addr
);
573 mr
= memory_region_from_host((void *)(uintptr_t)reg
->userspace_addr
,
575 fd
= memory_region_get_fd(mr
);
577 if (fd_num
== VHOST_MEMORY_MAX_NREGIONS
) {
578 error_report("Failed preparing vhost-user memory table msg");
581 msg
.payload
.memory
.regions
[fd_num
].userspace_addr
=
583 msg
.payload
.memory
.regions
[fd_num
].memory_size
= reg
->memory_size
;
584 msg
.payload
.memory
.regions
[fd_num
].guest_phys_addr
=
585 reg
->guest_phys_addr
;
586 msg
.payload
.memory
.regions
[fd_num
].mmap_offset
= offset
;
591 msg
.payload
.memory
.nregions
= fd_num
;
594 error_report("Failed initializing vhost-user memory map, "
595 "consider using -object memory-backend-file share=on");
599 msg
.hdr
.size
= sizeof(msg
.payload
.memory
.nregions
);
600 msg
.hdr
.size
+= sizeof(msg
.payload
.memory
.padding
);
601 msg
.hdr
.size
+= fd_num
* sizeof(VhostUserMemoryRegion
);
603 if (vhost_user_write(dev
, &msg
, fds
, fd_num
) < 0) {
607 if (reply_supported
) {
608 return process_message_reply(dev
, &msg
);
614 static int vhost_user_set_vring_addr(struct vhost_dev
*dev
,
615 struct vhost_vring_addr
*addr
)
618 .hdr
.request
= VHOST_USER_SET_VRING_ADDR
,
619 .hdr
.flags
= VHOST_USER_VERSION
,
620 .payload
.addr
= *addr
,
621 .hdr
.size
= sizeof(msg
.payload
.addr
),
624 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
631 static int vhost_user_set_vring_endian(struct vhost_dev
*dev
,
632 struct vhost_vring_state
*ring
)
634 bool cross_endian
= virtio_has_feature(dev
->protocol_features
,
635 VHOST_USER_PROTOCOL_F_CROSS_ENDIAN
);
637 .hdr
.request
= VHOST_USER_SET_VRING_ENDIAN
,
638 .hdr
.flags
= VHOST_USER_VERSION
,
639 .payload
.state
= *ring
,
640 .hdr
.size
= sizeof(msg
.payload
.state
),
644 error_report("vhost-user trying to send unhandled ioctl");
648 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
655 static int vhost_set_vring(struct vhost_dev
*dev
,
656 unsigned long int request
,
657 struct vhost_vring_state
*ring
)
660 .hdr
.request
= request
,
661 .hdr
.flags
= VHOST_USER_VERSION
,
662 .payload
.state
= *ring
,
663 .hdr
.size
= sizeof(msg
.payload
.state
),
666 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
673 static int vhost_user_set_vring_num(struct vhost_dev
*dev
,
674 struct vhost_vring_state
*ring
)
676 return vhost_set_vring(dev
, VHOST_USER_SET_VRING_NUM
, ring
);
679 static void vhost_user_host_notifier_restore(struct vhost_dev
*dev
,
682 struct vhost_user
*u
= dev
->opaque
;
683 VhostUserHostNotifier
*n
= &u
->user
->notifier
[queue_idx
];
684 VirtIODevice
*vdev
= dev
->vdev
;
686 if (n
->addr
&& !n
->set
) {
687 virtio_queue_set_host_notifier_mr(vdev
, queue_idx
, &n
->mr
, true);
692 static void vhost_user_host_notifier_remove(struct vhost_dev
*dev
,
695 struct vhost_user
*u
= dev
->opaque
;
696 VhostUserHostNotifier
*n
= &u
->user
->notifier
[queue_idx
];
697 VirtIODevice
*vdev
= dev
->vdev
;
699 if (n
->addr
&& n
->set
) {
700 virtio_queue_set_host_notifier_mr(vdev
, queue_idx
, &n
->mr
, false);
705 static int vhost_user_set_vring_base(struct vhost_dev
*dev
,
706 struct vhost_vring_state
*ring
)
708 vhost_user_host_notifier_restore(dev
, ring
->index
);
710 return vhost_set_vring(dev
, VHOST_USER_SET_VRING_BASE
, ring
);
713 static int vhost_user_set_vring_enable(struct vhost_dev
*dev
, int enable
)
717 if (!virtio_has_feature(dev
->features
, VHOST_USER_F_PROTOCOL_FEATURES
)) {
721 for (i
= 0; i
< dev
->nvqs
; ++i
) {
722 struct vhost_vring_state state
= {
723 .index
= dev
->vq_index
+ i
,
727 vhost_set_vring(dev
, VHOST_USER_SET_VRING_ENABLE
, &state
);
733 static int vhost_user_get_vring_base(struct vhost_dev
*dev
,
734 struct vhost_vring_state
*ring
)
737 .hdr
.request
= VHOST_USER_GET_VRING_BASE
,
738 .hdr
.flags
= VHOST_USER_VERSION
,
739 .payload
.state
= *ring
,
740 .hdr
.size
= sizeof(msg
.payload
.state
),
743 vhost_user_host_notifier_remove(dev
, ring
->index
);
745 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
749 if (vhost_user_read(dev
, &msg
) < 0) {
753 if (msg
.hdr
.request
!= VHOST_USER_GET_VRING_BASE
) {
754 error_report("Received unexpected msg type. Expected %d received %d",
755 VHOST_USER_GET_VRING_BASE
, msg
.hdr
.request
);
759 if (msg
.hdr
.size
!= sizeof(msg
.payload
.state
)) {
760 error_report("Received bad msg size.");
764 *ring
= msg
.payload
.state
;
769 static int vhost_set_vring_file(struct vhost_dev
*dev
,
770 VhostUserRequest request
,
771 struct vhost_vring_file
*file
)
773 int fds
[VHOST_MEMORY_MAX_NREGIONS
];
776 .hdr
.request
= request
,
777 .hdr
.flags
= VHOST_USER_VERSION
,
778 .payload
.u64
= file
->index
& VHOST_USER_VRING_IDX_MASK
,
779 .hdr
.size
= sizeof(msg
.payload
.u64
),
782 if (ioeventfd_enabled() && file
->fd
> 0) {
783 fds
[fd_num
++] = file
->fd
;
785 msg
.payload
.u64
|= VHOST_USER_VRING_NOFD_MASK
;
788 if (vhost_user_write(dev
, &msg
, fds
, fd_num
) < 0) {
795 static int vhost_user_set_vring_kick(struct vhost_dev
*dev
,
796 struct vhost_vring_file
*file
)
798 return vhost_set_vring_file(dev
, VHOST_USER_SET_VRING_KICK
, file
);
801 static int vhost_user_set_vring_call(struct vhost_dev
*dev
,
802 struct vhost_vring_file
*file
)
804 return vhost_set_vring_file(dev
, VHOST_USER_SET_VRING_CALL
, file
);
807 static int vhost_user_set_u64(struct vhost_dev
*dev
, int request
, uint64_t u64
)
810 .hdr
.request
= request
,
811 .hdr
.flags
= VHOST_USER_VERSION
,
813 .hdr
.size
= sizeof(msg
.payload
.u64
),
816 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
823 static int vhost_user_set_features(struct vhost_dev
*dev
,
826 return vhost_user_set_u64(dev
, VHOST_USER_SET_FEATURES
, features
);
829 static int vhost_user_set_protocol_features(struct vhost_dev
*dev
,
832 return vhost_user_set_u64(dev
, VHOST_USER_SET_PROTOCOL_FEATURES
, features
);
835 static int vhost_user_get_u64(struct vhost_dev
*dev
, int request
, uint64_t *u64
)
838 .hdr
.request
= request
,
839 .hdr
.flags
= VHOST_USER_VERSION
,
842 if (vhost_user_one_time_request(request
) && dev
->vq_index
!= 0) {
846 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
850 if (vhost_user_read(dev
, &msg
) < 0) {
854 if (msg
.hdr
.request
!= request
) {
855 error_report("Received unexpected msg type. Expected %d received %d",
856 request
, msg
.hdr
.request
);
860 if (msg
.hdr
.size
!= sizeof(msg
.payload
.u64
)) {
861 error_report("Received bad msg size.");
865 *u64
= msg
.payload
.u64
;
870 static int vhost_user_get_features(struct vhost_dev
*dev
, uint64_t *features
)
872 return vhost_user_get_u64(dev
, VHOST_USER_GET_FEATURES
, features
);
875 static int vhost_user_set_owner(struct vhost_dev
*dev
)
878 .hdr
.request
= VHOST_USER_SET_OWNER
,
879 .hdr
.flags
= VHOST_USER_VERSION
,
882 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
889 static int vhost_user_reset_device(struct vhost_dev
*dev
)
892 .hdr
.request
= VHOST_USER_RESET_OWNER
,
893 .hdr
.flags
= VHOST_USER_VERSION
,
896 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
903 static int vhost_user_slave_handle_config_change(struct vhost_dev
*dev
)
907 if (!dev
->config_ops
) {
911 if (dev
->config_ops
->vhost_dev_config_notifier
) {
912 ret
= dev
->config_ops
->vhost_dev_config_notifier(dev
);
918 static int vhost_user_slave_handle_vring_host_notifier(struct vhost_dev
*dev
,
919 VhostUserVringArea
*area
,
922 int queue_idx
= area
->u64
& VHOST_USER_VRING_IDX_MASK
;
923 size_t page_size
= qemu_real_host_page_size
;
924 struct vhost_user
*u
= dev
->opaque
;
925 VhostUserState
*user
= u
->user
;
926 VirtIODevice
*vdev
= dev
->vdev
;
927 VhostUserHostNotifier
*n
;
931 if (!virtio_has_feature(dev
->protocol_features
,
932 VHOST_USER_PROTOCOL_F_HOST_NOTIFIER
) ||
933 vdev
== NULL
|| queue_idx
>= virtio_get_num_queues(vdev
)) {
937 n
= &user
->notifier
[queue_idx
];
940 virtio_queue_set_host_notifier_mr(vdev
, queue_idx
, &n
->mr
, false);
941 object_unparent(OBJECT(&n
->mr
));
942 munmap(n
->addr
, page_size
);
946 if (area
->u64
& VHOST_USER_VRING_NOFD_MASK
) {
951 if (area
->size
!= page_size
) {
955 addr
= mmap(NULL
, page_size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
,
957 if (addr
== MAP_FAILED
) {
961 name
= g_strdup_printf("vhost-user/host-notifier@%p mmaps[%d]",
963 memory_region_init_ram_device_ptr(&n
->mr
, OBJECT(vdev
), name
,
967 if (virtio_queue_set_host_notifier_mr(vdev
, queue_idx
, &n
->mr
, true)) {
968 munmap(addr
, page_size
);
978 static void slave_read(void *opaque
)
980 struct vhost_dev
*dev
= opaque
;
981 struct vhost_user
*u
= dev
->opaque
;
982 VhostUserHeader hdr
= { 0, };
983 VhostUserPayload payload
= { 0, };
987 int fd
[VHOST_USER_SLAVE_MAX_FDS
];
988 char control
[CMSG_SPACE(sizeof(fd
))];
989 struct cmsghdr
*cmsg
;
992 memset(&msgh
, 0, sizeof(msgh
));
995 msgh
.msg_control
= control
;
996 msgh
.msg_controllen
= sizeof(control
);
998 memset(fd
, -1, sizeof(fd
));
1001 iov
.iov_base
= &hdr
;
1002 iov
.iov_len
= VHOST_USER_HDR_SIZE
;
1005 size
= recvmsg(u
->slave_fd
, &msgh
, 0);
1006 } while (size
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
1008 if (size
!= VHOST_USER_HDR_SIZE
) {
1009 error_report("Failed to read from slave.");
1013 if (msgh
.msg_flags
& MSG_CTRUNC
) {
1014 error_report("Truncated message.");
1018 for (cmsg
= CMSG_FIRSTHDR(&msgh
); cmsg
!= NULL
;
1019 cmsg
= CMSG_NXTHDR(&msgh
, cmsg
)) {
1020 if (cmsg
->cmsg_level
== SOL_SOCKET
&&
1021 cmsg
->cmsg_type
== SCM_RIGHTS
) {
1022 fdsize
= cmsg
->cmsg_len
- CMSG_LEN(0);
1023 memcpy(fd
, CMSG_DATA(cmsg
), fdsize
);
1028 if (hdr
.size
> VHOST_USER_PAYLOAD_SIZE
) {
1029 error_report("Failed to read msg header."
1030 " Size %d exceeds the maximum %zu.", hdr
.size
,
1031 VHOST_USER_PAYLOAD_SIZE
);
1037 size
= read(u
->slave_fd
, &payload
, hdr
.size
);
1038 } while (size
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
1040 if (size
!= hdr
.size
) {
1041 error_report("Failed to read payload from slave.");
1045 switch (hdr
.request
) {
1046 case VHOST_USER_SLAVE_IOTLB_MSG
:
1047 ret
= vhost_backend_handle_iotlb_msg(dev
, &payload
.iotlb
);
1049 case VHOST_USER_SLAVE_CONFIG_CHANGE_MSG
:
1050 ret
= vhost_user_slave_handle_config_change(dev
);
1052 case VHOST_USER_SLAVE_VRING_HOST_NOTIFIER_MSG
:
1053 ret
= vhost_user_slave_handle_vring_host_notifier(dev
, &payload
.area
,
1057 error_report("Received unexpected msg type.");
1061 /* Close the remaining file descriptors. */
1062 for (i
= 0; i
< fdsize
; i
++) {
1069 * REPLY_ACK feature handling. Other reply types has to be managed
1070 * directly in their request handlers.
1072 if (hdr
.flags
& VHOST_USER_NEED_REPLY_MASK
) {
1073 struct iovec iovec
[2];
1076 hdr
.flags
&= ~VHOST_USER_NEED_REPLY_MASK
;
1077 hdr
.flags
|= VHOST_USER_REPLY_MASK
;
1079 payload
.u64
= !!ret
;
1080 hdr
.size
= sizeof(payload
.u64
);
1082 iovec
[0].iov_base
= &hdr
;
1083 iovec
[0].iov_len
= VHOST_USER_HDR_SIZE
;
1084 iovec
[1].iov_base
= &payload
;
1085 iovec
[1].iov_len
= hdr
.size
;
1088 size
= writev(u
->slave_fd
, iovec
, ARRAY_SIZE(iovec
));
1089 } while (size
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
1091 if (size
!= VHOST_USER_HDR_SIZE
+ hdr
.size
) {
1092 error_report("Failed to send msg reply to slave.");
1100 qemu_set_fd_handler(u
->slave_fd
, NULL
, NULL
, NULL
);
1103 for (i
= 0; i
< fdsize
; i
++) {
1111 static int vhost_setup_slave_channel(struct vhost_dev
*dev
)
1113 VhostUserMsg msg
= {
1114 .hdr
.request
= VHOST_USER_SET_SLAVE_REQ_FD
,
1115 .hdr
.flags
= VHOST_USER_VERSION
,
1117 struct vhost_user
*u
= dev
->opaque
;
1119 bool reply_supported
= virtio_has_feature(dev
->protocol_features
,
1120 VHOST_USER_PROTOCOL_F_REPLY_ACK
);
1122 if (!virtio_has_feature(dev
->protocol_features
,
1123 VHOST_USER_PROTOCOL_F_SLAVE_REQ
)) {
1127 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, sv
) == -1) {
1128 error_report("socketpair() failed");
1132 u
->slave_fd
= sv
[0];
1133 qemu_set_fd_handler(u
->slave_fd
, slave_read
, NULL
, dev
);
1135 if (reply_supported
) {
1136 msg
.hdr
.flags
|= VHOST_USER_NEED_REPLY_MASK
;
1139 ret
= vhost_user_write(dev
, &msg
, &sv
[1], 1);
1144 if (reply_supported
) {
1145 ret
= process_message_reply(dev
, &msg
);
1151 qemu_set_fd_handler(u
->slave_fd
, NULL
, NULL
, NULL
);
1161 * Called back from the postcopy fault thread when a fault is received on our
1163 * TODO: This is Linux specific
1165 static int vhost_user_postcopy_fault_handler(struct PostCopyFD
*pcfd
,
1168 struct vhost_dev
*dev
= pcfd
->data
;
1169 struct vhost_user
*u
= dev
->opaque
;
1170 struct uffd_msg
*msg
= ufd
;
1171 uint64_t faultaddr
= msg
->arg
.pagefault
.address
;
1172 RAMBlock
*rb
= NULL
;
1176 trace_vhost_user_postcopy_fault_handler(pcfd
->idstr
, faultaddr
,
1177 dev
->mem
->nregions
);
1178 for (i
= 0; i
< MIN(dev
->mem
->nregions
, u
->region_rb_len
); i
++) {
1179 trace_vhost_user_postcopy_fault_handler_loop(i
,
1180 u
->postcopy_client_bases
[i
], dev
->mem
->regions
[i
].memory_size
);
1181 if (faultaddr
>= u
->postcopy_client_bases
[i
]) {
1182 /* Ofset of the fault address in the vhost region */
1183 uint64_t region_offset
= faultaddr
- u
->postcopy_client_bases
[i
];
1184 if (region_offset
< dev
->mem
->regions
[i
].memory_size
) {
1185 rb_offset
= region_offset
+ u
->region_rb_offset
[i
];
1186 trace_vhost_user_postcopy_fault_handler_found(i
,
1187 region_offset
, rb_offset
);
1188 rb
= u
->region_rb
[i
];
1189 return postcopy_request_shared_page(pcfd
, rb
, faultaddr
,
1194 error_report("%s: Failed to find region for fault %" PRIx64
,
1195 __func__
, faultaddr
);
1199 static int vhost_user_postcopy_waker(struct PostCopyFD
*pcfd
, RAMBlock
*rb
,
1202 struct vhost_dev
*dev
= pcfd
->data
;
1203 struct vhost_user
*u
= dev
->opaque
;
1206 trace_vhost_user_postcopy_waker(qemu_ram_get_idstr(rb
), offset
);
1211 /* Translate the offset into an address in the clients address space */
1212 for (i
= 0; i
< MIN(dev
->mem
->nregions
, u
->region_rb_len
); i
++) {
1213 if (u
->region_rb
[i
] == rb
&&
1214 offset
>= u
->region_rb_offset
[i
] &&
1215 offset
< (u
->region_rb_offset
[i
] +
1216 dev
->mem
->regions
[i
].memory_size
)) {
1217 uint64_t client_addr
= (offset
- u
->region_rb_offset
[i
]) +
1218 u
->postcopy_client_bases
[i
];
1219 trace_vhost_user_postcopy_waker_found(client_addr
);
1220 return postcopy_wake_shared(pcfd
, client_addr
, rb
);
1224 trace_vhost_user_postcopy_waker_nomatch(qemu_ram_get_idstr(rb
), offset
);
1230 * Called at the start of an inbound postcopy on reception of the
1233 static int vhost_user_postcopy_advise(struct vhost_dev
*dev
, Error
**errp
)
1236 struct vhost_user
*u
= dev
->opaque
;
1237 CharBackend
*chr
= u
->user
->chr
;
1239 VhostUserMsg msg
= {
1240 .hdr
.request
= VHOST_USER_POSTCOPY_ADVISE
,
1241 .hdr
.flags
= VHOST_USER_VERSION
,
1244 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
1245 error_setg(errp
, "Failed to send postcopy_advise to vhost");
1249 if (vhost_user_read(dev
, &msg
) < 0) {
1250 error_setg(errp
, "Failed to get postcopy_advise reply from vhost");
1254 if (msg
.hdr
.request
!= VHOST_USER_POSTCOPY_ADVISE
) {
1255 error_setg(errp
, "Unexpected msg type. Expected %d received %d",
1256 VHOST_USER_POSTCOPY_ADVISE
, msg
.hdr
.request
);
1261 error_setg(errp
, "Received bad msg size.");
1264 ufd
= qemu_chr_fe_get_msgfd(chr
);
1266 error_setg(errp
, "%s: Failed to get ufd", __func__
);
1269 qemu_set_nonblock(ufd
);
1271 /* register ufd with userfault thread */
1272 u
->postcopy_fd
.fd
= ufd
;
1273 u
->postcopy_fd
.data
= dev
;
1274 u
->postcopy_fd
.handler
= vhost_user_postcopy_fault_handler
;
1275 u
->postcopy_fd
.waker
= vhost_user_postcopy_waker
;
1276 u
->postcopy_fd
.idstr
= "vhost-user"; /* Need to find unique name */
1277 postcopy_register_shared_ufd(&u
->postcopy_fd
);
1280 error_setg(errp
, "Postcopy not supported on non-Linux systems");
1286 * Called at the switch to postcopy on reception of the 'listen' command.
1288 static int vhost_user_postcopy_listen(struct vhost_dev
*dev
, Error
**errp
)
1290 struct vhost_user
*u
= dev
->opaque
;
1292 VhostUserMsg msg
= {
1293 .hdr
.request
= VHOST_USER_POSTCOPY_LISTEN
,
1294 .hdr
.flags
= VHOST_USER_VERSION
| VHOST_USER_NEED_REPLY_MASK
,
1296 u
->postcopy_listen
= true;
1297 trace_vhost_user_postcopy_listen();
1298 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
1299 error_setg(errp
, "Failed to send postcopy_listen to vhost");
1303 ret
= process_message_reply(dev
, &msg
);
1305 error_setg(errp
, "Failed to receive reply to postcopy_listen");
1313 * Called at the end of postcopy
1315 static int vhost_user_postcopy_end(struct vhost_dev
*dev
, Error
**errp
)
1317 VhostUserMsg msg
= {
1318 .hdr
.request
= VHOST_USER_POSTCOPY_END
,
1319 .hdr
.flags
= VHOST_USER_VERSION
| VHOST_USER_NEED_REPLY_MASK
,
1322 struct vhost_user
*u
= dev
->opaque
;
1324 trace_vhost_user_postcopy_end_entry();
1325 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
1326 error_setg(errp
, "Failed to send postcopy_end to vhost");
1330 ret
= process_message_reply(dev
, &msg
);
1332 error_setg(errp
, "Failed to receive reply to postcopy_end");
1335 postcopy_unregister_shared_ufd(&u
->postcopy_fd
);
1336 close(u
->postcopy_fd
.fd
);
1337 u
->postcopy_fd
.handler
= NULL
;
1339 trace_vhost_user_postcopy_end_exit();
1344 static int vhost_user_postcopy_notifier(NotifierWithReturn
*notifier
,
1347 struct PostcopyNotifyData
*pnd
= opaque
;
1348 struct vhost_user
*u
= container_of(notifier
, struct vhost_user
,
1350 struct vhost_dev
*dev
= u
->dev
;
1352 switch (pnd
->reason
) {
1353 case POSTCOPY_NOTIFY_PROBE
:
1354 if (!virtio_has_feature(dev
->protocol_features
,
1355 VHOST_USER_PROTOCOL_F_PAGEFAULT
)) {
1356 /* TODO: Get the device name into this error somehow */
1357 error_setg(pnd
->errp
,
1358 "vhost-user backend not capable of postcopy");
1363 case POSTCOPY_NOTIFY_INBOUND_ADVISE
:
1364 return vhost_user_postcopy_advise(dev
, pnd
->errp
);
1366 case POSTCOPY_NOTIFY_INBOUND_LISTEN
:
1367 return vhost_user_postcopy_listen(dev
, pnd
->errp
);
1369 case POSTCOPY_NOTIFY_INBOUND_END
:
1370 return vhost_user_postcopy_end(dev
, pnd
->errp
);
1373 /* We ignore notifications we don't know */
1380 static int vhost_user_backend_init(struct vhost_dev
*dev
, void *opaque
)
1382 uint64_t features
, protocol_features
;
1383 struct vhost_user
*u
;
1386 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
1388 u
= g_new0(struct vhost_user
, 1);
1394 err
= vhost_user_get_features(dev
, &features
);
1399 if (virtio_has_feature(features
, VHOST_USER_F_PROTOCOL_FEATURES
)) {
1400 dev
->backend_features
|= 1ULL << VHOST_USER_F_PROTOCOL_FEATURES
;
1402 err
= vhost_user_get_u64(dev
, VHOST_USER_GET_PROTOCOL_FEATURES
,
1403 &protocol_features
);
1408 dev
->protocol_features
=
1409 protocol_features
& VHOST_USER_PROTOCOL_FEATURE_MASK
;
1411 if (!dev
->config_ops
|| !dev
->config_ops
->vhost_dev_config_notifier
) {
1412 /* Don't acknowledge CONFIG feature if device doesn't support it */
1413 dev
->protocol_features
&= ~(1ULL << VHOST_USER_PROTOCOL_F_CONFIG
);
1414 } else if (!(protocol_features
&
1415 (1ULL << VHOST_USER_PROTOCOL_F_CONFIG
))) {
1416 error_report("Device expects VHOST_USER_PROTOCOL_F_CONFIG "
1417 "but backend does not support it.");
1421 err
= vhost_user_set_protocol_features(dev
, dev
->protocol_features
);
1426 /* query the max queues we support if backend supports Multiple Queue */
1427 if (dev
->protocol_features
& (1ULL << VHOST_USER_PROTOCOL_F_MQ
)) {
1428 err
= vhost_user_get_u64(dev
, VHOST_USER_GET_QUEUE_NUM
,
1435 if (virtio_has_feature(features
, VIRTIO_F_IOMMU_PLATFORM
) &&
1436 !(virtio_has_feature(dev
->protocol_features
,
1437 VHOST_USER_PROTOCOL_F_SLAVE_REQ
) &&
1438 virtio_has_feature(dev
->protocol_features
,
1439 VHOST_USER_PROTOCOL_F_REPLY_ACK
))) {
1440 error_report("IOMMU support requires reply-ack and "
1441 "slave-req protocol features.");
1446 if (dev
->migration_blocker
== NULL
&&
1447 !virtio_has_feature(dev
->protocol_features
,
1448 VHOST_USER_PROTOCOL_F_LOG_SHMFD
)) {
1449 error_setg(&dev
->migration_blocker
,
1450 "Migration disabled: vhost-user backend lacks "
1451 "VHOST_USER_PROTOCOL_F_LOG_SHMFD feature.");
1454 err
= vhost_setup_slave_channel(dev
);
1459 u
->postcopy_notifier
.notify
= vhost_user_postcopy_notifier
;
1460 postcopy_add_notifier(&u
->postcopy_notifier
);
1465 static int vhost_user_backend_cleanup(struct vhost_dev
*dev
)
1467 struct vhost_user
*u
;
1469 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
1472 if (u
->postcopy_notifier
.notify
) {
1473 postcopy_remove_notifier(&u
->postcopy_notifier
);
1474 u
->postcopy_notifier
.notify
= NULL
;
1476 u
->postcopy_listen
= false;
1477 if (u
->postcopy_fd
.handler
) {
1478 postcopy_unregister_shared_ufd(&u
->postcopy_fd
);
1479 close(u
->postcopy_fd
.fd
);
1480 u
->postcopy_fd
.handler
= NULL
;
1482 if (u
->slave_fd
>= 0) {
1483 qemu_set_fd_handler(u
->slave_fd
, NULL
, NULL
, NULL
);
1487 g_free(u
->region_rb
);
1488 u
->region_rb
= NULL
;
1489 g_free(u
->region_rb_offset
);
1490 u
->region_rb_offset
= NULL
;
1491 u
->region_rb_len
= 0;
1498 static int vhost_user_get_vq_index(struct vhost_dev
*dev
, int idx
)
1500 assert(idx
>= dev
->vq_index
&& idx
< dev
->vq_index
+ dev
->nvqs
);
1505 static int vhost_user_memslots_limit(struct vhost_dev
*dev
)
1507 return VHOST_MEMORY_MAX_NREGIONS
;
1510 static bool vhost_user_requires_shm_log(struct vhost_dev
*dev
)
1512 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
1514 return virtio_has_feature(dev
->protocol_features
,
1515 VHOST_USER_PROTOCOL_F_LOG_SHMFD
);
1518 static int vhost_user_migration_done(struct vhost_dev
*dev
, char* mac_addr
)
1520 VhostUserMsg msg
= { };
1522 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
1524 /* If guest supports GUEST_ANNOUNCE do nothing */
1525 if (virtio_has_feature(dev
->acked_features
, VIRTIO_NET_F_GUEST_ANNOUNCE
)) {
1529 /* if backend supports VHOST_USER_PROTOCOL_F_RARP ask it to send the RARP */
1530 if (virtio_has_feature(dev
->protocol_features
,
1531 VHOST_USER_PROTOCOL_F_RARP
)) {
1532 msg
.hdr
.request
= VHOST_USER_SEND_RARP
;
1533 msg
.hdr
.flags
= VHOST_USER_VERSION
;
1534 memcpy((char *)&msg
.payload
.u64
, mac_addr
, 6);
1535 msg
.hdr
.size
= sizeof(msg
.payload
.u64
);
1537 return vhost_user_write(dev
, &msg
, NULL
, 0);
1542 static bool vhost_user_can_merge(struct vhost_dev
*dev
,
1543 uint64_t start1
, uint64_t size1
,
1544 uint64_t start2
, uint64_t size2
)
1550 mr
= memory_region_from_host((void *)(uintptr_t)start1
, &offset
);
1551 mfd
= memory_region_get_fd(mr
);
1553 mr
= memory_region_from_host((void *)(uintptr_t)start2
, &offset
);
1554 rfd
= memory_region_get_fd(mr
);
1559 static int vhost_user_net_set_mtu(struct vhost_dev
*dev
, uint16_t mtu
)
1562 bool reply_supported
= virtio_has_feature(dev
->protocol_features
,
1563 VHOST_USER_PROTOCOL_F_REPLY_ACK
);
1565 if (!(dev
->protocol_features
& (1ULL << VHOST_USER_PROTOCOL_F_NET_MTU
))) {
1569 msg
.hdr
.request
= VHOST_USER_NET_SET_MTU
;
1570 msg
.payload
.u64
= mtu
;
1571 msg
.hdr
.size
= sizeof(msg
.payload
.u64
);
1572 msg
.hdr
.flags
= VHOST_USER_VERSION
;
1573 if (reply_supported
) {
1574 msg
.hdr
.flags
|= VHOST_USER_NEED_REPLY_MASK
;
1577 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
1581 /* If reply_ack supported, slave has to ack specified MTU is valid */
1582 if (reply_supported
) {
1583 return process_message_reply(dev
, &msg
);
1589 static int vhost_user_send_device_iotlb_msg(struct vhost_dev
*dev
,
1590 struct vhost_iotlb_msg
*imsg
)
1592 VhostUserMsg msg
= {
1593 .hdr
.request
= VHOST_USER_IOTLB_MSG
,
1594 .hdr
.size
= sizeof(msg
.payload
.iotlb
),
1595 .hdr
.flags
= VHOST_USER_VERSION
| VHOST_USER_NEED_REPLY_MASK
,
1596 .payload
.iotlb
= *imsg
,
1599 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
1603 return process_message_reply(dev
, &msg
);
1607 static void vhost_user_set_iotlb_callback(struct vhost_dev
*dev
, int enabled
)
1609 /* No-op as the receive channel is not dedicated to IOTLB messages. */
1612 static int vhost_user_get_config(struct vhost_dev
*dev
, uint8_t *config
,
1613 uint32_t config_len
)
1615 VhostUserMsg msg
= {
1616 .hdr
.request
= VHOST_USER_GET_CONFIG
,
1617 .hdr
.flags
= VHOST_USER_VERSION
,
1618 .hdr
.size
= VHOST_USER_CONFIG_HDR_SIZE
+ config_len
,
1621 if (!virtio_has_feature(dev
->protocol_features
,
1622 VHOST_USER_PROTOCOL_F_CONFIG
)) {
1626 if (config_len
> VHOST_USER_MAX_CONFIG_SIZE
) {
1630 msg
.payload
.config
.offset
= 0;
1631 msg
.payload
.config
.size
= config_len
;
1632 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
1636 if (vhost_user_read(dev
, &msg
) < 0) {
1640 if (msg
.hdr
.request
!= VHOST_USER_GET_CONFIG
) {
1641 error_report("Received unexpected msg type. Expected %d received %d",
1642 VHOST_USER_GET_CONFIG
, msg
.hdr
.request
);
1646 if (msg
.hdr
.size
!= VHOST_USER_CONFIG_HDR_SIZE
+ config_len
) {
1647 error_report("Received bad msg size.");
1651 memcpy(config
, msg
.payload
.config
.region
, config_len
);
1656 static int vhost_user_set_config(struct vhost_dev
*dev
, const uint8_t *data
,
1657 uint32_t offset
, uint32_t size
, uint32_t flags
)
1660 bool reply_supported
= virtio_has_feature(dev
->protocol_features
,
1661 VHOST_USER_PROTOCOL_F_REPLY_ACK
);
1663 VhostUserMsg msg
= {
1664 .hdr
.request
= VHOST_USER_SET_CONFIG
,
1665 .hdr
.flags
= VHOST_USER_VERSION
,
1666 .hdr
.size
= VHOST_USER_CONFIG_HDR_SIZE
+ size
,
1669 if (!virtio_has_feature(dev
->protocol_features
,
1670 VHOST_USER_PROTOCOL_F_CONFIG
)) {
1674 if (reply_supported
) {
1675 msg
.hdr
.flags
|= VHOST_USER_NEED_REPLY_MASK
;
1678 if (size
> VHOST_USER_MAX_CONFIG_SIZE
) {
1682 msg
.payload
.config
.offset
= offset
,
1683 msg
.payload
.config
.size
= size
,
1684 msg
.payload
.config
.flags
= flags
,
1685 p
= msg
.payload
.config
.region
;
1686 memcpy(p
, data
, size
);
1688 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
1692 if (reply_supported
) {
1693 return process_message_reply(dev
, &msg
);
1699 static int vhost_user_crypto_create_session(struct vhost_dev
*dev
,
1701 uint64_t *session_id
)
1703 bool crypto_session
= virtio_has_feature(dev
->protocol_features
,
1704 VHOST_USER_PROTOCOL_F_CRYPTO_SESSION
);
1705 CryptoDevBackendSymSessionInfo
*sess_info
= session_info
;
1706 VhostUserMsg msg
= {
1707 .hdr
.request
= VHOST_USER_CREATE_CRYPTO_SESSION
,
1708 .hdr
.flags
= VHOST_USER_VERSION
,
1709 .hdr
.size
= sizeof(msg
.payload
.session
),
1712 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
1714 if (!crypto_session
) {
1715 error_report("vhost-user trying to send unhandled ioctl");
1719 memcpy(&msg
.payload
.session
.session_setup_data
, sess_info
,
1720 sizeof(CryptoDevBackendSymSessionInfo
));
1721 if (sess_info
->key_len
) {
1722 memcpy(&msg
.payload
.session
.key
, sess_info
->cipher_key
,
1723 sess_info
->key_len
);
1725 if (sess_info
->auth_key_len
> 0) {
1726 memcpy(&msg
.payload
.session
.auth_key
, sess_info
->auth_key
,
1727 sess_info
->auth_key_len
);
1729 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
1730 error_report("vhost_user_write() return -1, create session failed");
1734 if (vhost_user_read(dev
, &msg
) < 0) {
1735 error_report("vhost_user_read() return -1, create session failed");
1739 if (msg
.hdr
.request
!= VHOST_USER_CREATE_CRYPTO_SESSION
) {
1740 error_report("Received unexpected msg type. Expected %d received %d",
1741 VHOST_USER_CREATE_CRYPTO_SESSION
, msg
.hdr
.request
);
1745 if (msg
.hdr
.size
!= sizeof(msg
.payload
.session
)) {
1746 error_report("Received bad msg size.");
1750 if (msg
.payload
.session
.session_id
< 0) {
1751 error_report("Bad session id: %" PRId64
"",
1752 msg
.payload
.session
.session_id
);
1755 *session_id
= msg
.payload
.session
.session_id
;
1761 vhost_user_crypto_close_session(struct vhost_dev
*dev
, uint64_t session_id
)
1763 bool crypto_session
= virtio_has_feature(dev
->protocol_features
,
1764 VHOST_USER_PROTOCOL_F_CRYPTO_SESSION
);
1765 VhostUserMsg msg
= {
1766 .hdr
.request
= VHOST_USER_CLOSE_CRYPTO_SESSION
,
1767 .hdr
.flags
= VHOST_USER_VERSION
,
1768 .hdr
.size
= sizeof(msg
.payload
.u64
),
1770 msg
.payload
.u64
= session_id
;
1772 if (!crypto_session
) {
1773 error_report("vhost-user trying to send unhandled ioctl");
1777 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
1778 error_report("vhost_user_write() return -1, close session failed");
1785 static bool vhost_user_mem_section_filter(struct vhost_dev
*dev
,
1786 MemoryRegionSection
*section
)
1790 result
= memory_region_get_fd(section
->mr
) >= 0;
1795 static int vhost_user_get_inflight_fd(struct vhost_dev
*dev
,
1796 uint16_t queue_size
,
1797 struct vhost_inflight
*inflight
)
1801 struct vhost_user
*u
= dev
->opaque
;
1802 CharBackend
*chr
= u
->user
->chr
;
1803 VhostUserMsg msg
= {
1804 .hdr
.request
= VHOST_USER_GET_INFLIGHT_FD
,
1805 .hdr
.flags
= VHOST_USER_VERSION
,
1806 .payload
.inflight
.num_queues
= dev
->nvqs
,
1807 .payload
.inflight
.queue_size
= queue_size
,
1808 .hdr
.size
= sizeof(msg
.payload
.inflight
),
1811 if (!virtio_has_feature(dev
->protocol_features
,
1812 VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD
)) {
1816 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
1820 if (vhost_user_read(dev
, &msg
) < 0) {
1824 if (msg
.hdr
.request
!= VHOST_USER_GET_INFLIGHT_FD
) {
1825 error_report("Received unexpected msg type. "
1826 "Expected %d received %d",
1827 VHOST_USER_GET_INFLIGHT_FD
, msg
.hdr
.request
);
1831 if (msg
.hdr
.size
!= sizeof(msg
.payload
.inflight
)) {
1832 error_report("Received bad msg size.");
1836 if (!msg
.payload
.inflight
.mmap_size
) {
1840 fd
= qemu_chr_fe_get_msgfd(chr
);
1842 error_report("Failed to get mem fd");
1846 addr
= mmap(0, msg
.payload
.inflight
.mmap_size
, PROT_READ
| PROT_WRITE
,
1847 MAP_SHARED
, fd
, msg
.payload
.inflight
.mmap_offset
);
1849 if (addr
== MAP_FAILED
) {
1850 error_report("Failed to mmap mem fd");
1855 inflight
->addr
= addr
;
1857 inflight
->size
= msg
.payload
.inflight
.mmap_size
;
1858 inflight
->offset
= msg
.payload
.inflight
.mmap_offset
;
1859 inflight
->queue_size
= queue_size
;
1864 static int vhost_user_set_inflight_fd(struct vhost_dev
*dev
,
1865 struct vhost_inflight
*inflight
)
1867 VhostUserMsg msg
= {
1868 .hdr
.request
= VHOST_USER_SET_INFLIGHT_FD
,
1869 .hdr
.flags
= VHOST_USER_VERSION
,
1870 .payload
.inflight
.mmap_size
= inflight
->size
,
1871 .payload
.inflight
.mmap_offset
= inflight
->offset
,
1872 .payload
.inflight
.num_queues
= dev
->nvqs
,
1873 .payload
.inflight
.queue_size
= inflight
->queue_size
,
1874 .hdr
.size
= sizeof(msg
.payload
.inflight
),
1877 if (!virtio_has_feature(dev
->protocol_features
,
1878 VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD
)) {
1882 if (vhost_user_write(dev
, &msg
, &inflight
->fd
, 1) < 0) {
1889 bool vhost_user_init(VhostUserState
*user
, CharBackend
*chr
, Error
**errp
)
1892 error_setg(errp
, "Cannot initialize vhost-user state");
1899 void vhost_user_cleanup(VhostUserState
*user
)
1907 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
1908 if (user
->notifier
[i
].addr
) {
1909 object_unparent(OBJECT(&user
->notifier
[i
].mr
));
1910 munmap(user
->notifier
[i
].addr
, qemu_real_host_page_size
);
1911 user
->notifier
[i
].addr
= NULL
;
1917 const VhostOps user_ops
= {
1918 .backend_type
= VHOST_BACKEND_TYPE_USER
,
1919 .vhost_backend_init
= vhost_user_backend_init
,
1920 .vhost_backend_cleanup
= vhost_user_backend_cleanup
,
1921 .vhost_backend_memslots_limit
= vhost_user_memslots_limit
,
1922 .vhost_set_log_base
= vhost_user_set_log_base
,
1923 .vhost_set_mem_table
= vhost_user_set_mem_table
,
1924 .vhost_set_vring_addr
= vhost_user_set_vring_addr
,
1925 .vhost_set_vring_endian
= vhost_user_set_vring_endian
,
1926 .vhost_set_vring_num
= vhost_user_set_vring_num
,
1927 .vhost_set_vring_base
= vhost_user_set_vring_base
,
1928 .vhost_get_vring_base
= vhost_user_get_vring_base
,
1929 .vhost_set_vring_kick
= vhost_user_set_vring_kick
,
1930 .vhost_set_vring_call
= vhost_user_set_vring_call
,
1931 .vhost_set_features
= vhost_user_set_features
,
1932 .vhost_get_features
= vhost_user_get_features
,
1933 .vhost_set_owner
= vhost_user_set_owner
,
1934 .vhost_reset_device
= vhost_user_reset_device
,
1935 .vhost_get_vq_index
= vhost_user_get_vq_index
,
1936 .vhost_set_vring_enable
= vhost_user_set_vring_enable
,
1937 .vhost_requires_shm_log
= vhost_user_requires_shm_log
,
1938 .vhost_migration_done
= vhost_user_migration_done
,
1939 .vhost_backend_can_merge
= vhost_user_can_merge
,
1940 .vhost_net_set_mtu
= vhost_user_net_set_mtu
,
1941 .vhost_set_iotlb_callback
= vhost_user_set_iotlb_callback
,
1942 .vhost_send_device_iotlb_msg
= vhost_user_send_device_iotlb_msg
,
1943 .vhost_get_config
= vhost_user_get_config
,
1944 .vhost_set_config
= vhost_user_set_config
,
1945 .vhost_crypto_create_session
= vhost_user_crypto_create_session
,
1946 .vhost_crypto_close_session
= vhost_user_crypto_close_session
,
1947 .vhost_backend_mem_section_filter
= vhost_user_mem_section_filter
,
1948 .vhost_get_inflight_fd
= vhost_user_get_inflight_fd
,
1949 .vhost_set_inflight_fd
= vhost_user_set_inflight_fd
,