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/main-loop.h"
22 #include "qemu/sockets.h"
23 #include "sysemu/cryptodev.h"
24 #include "migration/migration.h"
25 #include "migration/postcopy-ram.h"
28 #include <sys/ioctl.h>
29 #include <sys/socket.h>
32 #include "standard-headers/linux/vhost_types.h"
35 #include <linux/userfaultfd.h>
38 #define VHOST_MEMORY_MAX_NREGIONS 8
39 #define VHOST_USER_F_PROTOCOL_FEATURES 30
40 #define VHOST_USER_SLAVE_MAX_FDS 8
43 * Maximum size of virtio device config space
45 #define VHOST_USER_MAX_CONFIG_SIZE 256
47 enum VhostUserProtocolFeature
{
48 VHOST_USER_PROTOCOL_F_MQ
= 0,
49 VHOST_USER_PROTOCOL_F_LOG_SHMFD
= 1,
50 VHOST_USER_PROTOCOL_F_RARP
= 2,
51 VHOST_USER_PROTOCOL_F_REPLY_ACK
= 3,
52 VHOST_USER_PROTOCOL_F_NET_MTU
= 4,
53 VHOST_USER_PROTOCOL_F_SLAVE_REQ
= 5,
54 VHOST_USER_PROTOCOL_F_CROSS_ENDIAN
= 6,
55 VHOST_USER_PROTOCOL_F_CRYPTO_SESSION
= 7,
56 VHOST_USER_PROTOCOL_F_PAGEFAULT
= 8,
57 VHOST_USER_PROTOCOL_F_CONFIG
= 9,
58 VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD
= 10,
59 VHOST_USER_PROTOCOL_F_HOST_NOTIFIER
= 11,
60 VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD
= 12,
61 VHOST_USER_PROTOCOL_F_RESET_DEVICE
= 13,
62 /* Feature 14 reserved for VHOST_USER_PROTOCOL_F_INBAND_NOTIFICATIONS. */
63 VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS
= 15,
64 VHOST_USER_PROTOCOL_F_MAX
67 #define VHOST_USER_PROTOCOL_FEATURE_MASK ((1 << VHOST_USER_PROTOCOL_F_MAX) - 1)
69 typedef enum VhostUserRequest
{
71 VHOST_USER_GET_FEATURES
= 1,
72 VHOST_USER_SET_FEATURES
= 2,
73 VHOST_USER_SET_OWNER
= 3,
74 VHOST_USER_RESET_OWNER
= 4,
75 VHOST_USER_SET_MEM_TABLE
= 5,
76 VHOST_USER_SET_LOG_BASE
= 6,
77 VHOST_USER_SET_LOG_FD
= 7,
78 VHOST_USER_SET_VRING_NUM
= 8,
79 VHOST_USER_SET_VRING_ADDR
= 9,
80 VHOST_USER_SET_VRING_BASE
= 10,
81 VHOST_USER_GET_VRING_BASE
= 11,
82 VHOST_USER_SET_VRING_KICK
= 12,
83 VHOST_USER_SET_VRING_CALL
= 13,
84 VHOST_USER_SET_VRING_ERR
= 14,
85 VHOST_USER_GET_PROTOCOL_FEATURES
= 15,
86 VHOST_USER_SET_PROTOCOL_FEATURES
= 16,
87 VHOST_USER_GET_QUEUE_NUM
= 17,
88 VHOST_USER_SET_VRING_ENABLE
= 18,
89 VHOST_USER_SEND_RARP
= 19,
90 VHOST_USER_NET_SET_MTU
= 20,
91 VHOST_USER_SET_SLAVE_REQ_FD
= 21,
92 VHOST_USER_IOTLB_MSG
= 22,
93 VHOST_USER_SET_VRING_ENDIAN
= 23,
94 VHOST_USER_GET_CONFIG
= 24,
95 VHOST_USER_SET_CONFIG
= 25,
96 VHOST_USER_CREATE_CRYPTO_SESSION
= 26,
97 VHOST_USER_CLOSE_CRYPTO_SESSION
= 27,
98 VHOST_USER_POSTCOPY_ADVISE
= 28,
99 VHOST_USER_POSTCOPY_LISTEN
= 29,
100 VHOST_USER_POSTCOPY_END
= 30,
101 VHOST_USER_GET_INFLIGHT_FD
= 31,
102 VHOST_USER_SET_INFLIGHT_FD
= 32,
103 VHOST_USER_GPU_SET_SOCKET
= 33,
104 VHOST_USER_RESET_DEVICE
= 34,
105 /* Message number 35 reserved for VHOST_USER_VRING_KICK. */
106 VHOST_USER_GET_MAX_MEM_SLOTS
= 36,
107 VHOST_USER_ADD_MEM_REG
= 37,
108 VHOST_USER_REM_MEM_REG
= 38,
112 typedef enum VhostUserSlaveRequest
{
113 VHOST_USER_SLAVE_NONE
= 0,
114 VHOST_USER_SLAVE_IOTLB_MSG
= 1,
115 VHOST_USER_SLAVE_CONFIG_CHANGE_MSG
= 2,
116 VHOST_USER_SLAVE_VRING_HOST_NOTIFIER_MSG
= 3,
118 } VhostUserSlaveRequest
;
120 typedef struct VhostUserMemoryRegion
{
121 uint64_t guest_phys_addr
;
122 uint64_t memory_size
;
123 uint64_t userspace_addr
;
124 uint64_t mmap_offset
;
125 } VhostUserMemoryRegion
;
127 typedef struct VhostUserMemory
{
130 VhostUserMemoryRegion regions
[VHOST_MEMORY_MAX_NREGIONS
];
133 typedef struct VhostUserMemRegMsg
{
135 VhostUserMemoryRegion region
;
136 } VhostUserMemRegMsg
;
138 typedef struct VhostUserLog
{
140 uint64_t mmap_offset
;
143 typedef struct VhostUserConfig
{
147 uint8_t region
[VHOST_USER_MAX_CONFIG_SIZE
];
150 #define VHOST_CRYPTO_SYM_HMAC_MAX_KEY_LEN 512
151 #define VHOST_CRYPTO_SYM_CIPHER_MAX_KEY_LEN 64
153 typedef struct VhostUserCryptoSession
{
154 /* session id for success, -1 on errors */
156 CryptoDevBackendSymSessionInfo session_setup_data
;
157 uint8_t key
[VHOST_CRYPTO_SYM_CIPHER_MAX_KEY_LEN
];
158 uint8_t auth_key
[VHOST_CRYPTO_SYM_HMAC_MAX_KEY_LEN
];
159 } VhostUserCryptoSession
;
161 static VhostUserConfig c
__attribute__ ((unused
));
162 #define VHOST_USER_CONFIG_HDR_SIZE (sizeof(c.offset) \
166 typedef struct VhostUserVringArea
{
170 } VhostUserVringArea
;
172 typedef struct VhostUserInflight
{
174 uint64_t mmap_offset
;
180 VhostUserRequest request
;
182 #define VHOST_USER_VERSION_MASK (0x3)
183 #define VHOST_USER_REPLY_MASK (0x1<<2)
184 #define VHOST_USER_NEED_REPLY_MASK (0x1 << 3)
186 uint32_t size
; /* the following payload size */
187 } QEMU_PACKED VhostUserHeader
;
190 #define VHOST_USER_VRING_IDX_MASK (0xff)
191 #define VHOST_USER_VRING_NOFD_MASK (0x1<<8)
193 struct vhost_vring_state state
;
194 struct vhost_vring_addr addr
;
195 VhostUserMemory memory
;
196 VhostUserMemRegMsg mem_reg
;
198 struct vhost_iotlb_msg iotlb
;
199 VhostUserConfig config
;
200 VhostUserCryptoSession session
;
201 VhostUserVringArea area
;
202 VhostUserInflight inflight
;
205 typedef struct VhostUserMsg
{
207 VhostUserPayload payload
;
208 } QEMU_PACKED VhostUserMsg
;
210 static VhostUserMsg m
__attribute__ ((unused
));
211 #define VHOST_USER_HDR_SIZE (sizeof(VhostUserHeader))
213 #define VHOST_USER_PAYLOAD_SIZE (sizeof(VhostUserPayload))
215 /* The version of the protocol we support */
216 #define VHOST_USER_VERSION (0x1)
219 struct vhost_dev
*dev
;
220 /* Shared between vhost devs of the same virtio device */
221 VhostUserState
*user
;
223 NotifierWithReturn postcopy_notifier
;
224 struct PostCopyFD postcopy_fd
;
225 uint64_t postcopy_client_bases
[VHOST_MEMORY_MAX_NREGIONS
];
226 /* Length of the region_rb and region_rb_offset arrays */
227 size_t region_rb_len
;
228 /* RAMBlock associated with a given region */
229 RAMBlock
**region_rb
;
230 /* The offset from the start of the RAMBlock to the start of the
233 ram_addr_t
*region_rb_offset
;
235 /* True once we've entered postcopy_listen */
236 bool postcopy_listen
;
238 /* Our current regions */
239 int num_shadow_regions
;
240 struct vhost_memory_region shadow_regions
[VHOST_MEMORY_MAX_NREGIONS
];
243 struct scrub_regions
{
244 struct vhost_memory_region
*region
;
249 static bool ioeventfd_enabled(void)
251 return !kvm_enabled() || kvm_eventfds_enabled();
254 static int vhost_user_read_header(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
;
259 int r
, size
= VHOST_USER_HDR_SIZE
;
261 r
= qemu_chr_fe_read_all(chr
, p
, size
);
263 error_report("Failed to read msg header. Read %d instead of %d."
264 " Original request %d.", r
, size
, msg
->hdr
.request
);
268 /* validate received flags */
269 if (msg
->hdr
.flags
!= (VHOST_USER_REPLY_MASK
| VHOST_USER_VERSION
)) {
270 error_report("Failed to read msg header."
271 " Flags 0x%x instead of 0x%x.", msg
->hdr
.flags
,
272 VHOST_USER_REPLY_MASK
| VHOST_USER_VERSION
);
279 static int vhost_user_read(struct vhost_dev
*dev
, VhostUserMsg
*msg
)
281 struct vhost_user
*u
= dev
->opaque
;
282 CharBackend
*chr
= u
->user
->chr
;
283 uint8_t *p
= (uint8_t *) msg
;
286 if (vhost_user_read_header(dev
, msg
) < 0) {
290 /* validate message size is sane */
291 if (msg
->hdr
.size
> VHOST_USER_PAYLOAD_SIZE
) {
292 error_report("Failed to read msg header."
293 " Size %d exceeds the maximum %zu.", msg
->hdr
.size
,
294 VHOST_USER_PAYLOAD_SIZE
);
299 p
+= VHOST_USER_HDR_SIZE
;
300 size
= msg
->hdr
.size
;
301 r
= qemu_chr_fe_read_all(chr
, p
, size
);
303 error_report("Failed to read msg payload."
304 " Read %d instead of %d.", r
, msg
->hdr
.size
);
312 static int process_message_reply(struct vhost_dev
*dev
,
313 const VhostUserMsg
*msg
)
315 VhostUserMsg msg_reply
;
317 if ((msg
->hdr
.flags
& VHOST_USER_NEED_REPLY_MASK
) == 0) {
321 if (vhost_user_read(dev
, &msg_reply
) < 0) {
325 if (msg_reply
.hdr
.request
!= msg
->hdr
.request
) {
326 error_report("Received unexpected msg type."
327 "Expected %d received %d",
328 msg
->hdr
.request
, msg_reply
.hdr
.request
);
332 return msg_reply
.payload
.u64
? -1 : 0;
335 static bool vhost_user_one_time_request(VhostUserRequest request
)
338 case VHOST_USER_SET_OWNER
:
339 case VHOST_USER_RESET_OWNER
:
340 case VHOST_USER_SET_MEM_TABLE
:
341 case VHOST_USER_GET_QUEUE_NUM
:
342 case VHOST_USER_NET_SET_MTU
:
349 /* most non-init callers ignore the error */
350 static int vhost_user_write(struct vhost_dev
*dev
, VhostUserMsg
*msg
,
351 int *fds
, int fd_num
)
353 struct vhost_user
*u
= dev
->opaque
;
354 CharBackend
*chr
= u
->user
->chr
;
355 int ret
, size
= VHOST_USER_HDR_SIZE
+ msg
->hdr
.size
;
358 * For non-vring specific requests, like VHOST_USER_SET_MEM_TABLE,
359 * we just need send it once in the first time. For later such
360 * request, we just ignore it.
362 if (vhost_user_one_time_request(msg
->hdr
.request
) && dev
->vq_index
!= 0) {
363 msg
->hdr
.flags
&= ~VHOST_USER_NEED_REPLY_MASK
;
367 if (qemu_chr_fe_set_msgfds(chr
, fds
, fd_num
) < 0) {
368 error_report("Failed to set msg fds.");
372 ret
= qemu_chr_fe_write_all(chr
, (const uint8_t *) msg
, size
);
374 error_report("Failed to write msg."
375 " Wrote %d instead of %d.", ret
, size
);
382 int vhost_user_gpu_set_socket(struct vhost_dev
*dev
, int fd
)
385 .hdr
.request
= VHOST_USER_GPU_SET_SOCKET
,
386 .hdr
.flags
= VHOST_USER_VERSION
,
389 return vhost_user_write(dev
, &msg
, &fd
, 1);
392 static int vhost_user_set_log_base(struct vhost_dev
*dev
, uint64_t base
,
393 struct vhost_log
*log
)
395 int fds
[VHOST_MEMORY_MAX_NREGIONS
];
397 bool shmfd
= virtio_has_feature(dev
->protocol_features
,
398 VHOST_USER_PROTOCOL_F_LOG_SHMFD
);
400 .hdr
.request
= VHOST_USER_SET_LOG_BASE
,
401 .hdr
.flags
= VHOST_USER_VERSION
,
402 .payload
.log
.mmap_size
= log
->size
* sizeof(*(log
->log
)),
403 .payload
.log
.mmap_offset
= 0,
404 .hdr
.size
= sizeof(msg
.payload
.log
),
407 if (shmfd
&& log
->fd
!= -1) {
408 fds
[fd_num
++] = log
->fd
;
411 if (vhost_user_write(dev
, &msg
, fds
, fd_num
) < 0) {
417 if (vhost_user_read(dev
, &msg
) < 0) {
421 if (msg
.hdr
.request
!= VHOST_USER_SET_LOG_BASE
) {
422 error_report("Received unexpected msg type. "
423 "Expected %d received %d",
424 VHOST_USER_SET_LOG_BASE
, msg
.hdr
.request
);
432 static MemoryRegion
*vhost_user_get_mr_data(uint64_t addr
, ram_addr_t
*offset
,
437 assert((uintptr_t)addr
== addr
);
438 mr
= memory_region_from_host((void *)(uintptr_t)addr
, offset
);
439 *fd
= memory_region_get_fd(mr
);
444 static void vhost_user_fill_msg_region(VhostUserMemoryRegion
*dst
,
445 struct vhost_memory_region
*src
)
447 assert(src
!= NULL
&& dst
!= NULL
);
448 dst
->userspace_addr
= src
->userspace_addr
;
449 dst
->memory_size
= src
->memory_size
;
450 dst
->guest_phys_addr
= src
->guest_phys_addr
;
453 static int vhost_user_fill_set_mem_table_msg(struct vhost_user
*u
,
454 struct vhost_dev
*dev
,
456 int *fds
, size_t *fd_num
,
457 bool track_ramblocks
)
462 struct vhost_memory_region
*reg
;
463 VhostUserMemoryRegion region_buffer
;
465 msg
->hdr
.request
= VHOST_USER_SET_MEM_TABLE
;
467 for (i
= 0; i
< dev
->mem
->nregions
; ++i
) {
468 reg
= dev
->mem
->regions
+ i
;
470 mr
= vhost_user_get_mr_data(reg
->userspace_addr
, &offset
, &fd
);
472 if (track_ramblocks
) {
473 assert(*fd_num
< VHOST_MEMORY_MAX_NREGIONS
);
474 trace_vhost_user_set_mem_table_withfd(*fd_num
, mr
->name
,
476 reg
->guest_phys_addr
,
479 u
->region_rb_offset
[i
] = offset
;
480 u
->region_rb
[i
] = mr
->ram_block
;
481 } else if (*fd_num
== VHOST_MEMORY_MAX_NREGIONS
) {
482 error_report("Failed preparing vhost-user memory table msg");
485 vhost_user_fill_msg_region(®ion_buffer
, reg
);
486 msg
->payload
.memory
.regions
[*fd_num
] = region_buffer
;
487 msg
->payload
.memory
.regions
[*fd_num
].mmap_offset
= offset
;
488 fds
[(*fd_num
)++] = fd
;
489 } else if (track_ramblocks
) {
490 u
->region_rb_offset
[i
] = 0;
491 u
->region_rb
[i
] = NULL
;
495 msg
->payload
.memory
.nregions
= *fd_num
;
498 error_report("Failed initializing vhost-user memory map, "
499 "consider using -object memory-backend-file share=on");
503 msg
->hdr
.size
= sizeof(msg
->payload
.memory
.nregions
);
504 msg
->hdr
.size
+= sizeof(msg
->payload
.memory
.padding
);
505 msg
->hdr
.size
+= *fd_num
* sizeof(VhostUserMemoryRegion
);
510 static inline bool reg_equal(struct vhost_memory_region
*shadow_reg
,
511 struct vhost_memory_region
*vdev_reg
)
513 return shadow_reg
->guest_phys_addr
== vdev_reg
->guest_phys_addr
&&
514 shadow_reg
->userspace_addr
== vdev_reg
->userspace_addr
&&
515 shadow_reg
->memory_size
== vdev_reg
->memory_size
;
518 static void scrub_shadow_regions(struct vhost_dev
*dev
,
519 struct scrub_regions
*add_reg
,
521 struct scrub_regions
*rem_reg
,
522 int *nr_rem_reg
, uint64_t *shadow_pcb
,
523 bool track_ramblocks
)
525 struct vhost_user
*u
= dev
->opaque
;
526 bool found
[VHOST_MEMORY_MAX_NREGIONS
] = {};
527 struct vhost_memory_region
*reg
, *shadow_reg
;
528 int i
, j
, fd
, add_idx
= 0, rm_idx
= 0, fd_num
= 0;
534 * Find memory regions present in our shadow state which are not in
535 * the device's current memory state.
537 * Mark regions in both the shadow and device state as "found".
539 for (i
= 0; i
< u
->num_shadow_regions
; i
++) {
540 shadow_reg
= &u
->shadow_regions
[i
];
543 for (j
= 0; j
< dev
->mem
->nregions
; j
++) {
544 reg
= &dev
->mem
->regions
[j
];
546 mr
= vhost_user_get_mr_data(reg
->userspace_addr
, &offset
, &fd
);
548 if (reg_equal(shadow_reg
, reg
)) {
551 if (track_ramblocks
) {
553 * Reset postcopy client bases, region_rb, and
554 * region_rb_offset in case regions are removed.
557 u
->region_rb_offset
[j
] = offset
;
558 u
->region_rb
[j
] = mr
->ram_block
;
559 shadow_pcb
[j
] = u
->postcopy_client_bases
[i
];
561 u
->region_rb_offset
[j
] = 0;
562 u
->region_rb
[j
] = NULL
;
570 * If the region was not found in the current device memory state
571 * create an entry for it in the removed list.
574 rem_reg
[rm_idx
].region
= shadow_reg
;
575 rem_reg
[rm_idx
++].reg_idx
= i
;
580 * For regions not marked "found", create entries in the added list.
582 * Note their indexes in the device memory state and the indexes of their
585 for (i
= 0; i
< dev
->mem
->nregions
; i
++) {
586 reg
= &dev
->mem
->regions
[i
];
587 mr
= vhost_user_get_mr_data(reg
->userspace_addr
, &offset
, &fd
);
593 * If the region was in both the shadow and device state we don't
594 * need to send a VHOST_USER_ADD_MEM_REG message for it.
600 add_reg
[add_idx
].region
= reg
;
601 add_reg
[add_idx
].reg_idx
= i
;
602 add_reg
[add_idx
++].fd_idx
= fd_num
;
604 *nr_rem_reg
= rm_idx
;
605 *nr_add_reg
= add_idx
;
610 static int send_remove_regions(struct vhost_dev
*dev
,
611 struct scrub_regions
*remove_reg
,
612 int nr_rem_reg
, VhostUserMsg
*msg
,
613 bool reply_supported
)
615 struct vhost_user
*u
= dev
->opaque
;
616 struct vhost_memory_region
*shadow_reg
;
617 int i
, fd
, shadow_reg_idx
, ret
;
619 VhostUserMemoryRegion region_buffer
;
622 * The regions in remove_reg appear in the same order they do in the
623 * shadow table. Therefore we can minimize memory copies by iterating
624 * through remove_reg backwards.
626 for (i
= nr_rem_reg
- 1; i
>= 0; i
--) {
627 shadow_reg
= remove_reg
[i
].region
;
628 shadow_reg_idx
= remove_reg
[i
].reg_idx
;
630 vhost_user_get_mr_data(shadow_reg
->userspace_addr
, &offset
, &fd
);
633 msg
->hdr
.request
= VHOST_USER_REM_MEM_REG
;
634 vhost_user_fill_msg_region(®ion_buffer
, shadow_reg
);
635 msg
->payload
.mem_reg
.region
= region_buffer
;
637 if (vhost_user_write(dev
, msg
, &fd
, 1) < 0) {
641 if (reply_supported
) {
642 ret
= process_message_reply(dev
, msg
);
650 * At this point we know the backend has unmapped the region. It is now
651 * safe to remove it from the shadow table.
653 memmove(&u
->shadow_regions
[shadow_reg_idx
],
654 &u
->shadow_regions
[shadow_reg_idx
+ 1],
655 sizeof(struct vhost_memory_region
) *
656 (u
->num_shadow_regions
- shadow_reg_idx
));
657 u
->num_shadow_regions
--;
663 static int send_add_regions(struct vhost_dev
*dev
,
664 struct scrub_regions
*add_reg
, int nr_add_reg
,
665 VhostUserMsg
*msg
, uint64_t *shadow_pcb
,
666 bool reply_supported
, bool track_ramblocks
)
668 struct vhost_user
*u
= dev
->opaque
;
669 int i
, fd
, ret
, reg_idx
, reg_fd_idx
;
670 struct vhost_memory_region
*reg
;
673 VhostUserMsg msg_reply
;
674 VhostUserMemoryRegion region_buffer
;
676 for (i
= 0; i
< nr_add_reg
; i
++) {
677 reg
= add_reg
[i
].region
;
678 reg_idx
= add_reg
[i
].reg_idx
;
679 reg_fd_idx
= add_reg
[i
].fd_idx
;
681 mr
= vhost_user_get_mr_data(reg
->userspace_addr
, &offset
, &fd
);
684 if (track_ramblocks
) {
685 trace_vhost_user_set_mem_table_withfd(reg_fd_idx
, mr
->name
,
687 reg
->guest_phys_addr
,
690 u
->region_rb_offset
[reg_idx
] = offset
;
691 u
->region_rb
[reg_idx
] = mr
->ram_block
;
693 msg
->hdr
.request
= VHOST_USER_ADD_MEM_REG
;
694 vhost_user_fill_msg_region(®ion_buffer
, reg
);
695 msg
->payload
.mem_reg
.region
= region_buffer
;
696 msg
->payload
.mem_reg
.region
.mmap_offset
= offset
;
698 if (vhost_user_write(dev
, msg
, &fd
, 1) < 0) {
702 if (track_ramblocks
) {
705 if (vhost_user_read(dev
, &msg_reply
) < 0) {
709 reply_gpa
= msg_reply
.payload
.mem_reg
.region
.guest_phys_addr
;
711 if (msg_reply
.hdr
.request
!= VHOST_USER_ADD_MEM_REG
) {
712 error_report("%s: Received unexpected msg type."
713 "Expected %d received %d", __func__
,
714 VHOST_USER_ADD_MEM_REG
,
715 msg_reply
.hdr
.request
);
720 * We're using the same structure, just reusing one of the
721 * fields, so it should be the same size.
723 if (msg_reply
.hdr
.size
!= msg
->hdr
.size
) {
724 error_report("%s: Unexpected size for postcopy reply "
725 "%d vs %d", __func__
, msg_reply
.hdr
.size
,
730 /* Get the postcopy client base from the backend's reply. */
731 if (reply_gpa
== dev
->mem
->regions
[reg_idx
].guest_phys_addr
) {
732 shadow_pcb
[reg_idx
] =
733 msg_reply
.payload
.mem_reg
.region
.userspace_addr
;
734 trace_vhost_user_set_mem_table_postcopy(
735 msg_reply
.payload
.mem_reg
.region
.userspace_addr
,
736 msg
->payload
.mem_reg
.region
.userspace_addr
,
737 reg_fd_idx
, reg_idx
);
739 error_report("%s: invalid postcopy reply for region. "
740 "Got guest physical address %" PRIX64
", expected "
741 "%" PRIX64
, __func__
, reply_gpa
,
742 dev
->mem
->regions
[reg_idx
].guest_phys_addr
);
745 } else if (reply_supported
) {
746 ret
= process_message_reply(dev
, msg
);
751 } else if (track_ramblocks
) {
752 u
->region_rb_offset
[reg_idx
] = 0;
753 u
->region_rb
[reg_idx
] = NULL
;
757 * At this point, we know the backend has mapped in the new
758 * region, if the region has a valid file descriptor.
760 * The region should now be added to the shadow table.
762 u
->shadow_regions
[u
->num_shadow_regions
].guest_phys_addr
=
763 reg
->guest_phys_addr
;
764 u
->shadow_regions
[u
->num_shadow_regions
].userspace_addr
=
766 u
->shadow_regions
[u
->num_shadow_regions
].memory_size
=
768 u
->num_shadow_regions
++;
774 static int vhost_user_add_remove_regions(struct vhost_dev
*dev
,
776 bool reply_supported
,
777 bool track_ramblocks
)
779 struct vhost_user
*u
= dev
->opaque
;
780 struct scrub_regions add_reg
[VHOST_MEMORY_MAX_NREGIONS
];
781 struct scrub_regions rem_reg
[VHOST_MEMORY_MAX_NREGIONS
];
782 uint64_t shadow_pcb
[VHOST_MEMORY_MAX_NREGIONS
] = {};
783 int nr_add_reg
, nr_rem_reg
;
785 msg
->hdr
.size
= sizeof(msg
->payload
.mem_reg
.padding
) +
786 sizeof(VhostUserMemoryRegion
);
788 /* Find the regions which need to be removed or added. */
789 scrub_shadow_regions(dev
, add_reg
, &nr_add_reg
, rem_reg
, &nr_rem_reg
,
790 shadow_pcb
, track_ramblocks
);
792 if (nr_rem_reg
&& send_remove_regions(dev
, rem_reg
, nr_rem_reg
, msg
,
793 reply_supported
) < 0)
798 if (nr_add_reg
&& send_add_regions(dev
, add_reg
, nr_add_reg
, msg
,
799 shadow_pcb
, reply_supported
, track_ramblocks
) < 0)
804 if (track_ramblocks
) {
805 memcpy(u
->postcopy_client_bases
, shadow_pcb
,
806 sizeof(uint64_t) * VHOST_MEMORY_MAX_NREGIONS
);
808 * Now we've registered this with the postcopy code, we ack to the
809 * client, because now we're in the position to be able to deal with
810 * any faults it generates.
812 /* TODO: Use this for failure cases as well with a bad value. */
813 msg
->hdr
.size
= sizeof(msg
->payload
.u64
);
814 msg
->payload
.u64
= 0; /* OK */
816 if (vhost_user_write(dev
, msg
, NULL
, 0) < 0) {
824 if (track_ramblocks
) {
825 memcpy(u
->postcopy_client_bases
, shadow_pcb
,
826 sizeof(uint64_t) * VHOST_MEMORY_MAX_NREGIONS
);
832 static int vhost_user_set_mem_table_postcopy(struct vhost_dev
*dev
,
833 struct vhost_memory
*mem
,
834 bool reply_supported
,
835 bool config_mem_slots
)
837 struct vhost_user
*u
= dev
->opaque
;
838 int fds
[VHOST_MEMORY_MAX_NREGIONS
];
840 VhostUserMsg msg_reply
;
844 .hdr
.flags
= VHOST_USER_VERSION
,
847 if (u
->region_rb_len
< dev
->mem
->nregions
) {
848 u
->region_rb
= g_renew(RAMBlock
*, u
->region_rb
, dev
->mem
->nregions
);
849 u
->region_rb_offset
= g_renew(ram_addr_t
, u
->region_rb_offset
,
851 memset(&(u
->region_rb
[u
->region_rb_len
]), '\0',
852 sizeof(RAMBlock
*) * (dev
->mem
->nregions
- u
->region_rb_len
));
853 memset(&(u
->region_rb_offset
[u
->region_rb_len
]), '\0',
854 sizeof(ram_addr_t
) * (dev
->mem
->nregions
- u
->region_rb_len
));
855 u
->region_rb_len
= dev
->mem
->nregions
;
858 if (config_mem_slots
) {
859 if (vhost_user_add_remove_regions(dev
, &msg
, reply_supported
,
864 if (vhost_user_fill_set_mem_table_msg(u
, dev
, &msg
, fds
, &fd_num
,
869 if (vhost_user_write(dev
, &msg
, fds
, fd_num
) < 0) {
873 if (vhost_user_read(dev
, &msg_reply
) < 0) {
877 if (msg_reply
.hdr
.request
!= VHOST_USER_SET_MEM_TABLE
) {
878 error_report("%s: Received unexpected msg type."
879 "Expected %d received %d", __func__
,
880 VHOST_USER_SET_MEM_TABLE
, msg_reply
.hdr
.request
);
885 * We're using the same structure, just reusing one of the
886 * fields, so it should be the same size.
888 if (msg_reply
.hdr
.size
!= msg
.hdr
.size
) {
889 error_report("%s: Unexpected size for postcopy reply "
890 "%d vs %d", __func__
, msg_reply
.hdr
.size
,
895 memset(u
->postcopy_client_bases
, 0,
896 sizeof(uint64_t) * VHOST_MEMORY_MAX_NREGIONS
);
899 * They're in the same order as the regions that were sent
900 * but some of the regions were skipped (above) if they
903 for (msg_i
= 0, region_i
= 0;
904 region_i
< dev
->mem
->nregions
;
906 if (msg_i
< fd_num
&&
907 msg_reply
.payload
.memory
.regions
[msg_i
].guest_phys_addr
==
908 dev
->mem
->regions
[region_i
].guest_phys_addr
) {
909 u
->postcopy_client_bases
[region_i
] =
910 msg_reply
.payload
.memory
.regions
[msg_i
].userspace_addr
;
911 trace_vhost_user_set_mem_table_postcopy(
912 msg_reply
.payload
.memory
.regions
[msg_i
].userspace_addr
,
913 msg
.payload
.memory
.regions
[msg_i
].userspace_addr
,
918 if (msg_i
!= fd_num
) {
919 error_report("%s: postcopy reply not fully consumed "
921 __func__
, msg_i
, fd_num
);
926 * Now we've registered this with the postcopy code, we ack to the
927 * client, because now we're in the position to be able to deal
928 * with any faults it generates.
930 /* TODO: Use this for failure cases as well with a bad value. */
931 msg
.hdr
.size
= sizeof(msg
.payload
.u64
);
932 msg
.payload
.u64
= 0; /* OK */
933 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
941 static int vhost_user_set_mem_table(struct vhost_dev
*dev
,
942 struct vhost_memory
*mem
)
944 struct vhost_user
*u
= dev
->opaque
;
945 int fds
[VHOST_MEMORY_MAX_NREGIONS
];
947 bool do_postcopy
= u
->postcopy_listen
&& u
->postcopy_fd
.handler
;
948 bool reply_supported
= virtio_has_feature(dev
->protocol_features
,
949 VHOST_USER_PROTOCOL_F_REPLY_ACK
);
950 bool config_mem_slots
=
951 virtio_has_feature(dev
->protocol_features
,
952 VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS
);
956 * Postcopy has enough differences that it's best done in it's own
959 return vhost_user_set_mem_table_postcopy(dev
, mem
, reply_supported
,
964 .hdr
.flags
= VHOST_USER_VERSION
,
967 if (reply_supported
) {
968 msg
.hdr
.flags
|= VHOST_USER_NEED_REPLY_MASK
;
971 if (config_mem_slots
) {
972 if (vhost_user_add_remove_regions(dev
, &msg
, reply_supported
,
977 if (vhost_user_fill_set_mem_table_msg(u
, dev
, &msg
, fds
, &fd_num
,
981 if (vhost_user_write(dev
, &msg
, fds
, fd_num
) < 0) {
985 if (reply_supported
) {
986 return process_message_reply(dev
, &msg
);
993 static int vhost_user_set_vring_addr(struct vhost_dev
*dev
,
994 struct vhost_vring_addr
*addr
)
997 .hdr
.request
= VHOST_USER_SET_VRING_ADDR
,
998 .hdr
.flags
= VHOST_USER_VERSION
,
999 .payload
.addr
= *addr
,
1000 .hdr
.size
= sizeof(msg
.payload
.addr
),
1003 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
1010 static int vhost_user_set_vring_endian(struct vhost_dev
*dev
,
1011 struct vhost_vring_state
*ring
)
1013 bool cross_endian
= virtio_has_feature(dev
->protocol_features
,
1014 VHOST_USER_PROTOCOL_F_CROSS_ENDIAN
);
1015 VhostUserMsg msg
= {
1016 .hdr
.request
= VHOST_USER_SET_VRING_ENDIAN
,
1017 .hdr
.flags
= VHOST_USER_VERSION
,
1018 .payload
.state
= *ring
,
1019 .hdr
.size
= sizeof(msg
.payload
.state
),
1022 if (!cross_endian
) {
1023 error_report("vhost-user trying to send unhandled ioctl");
1027 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
1034 static int vhost_set_vring(struct vhost_dev
*dev
,
1035 unsigned long int request
,
1036 struct vhost_vring_state
*ring
)
1038 VhostUserMsg msg
= {
1039 .hdr
.request
= request
,
1040 .hdr
.flags
= VHOST_USER_VERSION
,
1041 .payload
.state
= *ring
,
1042 .hdr
.size
= sizeof(msg
.payload
.state
),
1045 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
1052 static int vhost_user_set_vring_num(struct vhost_dev
*dev
,
1053 struct vhost_vring_state
*ring
)
1055 return vhost_set_vring(dev
, VHOST_USER_SET_VRING_NUM
, ring
);
1058 static void vhost_user_host_notifier_restore(struct vhost_dev
*dev
,
1061 struct vhost_user
*u
= dev
->opaque
;
1062 VhostUserHostNotifier
*n
= &u
->user
->notifier
[queue_idx
];
1063 VirtIODevice
*vdev
= dev
->vdev
;
1065 if (n
->addr
&& !n
->set
) {
1066 virtio_queue_set_host_notifier_mr(vdev
, queue_idx
, &n
->mr
, true);
1071 static void vhost_user_host_notifier_remove(struct vhost_dev
*dev
,
1074 struct vhost_user
*u
= dev
->opaque
;
1075 VhostUserHostNotifier
*n
= &u
->user
->notifier
[queue_idx
];
1076 VirtIODevice
*vdev
= dev
->vdev
;
1078 if (n
->addr
&& n
->set
) {
1079 virtio_queue_set_host_notifier_mr(vdev
, queue_idx
, &n
->mr
, false);
1084 static int vhost_user_set_vring_base(struct vhost_dev
*dev
,
1085 struct vhost_vring_state
*ring
)
1087 vhost_user_host_notifier_restore(dev
, ring
->index
);
1089 return vhost_set_vring(dev
, VHOST_USER_SET_VRING_BASE
, ring
);
1092 static int vhost_user_set_vring_enable(struct vhost_dev
*dev
, int enable
)
1096 if (!virtio_has_feature(dev
->features
, VHOST_USER_F_PROTOCOL_FEATURES
)) {
1100 for (i
= 0; i
< dev
->nvqs
; ++i
) {
1101 struct vhost_vring_state state
= {
1102 .index
= dev
->vq_index
+ i
,
1106 vhost_set_vring(dev
, VHOST_USER_SET_VRING_ENABLE
, &state
);
1112 static int vhost_user_get_vring_base(struct vhost_dev
*dev
,
1113 struct vhost_vring_state
*ring
)
1115 VhostUserMsg msg
= {
1116 .hdr
.request
= VHOST_USER_GET_VRING_BASE
,
1117 .hdr
.flags
= VHOST_USER_VERSION
,
1118 .payload
.state
= *ring
,
1119 .hdr
.size
= sizeof(msg
.payload
.state
),
1122 vhost_user_host_notifier_remove(dev
, ring
->index
);
1124 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
1128 if (vhost_user_read(dev
, &msg
) < 0) {
1132 if (msg
.hdr
.request
!= VHOST_USER_GET_VRING_BASE
) {
1133 error_report("Received unexpected msg type. Expected %d received %d",
1134 VHOST_USER_GET_VRING_BASE
, msg
.hdr
.request
);
1138 if (msg
.hdr
.size
!= sizeof(msg
.payload
.state
)) {
1139 error_report("Received bad msg size.");
1143 *ring
= msg
.payload
.state
;
1148 static int vhost_set_vring_file(struct vhost_dev
*dev
,
1149 VhostUserRequest request
,
1150 struct vhost_vring_file
*file
)
1152 int fds
[VHOST_MEMORY_MAX_NREGIONS
];
1154 VhostUserMsg msg
= {
1155 .hdr
.request
= request
,
1156 .hdr
.flags
= VHOST_USER_VERSION
,
1157 .payload
.u64
= file
->index
& VHOST_USER_VRING_IDX_MASK
,
1158 .hdr
.size
= sizeof(msg
.payload
.u64
),
1161 if (ioeventfd_enabled() && file
->fd
> 0) {
1162 fds
[fd_num
++] = file
->fd
;
1164 msg
.payload
.u64
|= VHOST_USER_VRING_NOFD_MASK
;
1167 if (vhost_user_write(dev
, &msg
, fds
, fd_num
) < 0) {
1174 static int vhost_user_set_vring_kick(struct vhost_dev
*dev
,
1175 struct vhost_vring_file
*file
)
1177 return vhost_set_vring_file(dev
, VHOST_USER_SET_VRING_KICK
, file
);
1180 static int vhost_user_set_vring_call(struct vhost_dev
*dev
,
1181 struct vhost_vring_file
*file
)
1183 return vhost_set_vring_file(dev
, VHOST_USER_SET_VRING_CALL
, file
);
1186 static int vhost_user_set_u64(struct vhost_dev
*dev
, int request
, uint64_t u64
)
1188 VhostUserMsg msg
= {
1189 .hdr
.request
= request
,
1190 .hdr
.flags
= VHOST_USER_VERSION
,
1192 .hdr
.size
= sizeof(msg
.payload
.u64
),
1195 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
1202 static int vhost_user_set_features(struct vhost_dev
*dev
,
1205 return vhost_user_set_u64(dev
, VHOST_USER_SET_FEATURES
, features
);
1208 static int vhost_user_set_protocol_features(struct vhost_dev
*dev
,
1211 return vhost_user_set_u64(dev
, VHOST_USER_SET_PROTOCOL_FEATURES
, features
);
1214 static int vhost_user_get_u64(struct vhost_dev
*dev
, int request
, uint64_t *u64
)
1216 VhostUserMsg msg
= {
1217 .hdr
.request
= request
,
1218 .hdr
.flags
= VHOST_USER_VERSION
,
1221 if (vhost_user_one_time_request(request
) && dev
->vq_index
!= 0) {
1225 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
1229 if (vhost_user_read(dev
, &msg
) < 0) {
1233 if (msg
.hdr
.request
!= request
) {
1234 error_report("Received unexpected msg type. Expected %d received %d",
1235 request
, msg
.hdr
.request
);
1239 if (msg
.hdr
.size
!= sizeof(msg
.payload
.u64
)) {
1240 error_report("Received bad msg size.");
1244 *u64
= msg
.payload
.u64
;
1249 static int vhost_user_get_features(struct vhost_dev
*dev
, uint64_t *features
)
1251 return vhost_user_get_u64(dev
, VHOST_USER_GET_FEATURES
, features
);
1254 static int vhost_user_set_owner(struct vhost_dev
*dev
)
1256 VhostUserMsg msg
= {
1257 .hdr
.request
= VHOST_USER_SET_OWNER
,
1258 .hdr
.flags
= VHOST_USER_VERSION
,
1261 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
1268 static int vhost_user_get_max_memslots(struct vhost_dev
*dev
,
1269 uint64_t *max_memslots
)
1271 uint64_t backend_max_memslots
;
1274 err
= vhost_user_get_u64(dev
, VHOST_USER_GET_MAX_MEM_SLOTS
,
1275 &backend_max_memslots
);
1280 *max_memslots
= backend_max_memslots
;
1285 static int vhost_user_reset_device(struct vhost_dev
*dev
)
1287 VhostUserMsg msg
= {
1288 .hdr
.flags
= VHOST_USER_VERSION
,
1291 msg
.hdr
.request
= virtio_has_feature(dev
->protocol_features
,
1292 VHOST_USER_PROTOCOL_F_RESET_DEVICE
)
1293 ? VHOST_USER_RESET_DEVICE
1294 : VHOST_USER_RESET_OWNER
;
1296 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
1303 static int vhost_user_slave_handle_config_change(struct vhost_dev
*dev
)
1307 if (!dev
->config_ops
) {
1311 if (dev
->config_ops
->vhost_dev_config_notifier
) {
1312 ret
= dev
->config_ops
->vhost_dev_config_notifier(dev
);
1318 static int vhost_user_slave_handle_vring_host_notifier(struct vhost_dev
*dev
,
1319 VhostUserVringArea
*area
,
1322 int queue_idx
= area
->u64
& VHOST_USER_VRING_IDX_MASK
;
1323 size_t page_size
= qemu_real_host_page_size
;
1324 struct vhost_user
*u
= dev
->opaque
;
1325 VhostUserState
*user
= u
->user
;
1326 VirtIODevice
*vdev
= dev
->vdev
;
1327 VhostUserHostNotifier
*n
;
1331 if (!virtio_has_feature(dev
->protocol_features
,
1332 VHOST_USER_PROTOCOL_F_HOST_NOTIFIER
) ||
1333 vdev
== NULL
|| queue_idx
>= virtio_get_num_queues(vdev
)) {
1337 n
= &user
->notifier
[queue_idx
];
1340 virtio_queue_set_host_notifier_mr(vdev
, queue_idx
, &n
->mr
, false);
1341 object_unparent(OBJECT(&n
->mr
));
1342 munmap(n
->addr
, page_size
);
1346 if (area
->u64
& VHOST_USER_VRING_NOFD_MASK
) {
1351 if (area
->size
!= page_size
) {
1355 addr
= mmap(NULL
, page_size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
,
1357 if (addr
== MAP_FAILED
) {
1361 name
= g_strdup_printf("vhost-user/host-notifier@%p mmaps[%d]",
1363 memory_region_init_ram_device_ptr(&n
->mr
, OBJECT(vdev
), name
,
1367 if (virtio_queue_set_host_notifier_mr(vdev
, queue_idx
, &n
->mr
, true)) {
1368 munmap(addr
, page_size
);
1378 static void slave_read(void *opaque
)
1380 struct vhost_dev
*dev
= opaque
;
1381 struct vhost_user
*u
= dev
->opaque
;
1382 VhostUserHeader hdr
= { 0, };
1383 VhostUserPayload payload
= { 0, };
1387 int fd
[VHOST_USER_SLAVE_MAX_FDS
];
1388 char control
[CMSG_SPACE(sizeof(fd
))];
1389 struct cmsghdr
*cmsg
;
1392 memset(&msgh
, 0, sizeof(msgh
));
1393 msgh
.msg_iov
= &iov
;
1394 msgh
.msg_iovlen
= 1;
1395 msgh
.msg_control
= control
;
1396 msgh
.msg_controllen
= sizeof(control
);
1398 memset(fd
, -1, sizeof(fd
));
1401 iov
.iov_base
= &hdr
;
1402 iov
.iov_len
= VHOST_USER_HDR_SIZE
;
1405 size
= recvmsg(u
->slave_fd
, &msgh
, 0);
1406 } while (size
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
1408 if (size
!= VHOST_USER_HDR_SIZE
) {
1409 error_report("Failed to read from slave.");
1413 if (msgh
.msg_flags
& MSG_CTRUNC
) {
1414 error_report("Truncated message.");
1418 for (cmsg
= CMSG_FIRSTHDR(&msgh
); cmsg
!= NULL
;
1419 cmsg
= CMSG_NXTHDR(&msgh
, cmsg
)) {
1420 if (cmsg
->cmsg_level
== SOL_SOCKET
&&
1421 cmsg
->cmsg_type
== SCM_RIGHTS
) {
1422 fdsize
= cmsg
->cmsg_len
- CMSG_LEN(0);
1423 memcpy(fd
, CMSG_DATA(cmsg
), fdsize
);
1428 if (hdr
.size
> VHOST_USER_PAYLOAD_SIZE
) {
1429 error_report("Failed to read msg header."
1430 " Size %d exceeds the maximum %zu.", hdr
.size
,
1431 VHOST_USER_PAYLOAD_SIZE
);
1437 size
= read(u
->slave_fd
, &payload
, hdr
.size
);
1438 } while (size
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
1440 if (size
!= hdr
.size
) {
1441 error_report("Failed to read payload from slave.");
1445 switch (hdr
.request
) {
1446 case VHOST_USER_SLAVE_IOTLB_MSG
:
1447 ret
= vhost_backend_handle_iotlb_msg(dev
, &payload
.iotlb
);
1449 case VHOST_USER_SLAVE_CONFIG_CHANGE_MSG
:
1450 ret
= vhost_user_slave_handle_config_change(dev
);
1452 case VHOST_USER_SLAVE_VRING_HOST_NOTIFIER_MSG
:
1453 ret
= vhost_user_slave_handle_vring_host_notifier(dev
, &payload
.area
,
1457 error_report("Received unexpected msg type: %d.", hdr
.request
);
1461 /* Close the remaining file descriptors. */
1462 for (i
= 0; i
< fdsize
; i
++) {
1469 * REPLY_ACK feature handling. Other reply types has to be managed
1470 * directly in their request handlers.
1472 if (hdr
.flags
& VHOST_USER_NEED_REPLY_MASK
) {
1473 struct iovec iovec
[2];
1476 hdr
.flags
&= ~VHOST_USER_NEED_REPLY_MASK
;
1477 hdr
.flags
|= VHOST_USER_REPLY_MASK
;
1479 payload
.u64
= !!ret
;
1480 hdr
.size
= sizeof(payload
.u64
);
1482 iovec
[0].iov_base
= &hdr
;
1483 iovec
[0].iov_len
= VHOST_USER_HDR_SIZE
;
1484 iovec
[1].iov_base
= &payload
;
1485 iovec
[1].iov_len
= hdr
.size
;
1488 size
= writev(u
->slave_fd
, iovec
, ARRAY_SIZE(iovec
));
1489 } while (size
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
1491 if (size
!= VHOST_USER_HDR_SIZE
+ hdr
.size
) {
1492 error_report("Failed to send msg reply to slave.");
1500 qemu_set_fd_handler(u
->slave_fd
, NULL
, NULL
, NULL
);
1503 for (i
= 0; i
< fdsize
; i
++) {
1511 static int vhost_setup_slave_channel(struct vhost_dev
*dev
)
1513 VhostUserMsg msg
= {
1514 .hdr
.request
= VHOST_USER_SET_SLAVE_REQ_FD
,
1515 .hdr
.flags
= VHOST_USER_VERSION
,
1517 struct vhost_user
*u
= dev
->opaque
;
1519 bool reply_supported
= virtio_has_feature(dev
->protocol_features
,
1520 VHOST_USER_PROTOCOL_F_REPLY_ACK
);
1522 if (!virtio_has_feature(dev
->protocol_features
,
1523 VHOST_USER_PROTOCOL_F_SLAVE_REQ
)) {
1527 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, sv
) == -1) {
1528 error_report("socketpair() failed");
1532 u
->slave_fd
= sv
[0];
1533 qemu_set_fd_handler(u
->slave_fd
, slave_read
, NULL
, dev
);
1535 if (reply_supported
) {
1536 msg
.hdr
.flags
|= VHOST_USER_NEED_REPLY_MASK
;
1539 ret
= vhost_user_write(dev
, &msg
, &sv
[1], 1);
1544 if (reply_supported
) {
1545 ret
= process_message_reply(dev
, &msg
);
1551 qemu_set_fd_handler(u
->slave_fd
, NULL
, NULL
, NULL
);
1561 * Called back from the postcopy fault thread when a fault is received on our
1563 * TODO: This is Linux specific
1565 static int vhost_user_postcopy_fault_handler(struct PostCopyFD
*pcfd
,
1568 struct vhost_dev
*dev
= pcfd
->data
;
1569 struct vhost_user
*u
= dev
->opaque
;
1570 struct uffd_msg
*msg
= ufd
;
1571 uint64_t faultaddr
= msg
->arg
.pagefault
.address
;
1572 RAMBlock
*rb
= NULL
;
1576 trace_vhost_user_postcopy_fault_handler(pcfd
->idstr
, faultaddr
,
1577 dev
->mem
->nregions
);
1578 for (i
= 0; i
< MIN(dev
->mem
->nregions
, u
->region_rb_len
); i
++) {
1579 trace_vhost_user_postcopy_fault_handler_loop(i
,
1580 u
->postcopy_client_bases
[i
], dev
->mem
->regions
[i
].memory_size
);
1581 if (faultaddr
>= u
->postcopy_client_bases
[i
]) {
1582 /* Ofset of the fault address in the vhost region */
1583 uint64_t region_offset
= faultaddr
- u
->postcopy_client_bases
[i
];
1584 if (region_offset
< dev
->mem
->regions
[i
].memory_size
) {
1585 rb_offset
= region_offset
+ u
->region_rb_offset
[i
];
1586 trace_vhost_user_postcopy_fault_handler_found(i
,
1587 region_offset
, rb_offset
);
1588 rb
= u
->region_rb
[i
];
1589 return postcopy_request_shared_page(pcfd
, rb
, faultaddr
,
1594 error_report("%s: Failed to find region for fault %" PRIx64
,
1595 __func__
, faultaddr
);
1599 static int vhost_user_postcopy_waker(struct PostCopyFD
*pcfd
, RAMBlock
*rb
,
1602 struct vhost_dev
*dev
= pcfd
->data
;
1603 struct vhost_user
*u
= dev
->opaque
;
1606 trace_vhost_user_postcopy_waker(qemu_ram_get_idstr(rb
), offset
);
1611 /* Translate the offset into an address in the clients address space */
1612 for (i
= 0; i
< MIN(dev
->mem
->nregions
, u
->region_rb_len
); i
++) {
1613 if (u
->region_rb
[i
] == rb
&&
1614 offset
>= u
->region_rb_offset
[i
] &&
1615 offset
< (u
->region_rb_offset
[i
] +
1616 dev
->mem
->regions
[i
].memory_size
)) {
1617 uint64_t client_addr
= (offset
- u
->region_rb_offset
[i
]) +
1618 u
->postcopy_client_bases
[i
];
1619 trace_vhost_user_postcopy_waker_found(client_addr
);
1620 return postcopy_wake_shared(pcfd
, client_addr
, rb
);
1624 trace_vhost_user_postcopy_waker_nomatch(qemu_ram_get_idstr(rb
), offset
);
1630 * Called at the start of an inbound postcopy on reception of the
1633 static int vhost_user_postcopy_advise(struct vhost_dev
*dev
, Error
**errp
)
1636 struct vhost_user
*u
= dev
->opaque
;
1637 CharBackend
*chr
= u
->user
->chr
;
1639 VhostUserMsg msg
= {
1640 .hdr
.request
= VHOST_USER_POSTCOPY_ADVISE
,
1641 .hdr
.flags
= VHOST_USER_VERSION
,
1644 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
1645 error_setg(errp
, "Failed to send postcopy_advise to vhost");
1649 if (vhost_user_read(dev
, &msg
) < 0) {
1650 error_setg(errp
, "Failed to get postcopy_advise reply from vhost");
1654 if (msg
.hdr
.request
!= VHOST_USER_POSTCOPY_ADVISE
) {
1655 error_setg(errp
, "Unexpected msg type. Expected %d received %d",
1656 VHOST_USER_POSTCOPY_ADVISE
, msg
.hdr
.request
);
1661 error_setg(errp
, "Received bad msg size.");
1664 ufd
= qemu_chr_fe_get_msgfd(chr
);
1666 error_setg(errp
, "%s: Failed to get ufd", __func__
);
1669 qemu_set_nonblock(ufd
);
1671 /* register ufd with userfault thread */
1672 u
->postcopy_fd
.fd
= ufd
;
1673 u
->postcopy_fd
.data
= dev
;
1674 u
->postcopy_fd
.handler
= vhost_user_postcopy_fault_handler
;
1675 u
->postcopy_fd
.waker
= vhost_user_postcopy_waker
;
1676 u
->postcopy_fd
.idstr
= "vhost-user"; /* Need to find unique name */
1677 postcopy_register_shared_ufd(&u
->postcopy_fd
);
1680 error_setg(errp
, "Postcopy not supported on non-Linux systems");
1686 * Called at the switch to postcopy on reception of the 'listen' command.
1688 static int vhost_user_postcopy_listen(struct vhost_dev
*dev
, Error
**errp
)
1690 struct vhost_user
*u
= dev
->opaque
;
1692 VhostUserMsg msg
= {
1693 .hdr
.request
= VHOST_USER_POSTCOPY_LISTEN
,
1694 .hdr
.flags
= VHOST_USER_VERSION
| VHOST_USER_NEED_REPLY_MASK
,
1696 u
->postcopy_listen
= true;
1697 trace_vhost_user_postcopy_listen();
1698 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
1699 error_setg(errp
, "Failed to send postcopy_listen to vhost");
1703 ret
= process_message_reply(dev
, &msg
);
1705 error_setg(errp
, "Failed to receive reply to postcopy_listen");
1713 * Called at the end of postcopy
1715 static int vhost_user_postcopy_end(struct vhost_dev
*dev
, Error
**errp
)
1717 VhostUserMsg msg
= {
1718 .hdr
.request
= VHOST_USER_POSTCOPY_END
,
1719 .hdr
.flags
= VHOST_USER_VERSION
| VHOST_USER_NEED_REPLY_MASK
,
1722 struct vhost_user
*u
= dev
->opaque
;
1724 trace_vhost_user_postcopy_end_entry();
1725 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
1726 error_setg(errp
, "Failed to send postcopy_end to vhost");
1730 ret
= process_message_reply(dev
, &msg
);
1732 error_setg(errp
, "Failed to receive reply to postcopy_end");
1735 postcopy_unregister_shared_ufd(&u
->postcopy_fd
);
1736 close(u
->postcopy_fd
.fd
);
1737 u
->postcopy_fd
.handler
= NULL
;
1739 trace_vhost_user_postcopy_end_exit();
1744 static int vhost_user_postcopy_notifier(NotifierWithReturn
*notifier
,
1747 struct PostcopyNotifyData
*pnd
= opaque
;
1748 struct vhost_user
*u
= container_of(notifier
, struct vhost_user
,
1750 struct vhost_dev
*dev
= u
->dev
;
1752 switch (pnd
->reason
) {
1753 case POSTCOPY_NOTIFY_PROBE
:
1754 if (!virtio_has_feature(dev
->protocol_features
,
1755 VHOST_USER_PROTOCOL_F_PAGEFAULT
)) {
1756 /* TODO: Get the device name into this error somehow */
1757 error_setg(pnd
->errp
,
1758 "vhost-user backend not capable of postcopy");
1763 case POSTCOPY_NOTIFY_INBOUND_ADVISE
:
1764 return vhost_user_postcopy_advise(dev
, pnd
->errp
);
1766 case POSTCOPY_NOTIFY_INBOUND_LISTEN
:
1767 return vhost_user_postcopy_listen(dev
, pnd
->errp
);
1769 case POSTCOPY_NOTIFY_INBOUND_END
:
1770 return vhost_user_postcopy_end(dev
, pnd
->errp
);
1773 /* We ignore notifications we don't know */
1780 static int vhost_user_backend_init(struct vhost_dev
*dev
, void *opaque
)
1782 uint64_t features
, protocol_features
, ram_slots
;
1783 struct vhost_user
*u
;
1786 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
1788 u
= g_new0(struct vhost_user
, 1);
1794 err
= vhost_user_get_features(dev
, &features
);
1799 if (virtio_has_feature(features
, VHOST_USER_F_PROTOCOL_FEATURES
)) {
1800 dev
->backend_features
|= 1ULL << VHOST_USER_F_PROTOCOL_FEATURES
;
1802 err
= vhost_user_get_u64(dev
, VHOST_USER_GET_PROTOCOL_FEATURES
,
1803 &protocol_features
);
1808 dev
->protocol_features
=
1809 protocol_features
& VHOST_USER_PROTOCOL_FEATURE_MASK
;
1811 if (!dev
->config_ops
|| !dev
->config_ops
->vhost_dev_config_notifier
) {
1812 /* Don't acknowledge CONFIG feature if device doesn't support it */
1813 dev
->protocol_features
&= ~(1ULL << VHOST_USER_PROTOCOL_F_CONFIG
);
1814 } else if (!(protocol_features
&
1815 (1ULL << VHOST_USER_PROTOCOL_F_CONFIG
))) {
1816 error_report("Device expects VHOST_USER_PROTOCOL_F_CONFIG "
1817 "but backend does not support it.");
1821 err
= vhost_user_set_protocol_features(dev
, dev
->protocol_features
);
1826 /* query the max queues we support if backend supports Multiple Queue */
1827 if (dev
->protocol_features
& (1ULL << VHOST_USER_PROTOCOL_F_MQ
)) {
1828 err
= vhost_user_get_u64(dev
, VHOST_USER_GET_QUEUE_NUM
,
1835 if (virtio_has_feature(features
, VIRTIO_F_IOMMU_PLATFORM
) &&
1836 !(virtio_has_feature(dev
->protocol_features
,
1837 VHOST_USER_PROTOCOL_F_SLAVE_REQ
) &&
1838 virtio_has_feature(dev
->protocol_features
,
1839 VHOST_USER_PROTOCOL_F_REPLY_ACK
))) {
1840 error_report("IOMMU support requires reply-ack and "
1841 "slave-req protocol features.");
1845 /* get max memory regions if backend supports configurable RAM slots */
1846 if (!virtio_has_feature(dev
->protocol_features
,
1847 VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS
)) {
1848 u
->user
->memory_slots
= VHOST_MEMORY_MAX_NREGIONS
;
1850 err
= vhost_user_get_max_memslots(dev
, &ram_slots
);
1855 if (ram_slots
< u
->user
->memory_slots
) {
1856 error_report("The backend specified a max ram slots limit "
1857 "of %" PRIu64
", when the prior validated limit was %d. "
1858 "This limit should never decrease.", ram_slots
,
1859 u
->user
->memory_slots
);
1863 u
->user
->memory_slots
= MIN(ram_slots
, VHOST_MEMORY_MAX_NREGIONS
);
1867 if (dev
->migration_blocker
== NULL
&&
1868 !virtio_has_feature(dev
->protocol_features
,
1869 VHOST_USER_PROTOCOL_F_LOG_SHMFD
)) {
1870 error_setg(&dev
->migration_blocker
,
1871 "Migration disabled: vhost-user backend lacks "
1872 "VHOST_USER_PROTOCOL_F_LOG_SHMFD feature.");
1875 if (dev
->vq_index
== 0) {
1876 err
= vhost_setup_slave_channel(dev
);
1882 u
->postcopy_notifier
.notify
= vhost_user_postcopy_notifier
;
1883 postcopy_add_notifier(&u
->postcopy_notifier
);
1888 static int vhost_user_backend_cleanup(struct vhost_dev
*dev
)
1890 struct vhost_user
*u
;
1892 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
1895 if (u
->postcopy_notifier
.notify
) {
1896 postcopy_remove_notifier(&u
->postcopy_notifier
);
1897 u
->postcopy_notifier
.notify
= NULL
;
1899 u
->postcopy_listen
= false;
1900 if (u
->postcopy_fd
.handler
) {
1901 postcopy_unregister_shared_ufd(&u
->postcopy_fd
);
1902 close(u
->postcopy_fd
.fd
);
1903 u
->postcopy_fd
.handler
= NULL
;
1905 if (u
->slave_fd
>= 0) {
1906 qemu_set_fd_handler(u
->slave_fd
, NULL
, NULL
, NULL
);
1910 g_free(u
->region_rb
);
1911 u
->region_rb
= NULL
;
1912 g_free(u
->region_rb_offset
);
1913 u
->region_rb_offset
= NULL
;
1914 u
->region_rb_len
= 0;
1921 static int vhost_user_get_vq_index(struct vhost_dev
*dev
, int idx
)
1923 assert(idx
>= dev
->vq_index
&& idx
< dev
->vq_index
+ dev
->nvqs
);
1928 static int vhost_user_memslots_limit(struct vhost_dev
*dev
)
1930 struct vhost_user
*u
= dev
->opaque
;
1932 return u
->user
->memory_slots
;
1935 static bool vhost_user_requires_shm_log(struct vhost_dev
*dev
)
1937 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
1939 return virtio_has_feature(dev
->protocol_features
,
1940 VHOST_USER_PROTOCOL_F_LOG_SHMFD
);
1943 static int vhost_user_migration_done(struct vhost_dev
*dev
, char* mac_addr
)
1945 VhostUserMsg msg
= { };
1947 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
1949 /* If guest supports GUEST_ANNOUNCE do nothing */
1950 if (virtio_has_feature(dev
->acked_features
, VIRTIO_NET_F_GUEST_ANNOUNCE
)) {
1954 /* if backend supports VHOST_USER_PROTOCOL_F_RARP ask it to send the RARP */
1955 if (virtio_has_feature(dev
->protocol_features
,
1956 VHOST_USER_PROTOCOL_F_RARP
)) {
1957 msg
.hdr
.request
= VHOST_USER_SEND_RARP
;
1958 msg
.hdr
.flags
= VHOST_USER_VERSION
;
1959 memcpy((char *)&msg
.payload
.u64
, mac_addr
, 6);
1960 msg
.hdr
.size
= sizeof(msg
.payload
.u64
);
1962 return vhost_user_write(dev
, &msg
, NULL
, 0);
1967 static bool vhost_user_can_merge(struct vhost_dev
*dev
,
1968 uint64_t start1
, uint64_t size1
,
1969 uint64_t start2
, uint64_t size2
)
1974 (void)vhost_user_get_mr_data(start1
, &offset
, &mfd
);
1975 (void)vhost_user_get_mr_data(start2
, &offset
, &rfd
);
1980 static int vhost_user_net_set_mtu(struct vhost_dev
*dev
, uint16_t mtu
)
1983 bool reply_supported
= virtio_has_feature(dev
->protocol_features
,
1984 VHOST_USER_PROTOCOL_F_REPLY_ACK
);
1986 if (!(dev
->protocol_features
& (1ULL << VHOST_USER_PROTOCOL_F_NET_MTU
))) {
1990 msg
.hdr
.request
= VHOST_USER_NET_SET_MTU
;
1991 msg
.payload
.u64
= mtu
;
1992 msg
.hdr
.size
= sizeof(msg
.payload
.u64
);
1993 msg
.hdr
.flags
= VHOST_USER_VERSION
;
1994 if (reply_supported
) {
1995 msg
.hdr
.flags
|= VHOST_USER_NEED_REPLY_MASK
;
1998 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
2002 /* If reply_ack supported, slave has to ack specified MTU is valid */
2003 if (reply_supported
) {
2004 return process_message_reply(dev
, &msg
);
2010 static int vhost_user_send_device_iotlb_msg(struct vhost_dev
*dev
,
2011 struct vhost_iotlb_msg
*imsg
)
2013 VhostUserMsg msg
= {
2014 .hdr
.request
= VHOST_USER_IOTLB_MSG
,
2015 .hdr
.size
= sizeof(msg
.payload
.iotlb
),
2016 .hdr
.flags
= VHOST_USER_VERSION
| VHOST_USER_NEED_REPLY_MASK
,
2017 .payload
.iotlb
= *imsg
,
2020 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
2024 return process_message_reply(dev
, &msg
);
2028 static void vhost_user_set_iotlb_callback(struct vhost_dev
*dev
, int enabled
)
2030 /* No-op as the receive channel is not dedicated to IOTLB messages. */
2033 static int vhost_user_get_config(struct vhost_dev
*dev
, uint8_t *config
,
2034 uint32_t config_len
)
2036 VhostUserMsg msg
= {
2037 .hdr
.request
= VHOST_USER_GET_CONFIG
,
2038 .hdr
.flags
= VHOST_USER_VERSION
,
2039 .hdr
.size
= VHOST_USER_CONFIG_HDR_SIZE
+ config_len
,
2042 if (!virtio_has_feature(dev
->protocol_features
,
2043 VHOST_USER_PROTOCOL_F_CONFIG
)) {
2047 if (config_len
> VHOST_USER_MAX_CONFIG_SIZE
) {
2051 msg
.payload
.config
.offset
= 0;
2052 msg
.payload
.config
.size
= config_len
;
2053 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
2057 if (vhost_user_read(dev
, &msg
) < 0) {
2061 if (msg
.hdr
.request
!= VHOST_USER_GET_CONFIG
) {
2062 error_report("Received unexpected msg type. Expected %d received %d",
2063 VHOST_USER_GET_CONFIG
, msg
.hdr
.request
);
2067 if (msg
.hdr
.size
!= VHOST_USER_CONFIG_HDR_SIZE
+ config_len
) {
2068 error_report("Received bad msg size.");
2072 memcpy(config
, msg
.payload
.config
.region
, config_len
);
2077 static int vhost_user_set_config(struct vhost_dev
*dev
, const uint8_t *data
,
2078 uint32_t offset
, uint32_t size
, uint32_t flags
)
2081 bool reply_supported
= virtio_has_feature(dev
->protocol_features
,
2082 VHOST_USER_PROTOCOL_F_REPLY_ACK
);
2084 VhostUserMsg msg
= {
2085 .hdr
.request
= VHOST_USER_SET_CONFIG
,
2086 .hdr
.flags
= VHOST_USER_VERSION
,
2087 .hdr
.size
= VHOST_USER_CONFIG_HDR_SIZE
+ size
,
2090 if (!virtio_has_feature(dev
->protocol_features
,
2091 VHOST_USER_PROTOCOL_F_CONFIG
)) {
2095 if (reply_supported
) {
2096 msg
.hdr
.flags
|= VHOST_USER_NEED_REPLY_MASK
;
2099 if (size
> VHOST_USER_MAX_CONFIG_SIZE
) {
2103 msg
.payload
.config
.offset
= offset
,
2104 msg
.payload
.config
.size
= size
,
2105 msg
.payload
.config
.flags
= flags
,
2106 p
= msg
.payload
.config
.region
;
2107 memcpy(p
, data
, size
);
2109 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
2113 if (reply_supported
) {
2114 return process_message_reply(dev
, &msg
);
2120 static int vhost_user_crypto_create_session(struct vhost_dev
*dev
,
2122 uint64_t *session_id
)
2124 bool crypto_session
= virtio_has_feature(dev
->protocol_features
,
2125 VHOST_USER_PROTOCOL_F_CRYPTO_SESSION
);
2126 CryptoDevBackendSymSessionInfo
*sess_info
= session_info
;
2127 VhostUserMsg msg
= {
2128 .hdr
.request
= VHOST_USER_CREATE_CRYPTO_SESSION
,
2129 .hdr
.flags
= VHOST_USER_VERSION
,
2130 .hdr
.size
= sizeof(msg
.payload
.session
),
2133 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
2135 if (!crypto_session
) {
2136 error_report("vhost-user trying to send unhandled ioctl");
2140 memcpy(&msg
.payload
.session
.session_setup_data
, sess_info
,
2141 sizeof(CryptoDevBackendSymSessionInfo
));
2142 if (sess_info
->key_len
) {
2143 memcpy(&msg
.payload
.session
.key
, sess_info
->cipher_key
,
2144 sess_info
->key_len
);
2146 if (sess_info
->auth_key_len
> 0) {
2147 memcpy(&msg
.payload
.session
.auth_key
, sess_info
->auth_key
,
2148 sess_info
->auth_key_len
);
2150 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
2151 error_report("vhost_user_write() return -1, create session failed");
2155 if (vhost_user_read(dev
, &msg
) < 0) {
2156 error_report("vhost_user_read() return -1, create session failed");
2160 if (msg
.hdr
.request
!= VHOST_USER_CREATE_CRYPTO_SESSION
) {
2161 error_report("Received unexpected msg type. Expected %d received %d",
2162 VHOST_USER_CREATE_CRYPTO_SESSION
, msg
.hdr
.request
);
2166 if (msg
.hdr
.size
!= sizeof(msg
.payload
.session
)) {
2167 error_report("Received bad msg size.");
2171 if (msg
.payload
.session
.session_id
< 0) {
2172 error_report("Bad session id: %" PRId64
"",
2173 msg
.payload
.session
.session_id
);
2176 *session_id
= msg
.payload
.session
.session_id
;
2182 vhost_user_crypto_close_session(struct vhost_dev
*dev
, uint64_t session_id
)
2184 bool crypto_session
= virtio_has_feature(dev
->protocol_features
,
2185 VHOST_USER_PROTOCOL_F_CRYPTO_SESSION
);
2186 VhostUserMsg msg
= {
2187 .hdr
.request
= VHOST_USER_CLOSE_CRYPTO_SESSION
,
2188 .hdr
.flags
= VHOST_USER_VERSION
,
2189 .hdr
.size
= sizeof(msg
.payload
.u64
),
2191 msg
.payload
.u64
= session_id
;
2193 if (!crypto_session
) {
2194 error_report("vhost-user trying to send unhandled ioctl");
2198 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
2199 error_report("vhost_user_write() return -1, close session failed");
2206 static bool vhost_user_mem_section_filter(struct vhost_dev
*dev
,
2207 MemoryRegionSection
*section
)
2211 result
= memory_region_get_fd(section
->mr
) >= 0;
2216 static int vhost_user_get_inflight_fd(struct vhost_dev
*dev
,
2217 uint16_t queue_size
,
2218 struct vhost_inflight
*inflight
)
2222 struct vhost_user
*u
= dev
->opaque
;
2223 CharBackend
*chr
= u
->user
->chr
;
2224 VhostUserMsg msg
= {
2225 .hdr
.request
= VHOST_USER_GET_INFLIGHT_FD
,
2226 .hdr
.flags
= VHOST_USER_VERSION
,
2227 .payload
.inflight
.num_queues
= dev
->nvqs
,
2228 .payload
.inflight
.queue_size
= queue_size
,
2229 .hdr
.size
= sizeof(msg
.payload
.inflight
),
2232 if (!virtio_has_feature(dev
->protocol_features
,
2233 VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD
)) {
2237 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
2241 if (vhost_user_read(dev
, &msg
) < 0) {
2245 if (msg
.hdr
.request
!= VHOST_USER_GET_INFLIGHT_FD
) {
2246 error_report("Received unexpected msg type. "
2247 "Expected %d received %d",
2248 VHOST_USER_GET_INFLIGHT_FD
, msg
.hdr
.request
);
2252 if (msg
.hdr
.size
!= sizeof(msg
.payload
.inflight
)) {
2253 error_report("Received bad msg size.");
2257 if (!msg
.payload
.inflight
.mmap_size
) {
2261 fd
= qemu_chr_fe_get_msgfd(chr
);
2263 error_report("Failed to get mem fd");
2267 addr
= mmap(0, msg
.payload
.inflight
.mmap_size
, PROT_READ
| PROT_WRITE
,
2268 MAP_SHARED
, fd
, msg
.payload
.inflight
.mmap_offset
);
2270 if (addr
== MAP_FAILED
) {
2271 error_report("Failed to mmap mem fd");
2276 inflight
->addr
= addr
;
2278 inflight
->size
= msg
.payload
.inflight
.mmap_size
;
2279 inflight
->offset
= msg
.payload
.inflight
.mmap_offset
;
2280 inflight
->queue_size
= queue_size
;
2285 static int vhost_user_set_inflight_fd(struct vhost_dev
*dev
,
2286 struct vhost_inflight
*inflight
)
2288 VhostUserMsg msg
= {
2289 .hdr
.request
= VHOST_USER_SET_INFLIGHT_FD
,
2290 .hdr
.flags
= VHOST_USER_VERSION
,
2291 .payload
.inflight
.mmap_size
= inflight
->size
,
2292 .payload
.inflight
.mmap_offset
= inflight
->offset
,
2293 .payload
.inflight
.num_queues
= dev
->nvqs
,
2294 .payload
.inflight
.queue_size
= inflight
->queue_size
,
2295 .hdr
.size
= sizeof(msg
.payload
.inflight
),
2298 if (!virtio_has_feature(dev
->protocol_features
,
2299 VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD
)) {
2303 if (vhost_user_write(dev
, &msg
, &inflight
->fd
, 1) < 0) {
2310 bool vhost_user_init(VhostUserState
*user
, CharBackend
*chr
, Error
**errp
)
2313 error_setg(errp
, "Cannot initialize vhost-user state");
2317 user
->memory_slots
= 0;
2321 void vhost_user_cleanup(VhostUserState
*user
)
2329 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
2330 if (user
->notifier
[i
].addr
) {
2331 object_unparent(OBJECT(&user
->notifier
[i
].mr
));
2332 munmap(user
->notifier
[i
].addr
, qemu_real_host_page_size
);
2333 user
->notifier
[i
].addr
= NULL
;
2339 const VhostOps user_ops
= {
2340 .backend_type
= VHOST_BACKEND_TYPE_USER
,
2341 .vhost_backend_init
= vhost_user_backend_init
,
2342 .vhost_backend_cleanup
= vhost_user_backend_cleanup
,
2343 .vhost_backend_memslots_limit
= vhost_user_memslots_limit
,
2344 .vhost_set_log_base
= vhost_user_set_log_base
,
2345 .vhost_set_mem_table
= vhost_user_set_mem_table
,
2346 .vhost_set_vring_addr
= vhost_user_set_vring_addr
,
2347 .vhost_set_vring_endian
= vhost_user_set_vring_endian
,
2348 .vhost_set_vring_num
= vhost_user_set_vring_num
,
2349 .vhost_set_vring_base
= vhost_user_set_vring_base
,
2350 .vhost_get_vring_base
= vhost_user_get_vring_base
,
2351 .vhost_set_vring_kick
= vhost_user_set_vring_kick
,
2352 .vhost_set_vring_call
= vhost_user_set_vring_call
,
2353 .vhost_set_features
= vhost_user_set_features
,
2354 .vhost_get_features
= vhost_user_get_features
,
2355 .vhost_set_owner
= vhost_user_set_owner
,
2356 .vhost_reset_device
= vhost_user_reset_device
,
2357 .vhost_get_vq_index
= vhost_user_get_vq_index
,
2358 .vhost_set_vring_enable
= vhost_user_set_vring_enable
,
2359 .vhost_requires_shm_log
= vhost_user_requires_shm_log
,
2360 .vhost_migration_done
= vhost_user_migration_done
,
2361 .vhost_backend_can_merge
= vhost_user_can_merge
,
2362 .vhost_net_set_mtu
= vhost_user_net_set_mtu
,
2363 .vhost_set_iotlb_callback
= vhost_user_set_iotlb_callback
,
2364 .vhost_send_device_iotlb_msg
= vhost_user_send_device_iotlb_msg
,
2365 .vhost_get_config
= vhost_user_get_config
,
2366 .vhost_set_config
= vhost_user_set_config
,
2367 .vhost_crypto_create_session
= vhost_user_crypto_create_session
,
2368 .vhost_crypto_close_session
= vhost_user_crypto_close_session
,
2369 .vhost_backend_mem_section_filter
= vhost_user_mem_section_filter
,
2370 .vhost_get_inflight_fd
= vhost_user_get_inflight_fd
,
2371 .vhost_set_inflight_fd
= vhost_user_set_inflight_fd
,