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/virtio-dmabuf.h"
14 #include "hw/virtio/vhost.h"
15 #include "hw/virtio/virtio-crypto.h"
16 #include "hw/virtio/vhost-user.h"
17 #include "hw/virtio/vhost-backend.h"
18 #include "hw/virtio/virtio.h"
19 #include "hw/virtio/virtio-net.h"
20 #include "chardev/char-fe.h"
21 #include "io/channel-socket.h"
22 #include "sysemu/kvm.h"
23 #include "qemu/error-report.h"
24 #include "qemu/main-loop.h"
25 #include "qemu/uuid.h"
26 #include "qemu/sockets.h"
27 #include "sysemu/runstate.h"
28 #include "sysemu/cryptodev.h"
29 #include "migration/migration.h"
30 #include "migration/postcopy-ram.h"
32 #include "exec/ramblock.h"
34 #include <sys/ioctl.h>
35 #include <sys/socket.h>
38 #include "standard-headers/linux/vhost_types.h"
41 #include <linux/userfaultfd.h>
44 #define VHOST_MEMORY_BASELINE_NREGIONS 8
45 #define VHOST_USER_F_PROTOCOL_FEATURES 30
46 #define VHOST_USER_BACKEND_MAX_FDS 8
48 #if defined(TARGET_PPC) || defined(TARGET_PPC64)
49 #include "hw/ppc/spapr.h"
50 #define VHOST_USER_MAX_RAM_SLOTS SPAPR_MAX_RAM_SLOTS
53 #define VHOST_USER_MAX_RAM_SLOTS 512
57 * Maximum size of virtio device config space
59 #define VHOST_USER_MAX_CONFIG_SIZE 256
61 #define VHOST_USER_PROTOCOL_FEATURE_MASK ((1 << VHOST_USER_PROTOCOL_F_MAX) - 1)
63 typedef enum VhostUserRequest
{
65 VHOST_USER_GET_FEATURES
= 1,
66 VHOST_USER_SET_FEATURES
= 2,
67 VHOST_USER_SET_OWNER
= 3,
68 VHOST_USER_RESET_OWNER
= 4,
69 VHOST_USER_SET_MEM_TABLE
= 5,
70 VHOST_USER_SET_LOG_BASE
= 6,
71 VHOST_USER_SET_LOG_FD
= 7,
72 VHOST_USER_SET_VRING_NUM
= 8,
73 VHOST_USER_SET_VRING_ADDR
= 9,
74 VHOST_USER_SET_VRING_BASE
= 10,
75 VHOST_USER_GET_VRING_BASE
= 11,
76 VHOST_USER_SET_VRING_KICK
= 12,
77 VHOST_USER_SET_VRING_CALL
= 13,
78 VHOST_USER_SET_VRING_ERR
= 14,
79 VHOST_USER_GET_PROTOCOL_FEATURES
= 15,
80 VHOST_USER_SET_PROTOCOL_FEATURES
= 16,
81 VHOST_USER_GET_QUEUE_NUM
= 17,
82 VHOST_USER_SET_VRING_ENABLE
= 18,
83 VHOST_USER_SEND_RARP
= 19,
84 VHOST_USER_NET_SET_MTU
= 20,
85 VHOST_USER_SET_BACKEND_REQ_FD
= 21,
86 VHOST_USER_IOTLB_MSG
= 22,
87 VHOST_USER_SET_VRING_ENDIAN
= 23,
88 VHOST_USER_GET_CONFIG
= 24,
89 VHOST_USER_SET_CONFIG
= 25,
90 VHOST_USER_CREATE_CRYPTO_SESSION
= 26,
91 VHOST_USER_CLOSE_CRYPTO_SESSION
= 27,
92 VHOST_USER_POSTCOPY_ADVISE
= 28,
93 VHOST_USER_POSTCOPY_LISTEN
= 29,
94 VHOST_USER_POSTCOPY_END
= 30,
95 VHOST_USER_GET_INFLIGHT_FD
= 31,
96 VHOST_USER_SET_INFLIGHT_FD
= 32,
97 VHOST_USER_GPU_SET_SOCKET
= 33,
98 VHOST_USER_RESET_DEVICE
= 34,
99 /* Message number 35 reserved for VHOST_USER_VRING_KICK. */
100 VHOST_USER_GET_MAX_MEM_SLOTS
= 36,
101 VHOST_USER_ADD_MEM_REG
= 37,
102 VHOST_USER_REM_MEM_REG
= 38,
103 VHOST_USER_SET_STATUS
= 39,
104 VHOST_USER_GET_STATUS
= 40,
105 VHOST_USER_GET_SHARED_OBJECT
= 41,
109 typedef enum VhostUserBackendRequest
{
110 VHOST_USER_BACKEND_NONE
= 0,
111 VHOST_USER_BACKEND_IOTLB_MSG
= 1,
112 VHOST_USER_BACKEND_CONFIG_CHANGE_MSG
= 2,
113 VHOST_USER_BACKEND_VRING_HOST_NOTIFIER_MSG
= 3,
114 VHOST_USER_BACKEND_SHARED_OBJECT_ADD
= 6,
115 VHOST_USER_BACKEND_SHARED_OBJECT_REMOVE
= 7,
116 VHOST_USER_BACKEND_SHARED_OBJECT_LOOKUP
= 8,
117 VHOST_USER_BACKEND_MAX
118 } VhostUserBackendRequest
;
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_BASELINE_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
152 #define VHOST_CRYPTO_ASYM_MAX_KEY_LEN 1024
154 typedef struct VhostUserCryptoSession
{
158 CryptoDevBackendSymSessionInfo session_setup_data
;
159 uint8_t key
[VHOST_CRYPTO_SYM_CIPHER_MAX_KEY_LEN
];
160 uint8_t auth_key
[VHOST_CRYPTO_SYM_HMAC_MAX_KEY_LEN
];
163 CryptoDevBackendAsymSessionInfo session_setup_data
;
164 uint8_t key
[VHOST_CRYPTO_ASYM_MAX_KEY_LEN
];
168 /* session id for success, -1 on errors */
170 } VhostUserCryptoSession
;
172 static VhostUserConfig c
__attribute__ ((unused
));
173 #define VHOST_USER_CONFIG_HDR_SIZE (sizeof(c.offset) \
177 typedef struct VhostUserVringArea
{
181 } VhostUserVringArea
;
183 typedef struct VhostUserInflight
{
185 uint64_t mmap_offset
;
190 typedef struct VhostUserShared
{
191 unsigned char uuid
[16];
195 VhostUserRequest request
;
197 #define VHOST_USER_VERSION_MASK (0x3)
198 #define VHOST_USER_REPLY_MASK (0x1 << 2)
199 #define VHOST_USER_NEED_REPLY_MASK (0x1 << 3)
201 uint32_t size
; /* the following payload size */
202 } QEMU_PACKED VhostUserHeader
;
205 #define VHOST_USER_VRING_IDX_MASK (0xff)
206 #define VHOST_USER_VRING_NOFD_MASK (0x1 << 8)
208 struct vhost_vring_state state
;
209 struct vhost_vring_addr addr
;
210 VhostUserMemory memory
;
211 VhostUserMemRegMsg mem_reg
;
213 struct vhost_iotlb_msg iotlb
;
214 VhostUserConfig config
;
215 VhostUserCryptoSession session
;
216 VhostUserVringArea area
;
217 VhostUserInflight inflight
;
218 VhostUserShared object
;
221 typedef struct VhostUserMsg
{
223 VhostUserPayload payload
;
224 } QEMU_PACKED VhostUserMsg
;
226 static VhostUserMsg m
__attribute__ ((unused
));
227 #define VHOST_USER_HDR_SIZE (sizeof(VhostUserHeader))
229 #define VHOST_USER_PAYLOAD_SIZE (sizeof(VhostUserPayload))
231 /* The version of the protocol we support */
232 #define VHOST_USER_VERSION (0x1)
235 struct vhost_dev
*dev
;
236 /* Shared between vhost devs of the same virtio device */
237 VhostUserState
*user
;
238 QIOChannel
*backend_ioc
;
239 GSource
*backend_src
;
240 NotifierWithReturn postcopy_notifier
;
241 struct PostCopyFD postcopy_fd
;
242 uint64_t postcopy_client_bases
[VHOST_USER_MAX_RAM_SLOTS
];
243 /* Length of the region_rb and region_rb_offset arrays */
244 size_t region_rb_len
;
245 /* RAMBlock associated with a given region */
246 RAMBlock
**region_rb
;
248 * The offset from the start of the RAMBlock to the start of the
251 ram_addr_t
*region_rb_offset
;
253 /* True once we've entered postcopy_listen */
254 bool postcopy_listen
;
256 /* Our current regions */
257 int num_shadow_regions
;
258 struct vhost_memory_region shadow_regions
[VHOST_USER_MAX_RAM_SLOTS
];
261 struct scrub_regions
{
262 struct vhost_memory_region
*region
;
267 static bool ioeventfd_enabled(void)
269 return !kvm_enabled() || kvm_eventfds_enabled();
272 static int vhost_user_read_header(struct vhost_dev
*dev
, VhostUserMsg
*msg
)
274 struct vhost_user
*u
= dev
->opaque
;
275 CharBackend
*chr
= u
->user
->chr
;
276 uint8_t *p
= (uint8_t *) msg
;
277 int r
, size
= VHOST_USER_HDR_SIZE
;
279 r
= qemu_chr_fe_read_all(chr
, p
, size
);
281 int saved_errno
= errno
;
282 error_report("Failed to read msg header. Read %d instead of %d."
283 " Original request %d.", r
, size
, msg
->hdr
.request
);
284 return r
< 0 ? -saved_errno
: -EIO
;
287 /* validate received flags */
288 if (msg
->hdr
.flags
!= (VHOST_USER_REPLY_MASK
| VHOST_USER_VERSION
)) {
289 error_report("Failed to read msg header."
290 " Flags 0x%x instead of 0x%x.", msg
->hdr
.flags
,
291 VHOST_USER_REPLY_MASK
| VHOST_USER_VERSION
);
295 trace_vhost_user_read(msg
->hdr
.request
, msg
->hdr
.flags
);
300 static int vhost_user_read(struct vhost_dev
*dev
, VhostUserMsg
*msg
)
302 struct vhost_user
*u
= dev
->opaque
;
303 CharBackend
*chr
= u
->user
->chr
;
304 uint8_t *p
= (uint8_t *) msg
;
307 r
= vhost_user_read_header(dev
, msg
);
312 /* validate message size is sane */
313 if (msg
->hdr
.size
> VHOST_USER_PAYLOAD_SIZE
) {
314 error_report("Failed to read msg header."
315 " Size %d exceeds the maximum %zu.", msg
->hdr
.size
,
316 VHOST_USER_PAYLOAD_SIZE
);
321 p
+= VHOST_USER_HDR_SIZE
;
322 size
= msg
->hdr
.size
;
323 r
= qemu_chr_fe_read_all(chr
, p
, size
);
325 int saved_errno
= errno
;
326 error_report("Failed to read msg payload."
327 " Read %d instead of %d.", r
, msg
->hdr
.size
);
328 return r
< 0 ? -saved_errno
: -EIO
;
335 static int process_message_reply(struct vhost_dev
*dev
,
336 const VhostUserMsg
*msg
)
339 VhostUserMsg msg_reply
;
341 if ((msg
->hdr
.flags
& VHOST_USER_NEED_REPLY_MASK
) == 0) {
345 ret
= vhost_user_read(dev
, &msg_reply
);
350 if (msg_reply
.hdr
.request
!= msg
->hdr
.request
) {
351 error_report("Received unexpected msg type. "
352 "Expected %d received %d",
353 msg
->hdr
.request
, msg_reply
.hdr
.request
);
357 return msg_reply
.payload
.u64
? -EIO
: 0;
360 static bool vhost_user_per_device_request(VhostUserRequest request
)
363 case VHOST_USER_SET_OWNER
:
364 case VHOST_USER_RESET_OWNER
:
365 case VHOST_USER_SET_MEM_TABLE
:
366 case VHOST_USER_GET_QUEUE_NUM
:
367 case VHOST_USER_NET_SET_MTU
:
368 case VHOST_USER_RESET_DEVICE
:
369 case VHOST_USER_ADD_MEM_REG
:
370 case VHOST_USER_REM_MEM_REG
:
377 /* most non-init callers ignore the error */
378 static int vhost_user_write(struct vhost_dev
*dev
, VhostUserMsg
*msg
,
379 int *fds
, int fd_num
)
381 struct vhost_user
*u
= dev
->opaque
;
382 CharBackend
*chr
= u
->user
->chr
;
383 int ret
, size
= VHOST_USER_HDR_SIZE
+ msg
->hdr
.size
;
386 * Some devices, like virtio-scsi, are implemented as a single vhost_dev,
387 * while others, like virtio-net, contain multiple vhost_devs. For
388 * operations such as configuring device memory mappings or issuing device
389 * resets, which affect the whole device instead of individual VQs,
390 * vhost-user messages should only be sent once.
392 * Devices with multiple vhost_devs are given an associated dev->vq_index
393 * so per_device requests are only sent if vq_index is 0.
395 if (vhost_user_per_device_request(msg
->hdr
.request
)
396 && dev
->vq_index
!= 0) {
397 msg
->hdr
.flags
&= ~VHOST_USER_NEED_REPLY_MASK
;
401 if (qemu_chr_fe_set_msgfds(chr
, fds
, fd_num
) < 0) {
402 error_report("Failed to set msg fds.");
406 ret
= qemu_chr_fe_write_all(chr
, (const uint8_t *) msg
, size
);
408 int saved_errno
= errno
;
409 error_report("Failed to write msg."
410 " Wrote %d instead of %d.", ret
, size
);
411 return ret
< 0 ? -saved_errno
: -EIO
;
414 trace_vhost_user_write(msg
->hdr
.request
, msg
->hdr
.flags
);
419 int vhost_user_gpu_set_socket(struct vhost_dev
*dev
, int fd
)
422 .hdr
.request
= VHOST_USER_GPU_SET_SOCKET
,
423 .hdr
.flags
= VHOST_USER_VERSION
,
426 return vhost_user_write(dev
, &msg
, &fd
, 1);
429 static int vhost_user_set_log_base(struct vhost_dev
*dev
, uint64_t base
,
430 struct vhost_log
*log
)
432 int fds
[VHOST_USER_MAX_RAM_SLOTS
];
434 bool shmfd
= virtio_has_feature(dev
->protocol_features
,
435 VHOST_USER_PROTOCOL_F_LOG_SHMFD
);
438 .hdr
.request
= VHOST_USER_SET_LOG_BASE
,
439 .hdr
.flags
= VHOST_USER_VERSION
,
440 .payload
.log
.mmap_size
= log
->size
* sizeof(*(log
->log
)),
441 .payload
.log
.mmap_offset
= 0,
442 .hdr
.size
= sizeof(msg
.payload
.log
),
445 /* Send only once with first queue pair */
446 if (dev
->vq_index
!= 0) {
450 if (shmfd
&& log
->fd
!= -1) {
451 fds
[fd_num
++] = log
->fd
;
454 ret
= vhost_user_write(dev
, &msg
, fds
, fd_num
);
461 ret
= vhost_user_read(dev
, &msg
);
466 if (msg
.hdr
.request
!= VHOST_USER_SET_LOG_BASE
) {
467 error_report("Received unexpected msg type. "
468 "Expected %d received %d",
469 VHOST_USER_SET_LOG_BASE
, msg
.hdr
.request
);
477 static MemoryRegion
*vhost_user_get_mr_data(uint64_t addr
, ram_addr_t
*offset
,
482 assert((uintptr_t)addr
== addr
);
483 mr
= memory_region_from_host((void *)(uintptr_t)addr
, offset
);
484 *fd
= memory_region_get_fd(mr
);
485 *offset
+= mr
->ram_block
->fd_offset
;
490 static void vhost_user_fill_msg_region(VhostUserMemoryRegion
*dst
,
491 struct vhost_memory_region
*src
,
492 uint64_t mmap_offset
)
494 assert(src
!= NULL
&& dst
!= NULL
);
495 dst
->userspace_addr
= src
->userspace_addr
;
496 dst
->memory_size
= src
->memory_size
;
497 dst
->guest_phys_addr
= src
->guest_phys_addr
;
498 dst
->mmap_offset
= mmap_offset
;
501 static int vhost_user_fill_set_mem_table_msg(struct vhost_user
*u
,
502 struct vhost_dev
*dev
,
504 int *fds
, size_t *fd_num
,
505 bool track_ramblocks
)
510 struct vhost_memory_region
*reg
;
511 VhostUserMemoryRegion region_buffer
;
513 msg
->hdr
.request
= VHOST_USER_SET_MEM_TABLE
;
515 for (i
= 0; i
< dev
->mem
->nregions
; ++i
) {
516 reg
= dev
->mem
->regions
+ i
;
518 mr
= vhost_user_get_mr_data(reg
->userspace_addr
, &offset
, &fd
);
520 if (track_ramblocks
) {
521 assert(*fd_num
< VHOST_MEMORY_BASELINE_NREGIONS
);
522 trace_vhost_user_set_mem_table_withfd(*fd_num
, mr
->name
,
524 reg
->guest_phys_addr
,
527 u
->region_rb_offset
[i
] = offset
;
528 u
->region_rb
[i
] = mr
->ram_block
;
529 } else if (*fd_num
== VHOST_MEMORY_BASELINE_NREGIONS
) {
530 error_report("Failed preparing vhost-user memory table msg");
533 vhost_user_fill_msg_region(®ion_buffer
, reg
, offset
);
534 msg
->payload
.memory
.regions
[*fd_num
] = region_buffer
;
535 fds
[(*fd_num
)++] = fd
;
536 } else if (track_ramblocks
) {
537 u
->region_rb_offset
[i
] = 0;
538 u
->region_rb
[i
] = NULL
;
542 msg
->payload
.memory
.nregions
= *fd_num
;
545 error_report("Failed initializing vhost-user memory map, "
546 "consider using -object memory-backend-file share=on");
550 msg
->hdr
.size
= sizeof(msg
->payload
.memory
.nregions
);
551 msg
->hdr
.size
+= sizeof(msg
->payload
.memory
.padding
);
552 msg
->hdr
.size
+= *fd_num
* sizeof(VhostUserMemoryRegion
);
557 static inline bool reg_equal(struct vhost_memory_region
*shadow_reg
,
558 struct vhost_memory_region
*vdev_reg
)
560 return shadow_reg
->guest_phys_addr
== vdev_reg
->guest_phys_addr
&&
561 shadow_reg
->userspace_addr
== vdev_reg
->userspace_addr
&&
562 shadow_reg
->memory_size
== vdev_reg
->memory_size
;
565 static void scrub_shadow_regions(struct vhost_dev
*dev
,
566 struct scrub_regions
*add_reg
,
568 struct scrub_regions
*rem_reg
,
569 int *nr_rem_reg
, uint64_t *shadow_pcb
,
570 bool track_ramblocks
)
572 struct vhost_user
*u
= dev
->opaque
;
573 bool found
[VHOST_USER_MAX_RAM_SLOTS
] = {};
574 struct vhost_memory_region
*reg
, *shadow_reg
;
575 int i
, j
, fd
, add_idx
= 0, rm_idx
= 0, fd_num
= 0;
581 * Find memory regions present in our shadow state which are not in
582 * the device's current memory state.
584 * Mark regions in both the shadow and device state as "found".
586 for (i
= 0; i
< u
->num_shadow_regions
; i
++) {
587 shadow_reg
= &u
->shadow_regions
[i
];
590 for (j
= 0; j
< dev
->mem
->nregions
; j
++) {
591 reg
= &dev
->mem
->regions
[j
];
593 mr
= vhost_user_get_mr_data(reg
->userspace_addr
, &offset
, &fd
);
595 if (reg_equal(shadow_reg
, reg
)) {
598 if (track_ramblocks
) {
600 * Reset postcopy client bases, region_rb, and
601 * region_rb_offset in case regions are removed.
604 u
->region_rb_offset
[j
] = offset
;
605 u
->region_rb
[j
] = mr
->ram_block
;
606 shadow_pcb
[j
] = u
->postcopy_client_bases
[i
];
608 u
->region_rb_offset
[j
] = 0;
609 u
->region_rb
[j
] = NULL
;
617 * If the region was not found in the current device memory state
618 * create an entry for it in the removed list.
621 rem_reg
[rm_idx
].region
= shadow_reg
;
622 rem_reg
[rm_idx
++].reg_idx
= i
;
627 * For regions not marked "found", create entries in the added list.
629 * Note their indexes in the device memory state and the indexes of their
632 for (i
= 0; i
< dev
->mem
->nregions
; i
++) {
633 reg
= &dev
->mem
->regions
[i
];
634 vhost_user_get_mr_data(reg
->userspace_addr
, &offset
, &fd
);
640 * If the region was in both the shadow and device state we don't
641 * need to send a VHOST_USER_ADD_MEM_REG message for it.
647 add_reg
[add_idx
].region
= reg
;
648 add_reg
[add_idx
].reg_idx
= i
;
649 add_reg
[add_idx
++].fd_idx
= fd_num
;
651 *nr_rem_reg
= rm_idx
;
652 *nr_add_reg
= add_idx
;
657 static int send_remove_regions(struct vhost_dev
*dev
,
658 struct scrub_regions
*remove_reg
,
659 int nr_rem_reg
, VhostUserMsg
*msg
,
660 bool reply_supported
)
662 struct vhost_user
*u
= dev
->opaque
;
663 struct vhost_memory_region
*shadow_reg
;
664 int i
, fd
, shadow_reg_idx
, ret
;
666 VhostUserMemoryRegion region_buffer
;
669 * The regions in remove_reg appear in the same order they do in the
670 * shadow table. Therefore we can minimize memory copies by iterating
671 * through remove_reg backwards.
673 for (i
= nr_rem_reg
- 1; i
>= 0; i
--) {
674 shadow_reg
= remove_reg
[i
].region
;
675 shadow_reg_idx
= remove_reg
[i
].reg_idx
;
677 vhost_user_get_mr_data(shadow_reg
->userspace_addr
, &offset
, &fd
);
680 msg
->hdr
.request
= VHOST_USER_REM_MEM_REG
;
681 vhost_user_fill_msg_region(®ion_buffer
, shadow_reg
, 0);
682 msg
->payload
.mem_reg
.region
= region_buffer
;
684 ret
= vhost_user_write(dev
, msg
, NULL
, 0);
689 if (reply_supported
) {
690 ret
= process_message_reply(dev
, msg
);
698 * At this point we know the backend has unmapped the region. It is now
699 * safe to remove it from the shadow table.
701 memmove(&u
->shadow_regions
[shadow_reg_idx
],
702 &u
->shadow_regions
[shadow_reg_idx
+ 1],
703 sizeof(struct vhost_memory_region
) *
704 (u
->num_shadow_regions
- shadow_reg_idx
- 1));
705 u
->num_shadow_regions
--;
711 static int send_add_regions(struct vhost_dev
*dev
,
712 struct scrub_regions
*add_reg
, int nr_add_reg
,
713 VhostUserMsg
*msg
, uint64_t *shadow_pcb
,
714 bool reply_supported
, bool track_ramblocks
)
716 struct vhost_user
*u
= dev
->opaque
;
717 int i
, fd
, ret
, reg_idx
, reg_fd_idx
;
718 struct vhost_memory_region
*reg
;
721 VhostUserMsg msg_reply
;
722 VhostUserMemoryRegion region_buffer
;
724 for (i
= 0; i
< nr_add_reg
; i
++) {
725 reg
= add_reg
[i
].region
;
726 reg_idx
= add_reg
[i
].reg_idx
;
727 reg_fd_idx
= add_reg
[i
].fd_idx
;
729 mr
= vhost_user_get_mr_data(reg
->userspace_addr
, &offset
, &fd
);
732 if (track_ramblocks
) {
733 trace_vhost_user_set_mem_table_withfd(reg_fd_idx
, mr
->name
,
735 reg
->guest_phys_addr
,
738 u
->region_rb_offset
[reg_idx
] = offset
;
739 u
->region_rb
[reg_idx
] = mr
->ram_block
;
741 msg
->hdr
.request
= VHOST_USER_ADD_MEM_REG
;
742 vhost_user_fill_msg_region(®ion_buffer
, reg
, offset
);
743 msg
->payload
.mem_reg
.region
= region_buffer
;
745 ret
= vhost_user_write(dev
, msg
, &fd
, 1);
750 if (track_ramblocks
) {
753 ret
= vhost_user_read(dev
, &msg_reply
);
758 reply_gpa
= msg_reply
.payload
.mem_reg
.region
.guest_phys_addr
;
760 if (msg_reply
.hdr
.request
!= VHOST_USER_ADD_MEM_REG
) {
761 error_report("%s: Received unexpected msg type."
762 "Expected %d received %d", __func__
,
763 VHOST_USER_ADD_MEM_REG
,
764 msg_reply
.hdr
.request
);
769 * We're using the same structure, just reusing one of the
770 * fields, so it should be the same size.
772 if (msg_reply
.hdr
.size
!= msg
->hdr
.size
) {
773 error_report("%s: Unexpected size for postcopy reply "
774 "%d vs %d", __func__
, msg_reply
.hdr
.size
,
779 /* Get the postcopy client base from the backend's reply. */
780 if (reply_gpa
== dev
->mem
->regions
[reg_idx
].guest_phys_addr
) {
781 shadow_pcb
[reg_idx
] =
782 msg_reply
.payload
.mem_reg
.region
.userspace_addr
;
783 trace_vhost_user_set_mem_table_postcopy(
784 msg_reply
.payload
.mem_reg
.region
.userspace_addr
,
785 msg
->payload
.mem_reg
.region
.userspace_addr
,
786 reg_fd_idx
, reg_idx
);
788 error_report("%s: invalid postcopy reply for region. "
789 "Got guest physical address %" PRIX64
", expected "
790 "%" PRIX64
, __func__
, reply_gpa
,
791 dev
->mem
->regions
[reg_idx
].guest_phys_addr
);
794 } else if (reply_supported
) {
795 ret
= process_message_reply(dev
, msg
);
800 } else if (track_ramblocks
) {
801 u
->region_rb_offset
[reg_idx
] = 0;
802 u
->region_rb
[reg_idx
] = NULL
;
806 * At this point, we know the backend has mapped in the new
807 * region, if the region has a valid file descriptor.
809 * The region should now be added to the shadow table.
811 u
->shadow_regions
[u
->num_shadow_regions
].guest_phys_addr
=
812 reg
->guest_phys_addr
;
813 u
->shadow_regions
[u
->num_shadow_regions
].userspace_addr
=
815 u
->shadow_regions
[u
->num_shadow_regions
].memory_size
=
817 u
->num_shadow_regions
++;
823 static int vhost_user_add_remove_regions(struct vhost_dev
*dev
,
825 bool reply_supported
,
826 bool track_ramblocks
)
828 struct vhost_user
*u
= dev
->opaque
;
829 struct scrub_regions add_reg
[VHOST_USER_MAX_RAM_SLOTS
];
830 struct scrub_regions rem_reg
[VHOST_USER_MAX_RAM_SLOTS
];
831 uint64_t shadow_pcb
[VHOST_USER_MAX_RAM_SLOTS
] = {};
832 int nr_add_reg
, nr_rem_reg
;
835 msg
->hdr
.size
= sizeof(msg
->payload
.mem_reg
);
837 /* Find the regions which need to be removed or added. */
838 scrub_shadow_regions(dev
, add_reg
, &nr_add_reg
, rem_reg
, &nr_rem_reg
,
839 shadow_pcb
, track_ramblocks
);
842 ret
= send_remove_regions(dev
, rem_reg
, nr_rem_reg
, msg
,
850 ret
= send_add_regions(dev
, add_reg
, nr_add_reg
, msg
, shadow_pcb
,
851 reply_supported
, track_ramblocks
);
857 if (track_ramblocks
) {
858 memcpy(u
->postcopy_client_bases
, shadow_pcb
,
859 sizeof(uint64_t) * VHOST_USER_MAX_RAM_SLOTS
);
861 * Now we've registered this with the postcopy code, we ack to the
862 * client, because now we're in the position to be able to deal with
863 * any faults it generates.
865 /* TODO: Use this for failure cases as well with a bad value. */
866 msg
->hdr
.size
= sizeof(msg
->payload
.u64
);
867 msg
->payload
.u64
= 0; /* OK */
869 ret
= vhost_user_write(dev
, msg
, NULL
, 0);
878 if (track_ramblocks
) {
879 memcpy(u
->postcopy_client_bases
, shadow_pcb
,
880 sizeof(uint64_t) * VHOST_USER_MAX_RAM_SLOTS
);
886 static int vhost_user_set_mem_table_postcopy(struct vhost_dev
*dev
,
887 struct vhost_memory
*mem
,
888 bool reply_supported
,
889 bool config_mem_slots
)
891 struct vhost_user
*u
= dev
->opaque
;
892 int fds
[VHOST_MEMORY_BASELINE_NREGIONS
];
894 VhostUserMsg msg_reply
;
899 .hdr
.flags
= VHOST_USER_VERSION
,
902 if (u
->region_rb_len
< dev
->mem
->nregions
) {
903 u
->region_rb
= g_renew(RAMBlock
*, u
->region_rb
, dev
->mem
->nregions
);
904 u
->region_rb_offset
= g_renew(ram_addr_t
, u
->region_rb_offset
,
906 memset(&(u
->region_rb
[u
->region_rb_len
]), '\0',
907 sizeof(RAMBlock
*) * (dev
->mem
->nregions
- u
->region_rb_len
));
908 memset(&(u
->region_rb_offset
[u
->region_rb_len
]), '\0',
909 sizeof(ram_addr_t
) * (dev
->mem
->nregions
- u
->region_rb_len
));
910 u
->region_rb_len
= dev
->mem
->nregions
;
913 if (config_mem_slots
) {
914 ret
= vhost_user_add_remove_regions(dev
, &msg
, reply_supported
, true);
919 ret
= vhost_user_fill_set_mem_table_msg(u
, dev
, &msg
, fds
, &fd_num
,
925 ret
= vhost_user_write(dev
, &msg
, fds
, fd_num
);
930 ret
= vhost_user_read(dev
, &msg_reply
);
935 if (msg_reply
.hdr
.request
!= VHOST_USER_SET_MEM_TABLE
) {
936 error_report("%s: Received unexpected msg type."
937 "Expected %d received %d", __func__
,
938 VHOST_USER_SET_MEM_TABLE
, msg_reply
.hdr
.request
);
943 * We're using the same structure, just reusing one of the
944 * fields, so it should be the same size.
946 if (msg_reply
.hdr
.size
!= msg
.hdr
.size
) {
947 error_report("%s: Unexpected size for postcopy reply "
948 "%d vs %d", __func__
, msg_reply
.hdr
.size
,
953 memset(u
->postcopy_client_bases
, 0,
954 sizeof(uint64_t) * VHOST_USER_MAX_RAM_SLOTS
);
957 * They're in the same order as the regions that were sent
958 * but some of the regions were skipped (above) if they
961 for (msg_i
= 0, region_i
= 0;
962 region_i
< dev
->mem
->nregions
;
964 if (msg_i
< fd_num
&&
965 msg_reply
.payload
.memory
.regions
[msg_i
].guest_phys_addr
==
966 dev
->mem
->regions
[region_i
].guest_phys_addr
) {
967 u
->postcopy_client_bases
[region_i
] =
968 msg_reply
.payload
.memory
.regions
[msg_i
].userspace_addr
;
969 trace_vhost_user_set_mem_table_postcopy(
970 msg_reply
.payload
.memory
.regions
[msg_i
].userspace_addr
,
971 msg
.payload
.memory
.regions
[msg_i
].userspace_addr
,
976 if (msg_i
!= fd_num
) {
977 error_report("%s: postcopy reply not fully consumed "
979 __func__
, msg_i
, fd_num
);
984 * Now we've registered this with the postcopy code, we ack to the
985 * client, because now we're in the position to be able to deal
986 * with any faults it generates.
988 /* TODO: Use this for failure cases as well with a bad value. */
989 msg
.hdr
.size
= sizeof(msg
.payload
.u64
);
990 msg
.payload
.u64
= 0; /* OK */
991 ret
= vhost_user_write(dev
, &msg
, NULL
, 0);
1000 static int vhost_user_set_mem_table(struct vhost_dev
*dev
,
1001 struct vhost_memory
*mem
)
1003 struct vhost_user
*u
= dev
->opaque
;
1004 int fds
[VHOST_MEMORY_BASELINE_NREGIONS
];
1006 bool do_postcopy
= u
->postcopy_listen
&& u
->postcopy_fd
.handler
;
1007 bool reply_supported
= virtio_has_feature(dev
->protocol_features
,
1008 VHOST_USER_PROTOCOL_F_REPLY_ACK
);
1009 bool config_mem_slots
=
1010 virtio_has_feature(dev
->protocol_features
,
1011 VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS
);
1016 * Postcopy has enough differences that it's best done in it's own
1019 return vhost_user_set_mem_table_postcopy(dev
, mem
, reply_supported
,
1023 VhostUserMsg msg
= {
1024 .hdr
.flags
= VHOST_USER_VERSION
,
1027 if (reply_supported
) {
1028 msg
.hdr
.flags
|= VHOST_USER_NEED_REPLY_MASK
;
1031 if (config_mem_slots
) {
1032 ret
= vhost_user_add_remove_regions(dev
, &msg
, reply_supported
, false);
1037 ret
= vhost_user_fill_set_mem_table_msg(u
, dev
, &msg
, fds
, &fd_num
,
1043 ret
= vhost_user_write(dev
, &msg
, fds
, fd_num
);
1048 if (reply_supported
) {
1049 return process_message_reply(dev
, &msg
);
1056 static int vhost_user_set_vring_endian(struct vhost_dev
*dev
,
1057 struct vhost_vring_state
*ring
)
1059 bool cross_endian
= virtio_has_feature(dev
->protocol_features
,
1060 VHOST_USER_PROTOCOL_F_CROSS_ENDIAN
);
1061 VhostUserMsg msg
= {
1062 .hdr
.request
= VHOST_USER_SET_VRING_ENDIAN
,
1063 .hdr
.flags
= VHOST_USER_VERSION
,
1064 .payload
.state
= *ring
,
1065 .hdr
.size
= sizeof(msg
.payload
.state
),
1068 if (!cross_endian
) {
1069 error_report("vhost-user trying to send unhandled ioctl");
1073 return vhost_user_write(dev
, &msg
, NULL
, 0);
1076 static int vhost_user_get_u64(struct vhost_dev
*dev
, int request
, uint64_t *u64
)
1079 VhostUserMsg msg
= {
1080 .hdr
.request
= request
,
1081 .hdr
.flags
= VHOST_USER_VERSION
,
1084 if (vhost_user_per_device_request(request
) && dev
->vq_index
!= 0) {
1088 ret
= vhost_user_write(dev
, &msg
, NULL
, 0);
1093 ret
= vhost_user_read(dev
, &msg
);
1098 if (msg
.hdr
.request
!= request
) {
1099 error_report("Received unexpected msg type. Expected %d received %d",
1100 request
, msg
.hdr
.request
);
1104 if (msg
.hdr
.size
!= sizeof(msg
.payload
.u64
)) {
1105 error_report("Received bad msg size.");
1109 *u64
= msg
.payload
.u64
;
1114 static int vhost_user_get_features(struct vhost_dev
*dev
, uint64_t *features
)
1116 if (vhost_user_get_u64(dev
, VHOST_USER_GET_FEATURES
, features
) < 0) {
1123 /* Note: "msg->hdr.flags" may be modified. */
1124 static int vhost_user_write_sync(struct vhost_dev
*dev
, VhostUserMsg
*msg
,
1125 bool wait_for_reply
)
1129 if (wait_for_reply
) {
1130 bool reply_supported
= virtio_has_feature(dev
->protocol_features
,
1131 VHOST_USER_PROTOCOL_F_REPLY_ACK
);
1132 if (reply_supported
) {
1133 msg
->hdr
.flags
|= VHOST_USER_NEED_REPLY_MASK
;
1137 ret
= vhost_user_write(dev
, msg
, NULL
, 0);
1142 if (wait_for_reply
) {
1145 if (msg
->hdr
.flags
& VHOST_USER_NEED_REPLY_MASK
) {
1146 return process_message_reply(dev
, msg
);
1150 * We need to wait for a reply but the backend does not
1151 * support replies for the command we just sent.
1152 * Send VHOST_USER_GET_FEATURES which makes all backends
1155 return vhost_user_get_features(dev
, &dummy
);
1161 static int vhost_set_vring(struct vhost_dev
*dev
,
1162 unsigned long int request
,
1163 struct vhost_vring_state
*ring
,
1164 bool wait_for_reply
)
1166 VhostUserMsg msg
= {
1167 .hdr
.request
= request
,
1168 .hdr
.flags
= VHOST_USER_VERSION
,
1169 .payload
.state
= *ring
,
1170 .hdr
.size
= sizeof(msg
.payload
.state
),
1173 return vhost_user_write_sync(dev
, &msg
, wait_for_reply
);
1176 static int vhost_user_set_vring_num(struct vhost_dev
*dev
,
1177 struct vhost_vring_state
*ring
)
1179 return vhost_set_vring(dev
, VHOST_USER_SET_VRING_NUM
, ring
, false);
1182 static void vhost_user_host_notifier_free(VhostUserHostNotifier
*n
)
1184 assert(n
&& n
->unmap_addr
);
1185 munmap(n
->unmap_addr
, qemu_real_host_page_size());
1186 n
->unmap_addr
= NULL
;
1190 * clean-up function for notifier, will finally free the structure
1193 static void vhost_user_host_notifier_remove(VhostUserHostNotifier
*n
,
1198 virtio_queue_set_host_notifier_mr(vdev
, n
->idx
, &n
->mr
, false);
1200 assert(!n
->unmap_addr
);
1201 n
->unmap_addr
= n
->addr
;
1203 call_rcu(n
, vhost_user_host_notifier_free
, rcu
);
1207 static int vhost_user_set_vring_base(struct vhost_dev
*dev
,
1208 struct vhost_vring_state
*ring
)
1210 return vhost_set_vring(dev
, VHOST_USER_SET_VRING_BASE
, ring
, false);
1213 static int vhost_user_set_vring_enable(struct vhost_dev
*dev
, int enable
)
1217 if (!virtio_has_feature(dev
->features
, VHOST_USER_F_PROTOCOL_FEATURES
)) {
1221 for (i
= 0; i
< dev
->nvqs
; ++i
) {
1223 struct vhost_vring_state state
= {
1224 .index
= dev
->vq_index
+ i
,
1229 * SET_VRING_ENABLE travels from guest to QEMU to vhost-user backend /
1230 * control plane thread via unix domain socket. Virtio requests travel
1231 * from guest to vhost-user backend / data plane thread via eventfd.
1232 * Even if the guest enables the ring first, and pushes its first virtio
1233 * request second (conforming to the virtio spec), the data plane thread
1234 * in the backend may see the virtio request before the control plane
1235 * thread sees the queue enablement. This causes (in fact, requires) the
1236 * data plane thread to discard the virtio request (it arrived on a
1237 * seemingly disabled queue). To prevent this out-of-order delivery,
1238 * don't let the guest proceed to pushing the virtio request until the
1239 * backend control plane acknowledges enabling the queue -- IOW, pass
1240 * wait_for_reply=true below.
1242 ret
= vhost_set_vring(dev
, VHOST_USER_SET_VRING_ENABLE
, &state
, true);
1245 * Restoring the previous state is likely infeasible, as well as
1246 * proceeding regardless the error, so just bail out and hope for
1247 * the device-level recovery.
1256 static VhostUserHostNotifier
*fetch_notifier(VhostUserState
*u
,
1259 if (idx
>= u
->notifiers
->len
) {
1262 return g_ptr_array_index(u
->notifiers
, idx
);
1265 static int vhost_user_get_vring_base(struct vhost_dev
*dev
,
1266 struct vhost_vring_state
*ring
)
1269 VhostUserMsg msg
= {
1270 .hdr
.request
= VHOST_USER_GET_VRING_BASE
,
1271 .hdr
.flags
= VHOST_USER_VERSION
,
1272 .payload
.state
= *ring
,
1273 .hdr
.size
= sizeof(msg
.payload
.state
),
1275 struct vhost_user
*u
= dev
->opaque
;
1277 VhostUserHostNotifier
*n
= fetch_notifier(u
->user
, ring
->index
);
1279 vhost_user_host_notifier_remove(n
, dev
->vdev
);
1282 ret
= vhost_user_write(dev
, &msg
, NULL
, 0);
1287 ret
= vhost_user_read(dev
, &msg
);
1292 if (msg
.hdr
.request
!= VHOST_USER_GET_VRING_BASE
) {
1293 error_report("Received unexpected msg type. Expected %d received %d",
1294 VHOST_USER_GET_VRING_BASE
, msg
.hdr
.request
);
1298 if (msg
.hdr
.size
!= sizeof(msg
.payload
.state
)) {
1299 error_report("Received bad msg size.");
1303 *ring
= msg
.payload
.state
;
1308 static int vhost_set_vring_file(struct vhost_dev
*dev
,
1309 VhostUserRequest request
,
1310 struct vhost_vring_file
*file
)
1312 int fds
[VHOST_USER_MAX_RAM_SLOTS
];
1314 VhostUserMsg msg
= {
1315 .hdr
.request
= request
,
1316 .hdr
.flags
= VHOST_USER_VERSION
,
1317 .payload
.u64
= file
->index
& VHOST_USER_VRING_IDX_MASK
,
1318 .hdr
.size
= sizeof(msg
.payload
.u64
),
1321 if (ioeventfd_enabled() && file
->fd
> 0) {
1322 fds
[fd_num
++] = file
->fd
;
1324 msg
.payload
.u64
|= VHOST_USER_VRING_NOFD_MASK
;
1327 return vhost_user_write(dev
, &msg
, fds
, fd_num
);
1330 static int vhost_user_set_vring_kick(struct vhost_dev
*dev
,
1331 struct vhost_vring_file
*file
)
1333 return vhost_set_vring_file(dev
, VHOST_USER_SET_VRING_KICK
, file
);
1336 static int vhost_user_set_vring_call(struct vhost_dev
*dev
,
1337 struct vhost_vring_file
*file
)
1339 return vhost_set_vring_file(dev
, VHOST_USER_SET_VRING_CALL
, file
);
1342 static int vhost_user_set_vring_err(struct vhost_dev
*dev
,
1343 struct vhost_vring_file
*file
)
1345 return vhost_set_vring_file(dev
, VHOST_USER_SET_VRING_ERR
, file
);
1348 static int vhost_user_set_vring_addr(struct vhost_dev
*dev
,
1349 struct vhost_vring_addr
*addr
)
1351 VhostUserMsg msg
= {
1352 .hdr
.request
= VHOST_USER_SET_VRING_ADDR
,
1353 .hdr
.flags
= VHOST_USER_VERSION
,
1354 .payload
.addr
= *addr
,
1355 .hdr
.size
= sizeof(msg
.payload
.addr
),
1359 * wait for a reply if logging is enabled to make sure
1360 * backend is actually logging changes
1362 bool wait_for_reply
= addr
->flags
& (1 << VHOST_VRING_F_LOG
);
1364 return vhost_user_write_sync(dev
, &msg
, wait_for_reply
);
1367 static int vhost_user_set_u64(struct vhost_dev
*dev
, int request
, uint64_t u64
,
1368 bool wait_for_reply
)
1370 VhostUserMsg msg
= {
1371 .hdr
.request
= request
,
1372 .hdr
.flags
= VHOST_USER_VERSION
,
1374 .hdr
.size
= sizeof(msg
.payload
.u64
),
1377 return vhost_user_write_sync(dev
, &msg
, wait_for_reply
);
1380 static int vhost_user_set_status(struct vhost_dev
*dev
, uint8_t status
)
1382 return vhost_user_set_u64(dev
, VHOST_USER_SET_STATUS
, status
, false);
1385 static int vhost_user_get_status(struct vhost_dev
*dev
, uint8_t *status
)
1390 ret
= vhost_user_get_u64(dev
, VHOST_USER_GET_STATUS
, &value
);
1399 static int vhost_user_add_status(struct vhost_dev
*dev
, uint8_t status
)
1404 ret
= vhost_user_get_status(dev
, &s
);
1409 if ((s
& status
) == status
) {
1414 return vhost_user_set_status(dev
, s
);
1417 static int vhost_user_set_features(struct vhost_dev
*dev
,
1421 * wait for a reply if logging is enabled to make sure
1422 * backend is actually logging changes
1424 bool log_enabled
= features
& (0x1ULL
<< VHOST_F_LOG_ALL
);
1428 * We need to include any extra backend only feature bits that
1429 * might be needed by our device. Currently this includes the
1430 * VHOST_USER_F_PROTOCOL_FEATURES bit for enabling protocol
1433 ret
= vhost_user_set_u64(dev
, VHOST_USER_SET_FEATURES
,
1434 features
| dev
->backend_features
,
1437 if (virtio_has_feature(dev
->protocol_features
,
1438 VHOST_USER_PROTOCOL_F_STATUS
)) {
1440 return vhost_user_add_status(dev
, VIRTIO_CONFIG_S_FEATURES_OK
);
1447 static int vhost_user_set_protocol_features(struct vhost_dev
*dev
,
1450 return vhost_user_set_u64(dev
, VHOST_USER_SET_PROTOCOL_FEATURES
, features
,
1454 static int vhost_user_set_owner(struct vhost_dev
*dev
)
1456 VhostUserMsg msg
= {
1457 .hdr
.request
= VHOST_USER_SET_OWNER
,
1458 .hdr
.flags
= VHOST_USER_VERSION
,
1461 return vhost_user_write(dev
, &msg
, NULL
, 0);
1464 static int vhost_user_get_max_memslots(struct vhost_dev
*dev
,
1465 uint64_t *max_memslots
)
1467 uint64_t backend_max_memslots
;
1470 err
= vhost_user_get_u64(dev
, VHOST_USER_GET_MAX_MEM_SLOTS
,
1471 &backend_max_memslots
);
1476 *max_memslots
= backend_max_memslots
;
1481 static int vhost_user_reset_device(struct vhost_dev
*dev
)
1483 VhostUserMsg msg
= {
1484 .hdr
.flags
= VHOST_USER_VERSION
,
1485 .hdr
.request
= VHOST_USER_RESET_DEVICE
,
1489 * Historically, reset was not implemented so only reset devices
1490 * that are expecting it.
1492 if (!virtio_has_feature(dev
->protocol_features
,
1493 VHOST_USER_PROTOCOL_F_RESET_DEVICE
)) {
1497 return vhost_user_write(dev
, &msg
, NULL
, 0);
1500 static int vhost_user_backend_handle_config_change(struct vhost_dev
*dev
)
1502 if (!dev
->config_ops
|| !dev
->config_ops
->vhost_dev_config_notifier
) {
1506 return dev
->config_ops
->vhost_dev_config_notifier(dev
);
1510 * Fetch or create the notifier for a given idx. Newly created
1511 * notifiers are added to the pointer array that tracks them.
1513 static VhostUserHostNotifier
*fetch_or_create_notifier(VhostUserState
*u
,
1516 VhostUserHostNotifier
*n
= NULL
;
1517 if (idx
>= u
->notifiers
->len
) {
1518 g_ptr_array_set_size(u
->notifiers
, idx
+ 1);
1521 n
= g_ptr_array_index(u
->notifiers
, idx
);
1524 * In case notification arrive out-of-order,
1525 * make room for current index.
1527 g_ptr_array_remove_index(u
->notifiers
, idx
);
1528 n
= g_new0(VhostUserHostNotifier
, 1);
1530 g_ptr_array_insert(u
->notifiers
, idx
, n
);
1531 trace_vhost_user_create_notifier(idx
, n
);
1537 static int vhost_user_backend_handle_vring_host_notifier(struct vhost_dev
*dev
,
1538 VhostUserVringArea
*area
,
1541 int queue_idx
= area
->u64
& VHOST_USER_VRING_IDX_MASK
;
1542 size_t page_size
= qemu_real_host_page_size();
1543 struct vhost_user
*u
= dev
->opaque
;
1544 VhostUserState
*user
= u
->user
;
1545 VirtIODevice
*vdev
= dev
->vdev
;
1546 VhostUserHostNotifier
*n
;
1550 if (!virtio_has_feature(dev
->protocol_features
,
1551 VHOST_USER_PROTOCOL_F_HOST_NOTIFIER
) ||
1552 vdev
== NULL
|| queue_idx
>= virtio_get_num_queues(vdev
)) {
1557 * Fetch notifier and invalidate any old data before setting up
1558 * new mapped address.
1560 n
= fetch_or_create_notifier(user
, queue_idx
);
1561 vhost_user_host_notifier_remove(n
, vdev
);
1563 if (area
->u64
& VHOST_USER_VRING_NOFD_MASK
) {
1568 if (area
->size
!= page_size
) {
1572 addr
= mmap(NULL
, page_size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
,
1574 if (addr
== MAP_FAILED
) {
1578 name
= g_strdup_printf("vhost-user/host-notifier@%p mmaps[%d]",
1580 if (!n
->mr
.ram
) { /* Don't init again after suspend. */
1581 memory_region_init_ram_device_ptr(&n
->mr
, OBJECT(vdev
), name
,
1584 n
->mr
.ram_block
->host
= addr
;
1588 if (virtio_queue_set_host_notifier_mr(vdev
, queue_idx
, &n
->mr
, true)) {
1589 object_unparent(OBJECT(&n
->mr
));
1590 munmap(addr
, page_size
);
1600 vhost_user_backend_handle_shared_object_add(struct vhost_dev
*dev
,
1601 VhostUserShared
*object
)
1605 memcpy(uuid
.data
, object
->uuid
, sizeof(object
->uuid
));
1606 return virtio_add_vhost_device(&uuid
, dev
);
1610 vhost_user_backend_handle_shared_object_remove(VhostUserShared
*object
)
1614 memcpy(uuid
.data
, object
->uuid
, sizeof(object
->uuid
));
1615 return virtio_remove_resource(&uuid
);
1618 static bool vhost_user_send_resp(QIOChannel
*ioc
, VhostUserHeader
*hdr
,
1619 VhostUserPayload
*payload
, Error
**errp
)
1621 struct iovec iov
[] = {
1622 { .iov_base
= hdr
, .iov_len
= VHOST_USER_HDR_SIZE
},
1623 { .iov_base
= payload
, .iov_len
= hdr
->size
},
1626 hdr
->flags
&= ~VHOST_USER_NEED_REPLY_MASK
;
1627 hdr
->flags
|= VHOST_USER_REPLY_MASK
;
1629 return !qio_channel_writev_all(ioc
, iov
, ARRAY_SIZE(iov
), errp
);
1633 vhost_user_backend_send_dmabuf_fd(QIOChannel
*ioc
, VhostUserHeader
*hdr
,
1634 VhostUserPayload
*payload
, Error
**errp
)
1636 hdr
->size
= sizeof(payload
->u64
);
1637 return vhost_user_send_resp(ioc
, hdr
, payload
, errp
);
1640 int vhost_user_get_shared_object(struct vhost_dev
*dev
, unsigned char *uuid
,
1643 struct vhost_user
*u
= dev
->opaque
;
1644 CharBackend
*chr
= u
->user
->chr
;
1646 VhostUserMsg msg
= {
1647 .hdr
.request
= VHOST_USER_GET_SHARED_OBJECT
,
1648 .hdr
.flags
= VHOST_USER_VERSION
,
1650 memcpy(msg
.payload
.object
.uuid
, uuid
, sizeof(msg
.payload
.object
.uuid
));
1652 ret
= vhost_user_write(dev
, &msg
, NULL
, 0);
1657 ret
= vhost_user_read(dev
, &msg
);
1662 if (msg
.hdr
.request
!= VHOST_USER_GET_SHARED_OBJECT
) {
1663 error_report("Received unexpected msg type. "
1664 "Expected %d received %d",
1665 VHOST_USER_GET_SHARED_OBJECT
, msg
.hdr
.request
);
1669 *dmabuf_fd
= qemu_chr_fe_get_msgfd(chr
);
1670 if (*dmabuf_fd
< 0) {
1671 error_report("Failed to get dmabuf fd");
1679 vhost_user_backend_handle_shared_object_lookup(struct vhost_user
*u
,
1681 VhostUserHeader
*hdr
,
1682 VhostUserPayload
*payload
)
1685 CharBackend
*chr
= u
->user
->chr
;
1686 Error
*local_err
= NULL
;
1690 memcpy(uuid
.data
, payload
->object
.uuid
, sizeof(payload
->object
.uuid
));
1693 switch (virtio_object_type(&uuid
)) {
1695 dmabuf_fd
= virtio_lookup_dmabuf(&uuid
);
1697 case TYPE_VHOST_DEV
:
1699 struct vhost_dev
*dev
= virtio_lookup_vhost_device(&uuid
);
1701 payload
->u64
= -EINVAL
;
1704 int ret
= vhost_user_get_shared_object(dev
, uuid
.data
, &dmabuf_fd
);
1711 payload
->u64
= -EINVAL
;
1715 if (dmabuf_fd
!= -1) {
1719 if (qemu_chr_fe_set_msgfds(chr
, &dmabuf_fd
, fd_num
) < 0) {
1720 error_report("Failed to set msg fds.");
1721 payload
->u64
= -EINVAL
;
1724 if (!vhost_user_backend_send_dmabuf_fd(ioc
, hdr
, payload
, &local_err
)) {
1725 error_report_err(local_err
);
1732 static void close_backend_channel(struct vhost_user
*u
)
1734 g_source_destroy(u
->backend_src
);
1735 g_source_unref(u
->backend_src
);
1736 u
->backend_src
= NULL
;
1737 object_unref(OBJECT(u
->backend_ioc
));
1738 u
->backend_ioc
= NULL
;
1741 static gboolean
backend_read(QIOChannel
*ioc
, GIOCondition condition
,
1744 struct vhost_dev
*dev
= opaque
;
1745 struct vhost_user
*u
= dev
->opaque
;
1746 VhostUserHeader hdr
= { 0, };
1747 VhostUserPayload payload
= { 0, };
1748 Error
*local_err
= NULL
;
1749 gboolean rc
= G_SOURCE_CONTINUE
;
1752 g_autofree
int *fd
= NULL
;
1757 iov
.iov_base
= &hdr
;
1758 iov
.iov_len
= VHOST_USER_HDR_SIZE
;
1760 if (qio_channel_readv_full_all(ioc
, &iov
, 1, &fd
, &fdsize
, &local_err
)) {
1761 error_report_err(local_err
);
1765 if (hdr
.size
> VHOST_USER_PAYLOAD_SIZE
) {
1766 error_report("Failed to read msg header."
1767 " Size %d exceeds the maximum %zu.", hdr
.size
,
1768 VHOST_USER_PAYLOAD_SIZE
);
1773 if (qio_channel_read_all(ioc
, (char *) &payload
, hdr
.size
, &local_err
)) {
1774 error_report_err(local_err
);
1778 switch (hdr
.request
) {
1779 case VHOST_USER_BACKEND_IOTLB_MSG
:
1780 ret
= vhost_backend_handle_iotlb_msg(dev
, &payload
.iotlb
);
1782 case VHOST_USER_BACKEND_CONFIG_CHANGE_MSG
:
1783 ret
= vhost_user_backend_handle_config_change(dev
);
1785 case VHOST_USER_BACKEND_VRING_HOST_NOTIFIER_MSG
:
1786 ret
= vhost_user_backend_handle_vring_host_notifier(dev
, &payload
.area
,
1789 case VHOST_USER_BACKEND_SHARED_OBJECT_ADD
:
1790 ret
= vhost_user_backend_handle_shared_object_add(dev
, &payload
.object
);
1792 case VHOST_USER_BACKEND_SHARED_OBJECT_REMOVE
:
1793 ret
= vhost_user_backend_handle_shared_object_remove(&payload
.object
);
1795 case VHOST_USER_BACKEND_SHARED_OBJECT_LOOKUP
:
1796 ret
= vhost_user_backend_handle_shared_object_lookup(dev
->opaque
, ioc
,
1800 error_report("Received unexpected msg type: %d.", hdr
.request
);
1805 * REPLY_ACK feature handling. Other reply types has to be managed
1806 * directly in their request handlers.
1808 if (hdr
.flags
& VHOST_USER_NEED_REPLY_MASK
) {
1809 payload
.u64
= !!ret
;
1810 hdr
.size
= sizeof(payload
.u64
);
1812 if (!vhost_user_send_resp(ioc
, &hdr
, &payload
, &local_err
)) {
1813 error_report_err(local_err
);
1821 close_backend_channel(u
);
1822 rc
= G_SOURCE_REMOVE
;
1826 for (i
= 0; i
< fdsize
; i
++) {
1833 static int vhost_setup_backend_channel(struct vhost_dev
*dev
)
1835 VhostUserMsg msg
= {
1836 .hdr
.request
= VHOST_USER_SET_BACKEND_REQ_FD
,
1837 .hdr
.flags
= VHOST_USER_VERSION
,
1839 struct vhost_user
*u
= dev
->opaque
;
1841 bool reply_supported
= virtio_has_feature(dev
->protocol_features
,
1842 VHOST_USER_PROTOCOL_F_REPLY_ACK
);
1843 Error
*local_err
= NULL
;
1846 if (!virtio_has_feature(dev
->protocol_features
,
1847 VHOST_USER_PROTOCOL_F_BACKEND_REQ
)) {
1851 if (qemu_socketpair(PF_UNIX
, SOCK_STREAM
, 0, sv
) == -1) {
1852 int saved_errno
= errno
;
1853 error_report("socketpair() failed");
1854 return -saved_errno
;
1857 ioc
= QIO_CHANNEL(qio_channel_socket_new_fd(sv
[0], &local_err
));
1859 error_report_err(local_err
);
1860 return -ECONNREFUSED
;
1862 u
->backend_ioc
= ioc
;
1863 u
->backend_src
= qio_channel_add_watch_source(u
->backend_ioc
,
1865 backend_read
, dev
, NULL
, NULL
);
1867 if (reply_supported
) {
1868 msg
.hdr
.flags
|= VHOST_USER_NEED_REPLY_MASK
;
1871 ret
= vhost_user_write(dev
, &msg
, &sv
[1], 1);
1876 if (reply_supported
) {
1877 ret
= process_message_reply(dev
, &msg
);
1883 close_backend_channel(u
);
1891 * Called back from the postcopy fault thread when a fault is received on our
1893 * TODO: This is Linux specific
1895 static int vhost_user_postcopy_fault_handler(struct PostCopyFD
*pcfd
,
1898 struct vhost_dev
*dev
= pcfd
->data
;
1899 struct vhost_user
*u
= dev
->opaque
;
1900 struct uffd_msg
*msg
= ufd
;
1901 uint64_t faultaddr
= msg
->arg
.pagefault
.address
;
1902 RAMBlock
*rb
= NULL
;
1906 trace_vhost_user_postcopy_fault_handler(pcfd
->idstr
, faultaddr
,
1907 dev
->mem
->nregions
);
1908 for (i
= 0; i
< MIN(dev
->mem
->nregions
, u
->region_rb_len
); i
++) {
1909 trace_vhost_user_postcopy_fault_handler_loop(i
,
1910 u
->postcopy_client_bases
[i
], dev
->mem
->regions
[i
].memory_size
);
1911 if (faultaddr
>= u
->postcopy_client_bases
[i
]) {
1912 /* Ofset of the fault address in the vhost region */
1913 uint64_t region_offset
= faultaddr
- u
->postcopy_client_bases
[i
];
1914 if (region_offset
< dev
->mem
->regions
[i
].memory_size
) {
1915 rb_offset
= region_offset
+ u
->region_rb_offset
[i
];
1916 trace_vhost_user_postcopy_fault_handler_found(i
,
1917 region_offset
, rb_offset
);
1918 rb
= u
->region_rb
[i
];
1919 return postcopy_request_shared_page(pcfd
, rb
, faultaddr
,
1924 error_report("%s: Failed to find region for fault %" PRIx64
,
1925 __func__
, faultaddr
);
1929 static int vhost_user_postcopy_waker(struct PostCopyFD
*pcfd
, RAMBlock
*rb
,
1932 struct vhost_dev
*dev
= pcfd
->data
;
1933 struct vhost_user
*u
= dev
->opaque
;
1936 trace_vhost_user_postcopy_waker(qemu_ram_get_idstr(rb
), offset
);
1941 /* Translate the offset into an address in the clients address space */
1942 for (i
= 0; i
< MIN(dev
->mem
->nregions
, u
->region_rb_len
); i
++) {
1943 if (u
->region_rb
[i
] == rb
&&
1944 offset
>= u
->region_rb_offset
[i
] &&
1945 offset
< (u
->region_rb_offset
[i
] +
1946 dev
->mem
->regions
[i
].memory_size
)) {
1947 uint64_t client_addr
= (offset
- u
->region_rb_offset
[i
]) +
1948 u
->postcopy_client_bases
[i
];
1949 trace_vhost_user_postcopy_waker_found(client_addr
);
1950 return postcopy_wake_shared(pcfd
, client_addr
, rb
);
1954 trace_vhost_user_postcopy_waker_nomatch(qemu_ram_get_idstr(rb
), offset
);
1960 * Called at the start of an inbound postcopy on reception of the
1963 static int vhost_user_postcopy_advise(struct vhost_dev
*dev
, Error
**errp
)
1966 struct vhost_user
*u
= dev
->opaque
;
1967 CharBackend
*chr
= u
->user
->chr
;
1970 VhostUserMsg msg
= {
1971 .hdr
.request
= VHOST_USER_POSTCOPY_ADVISE
,
1972 .hdr
.flags
= VHOST_USER_VERSION
,
1975 ret
= vhost_user_write(dev
, &msg
, NULL
, 0);
1977 error_setg(errp
, "Failed to send postcopy_advise to vhost");
1981 ret
= vhost_user_read(dev
, &msg
);
1983 error_setg(errp
, "Failed to get postcopy_advise reply from vhost");
1987 if (msg
.hdr
.request
!= VHOST_USER_POSTCOPY_ADVISE
) {
1988 error_setg(errp
, "Unexpected msg type. Expected %d received %d",
1989 VHOST_USER_POSTCOPY_ADVISE
, msg
.hdr
.request
);
1994 error_setg(errp
, "Received bad msg size.");
1997 ufd
= qemu_chr_fe_get_msgfd(chr
);
1999 error_setg(errp
, "%s: Failed to get ufd", __func__
);
2002 qemu_socket_set_nonblock(ufd
);
2004 /* register ufd with userfault thread */
2005 u
->postcopy_fd
.fd
= ufd
;
2006 u
->postcopy_fd
.data
= dev
;
2007 u
->postcopy_fd
.handler
= vhost_user_postcopy_fault_handler
;
2008 u
->postcopy_fd
.waker
= vhost_user_postcopy_waker
;
2009 u
->postcopy_fd
.idstr
= "vhost-user"; /* Need to find unique name */
2010 postcopy_register_shared_ufd(&u
->postcopy_fd
);
2013 error_setg(errp
, "Postcopy not supported on non-Linux systems");
2019 * Called at the switch to postcopy on reception of the 'listen' command.
2021 static int vhost_user_postcopy_listen(struct vhost_dev
*dev
, Error
**errp
)
2023 struct vhost_user
*u
= dev
->opaque
;
2025 VhostUserMsg msg
= {
2026 .hdr
.request
= VHOST_USER_POSTCOPY_LISTEN
,
2027 .hdr
.flags
= VHOST_USER_VERSION
| VHOST_USER_NEED_REPLY_MASK
,
2029 u
->postcopy_listen
= true;
2031 trace_vhost_user_postcopy_listen();
2033 ret
= vhost_user_write(dev
, &msg
, NULL
, 0);
2035 error_setg(errp
, "Failed to send postcopy_listen to vhost");
2039 ret
= process_message_reply(dev
, &msg
);
2041 error_setg(errp
, "Failed to receive reply to postcopy_listen");
2049 * Called at the end of postcopy
2051 static int vhost_user_postcopy_end(struct vhost_dev
*dev
, Error
**errp
)
2053 VhostUserMsg msg
= {
2054 .hdr
.request
= VHOST_USER_POSTCOPY_END
,
2055 .hdr
.flags
= VHOST_USER_VERSION
| VHOST_USER_NEED_REPLY_MASK
,
2058 struct vhost_user
*u
= dev
->opaque
;
2060 trace_vhost_user_postcopy_end_entry();
2062 ret
= vhost_user_write(dev
, &msg
, NULL
, 0);
2064 error_setg(errp
, "Failed to send postcopy_end to vhost");
2068 ret
= process_message_reply(dev
, &msg
);
2070 error_setg(errp
, "Failed to receive reply to postcopy_end");
2073 postcopy_unregister_shared_ufd(&u
->postcopy_fd
);
2074 close(u
->postcopy_fd
.fd
);
2075 u
->postcopy_fd
.handler
= NULL
;
2077 trace_vhost_user_postcopy_end_exit();
2082 static int vhost_user_postcopy_notifier(NotifierWithReturn
*notifier
,
2085 struct PostcopyNotifyData
*pnd
= opaque
;
2086 struct vhost_user
*u
= container_of(notifier
, struct vhost_user
,
2088 struct vhost_dev
*dev
= u
->dev
;
2090 switch (pnd
->reason
) {
2091 case POSTCOPY_NOTIFY_PROBE
:
2092 if (!virtio_has_feature(dev
->protocol_features
,
2093 VHOST_USER_PROTOCOL_F_PAGEFAULT
)) {
2094 /* TODO: Get the device name into this error somehow */
2095 error_setg(pnd
->errp
,
2096 "vhost-user backend not capable of postcopy");
2101 case POSTCOPY_NOTIFY_INBOUND_ADVISE
:
2102 return vhost_user_postcopy_advise(dev
, pnd
->errp
);
2104 case POSTCOPY_NOTIFY_INBOUND_LISTEN
:
2105 return vhost_user_postcopy_listen(dev
, pnd
->errp
);
2107 case POSTCOPY_NOTIFY_INBOUND_END
:
2108 return vhost_user_postcopy_end(dev
, pnd
->errp
);
2111 /* We ignore notifications we don't know */
2118 static int vhost_user_backend_init(struct vhost_dev
*dev
, void *opaque
,
2121 uint64_t features
, ram_slots
;
2122 struct vhost_user
*u
;
2123 VhostUserState
*vus
= (VhostUserState
*) opaque
;
2126 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
2128 u
= g_new0(struct vhost_user
, 1);
2133 err
= vhost_user_get_features(dev
, &features
);
2135 error_setg_errno(errp
, -err
, "vhost_backend_init failed");
2139 if (virtio_has_feature(features
, VHOST_USER_F_PROTOCOL_FEATURES
)) {
2140 bool supports_f_config
= vus
->supports_config
||
2141 (dev
->config_ops
&& dev
->config_ops
->vhost_dev_config_notifier
);
2142 uint64_t protocol_features
;
2144 dev
->backend_features
|= 1ULL << VHOST_USER_F_PROTOCOL_FEATURES
;
2146 err
= vhost_user_get_u64(dev
, VHOST_USER_GET_PROTOCOL_FEATURES
,
2147 &protocol_features
);
2149 error_setg_errno(errp
, EPROTO
, "vhost_backend_init failed");
2154 * We will use all the protocol features we support - although
2155 * we suppress F_CONFIG if we know QEMUs internal code can not support
2158 protocol_features
&= VHOST_USER_PROTOCOL_FEATURE_MASK
;
2160 if (supports_f_config
) {
2161 if (!virtio_has_feature(protocol_features
,
2162 VHOST_USER_PROTOCOL_F_CONFIG
)) {
2163 error_setg(errp
, "vhost-user device expecting "
2164 "VHOST_USER_PROTOCOL_F_CONFIG but the vhost-user backend does "
2169 if (virtio_has_feature(protocol_features
,
2170 VHOST_USER_PROTOCOL_F_CONFIG
)) {
2171 warn_report("vhost-user backend supports "
2172 "VHOST_USER_PROTOCOL_F_CONFIG but QEMU does not.");
2173 protocol_features
&= ~(1ULL << VHOST_USER_PROTOCOL_F_CONFIG
);
2177 /* final set of protocol features */
2178 dev
->protocol_features
= protocol_features
;
2179 err
= vhost_user_set_protocol_features(dev
, dev
->protocol_features
);
2181 error_setg_errno(errp
, EPROTO
, "vhost_backend_init failed");
2185 /* query the max queues we support if backend supports Multiple Queue */
2186 if (dev
->protocol_features
& (1ULL << VHOST_USER_PROTOCOL_F_MQ
)) {
2187 err
= vhost_user_get_u64(dev
, VHOST_USER_GET_QUEUE_NUM
,
2190 error_setg_errno(errp
, EPROTO
, "vhost_backend_init failed");
2194 dev
->max_queues
= 1;
2197 if (dev
->num_queues
&& dev
->max_queues
< dev
->num_queues
) {
2198 error_setg(errp
, "The maximum number of queues supported by the "
2199 "backend is %" PRIu64
, dev
->max_queues
);
2203 if (virtio_has_feature(features
, VIRTIO_F_IOMMU_PLATFORM
) &&
2204 !(virtio_has_feature(dev
->protocol_features
,
2205 VHOST_USER_PROTOCOL_F_BACKEND_REQ
) &&
2206 virtio_has_feature(dev
->protocol_features
,
2207 VHOST_USER_PROTOCOL_F_REPLY_ACK
))) {
2208 error_setg(errp
, "IOMMU support requires reply-ack and "
2209 "backend-req protocol features.");
2213 /* get max memory regions if backend supports configurable RAM slots */
2214 if (!virtio_has_feature(dev
->protocol_features
,
2215 VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS
)) {
2216 u
->user
->memory_slots
= VHOST_MEMORY_BASELINE_NREGIONS
;
2218 err
= vhost_user_get_max_memslots(dev
, &ram_slots
);
2220 error_setg_errno(errp
, EPROTO
, "vhost_backend_init failed");
2224 if (ram_slots
< u
->user
->memory_slots
) {
2225 error_setg(errp
, "The backend specified a max ram slots limit "
2226 "of %" PRIu64
", when the prior validated limit was "
2227 "%d. This limit should never decrease.", ram_slots
,
2228 u
->user
->memory_slots
);
2232 u
->user
->memory_slots
= MIN(ram_slots
, VHOST_USER_MAX_RAM_SLOTS
);
2236 if (dev
->migration_blocker
== NULL
&&
2237 !virtio_has_feature(dev
->protocol_features
,
2238 VHOST_USER_PROTOCOL_F_LOG_SHMFD
)) {
2239 error_setg(&dev
->migration_blocker
,
2240 "Migration disabled: vhost-user backend lacks "
2241 "VHOST_USER_PROTOCOL_F_LOG_SHMFD feature.");
2244 if (dev
->vq_index
== 0) {
2245 err
= vhost_setup_backend_channel(dev
);
2247 error_setg_errno(errp
, EPROTO
, "vhost_backend_init failed");
2252 u
->postcopy_notifier
.notify
= vhost_user_postcopy_notifier
;
2253 postcopy_add_notifier(&u
->postcopy_notifier
);
2258 static int vhost_user_backend_cleanup(struct vhost_dev
*dev
)
2260 struct vhost_user
*u
;
2262 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
2265 if (u
->postcopy_notifier
.notify
) {
2266 postcopy_remove_notifier(&u
->postcopy_notifier
);
2267 u
->postcopy_notifier
.notify
= NULL
;
2269 u
->postcopy_listen
= false;
2270 if (u
->postcopy_fd
.handler
) {
2271 postcopy_unregister_shared_ufd(&u
->postcopy_fd
);
2272 close(u
->postcopy_fd
.fd
);
2273 u
->postcopy_fd
.handler
= NULL
;
2275 if (u
->backend_ioc
) {
2276 close_backend_channel(u
);
2278 g_free(u
->region_rb
);
2279 u
->region_rb
= NULL
;
2280 g_free(u
->region_rb_offset
);
2281 u
->region_rb_offset
= NULL
;
2282 u
->region_rb_len
= 0;
2289 static int vhost_user_get_vq_index(struct vhost_dev
*dev
, int idx
)
2291 assert(idx
>= dev
->vq_index
&& idx
< dev
->vq_index
+ dev
->nvqs
);
2296 static int vhost_user_memslots_limit(struct vhost_dev
*dev
)
2298 struct vhost_user
*u
= dev
->opaque
;
2300 return u
->user
->memory_slots
;
2303 static bool vhost_user_requires_shm_log(struct vhost_dev
*dev
)
2305 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
2307 return virtio_has_feature(dev
->protocol_features
,
2308 VHOST_USER_PROTOCOL_F_LOG_SHMFD
);
2311 static int vhost_user_migration_done(struct vhost_dev
*dev
, char* mac_addr
)
2313 VhostUserMsg msg
= { };
2315 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
2317 /* If guest supports GUEST_ANNOUNCE do nothing */
2318 if (virtio_has_feature(dev
->acked_features
, VIRTIO_NET_F_GUEST_ANNOUNCE
)) {
2322 /* if backend supports VHOST_USER_PROTOCOL_F_RARP ask it to send the RARP */
2323 if (virtio_has_feature(dev
->protocol_features
,
2324 VHOST_USER_PROTOCOL_F_RARP
)) {
2325 msg
.hdr
.request
= VHOST_USER_SEND_RARP
;
2326 msg
.hdr
.flags
= VHOST_USER_VERSION
;
2327 memcpy((char *)&msg
.payload
.u64
, mac_addr
, 6);
2328 msg
.hdr
.size
= sizeof(msg
.payload
.u64
);
2330 return vhost_user_write(dev
, &msg
, NULL
, 0);
2335 static int vhost_user_net_set_mtu(struct vhost_dev
*dev
, uint16_t mtu
)
2338 bool reply_supported
= virtio_has_feature(dev
->protocol_features
,
2339 VHOST_USER_PROTOCOL_F_REPLY_ACK
);
2342 if (!(dev
->protocol_features
& (1ULL << VHOST_USER_PROTOCOL_F_NET_MTU
))) {
2346 msg
.hdr
.request
= VHOST_USER_NET_SET_MTU
;
2347 msg
.payload
.u64
= mtu
;
2348 msg
.hdr
.size
= sizeof(msg
.payload
.u64
);
2349 msg
.hdr
.flags
= VHOST_USER_VERSION
;
2350 if (reply_supported
) {
2351 msg
.hdr
.flags
|= VHOST_USER_NEED_REPLY_MASK
;
2354 ret
= vhost_user_write(dev
, &msg
, NULL
, 0);
2359 /* If reply_ack supported, backend has to ack specified MTU is valid */
2360 if (reply_supported
) {
2361 return process_message_reply(dev
, &msg
);
2367 static int vhost_user_send_device_iotlb_msg(struct vhost_dev
*dev
,
2368 struct vhost_iotlb_msg
*imsg
)
2371 VhostUserMsg msg
= {
2372 .hdr
.request
= VHOST_USER_IOTLB_MSG
,
2373 .hdr
.size
= sizeof(msg
.payload
.iotlb
),
2374 .hdr
.flags
= VHOST_USER_VERSION
| VHOST_USER_NEED_REPLY_MASK
,
2375 .payload
.iotlb
= *imsg
,
2378 ret
= vhost_user_write(dev
, &msg
, NULL
, 0);
2383 return process_message_reply(dev
, &msg
);
2387 static void vhost_user_set_iotlb_callback(struct vhost_dev
*dev
, int enabled
)
2389 /* No-op as the receive channel is not dedicated to IOTLB messages. */
2392 static int vhost_user_get_config(struct vhost_dev
*dev
, uint8_t *config
,
2393 uint32_t config_len
, Error
**errp
)
2396 VhostUserMsg msg
= {
2397 .hdr
.request
= VHOST_USER_GET_CONFIG
,
2398 .hdr
.flags
= VHOST_USER_VERSION
,
2399 .hdr
.size
= VHOST_USER_CONFIG_HDR_SIZE
+ config_len
,
2402 if (!virtio_has_feature(dev
->protocol_features
,
2403 VHOST_USER_PROTOCOL_F_CONFIG
)) {
2404 error_setg(errp
, "VHOST_USER_PROTOCOL_F_CONFIG not supported");
2408 assert(config_len
<= VHOST_USER_MAX_CONFIG_SIZE
);
2410 msg
.payload
.config
.offset
= 0;
2411 msg
.payload
.config
.size
= config_len
;
2412 ret
= vhost_user_write(dev
, &msg
, NULL
, 0);
2414 error_setg_errno(errp
, -ret
, "vhost_get_config failed");
2418 ret
= vhost_user_read(dev
, &msg
);
2420 error_setg_errno(errp
, -ret
, "vhost_get_config failed");
2424 if (msg
.hdr
.request
!= VHOST_USER_GET_CONFIG
) {
2426 "Received unexpected msg type. Expected %d received %d",
2427 VHOST_USER_GET_CONFIG
, msg
.hdr
.request
);
2431 if (msg
.hdr
.size
!= VHOST_USER_CONFIG_HDR_SIZE
+ config_len
) {
2432 error_setg(errp
, "Received bad msg size.");
2436 memcpy(config
, msg
.payload
.config
.region
, config_len
);
2441 static int vhost_user_set_config(struct vhost_dev
*dev
, const uint8_t *data
,
2442 uint32_t offset
, uint32_t size
, uint32_t flags
)
2446 bool reply_supported
= virtio_has_feature(dev
->protocol_features
,
2447 VHOST_USER_PROTOCOL_F_REPLY_ACK
);
2449 VhostUserMsg msg
= {
2450 .hdr
.request
= VHOST_USER_SET_CONFIG
,
2451 .hdr
.flags
= VHOST_USER_VERSION
,
2452 .hdr
.size
= VHOST_USER_CONFIG_HDR_SIZE
+ size
,
2455 if (!virtio_has_feature(dev
->protocol_features
,
2456 VHOST_USER_PROTOCOL_F_CONFIG
)) {
2460 if (reply_supported
) {
2461 msg
.hdr
.flags
|= VHOST_USER_NEED_REPLY_MASK
;
2464 if (size
> VHOST_USER_MAX_CONFIG_SIZE
) {
2468 msg
.payload
.config
.offset
= offset
,
2469 msg
.payload
.config
.size
= size
,
2470 msg
.payload
.config
.flags
= flags
,
2471 p
= msg
.payload
.config
.region
;
2472 memcpy(p
, data
, size
);
2474 ret
= vhost_user_write(dev
, &msg
, NULL
, 0);
2479 if (reply_supported
) {
2480 return process_message_reply(dev
, &msg
);
2486 static int vhost_user_crypto_create_session(struct vhost_dev
*dev
,
2488 uint64_t *session_id
)
2491 bool crypto_session
= virtio_has_feature(dev
->protocol_features
,
2492 VHOST_USER_PROTOCOL_F_CRYPTO_SESSION
);
2493 CryptoDevBackendSessionInfo
*backend_info
= session_info
;
2494 VhostUserMsg msg
= {
2495 .hdr
.request
= VHOST_USER_CREATE_CRYPTO_SESSION
,
2496 .hdr
.flags
= VHOST_USER_VERSION
,
2497 .hdr
.size
= sizeof(msg
.payload
.session
),
2500 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
2502 if (!crypto_session
) {
2503 error_report("vhost-user trying to send unhandled ioctl");
2507 if (backend_info
->op_code
== VIRTIO_CRYPTO_AKCIPHER_CREATE_SESSION
) {
2508 CryptoDevBackendAsymSessionInfo
*sess
= &backend_info
->u
.asym_sess_info
;
2511 memcpy(&msg
.payload
.session
.u
.asym
.session_setup_data
, sess
,
2512 sizeof(CryptoDevBackendAsymSessionInfo
));
2514 keylen
= sizeof(msg
.payload
.session
.u
.asym
.key
);
2515 if (sess
->keylen
> keylen
) {
2516 error_report("Unsupported asymmetric key size");
2520 memcpy(&msg
.payload
.session
.u
.asym
.key
, sess
->key
,
2524 CryptoDevBackendSymSessionInfo
*sess
= &backend_info
->u
.sym_sess_info
;
2527 memcpy(&msg
.payload
.session
.u
.sym
.session_setup_data
, sess
,
2528 sizeof(CryptoDevBackendSymSessionInfo
));
2529 if (sess
->key_len
) {
2530 keylen
= sizeof(msg
.payload
.session
.u
.sym
.key
);
2531 if (sess
->key_len
> keylen
) {
2532 error_report("Unsupported cipher key size");
2536 memcpy(&msg
.payload
.session
.u
.sym
.key
, sess
->cipher_key
,
2540 if (sess
->auth_key_len
> 0) {
2541 keylen
= sizeof(msg
.payload
.session
.u
.sym
.auth_key
);
2542 if (sess
->auth_key_len
> keylen
) {
2543 error_report("Unsupported auth key size");
2547 memcpy(&msg
.payload
.session
.u
.sym
.auth_key
, sess
->auth_key
,
2548 sess
->auth_key_len
);
2552 msg
.payload
.session
.op_code
= backend_info
->op_code
;
2553 msg
.payload
.session
.session_id
= backend_info
->session_id
;
2554 ret
= vhost_user_write(dev
, &msg
, NULL
, 0);
2556 error_report("vhost_user_write() return %d, create session failed",
2561 ret
= vhost_user_read(dev
, &msg
);
2563 error_report("vhost_user_read() return %d, create session failed",
2568 if (msg
.hdr
.request
!= VHOST_USER_CREATE_CRYPTO_SESSION
) {
2569 error_report("Received unexpected msg type. Expected %d received %d",
2570 VHOST_USER_CREATE_CRYPTO_SESSION
, msg
.hdr
.request
);
2574 if (msg
.hdr
.size
!= sizeof(msg
.payload
.session
)) {
2575 error_report("Received bad msg size.");
2579 if (msg
.payload
.session
.session_id
< 0) {
2580 error_report("Bad session id: %" PRId64
"",
2581 msg
.payload
.session
.session_id
);
2584 *session_id
= msg
.payload
.session
.session_id
;
2590 vhost_user_crypto_close_session(struct vhost_dev
*dev
, uint64_t session_id
)
2593 bool crypto_session
= virtio_has_feature(dev
->protocol_features
,
2594 VHOST_USER_PROTOCOL_F_CRYPTO_SESSION
);
2595 VhostUserMsg msg
= {
2596 .hdr
.request
= VHOST_USER_CLOSE_CRYPTO_SESSION
,
2597 .hdr
.flags
= VHOST_USER_VERSION
,
2598 .hdr
.size
= sizeof(msg
.payload
.u64
),
2600 msg
.payload
.u64
= session_id
;
2602 if (!crypto_session
) {
2603 error_report("vhost-user trying to send unhandled ioctl");
2607 ret
= vhost_user_write(dev
, &msg
, NULL
, 0);
2609 error_report("vhost_user_write() return %d, close session failed",
2617 static bool vhost_user_no_private_memslots(struct vhost_dev
*dev
)
2622 static int vhost_user_get_inflight_fd(struct vhost_dev
*dev
,
2623 uint16_t queue_size
,
2624 struct vhost_inflight
*inflight
)
2629 struct vhost_user
*u
= dev
->opaque
;
2630 CharBackend
*chr
= u
->user
->chr
;
2631 VhostUserMsg msg
= {
2632 .hdr
.request
= VHOST_USER_GET_INFLIGHT_FD
,
2633 .hdr
.flags
= VHOST_USER_VERSION
,
2634 .payload
.inflight
.num_queues
= dev
->nvqs
,
2635 .payload
.inflight
.queue_size
= queue_size
,
2636 .hdr
.size
= sizeof(msg
.payload
.inflight
),
2639 if (!virtio_has_feature(dev
->protocol_features
,
2640 VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD
)) {
2644 ret
= vhost_user_write(dev
, &msg
, NULL
, 0);
2649 ret
= vhost_user_read(dev
, &msg
);
2654 if (msg
.hdr
.request
!= VHOST_USER_GET_INFLIGHT_FD
) {
2655 error_report("Received unexpected msg type. "
2656 "Expected %d received %d",
2657 VHOST_USER_GET_INFLIGHT_FD
, msg
.hdr
.request
);
2661 if (msg
.hdr
.size
!= sizeof(msg
.payload
.inflight
)) {
2662 error_report("Received bad msg size.");
2666 if (!msg
.payload
.inflight
.mmap_size
) {
2670 fd
= qemu_chr_fe_get_msgfd(chr
);
2672 error_report("Failed to get mem fd");
2676 addr
= mmap(0, msg
.payload
.inflight
.mmap_size
, PROT_READ
| PROT_WRITE
,
2677 MAP_SHARED
, fd
, msg
.payload
.inflight
.mmap_offset
);
2679 if (addr
== MAP_FAILED
) {
2680 error_report("Failed to mmap mem fd");
2685 inflight
->addr
= addr
;
2687 inflight
->size
= msg
.payload
.inflight
.mmap_size
;
2688 inflight
->offset
= msg
.payload
.inflight
.mmap_offset
;
2689 inflight
->queue_size
= queue_size
;
2694 static int vhost_user_set_inflight_fd(struct vhost_dev
*dev
,
2695 struct vhost_inflight
*inflight
)
2697 VhostUserMsg msg
= {
2698 .hdr
.request
= VHOST_USER_SET_INFLIGHT_FD
,
2699 .hdr
.flags
= VHOST_USER_VERSION
,
2700 .payload
.inflight
.mmap_size
= inflight
->size
,
2701 .payload
.inflight
.mmap_offset
= inflight
->offset
,
2702 .payload
.inflight
.num_queues
= dev
->nvqs
,
2703 .payload
.inflight
.queue_size
= inflight
->queue_size
,
2704 .hdr
.size
= sizeof(msg
.payload
.inflight
),
2707 if (!virtio_has_feature(dev
->protocol_features
,
2708 VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD
)) {
2712 return vhost_user_write(dev
, &msg
, &inflight
->fd
, 1);
2715 static void vhost_user_state_destroy(gpointer data
)
2717 VhostUserHostNotifier
*n
= (VhostUserHostNotifier
*) data
;
2719 vhost_user_host_notifier_remove(n
, NULL
);
2720 object_unparent(OBJECT(&n
->mr
));
2722 * We can't free until vhost_user_host_notifier_remove has
2723 * done it's thing so schedule the free with RCU.
2729 bool vhost_user_init(VhostUserState
*user
, CharBackend
*chr
, Error
**errp
)
2732 error_setg(errp
, "Cannot initialize vhost-user state");
2736 user
->memory_slots
= 0;
2737 user
->notifiers
= g_ptr_array_new_full(VIRTIO_QUEUE_MAX
/ 4,
2738 &vhost_user_state_destroy
);
2742 void vhost_user_cleanup(VhostUserState
*user
)
2747 memory_region_transaction_begin();
2748 user
->notifiers
= (GPtrArray
*) g_ptr_array_free(user
->notifiers
, true);
2749 memory_region_transaction_commit();
2755 vu_async_close_fn cb
;
2758 struct vhost_dev
*vhost
;
2759 } VhostAsyncCallback
;
2761 static void vhost_user_async_close_bh(void *opaque
)
2763 VhostAsyncCallback
*data
= opaque
;
2764 struct vhost_dev
*vhost
= data
->vhost
;
2767 * If the vhost_dev has been cleared in the meantime there is
2768 * nothing left to do as some other path has completed the
2772 data
->cb(data
->dev
);
2779 * We only schedule the work if the machine is running. If suspended
2780 * we want to keep all the in-flight data as is for migration
2783 void vhost_user_async_close(DeviceState
*d
,
2784 CharBackend
*chardev
, struct vhost_dev
*vhost
,
2785 vu_async_close_fn cb
)
2787 if (!runstate_check(RUN_STATE_SHUTDOWN
)) {
2789 * A close event may happen during a read/write, but vhost
2790 * code assumes the vhost_dev remains setup, so delay the
2793 AioContext
*ctx
= qemu_get_current_aio_context();
2794 VhostAsyncCallback
*data
= g_new0(VhostAsyncCallback
, 1);
2796 /* Save data for the callback */
2800 data
->vhost
= vhost
;
2802 /* Disable any further notifications on the chardev */
2803 qemu_chr_fe_set_handlers(chardev
,
2804 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
2807 aio_bh_schedule_oneshot(ctx
, vhost_user_async_close_bh
, data
);
2810 * Move vhost device to the stopped state. The vhost-user device
2811 * will be clean up and disconnected in BH. This can be useful in
2812 * the vhost migration code. If disconnect was caught there is an
2813 * option for the general vhost code to get the dev state without
2814 * knowing its type (in this case vhost-user).
2816 * Note if the vhost device is fully cleared by the time we
2817 * execute the bottom half we won't continue with the cleanup.
2819 vhost
->started
= false;
2823 static int vhost_user_dev_start(struct vhost_dev
*dev
, bool started
)
2825 if (!virtio_has_feature(dev
->protocol_features
,
2826 VHOST_USER_PROTOCOL_F_STATUS
)) {
2830 /* Set device status only for last queue pair */
2831 if (dev
->vq_index
+ dev
->nvqs
!= dev
->vq_index_end
) {
2836 return vhost_user_add_status(dev
, VIRTIO_CONFIG_S_ACKNOWLEDGE
|
2837 VIRTIO_CONFIG_S_DRIVER
|
2838 VIRTIO_CONFIG_S_DRIVER_OK
);
2844 static void vhost_user_reset_status(struct vhost_dev
*dev
)
2846 /* Set device status only for last queue pair */
2847 if (dev
->vq_index
+ dev
->nvqs
!= dev
->vq_index_end
) {
2851 if (virtio_has_feature(dev
->protocol_features
,
2852 VHOST_USER_PROTOCOL_F_STATUS
)) {
2853 vhost_user_set_status(dev
, 0);
2857 const VhostOps user_ops
= {
2858 .backend_type
= VHOST_BACKEND_TYPE_USER
,
2859 .vhost_backend_init
= vhost_user_backend_init
,
2860 .vhost_backend_cleanup
= vhost_user_backend_cleanup
,
2861 .vhost_backend_memslots_limit
= vhost_user_memslots_limit
,
2862 .vhost_backend_no_private_memslots
= vhost_user_no_private_memslots
,
2863 .vhost_set_log_base
= vhost_user_set_log_base
,
2864 .vhost_set_mem_table
= vhost_user_set_mem_table
,
2865 .vhost_set_vring_addr
= vhost_user_set_vring_addr
,
2866 .vhost_set_vring_endian
= vhost_user_set_vring_endian
,
2867 .vhost_set_vring_num
= vhost_user_set_vring_num
,
2868 .vhost_set_vring_base
= vhost_user_set_vring_base
,
2869 .vhost_get_vring_base
= vhost_user_get_vring_base
,
2870 .vhost_set_vring_kick
= vhost_user_set_vring_kick
,
2871 .vhost_set_vring_call
= vhost_user_set_vring_call
,
2872 .vhost_set_vring_err
= vhost_user_set_vring_err
,
2873 .vhost_set_features
= vhost_user_set_features
,
2874 .vhost_get_features
= vhost_user_get_features
,
2875 .vhost_set_owner
= vhost_user_set_owner
,
2876 .vhost_reset_device
= vhost_user_reset_device
,
2877 .vhost_get_vq_index
= vhost_user_get_vq_index
,
2878 .vhost_set_vring_enable
= vhost_user_set_vring_enable
,
2879 .vhost_requires_shm_log
= vhost_user_requires_shm_log
,
2880 .vhost_migration_done
= vhost_user_migration_done
,
2881 .vhost_net_set_mtu
= vhost_user_net_set_mtu
,
2882 .vhost_set_iotlb_callback
= vhost_user_set_iotlb_callback
,
2883 .vhost_send_device_iotlb_msg
= vhost_user_send_device_iotlb_msg
,
2884 .vhost_get_config
= vhost_user_get_config
,
2885 .vhost_set_config
= vhost_user_set_config
,
2886 .vhost_crypto_create_session
= vhost_user_crypto_create_session
,
2887 .vhost_crypto_close_session
= vhost_user_crypto_close_session
,
2888 .vhost_get_inflight_fd
= vhost_user_get_inflight_fd
,
2889 .vhost_set_inflight_fd
= vhost_user_set_inflight_fd
,
2890 .vhost_dev_start
= vhost_user_dev_start
,
2891 .vhost_reset_status
= vhost_user_reset_status
,