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 VHOST_USER_PROTOCOL_F_MAX
65 #define VHOST_USER_PROTOCOL_FEATURE_MASK ((1 << VHOST_USER_PROTOCOL_F_MAX) - 1)
67 typedef enum VhostUserRequest
{
69 VHOST_USER_GET_FEATURES
= 1,
70 VHOST_USER_SET_FEATURES
= 2,
71 VHOST_USER_SET_OWNER
= 3,
72 VHOST_USER_RESET_OWNER
= 4,
73 VHOST_USER_SET_MEM_TABLE
= 5,
74 VHOST_USER_SET_LOG_BASE
= 6,
75 VHOST_USER_SET_LOG_FD
= 7,
76 VHOST_USER_SET_VRING_NUM
= 8,
77 VHOST_USER_SET_VRING_ADDR
= 9,
78 VHOST_USER_SET_VRING_BASE
= 10,
79 VHOST_USER_GET_VRING_BASE
= 11,
80 VHOST_USER_SET_VRING_KICK
= 12,
81 VHOST_USER_SET_VRING_CALL
= 13,
82 VHOST_USER_SET_VRING_ERR
= 14,
83 VHOST_USER_GET_PROTOCOL_FEATURES
= 15,
84 VHOST_USER_SET_PROTOCOL_FEATURES
= 16,
85 VHOST_USER_GET_QUEUE_NUM
= 17,
86 VHOST_USER_SET_VRING_ENABLE
= 18,
87 VHOST_USER_SEND_RARP
= 19,
88 VHOST_USER_NET_SET_MTU
= 20,
89 VHOST_USER_SET_SLAVE_REQ_FD
= 21,
90 VHOST_USER_IOTLB_MSG
= 22,
91 VHOST_USER_SET_VRING_ENDIAN
= 23,
92 VHOST_USER_GET_CONFIG
= 24,
93 VHOST_USER_SET_CONFIG
= 25,
94 VHOST_USER_CREATE_CRYPTO_SESSION
= 26,
95 VHOST_USER_CLOSE_CRYPTO_SESSION
= 27,
96 VHOST_USER_POSTCOPY_ADVISE
= 28,
97 VHOST_USER_POSTCOPY_LISTEN
= 29,
98 VHOST_USER_POSTCOPY_END
= 30,
99 VHOST_USER_GET_INFLIGHT_FD
= 31,
100 VHOST_USER_SET_INFLIGHT_FD
= 32,
101 VHOST_USER_GPU_SET_SOCKET
= 33,
102 VHOST_USER_RESET_DEVICE
= 34,
106 typedef enum VhostUserSlaveRequest
{
107 VHOST_USER_SLAVE_NONE
= 0,
108 VHOST_USER_SLAVE_IOTLB_MSG
= 1,
109 VHOST_USER_SLAVE_CONFIG_CHANGE_MSG
= 2,
110 VHOST_USER_SLAVE_VRING_HOST_NOTIFIER_MSG
= 3,
112 } VhostUserSlaveRequest
;
114 typedef struct VhostUserMemoryRegion
{
115 uint64_t guest_phys_addr
;
116 uint64_t memory_size
;
117 uint64_t userspace_addr
;
118 uint64_t mmap_offset
;
119 } VhostUserMemoryRegion
;
121 typedef struct VhostUserMemory
{
124 VhostUserMemoryRegion regions
[VHOST_MEMORY_MAX_NREGIONS
];
127 typedef struct VhostUserLog
{
129 uint64_t mmap_offset
;
132 typedef struct VhostUserConfig
{
136 uint8_t region
[VHOST_USER_MAX_CONFIG_SIZE
];
139 #define VHOST_CRYPTO_SYM_HMAC_MAX_KEY_LEN 512
140 #define VHOST_CRYPTO_SYM_CIPHER_MAX_KEY_LEN 64
142 typedef struct VhostUserCryptoSession
{
143 /* session id for success, -1 on errors */
145 CryptoDevBackendSymSessionInfo session_setup_data
;
146 uint8_t key
[VHOST_CRYPTO_SYM_CIPHER_MAX_KEY_LEN
];
147 uint8_t auth_key
[VHOST_CRYPTO_SYM_HMAC_MAX_KEY_LEN
];
148 } VhostUserCryptoSession
;
150 static VhostUserConfig c
__attribute__ ((unused
));
151 #define VHOST_USER_CONFIG_HDR_SIZE (sizeof(c.offset) \
155 typedef struct VhostUserVringArea
{
159 } VhostUserVringArea
;
161 typedef struct VhostUserInflight
{
163 uint64_t mmap_offset
;
169 VhostUserRequest request
;
171 #define VHOST_USER_VERSION_MASK (0x3)
172 #define VHOST_USER_REPLY_MASK (0x1<<2)
173 #define VHOST_USER_NEED_REPLY_MASK (0x1 << 3)
175 uint32_t size
; /* the following payload size */
176 } QEMU_PACKED VhostUserHeader
;
179 #define VHOST_USER_VRING_IDX_MASK (0xff)
180 #define VHOST_USER_VRING_NOFD_MASK (0x1<<8)
182 struct vhost_vring_state state
;
183 struct vhost_vring_addr addr
;
184 VhostUserMemory memory
;
186 struct vhost_iotlb_msg iotlb
;
187 VhostUserConfig config
;
188 VhostUserCryptoSession session
;
189 VhostUserVringArea area
;
190 VhostUserInflight inflight
;
193 typedef struct VhostUserMsg
{
195 VhostUserPayload payload
;
196 } QEMU_PACKED VhostUserMsg
;
198 static VhostUserMsg m
__attribute__ ((unused
));
199 #define VHOST_USER_HDR_SIZE (sizeof(VhostUserHeader))
201 #define VHOST_USER_PAYLOAD_SIZE (sizeof(VhostUserPayload))
203 /* The version of the protocol we support */
204 #define VHOST_USER_VERSION (0x1)
207 struct vhost_dev
*dev
;
208 /* Shared between vhost devs of the same virtio device */
209 VhostUserState
*user
;
211 NotifierWithReturn postcopy_notifier
;
212 struct PostCopyFD postcopy_fd
;
213 uint64_t postcopy_client_bases
[VHOST_MEMORY_MAX_NREGIONS
];
214 /* Length of the region_rb and region_rb_offset arrays */
215 size_t region_rb_len
;
216 /* RAMBlock associated with a given region */
217 RAMBlock
**region_rb
;
218 /* The offset from the start of the RAMBlock to the start of the
221 ram_addr_t
*region_rb_offset
;
223 /* True once we've entered postcopy_listen */
224 bool postcopy_listen
;
227 static bool ioeventfd_enabled(void)
229 return !kvm_enabled() || kvm_eventfds_enabled();
232 static int vhost_user_read_header(struct vhost_dev
*dev
, VhostUserMsg
*msg
)
234 struct vhost_user
*u
= dev
->opaque
;
235 CharBackend
*chr
= u
->user
->chr
;
236 uint8_t *p
= (uint8_t *) msg
;
237 int r
, size
= VHOST_USER_HDR_SIZE
;
239 r
= qemu_chr_fe_read_all(chr
, p
, size
);
241 error_report("Failed to read msg header. Read %d instead of %d."
242 " Original request %d.", r
, size
, msg
->hdr
.request
);
246 /* validate received flags */
247 if (msg
->hdr
.flags
!= (VHOST_USER_REPLY_MASK
| VHOST_USER_VERSION
)) {
248 error_report("Failed to read msg header."
249 " Flags 0x%x instead of 0x%x.", msg
->hdr
.flags
,
250 VHOST_USER_REPLY_MASK
| VHOST_USER_VERSION
);
257 static int vhost_user_read(struct vhost_dev
*dev
, VhostUserMsg
*msg
)
259 struct vhost_user
*u
= dev
->opaque
;
260 CharBackend
*chr
= u
->user
->chr
;
261 uint8_t *p
= (uint8_t *) msg
;
264 if (vhost_user_read_header(dev
, msg
) < 0) {
268 /* validate message size is sane */
269 if (msg
->hdr
.size
> VHOST_USER_PAYLOAD_SIZE
) {
270 error_report("Failed to read msg header."
271 " Size %d exceeds the maximum %zu.", msg
->hdr
.size
,
272 VHOST_USER_PAYLOAD_SIZE
);
277 p
+= VHOST_USER_HDR_SIZE
;
278 size
= msg
->hdr
.size
;
279 r
= qemu_chr_fe_read_all(chr
, p
, size
);
281 error_report("Failed to read msg payload."
282 " Read %d instead of %d.", r
, msg
->hdr
.size
);
290 static int process_message_reply(struct vhost_dev
*dev
,
291 const VhostUserMsg
*msg
)
293 VhostUserMsg msg_reply
;
295 if ((msg
->hdr
.flags
& VHOST_USER_NEED_REPLY_MASK
) == 0) {
299 if (vhost_user_read(dev
, &msg_reply
) < 0) {
303 if (msg_reply
.hdr
.request
!= msg
->hdr
.request
) {
304 error_report("Received unexpected msg type."
305 "Expected %d received %d",
306 msg
->hdr
.request
, msg_reply
.hdr
.request
);
310 return msg_reply
.payload
.u64
? -1 : 0;
313 static bool vhost_user_one_time_request(VhostUserRequest request
)
316 case VHOST_USER_SET_OWNER
:
317 case VHOST_USER_RESET_OWNER
:
318 case VHOST_USER_SET_MEM_TABLE
:
319 case VHOST_USER_GET_QUEUE_NUM
:
320 case VHOST_USER_NET_SET_MTU
:
327 /* most non-init callers ignore the error */
328 static int vhost_user_write(struct vhost_dev
*dev
, VhostUserMsg
*msg
,
329 int *fds
, int fd_num
)
331 struct vhost_user
*u
= dev
->opaque
;
332 CharBackend
*chr
= u
->user
->chr
;
333 int ret
, size
= VHOST_USER_HDR_SIZE
+ msg
->hdr
.size
;
336 * For non-vring specific requests, like VHOST_USER_SET_MEM_TABLE,
337 * we just need send it once in the first time. For later such
338 * request, we just ignore it.
340 if (vhost_user_one_time_request(msg
->hdr
.request
) && dev
->vq_index
!= 0) {
341 msg
->hdr
.flags
&= ~VHOST_USER_NEED_REPLY_MASK
;
345 if (qemu_chr_fe_set_msgfds(chr
, fds
, fd_num
) < 0) {
346 error_report("Failed to set msg fds.");
350 ret
= qemu_chr_fe_write_all(chr
, (const uint8_t *) msg
, size
);
352 error_report("Failed to write msg."
353 " Wrote %d instead of %d.", ret
, size
);
360 int vhost_user_gpu_set_socket(struct vhost_dev
*dev
, int fd
)
363 .hdr
.request
= VHOST_USER_GPU_SET_SOCKET
,
364 .hdr
.flags
= VHOST_USER_VERSION
,
367 return vhost_user_write(dev
, &msg
, &fd
, 1);
370 static int vhost_user_set_log_base(struct vhost_dev
*dev
, uint64_t base
,
371 struct vhost_log
*log
)
373 int fds
[VHOST_MEMORY_MAX_NREGIONS
];
375 bool shmfd
= virtio_has_feature(dev
->protocol_features
,
376 VHOST_USER_PROTOCOL_F_LOG_SHMFD
);
378 .hdr
.request
= VHOST_USER_SET_LOG_BASE
,
379 .hdr
.flags
= VHOST_USER_VERSION
,
380 .payload
.log
.mmap_size
= log
->size
* sizeof(*(log
->log
)),
381 .payload
.log
.mmap_offset
= 0,
382 .hdr
.size
= sizeof(msg
.payload
.log
),
385 if (shmfd
&& log
->fd
!= -1) {
386 fds
[fd_num
++] = log
->fd
;
389 if (vhost_user_write(dev
, &msg
, fds
, fd_num
) < 0) {
395 if (vhost_user_read(dev
, &msg
) < 0) {
399 if (msg
.hdr
.request
!= VHOST_USER_SET_LOG_BASE
) {
400 error_report("Received unexpected msg type. "
401 "Expected %d received %d",
402 VHOST_USER_SET_LOG_BASE
, msg
.hdr
.request
);
410 static int vhost_user_fill_set_mem_table_msg(struct vhost_user
*u
,
411 struct vhost_dev
*dev
,
413 int *fds
, size_t *fd_num
,
414 bool track_ramblocks
)
419 struct vhost_memory_region
*reg
;
421 msg
->hdr
.request
= VHOST_USER_SET_MEM_TABLE
;
423 for (i
= 0; i
< dev
->mem
->nregions
; ++i
) {
424 reg
= dev
->mem
->regions
+ i
;
426 assert((uintptr_t)reg
->userspace_addr
== reg
->userspace_addr
);
427 mr
= memory_region_from_host((void *)(uintptr_t)reg
->userspace_addr
,
429 fd
= memory_region_get_fd(mr
);
431 if (track_ramblocks
) {
432 assert(*fd_num
< VHOST_MEMORY_MAX_NREGIONS
);
433 trace_vhost_user_set_mem_table_withfd(*fd_num
, mr
->name
,
435 reg
->guest_phys_addr
,
438 u
->region_rb_offset
[i
] = offset
;
439 u
->region_rb
[i
] = mr
->ram_block
;
440 } else if (*fd_num
== VHOST_MEMORY_MAX_NREGIONS
) {
441 error_report("Failed preparing vhost-user memory table msg");
444 msg
->payload
.memory
.regions
[*fd_num
].userspace_addr
=
446 msg
->payload
.memory
.regions
[*fd_num
].memory_size
=
448 msg
->payload
.memory
.regions
[*fd_num
].guest_phys_addr
=
449 reg
->guest_phys_addr
;
450 msg
->payload
.memory
.regions
[*fd_num
].mmap_offset
= offset
;
451 fds
[(*fd_num
)++] = fd
;
452 } else if (track_ramblocks
) {
453 u
->region_rb_offset
[i
] = 0;
454 u
->region_rb
[i
] = NULL
;
458 msg
->payload
.memory
.nregions
= *fd_num
;
461 error_report("Failed initializing vhost-user memory map, "
462 "consider using -object memory-backend-file share=on");
466 msg
->hdr
.size
= sizeof(msg
->payload
.memory
.nregions
);
467 msg
->hdr
.size
+= sizeof(msg
->payload
.memory
.padding
);
468 msg
->hdr
.size
+= *fd_num
* sizeof(VhostUserMemoryRegion
);
473 static int vhost_user_set_mem_table_postcopy(struct vhost_dev
*dev
,
474 struct vhost_memory
*mem
)
476 struct vhost_user
*u
= dev
->opaque
;
477 int fds
[VHOST_MEMORY_MAX_NREGIONS
];
479 VhostUserMsg msg_reply
;
483 .hdr
.flags
= VHOST_USER_VERSION
,
486 if (u
->region_rb_len
< dev
->mem
->nregions
) {
487 u
->region_rb
= g_renew(RAMBlock
*, u
->region_rb
, dev
->mem
->nregions
);
488 u
->region_rb_offset
= g_renew(ram_addr_t
, u
->region_rb_offset
,
490 memset(&(u
->region_rb
[u
->region_rb_len
]), '\0',
491 sizeof(RAMBlock
*) * (dev
->mem
->nregions
- u
->region_rb_len
));
492 memset(&(u
->region_rb_offset
[u
->region_rb_len
]), '\0',
493 sizeof(ram_addr_t
) * (dev
->mem
->nregions
- u
->region_rb_len
));
494 u
->region_rb_len
= dev
->mem
->nregions
;
497 if (vhost_user_fill_set_mem_table_msg(u
, dev
, &msg
, fds
, &fd_num
,
502 if (vhost_user_write(dev
, &msg
, fds
, fd_num
) < 0) {
506 if (vhost_user_read(dev
, &msg_reply
) < 0) {
510 if (msg_reply
.hdr
.request
!= VHOST_USER_SET_MEM_TABLE
) {
511 error_report("%s: Received unexpected msg type."
512 "Expected %d received %d", __func__
,
513 VHOST_USER_SET_MEM_TABLE
, msg_reply
.hdr
.request
);
516 /* We're using the same structure, just reusing one of the
517 * fields, so it should be the same size.
519 if (msg_reply
.hdr
.size
!= msg
.hdr
.size
) {
520 error_report("%s: Unexpected size for postcopy reply "
521 "%d vs %d", __func__
, msg_reply
.hdr
.size
, msg
.hdr
.size
);
525 memset(u
->postcopy_client_bases
, 0,
526 sizeof(uint64_t) * VHOST_MEMORY_MAX_NREGIONS
);
528 /* They're in the same order as the regions that were sent
529 * but some of the regions were skipped (above) if they
532 for (msg_i
= 0, region_i
= 0;
533 region_i
< dev
->mem
->nregions
;
535 if (msg_i
< fd_num
&&
536 msg_reply
.payload
.memory
.regions
[msg_i
].guest_phys_addr
==
537 dev
->mem
->regions
[region_i
].guest_phys_addr
) {
538 u
->postcopy_client_bases
[region_i
] =
539 msg_reply
.payload
.memory
.regions
[msg_i
].userspace_addr
;
540 trace_vhost_user_set_mem_table_postcopy(
541 msg_reply
.payload
.memory
.regions
[msg_i
].userspace_addr
,
542 msg
.payload
.memory
.regions
[msg_i
].userspace_addr
,
547 if (msg_i
!= fd_num
) {
548 error_report("%s: postcopy reply not fully consumed "
550 __func__
, msg_i
, fd_num
);
553 /* Now we've registered this with the postcopy code, we ack to the client,
554 * because now we're in the position to be able to deal with any faults
557 /* TODO: Use this for failure cases as well with a bad value */
558 msg
.hdr
.size
= sizeof(msg
.payload
.u64
);
559 msg
.payload
.u64
= 0; /* OK */
560 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
567 static int vhost_user_set_mem_table(struct vhost_dev
*dev
,
568 struct vhost_memory
*mem
)
570 struct vhost_user
*u
= dev
->opaque
;
571 int fds
[VHOST_MEMORY_MAX_NREGIONS
];
573 bool do_postcopy
= u
->postcopy_listen
&& u
->postcopy_fd
.handler
;
574 bool reply_supported
= virtio_has_feature(dev
->protocol_features
,
575 VHOST_USER_PROTOCOL_F_REPLY_ACK
);
578 /* Postcopy has enough differences that it's best done in it's own
581 return vhost_user_set_mem_table_postcopy(dev
, mem
);
585 .hdr
.flags
= VHOST_USER_VERSION
,
588 if (reply_supported
) {
589 msg
.hdr
.flags
|= VHOST_USER_NEED_REPLY_MASK
;
592 if (vhost_user_fill_set_mem_table_msg(u
, dev
, &msg
, fds
, &fd_num
,
597 if (vhost_user_write(dev
, &msg
, fds
, fd_num
) < 0) {
601 if (reply_supported
) {
602 return process_message_reply(dev
, &msg
);
608 static int vhost_user_set_vring_addr(struct vhost_dev
*dev
,
609 struct vhost_vring_addr
*addr
)
612 .hdr
.request
= VHOST_USER_SET_VRING_ADDR
,
613 .hdr
.flags
= VHOST_USER_VERSION
,
614 .payload
.addr
= *addr
,
615 .hdr
.size
= sizeof(msg
.payload
.addr
),
618 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
625 static int vhost_user_set_vring_endian(struct vhost_dev
*dev
,
626 struct vhost_vring_state
*ring
)
628 bool cross_endian
= virtio_has_feature(dev
->protocol_features
,
629 VHOST_USER_PROTOCOL_F_CROSS_ENDIAN
);
631 .hdr
.request
= VHOST_USER_SET_VRING_ENDIAN
,
632 .hdr
.flags
= VHOST_USER_VERSION
,
633 .payload
.state
= *ring
,
634 .hdr
.size
= sizeof(msg
.payload
.state
),
638 error_report("vhost-user trying to send unhandled ioctl");
642 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
649 static int vhost_set_vring(struct vhost_dev
*dev
,
650 unsigned long int request
,
651 struct vhost_vring_state
*ring
)
654 .hdr
.request
= request
,
655 .hdr
.flags
= VHOST_USER_VERSION
,
656 .payload
.state
= *ring
,
657 .hdr
.size
= sizeof(msg
.payload
.state
),
660 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
667 static int vhost_user_set_vring_num(struct vhost_dev
*dev
,
668 struct vhost_vring_state
*ring
)
670 return vhost_set_vring(dev
, VHOST_USER_SET_VRING_NUM
, ring
);
673 static void vhost_user_host_notifier_restore(struct vhost_dev
*dev
,
676 struct vhost_user
*u
= dev
->opaque
;
677 VhostUserHostNotifier
*n
= &u
->user
->notifier
[queue_idx
];
678 VirtIODevice
*vdev
= dev
->vdev
;
680 if (n
->addr
&& !n
->set
) {
681 virtio_queue_set_host_notifier_mr(vdev
, queue_idx
, &n
->mr
, true);
686 static void vhost_user_host_notifier_remove(struct vhost_dev
*dev
,
689 struct vhost_user
*u
= dev
->opaque
;
690 VhostUserHostNotifier
*n
= &u
->user
->notifier
[queue_idx
];
691 VirtIODevice
*vdev
= dev
->vdev
;
693 if (n
->addr
&& n
->set
) {
694 virtio_queue_set_host_notifier_mr(vdev
, queue_idx
, &n
->mr
, false);
699 static int vhost_user_set_vring_base(struct vhost_dev
*dev
,
700 struct vhost_vring_state
*ring
)
702 vhost_user_host_notifier_restore(dev
, ring
->index
);
704 return vhost_set_vring(dev
, VHOST_USER_SET_VRING_BASE
, ring
);
707 static int vhost_user_set_vring_enable(struct vhost_dev
*dev
, int enable
)
711 if (!virtio_has_feature(dev
->features
, VHOST_USER_F_PROTOCOL_FEATURES
)) {
715 for (i
= 0; i
< dev
->nvqs
; ++i
) {
716 struct vhost_vring_state state
= {
717 .index
= dev
->vq_index
+ i
,
721 vhost_set_vring(dev
, VHOST_USER_SET_VRING_ENABLE
, &state
);
727 static int vhost_user_get_vring_base(struct vhost_dev
*dev
,
728 struct vhost_vring_state
*ring
)
731 .hdr
.request
= VHOST_USER_GET_VRING_BASE
,
732 .hdr
.flags
= VHOST_USER_VERSION
,
733 .payload
.state
= *ring
,
734 .hdr
.size
= sizeof(msg
.payload
.state
),
737 vhost_user_host_notifier_remove(dev
, ring
->index
);
739 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
743 if (vhost_user_read(dev
, &msg
) < 0) {
747 if (msg
.hdr
.request
!= VHOST_USER_GET_VRING_BASE
) {
748 error_report("Received unexpected msg type. Expected %d received %d",
749 VHOST_USER_GET_VRING_BASE
, msg
.hdr
.request
);
753 if (msg
.hdr
.size
!= sizeof(msg
.payload
.state
)) {
754 error_report("Received bad msg size.");
758 *ring
= msg
.payload
.state
;
763 static int vhost_set_vring_file(struct vhost_dev
*dev
,
764 VhostUserRequest request
,
765 struct vhost_vring_file
*file
)
767 int fds
[VHOST_MEMORY_MAX_NREGIONS
];
770 .hdr
.request
= request
,
771 .hdr
.flags
= VHOST_USER_VERSION
,
772 .payload
.u64
= file
->index
& VHOST_USER_VRING_IDX_MASK
,
773 .hdr
.size
= sizeof(msg
.payload
.u64
),
776 if (ioeventfd_enabled() && file
->fd
> 0) {
777 fds
[fd_num
++] = file
->fd
;
779 msg
.payload
.u64
|= VHOST_USER_VRING_NOFD_MASK
;
782 if (vhost_user_write(dev
, &msg
, fds
, fd_num
) < 0) {
789 static int vhost_user_set_vring_kick(struct vhost_dev
*dev
,
790 struct vhost_vring_file
*file
)
792 return vhost_set_vring_file(dev
, VHOST_USER_SET_VRING_KICK
, file
);
795 static int vhost_user_set_vring_call(struct vhost_dev
*dev
,
796 struct vhost_vring_file
*file
)
798 return vhost_set_vring_file(dev
, VHOST_USER_SET_VRING_CALL
, file
);
801 static int vhost_user_set_u64(struct vhost_dev
*dev
, int request
, uint64_t u64
)
804 .hdr
.request
= request
,
805 .hdr
.flags
= VHOST_USER_VERSION
,
807 .hdr
.size
= sizeof(msg
.payload
.u64
),
810 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
817 static int vhost_user_set_features(struct vhost_dev
*dev
,
820 return vhost_user_set_u64(dev
, VHOST_USER_SET_FEATURES
, features
);
823 static int vhost_user_set_protocol_features(struct vhost_dev
*dev
,
826 return vhost_user_set_u64(dev
, VHOST_USER_SET_PROTOCOL_FEATURES
, features
);
829 static int vhost_user_get_u64(struct vhost_dev
*dev
, int request
, uint64_t *u64
)
832 .hdr
.request
= request
,
833 .hdr
.flags
= VHOST_USER_VERSION
,
836 if (vhost_user_one_time_request(request
) && dev
->vq_index
!= 0) {
840 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
844 if (vhost_user_read(dev
, &msg
) < 0) {
848 if (msg
.hdr
.request
!= request
) {
849 error_report("Received unexpected msg type. Expected %d received %d",
850 request
, msg
.hdr
.request
);
854 if (msg
.hdr
.size
!= sizeof(msg
.payload
.u64
)) {
855 error_report("Received bad msg size.");
859 *u64
= msg
.payload
.u64
;
864 static int vhost_user_get_features(struct vhost_dev
*dev
, uint64_t *features
)
866 return vhost_user_get_u64(dev
, VHOST_USER_GET_FEATURES
, features
);
869 static int vhost_user_set_owner(struct vhost_dev
*dev
)
872 .hdr
.request
= VHOST_USER_SET_OWNER
,
873 .hdr
.flags
= VHOST_USER_VERSION
,
876 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
883 static int vhost_user_reset_device(struct vhost_dev
*dev
)
886 .hdr
.flags
= VHOST_USER_VERSION
,
889 msg
.hdr
.request
= virtio_has_feature(dev
->protocol_features
,
890 VHOST_USER_PROTOCOL_F_RESET_DEVICE
)
891 ? VHOST_USER_RESET_DEVICE
892 : VHOST_USER_RESET_OWNER
;
894 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
901 static int vhost_user_slave_handle_config_change(struct vhost_dev
*dev
)
905 if (!dev
->config_ops
) {
909 if (dev
->config_ops
->vhost_dev_config_notifier
) {
910 ret
= dev
->config_ops
->vhost_dev_config_notifier(dev
);
916 static int vhost_user_slave_handle_vring_host_notifier(struct vhost_dev
*dev
,
917 VhostUserVringArea
*area
,
920 int queue_idx
= area
->u64
& VHOST_USER_VRING_IDX_MASK
;
921 size_t page_size
= qemu_real_host_page_size
;
922 struct vhost_user
*u
= dev
->opaque
;
923 VhostUserState
*user
= u
->user
;
924 VirtIODevice
*vdev
= dev
->vdev
;
925 VhostUserHostNotifier
*n
;
929 if (!virtio_has_feature(dev
->protocol_features
,
930 VHOST_USER_PROTOCOL_F_HOST_NOTIFIER
) ||
931 vdev
== NULL
|| queue_idx
>= virtio_get_num_queues(vdev
)) {
935 n
= &user
->notifier
[queue_idx
];
938 virtio_queue_set_host_notifier_mr(vdev
, queue_idx
, &n
->mr
, false);
939 object_unparent(OBJECT(&n
->mr
));
940 munmap(n
->addr
, page_size
);
944 if (area
->u64
& VHOST_USER_VRING_NOFD_MASK
) {
949 if (area
->size
!= page_size
) {
953 addr
= mmap(NULL
, page_size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
,
955 if (addr
== MAP_FAILED
) {
959 name
= g_strdup_printf("vhost-user/host-notifier@%p mmaps[%d]",
961 memory_region_init_ram_device_ptr(&n
->mr
, OBJECT(vdev
), name
,
965 if (virtio_queue_set_host_notifier_mr(vdev
, queue_idx
, &n
->mr
, true)) {
966 munmap(addr
, page_size
);
976 static void slave_read(void *opaque
)
978 struct vhost_dev
*dev
= opaque
;
979 struct vhost_user
*u
= dev
->opaque
;
980 VhostUserHeader hdr
= { 0, };
981 VhostUserPayload payload
= { 0, };
985 int fd
[VHOST_USER_SLAVE_MAX_FDS
];
986 char control
[CMSG_SPACE(sizeof(fd
))];
987 struct cmsghdr
*cmsg
;
990 memset(&msgh
, 0, sizeof(msgh
));
993 msgh
.msg_control
= control
;
994 msgh
.msg_controllen
= sizeof(control
);
996 memset(fd
, -1, sizeof(fd
));
1000 iov
.iov_len
= VHOST_USER_HDR_SIZE
;
1003 size
= recvmsg(u
->slave_fd
, &msgh
, 0);
1004 } while (size
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
1006 if (size
!= VHOST_USER_HDR_SIZE
) {
1007 error_report("Failed to read from slave.");
1011 if (msgh
.msg_flags
& MSG_CTRUNC
) {
1012 error_report("Truncated message.");
1016 for (cmsg
= CMSG_FIRSTHDR(&msgh
); cmsg
!= NULL
;
1017 cmsg
= CMSG_NXTHDR(&msgh
, cmsg
)) {
1018 if (cmsg
->cmsg_level
== SOL_SOCKET
&&
1019 cmsg
->cmsg_type
== SCM_RIGHTS
) {
1020 fdsize
= cmsg
->cmsg_len
- CMSG_LEN(0);
1021 memcpy(fd
, CMSG_DATA(cmsg
), fdsize
);
1026 if (hdr
.size
> VHOST_USER_PAYLOAD_SIZE
) {
1027 error_report("Failed to read msg header."
1028 " Size %d exceeds the maximum %zu.", hdr
.size
,
1029 VHOST_USER_PAYLOAD_SIZE
);
1035 size
= read(u
->slave_fd
, &payload
, hdr
.size
);
1036 } while (size
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
1038 if (size
!= hdr
.size
) {
1039 error_report("Failed to read payload from slave.");
1043 switch (hdr
.request
) {
1044 case VHOST_USER_SLAVE_IOTLB_MSG
:
1045 ret
= vhost_backend_handle_iotlb_msg(dev
, &payload
.iotlb
);
1047 case VHOST_USER_SLAVE_CONFIG_CHANGE_MSG
:
1048 ret
= vhost_user_slave_handle_config_change(dev
);
1050 case VHOST_USER_SLAVE_VRING_HOST_NOTIFIER_MSG
:
1051 ret
= vhost_user_slave_handle_vring_host_notifier(dev
, &payload
.area
,
1055 error_report("Received unexpected msg type: %d.", hdr
.request
);
1059 /* Close the remaining file descriptors. */
1060 for (i
= 0; i
< fdsize
; i
++) {
1067 * REPLY_ACK feature handling. Other reply types has to be managed
1068 * directly in their request handlers.
1070 if (hdr
.flags
& VHOST_USER_NEED_REPLY_MASK
) {
1071 struct iovec iovec
[2];
1074 hdr
.flags
&= ~VHOST_USER_NEED_REPLY_MASK
;
1075 hdr
.flags
|= VHOST_USER_REPLY_MASK
;
1077 payload
.u64
= !!ret
;
1078 hdr
.size
= sizeof(payload
.u64
);
1080 iovec
[0].iov_base
= &hdr
;
1081 iovec
[0].iov_len
= VHOST_USER_HDR_SIZE
;
1082 iovec
[1].iov_base
= &payload
;
1083 iovec
[1].iov_len
= hdr
.size
;
1086 size
= writev(u
->slave_fd
, iovec
, ARRAY_SIZE(iovec
));
1087 } while (size
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
1089 if (size
!= VHOST_USER_HDR_SIZE
+ hdr
.size
) {
1090 error_report("Failed to send msg reply to slave.");
1098 qemu_set_fd_handler(u
->slave_fd
, NULL
, NULL
, NULL
);
1101 for (i
= 0; i
< fdsize
; i
++) {
1109 static int vhost_setup_slave_channel(struct vhost_dev
*dev
)
1111 VhostUserMsg msg
= {
1112 .hdr
.request
= VHOST_USER_SET_SLAVE_REQ_FD
,
1113 .hdr
.flags
= VHOST_USER_VERSION
,
1115 struct vhost_user
*u
= dev
->opaque
;
1117 bool reply_supported
= virtio_has_feature(dev
->protocol_features
,
1118 VHOST_USER_PROTOCOL_F_REPLY_ACK
);
1120 if (!virtio_has_feature(dev
->protocol_features
,
1121 VHOST_USER_PROTOCOL_F_SLAVE_REQ
)) {
1125 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, sv
) == -1) {
1126 error_report("socketpair() failed");
1130 u
->slave_fd
= sv
[0];
1131 qemu_set_fd_handler(u
->slave_fd
, slave_read
, NULL
, dev
);
1133 if (reply_supported
) {
1134 msg
.hdr
.flags
|= VHOST_USER_NEED_REPLY_MASK
;
1137 ret
= vhost_user_write(dev
, &msg
, &sv
[1], 1);
1142 if (reply_supported
) {
1143 ret
= process_message_reply(dev
, &msg
);
1149 qemu_set_fd_handler(u
->slave_fd
, NULL
, NULL
, NULL
);
1159 * Called back from the postcopy fault thread when a fault is received on our
1161 * TODO: This is Linux specific
1163 static int vhost_user_postcopy_fault_handler(struct PostCopyFD
*pcfd
,
1166 struct vhost_dev
*dev
= pcfd
->data
;
1167 struct vhost_user
*u
= dev
->opaque
;
1168 struct uffd_msg
*msg
= ufd
;
1169 uint64_t faultaddr
= msg
->arg
.pagefault
.address
;
1170 RAMBlock
*rb
= NULL
;
1174 trace_vhost_user_postcopy_fault_handler(pcfd
->idstr
, faultaddr
,
1175 dev
->mem
->nregions
);
1176 for (i
= 0; i
< MIN(dev
->mem
->nregions
, u
->region_rb_len
); i
++) {
1177 trace_vhost_user_postcopy_fault_handler_loop(i
,
1178 u
->postcopy_client_bases
[i
], dev
->mem
->regions
[i
].memory_size
);
1179 if (faultaddr
>= u
->postcopy_client_bases
[i
]) {
1180 /* Ofset of the fault address in the vhost region */
1181 uint64_t region_offset
= faultaddr
- u
->postcopy_client_bases
[i
];
1182 if (region_offset
< dev
->mem
->regions
[i
].memory_size
) {
1183 rb_offset
= region_offset
+ u
->region_rb_offset
[i
];
1184 trace_vhost_user_postcopy_fault_handler_found(i
,
1185 region_offset
, rb_offset
);
1186 rb
= u
->region_rb
[i
];
1187 return postcopy_request_shared_page(pcfd
, rb
, faultaddr
,
1192 error_report("%s: Failed to find region for fault %" PRIx64
,
1193 __func__
, faultaddr
);
1197 static int vhost_user_postcopy_waker(struct PostCopyFD
*pcfd
, RAMBlock
*rb
,
1200 struct vhost_dev
*dev
= pcfd
->data
;
1201 struct vhost_user
*u
= dev
->opaque
;
1204 trace_vhost_user_postcopy_waker(qemu_ram_get_idstr(rb
), offset
);
1209 /* Translate the offset into an address in the clients address space */
1210 for (i
= 0; i
< MIN(dev
->mem
->nregions
, u
->region_rb_len
); i
++) {
1211 if (u
->region_rb
[i
] == rb
&&
1212 offset
>= u
->region_rb_offset
[i
] &&
1213 offset
< (u
->region_rb_offset
[i
] +
1214 dev
->mem
->regions
[i
].memory_size
)) {
1215 uint64_t client_addr
= (offset
- u
->region_rb_offset
[i
]) +
1216 u
->postcopy_client_bases
[i
];
1217 trace_vhost_user_postcopy_waker_found(client_addr
);
1218 return postcopy_wake_shared(pcfd
, client_addr
, rb
);
1222 trace_vhost_user_postcopy_waker_nomatch(qemu_ram_get_idstr(rb
), offset
);
1228 * Called at the start of an inbound postcopy on reception of the
1231 static int vhost_user_postcopy_advise(struct vhost_dev
*dev
, Error
**errp
)
1234 struct vhost_user
*u
= dev
->opaque
;
1235 CharBackend
*chr
= u
->user
->chr
;
1237 VhostUserMsg msg
= {
1238 .hdr
.request
= VHOST_USER_POSTCOPY_ADVISE
,
1239 .hdr
.flags
= VHOST_USER_VERSION
,
1242 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
1243 error_setg(errp
, "Failed to send postcopy_advise to vhost");
1247 if (vhost_user_read(dev
, &msg
) < 0) {
1248 error_setg(errp
, "Failed to get postcopy_advise reply from vhost");
1252 if (msg
.hdr
.request
!= VHOST_USER_POSTCOPY_ADVISE
) {
1253 error_setg(errp
, "Unexpected msg type. Expected %d received %d",
1254 VHOST_USER_POSTCOPY_ADVISE
, msg
.hdr
.request
);
1259 error_setg(errp
, "Received bad msg size.");
1262 ufd
= qemu_chr_fe_get_msgfd(chr
);
1264 error_setg(errp
, "%s: Failed to get ufd", __func__
);
1267 qemu_set_nonblock(ufd
);
1269 /* register ufd with userfault thread */
1270 u
->postcopy_fd
.fd
= ufd
;
1271 u
->postcopy_fd
.data
= dev
;
1272 u
->postcopy_fd
.handler
= vhost_user_postcopy_fault_handler
;
1273 u
->postcopy_fd
.waker
= vhost_user_postcopy_waker
;
1274 u
->postcopy_fd
.idstr
= "vhost-user"; /* Need to find unique name */
1275 postcopy_register_shared_ufd(&u
->postcopy_fd
);
1278 error_setg(errp
, "Postcopy not supported on non-Linux systems");
1284 * Called at the switch to postcopy on reception of the 'listen' command.
1286 static int vhost_user_postcopy_listen(struct vhost_dev
*dev
, Error
**errp
)
1288 struct vhost_user
*u
= dev
->opaque
;
1290 VhostUserMsg msg
= {
1291 .hdr
.request
= VHOST_USER_POSTCOPY_LISTEN
,
1292 .hdr
.flags
= VHOST_USER_VERSION
| VHOST_USER_NEED_REPLY_MASK
,
1294 u
->postcopy_listen
= true;
1295 trace_vhost_user_postcopy_listen();
1296 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
1297 error_setg(errp
, "Failed to send postcopy_listen to vhost");
1301 ret
= process_message_reply(dev
, &msg
);
1303 error_setg(errp
, "Failed to receive reply to postcopy_listen");
1311 * Called at the end of postcopy
1313 static int vhost_user_postcopy_end(struct vhost_dev
*dev
, Error
**errp
)
1315 VhostUserMsg msg
= {
1316 .hdr
.request
= VHOST_USER_POSTCOPY_END
,
1317 .hdr
.flags
= VHOST_USER_VERSION
| VHOST_USER_NEED_REPLY_MASK
,
1320 struct vhost_user
*u
= dev
->opaque
;
1322 trace_vhost_user_postcopy_end_entry();
1323 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
1324 error_setg(errp
, "Failed to send postcopy_end to vhost");
1328 ret
= process_message_reply(dev
, &msg
);
1330 error_setg(errp
, "Failed to receive reply to postcopy_end");
1333 postcopy_unregister_shared_ufd(&u
->postcopy_fd
);
1334 close(u
->postcopy_fd
.fd
);
1335 u
->postcopy_fd
.handler
= NULL
;
1337 trace_vhost_user_postcopy_end_exit();
1342 static int vhost_user_postcopy_notifier(NotifierWithReturn
*notifier
,
1345 struct PostcopyNotifyData
*pnd
= opaque
;
1346 struct vhost_user
*u
= container_of(notifier
, struct vhost_user
,
1348 struct vhost_dev
*dev
= u
->dev
;
1350 switch (pnd
->reason
) {
1351 case POSTCOPY_NOTIFY_PROBE
:
1352 if (!virtio_has_feature(dev
->protocol_features
,
1353 VHOST_USER_PROTOCOL_F_PAGEFAULT
)) {
1354 /* TODO: Get the device name into this error somehow */
1355 error_setg(pnd
->errp
,
1356 "vhost-user backend not capable of postcopy");
1361 case POSTCOPY_NOTIFY_INBOUND_ADVISE
:
1362 return vhost_user_postcopy_advise(dev
, pnd
->errp
);
1364 case POSTCOPY_NOTIFY_INBOUND_LISTEN
:
1365 return vhost_user_postcopy_listen(dev
, pnd
->errp
);
1367 case POSTCOPY_NOTIFY_INBOUND_END
:
1368 return vhost_user_postcopy_end(dev
, pnd
->errp
);
1371 /* We ignore notifications we don't know */
1378 static int vhost_user_backend_init(struct vhost_dev
*dev
, void *opaque
)
1380 uint64_t features
, protocol_features
;
1381 struct vhost_user
*u
;
1384 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
1386 u
= g_new0(struct vhost_user
, 1);
1392 err
= vhost_user_get_features(dev
, &features
);
1397 if (virtio_has_feature(features
, VHOST_USER_F_PROTOCOL_FEATURES
)) {
1398 dev
->backend_features
|= 1ULL << VHOST_USER_F_PROTOCOL_FEATURES
;
1400 err
= vhost_user_get_u64(dev
, VHOST_USER_GET_PROTOCOL_FEATURES
,
1401 &protocol_features
);
1406 dev
->protocol_features
=
1407 protocol_features
& VHOST_USER_PROTOCOL_FEATURE_MASK
;
1409 if (!dev
->config_ops
|| !dev
->config_ops
->vhost_dev_config_notifier
) {
1410 /* Don't acknowledge CONFIG feature if device doesn't support it */
1411 dev
->protocol_features
&= ~(1ULL << VHOST_USER_PROTOCOL_F_CONFIG
);
1412 } else if (!(protocol_features
&
1413 (1ULL << VHOST_USER_PROTOCOL_F_CONFIG
))) {
1414 error_report("Device expects VHOST_USER_PROTOCOL_F_CONFIG "
1415 "but backend does not support it.");
1419 err
= vhost_user_set_protocol_features(dev
, dev
->protocol_features
);
1424 /* query the max queues we support if backend supports Multiple Queue */
1425 if (dev
->protocol_features
& (1ULL << VHOST_USER_PROTOCOL_F_MQ
)) {
1426 err
= vhost_user_get_u64(dev
, VHOST_USER_GET_QUEUE_NUM
,
1433 if (virtio_has_feature(features
, VIRTIO_F_IOMMU_PLATFORM
) &&
1434 !(virtio_has_feature(dev
->protocol_features
,
1435 VHOST_USER_PROTOCOL_F_SLAVE_REQ
) &&
1436 virtio_has_feature(dev
->protocol_features
,
1437 VHOST_USER_PROTOCOL_F_REPLY_ACK
))) {
1438 error_report("IOMMU support requires reply-ack and "
1439 "slave-req protocol features.");
1444 if (dev
->migration_blocker
== NULL
&&
1445 !virtio_has_feature(dev
->protocol_features
,
1446 VHOST_USER_PROTOCOL_F_LOG_SHMFD
)) {
1447 error_setg(&dev
->migration_blocker
,
1448 "Migration disabled: vhost-user backend lacks "
1449 "VHOST_USER_PROTOCOL_F_LOG_SHMFD feature.");
1452 if (dev
->vq_index
== 0) {
1453 err
= vhost_setup_slave_channel(dev
);
1459 u
->postcopy_notifier
.notify
= vhost_user_postcopy_notifier
;
1460 postcopy_add_notifier(&u
->postcopy_notifier
);
1465 static int vhost_user_backend_cleanup(struct vhost_dev
*dev
)
1467 struct vhost_user
*u
;
1469 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
1472 if (u
->postcopy_notifier
.notify
) {
1473 postcopy_remove_notifier(&u
->postcopy_notifier
);
1474 u
->postcopy_notifier
.notify
= NULL
;
1476 u
->postcopy_listen
= false;
1477 if (u
->postcopy_fd
.handler
) {
1478 postcopy_unregister_shared_ufd(&u
->postcopy_fd
);
1479 close(u
->postcopy_fd
.fd
);
1480 u
->postcopy_fd
.handler
= NULL
;
1482 if (u
->slave_fd
>= 0) {
1483 qemu_set_fd_handler(u
->slave_fd
, NULL
, NULL
, NULL
);
1487 g_free(u
->region_rb
);
1488 u
->region_rb
= NULL
;
1489 g_free(u
->region_rb_offset
);
1490 u
->region_rb_offset
= NULL
;
1491 u
->region_rb_len
= 0;
1498 static int vhost_user_get_vq_index(struct vhost_dev
*dev
, int idx
)
1500 assert(idx
>= dev
->vq_index
&& idx
< dev
->vq_index
+ dev
->nvqs
);
1505 static int vhost_user_memslots_limit(struct vhost_dev
*dev
)
1507 return VHOST_MEMORY_MAX_NREGIONS
;
1510 static bool vhost_user_requires_shm_log(struct vhost_dev
*dev
)
1512 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
1514 return virtio_has_feature(dev
->protocol_features
,
1515 VHOST_USER_PROTOCOL_F_LOG_SHMFD
);
1518 static int vhost_user_migration_done(struct vhost_dev
*dev
, char* mac_addr
)
1520 VhostUserMsg msg
= { };
1522 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
1524 /* If guest supports GUEST_ANNOUNCE do nothing */
1525 if (virtio_has_feature(dev
->acked_features
, VIRTIO_NET_F_GUEST_ANNOUNCE
)) {
1529 /* if backend supports VHOST_USER_PROTOCOL_F_RARP ask it to send the RARP */
1530 if (virtio_has_feature(dev
->protocol_features
,
1531 VHOST_USER_PROTOCOL_F_RARP
)) {
1532 msg
.hdr
.request
= VHOST_USER_SEND_RARP
;
1533 msg
.hdr
.flags
= VHOST_USER_VERSION
;
1534 memcpy((char *)&msg
.payload
.u64
, mac_addr
, 6);
1535 msg
.hdr
.size
= sizeof(msg
.payload
.u64
);
1537 return vhost_user_write(dev
, &msg
, NULL
, 0);
1542 static bool vhost_user_can_merge(struct vhost_dev
*dev
,
1543 uint64_t start1
, uint64_t size1
,
1544 uint64_t start2
, uint64_t size2
)
1550 mr
= memory_region_from_host((void *)(uintptr_t)start1
, &offset
);
1551 mfd
= memory_region_get_fd(mr
);
1553 mr
= memory_region_from_host((void *)(uintptr_t)start2
, &offset
);
1554 rfd
= memory_region_get_fd(mr
);
1559 static int vhost_user_net_set_mtu(struct vhost_dev
*dev
, uint16_t mtu
)
1562 bool reply_supported
= virtio_has_feature(dev
->protocol_features
,
1563 VHOST_USER_PROTOCOL_F_REPLY_ACK
);
1565 if (!(dev
->protocol_features
& (1ULL << VHOST_USER_PROTOCOL_F_NET_MTU
))) {
1569 msg
.hdr
.request
= VHOST_USER_NET_SET_MTU
;
1570 msg
.payload
.u64
= mtu
;
1571 msg
.hdr
.size
= sizeof(msg
.payload
.u64
);
1572 msg
.hdr
.flags
= VHOST_USER_VERSION
;
1573 if (reply_supported
) {
1574 msg
.hdr
.flags
|= VHOST_USER_NEED_REPLY_MASK
;
1577 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
1581 /* If reply_ack supported, slave has to ack specified MTU is valid */
1582 if (reply_supported
) {
1583 return process_message_reply(dev
, &msg
);
1589 static int vhost_user_send_device_iotlb_msg(struct vhost_dev
*dev
,
1590 struct vhost_iotlb_msg
*imsg
)
1592 VhostUserMsg msg
= {
1593 .hdr
.request
= VHOST_USER_IOTLB_MSG
,
1594 .hdr
.size
= sizeof(msg
.payload
.iotlb
),
1595 .hdr
.flags
= VHOST_USER_VERSION
| VHOST_USER_NEED_REPLY_MASK
,
1596 .payload
.iotlb
= *imsg
,
1599 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
1603 return process_message_reply(dev
, &msg
);
1607 static void vhost_user_set_iotlb_callback(struct vhost_dev
*dev
, int enabled
)
1609 /* No-op as the receive channel is not dedicated to IOTLB messages. */
1612 static int vhost_user_get_config(struct vhost_dev
*dev
, uint8_t *config
,
1613 uint32_t config_len
)
1615 VhostUserMsg msg
= {
1616 .hdr
.request
= VHOST_USER_GET_CONFIG
,
1617 .hdr
.flags
= VHOST_USER_VERSION
,
1618 .hdr
.size
= VHOST_USER_CONFIG_HDR_SIZE
+ config_len
,
1621 if (!virtio_has_feature(dev
->protocol_features
,
1622 VHOST_USER_PROTOCOL_F_CONFIG
)) {
1626 if (config_len
> VHOST_USER_MAX_CONFIG_SIZE
) {
1630 msg
.payload
.config
.offset
= 0;
1631 msg
.payload
.config
.size
= config_len
;
1632 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
1636 if (vhost_user_read(dev
, &msg
) < 0) {
1640 if (msg
.hdr
.request
!= VHOST_USER_GET_CONFIG
) {
1641 error_report("Received unexpected msg type. Expected %d received %d",
1642 VHOST_USER_GET_CONFIG
, msg
.hdr
.request
);
1646 if (msg
.hdr
.size
!= VHOST_USER_CONFIG_HDR_SIZE
+ config_len
) {
1647 error_report("Received bad msg size.");
1651 memcpy(config
, msg
.payload
.config
.region
, config_len
);
1656 static int vhost_user_set_config(struct vhost_dev
*dev
, const uint8_t *data
,
1657 uint32_t offset
, uint32_t size
, uint32_t flags
)
1660 bool reply_supported
= virtio_has_feature(dev
->protocol_features
,
1661 VHOST_USER_PROTOCOL_F_REPLY_ACK
);
1663 VhostUserMsg msg
= {
1664 .hdr
.request
= VHOST_USER_SET_CONFIG
,
1665 .hdr
.flags
= VHOST_USER_VERSION
,
1666 .hdr
.size
= VHOST_USER_CONFIG_HDR_SIZE
+ size
,
1669 if (!virtio_has_feature(dev
->protocol_features
,
1670 VHOST_USER_PROTOCOL_F_CONFIG
)) {
1674 if (reply_supported
) {
1675 msg
.hdr
.flags
|= VHOST_USER_NEED_REPLY_MASK
;
1678 if (size
> VHOST_USER_MAX_CONFIG_SIZE
) {
1682 msg
.payload
.config
.offset
= offset
,
1683 msg
.payload
.config
.size
= size
,
1684 msg
.payload
.config
.flags
= flags
,
1685 p
= msg
.payload
.config
.region
;
1686 memcpy(p
, data
, size
);
1688 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
1692 if (reply_supported
) {
1693 return process_message_reply(dev
, &msg
);
1699 static int vhost_user_crypto_create_session(struct vhost_dev
*dev
,
1701 uint64_t *session_id
)
1703 bool crypto_session
= virtio_has_feature(dev
->protocol_features
,
1704 VHOST_USER_PROTOCOL_F_CRYPTO_SESSION
);
1705 CryptoDevBackendSymSessionInfo
*sess_info
= session_info
;
1706 VhostUserMsg msg
= {
1707 .hdr
.request
= VHOST_USER_CREATE_CRYPTO_SESSION
,
1708 .hdr
.flags
= VHOST_USER_VERSION
,
1709 .hdr
.size
= sizeof(msg
.payload
.session
),
1712 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
1714 if (!crypto_session
) {
1715 error_report("vhost-user trying to send unhandled ioctl");
1719 memcpy(&msg
.payload
.session
.session_setup_data
, sess_info
,
1720 sizeof(CryptoDevBackendSymSessionInfo
));
1721 if (sess_info
->key_len
) {
1722 memcpy(&msg
.payload
.session
.key
, sess_info
->cipher_key
,
1723 sess_info
->key_len
);
1725 if (sess_info
->auth_key_len
> 0) {
1726 memcpy(&msg
.payload
.session
.auth_key
, sess_info
->auth_key
,
1727 sess_info
->auth_key_len
);
1729 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
1730 error_report("vhost_user_write() return -1, create session failed");
1734 if (vhost_user_read(dev
, &msg
) < 0) {
1735 error_report("vhost_user_read() return -1, create session failed");
1739 if (msg
.hdr
.request
!= VHOST_USER_CREATE_CRYPTO_SESSION
) {
1740 error_report("Received unexpected msg type. Expected %d received %d",
1741 VHOST_USER_CREATE_CRYPTO_SESSION
, msg
.hdr
.request
);
1745 if (msg
.hdr
.size
!= sizeof(msg
.payload
.session
)) {
1746 error_report("Received bad msg size.");
1750 if (msg
.payload
.session
.session_id
< 0) {
1751 error_report("Bad session id: %" PRId64
"",
1752 msg
.payload
.session
.session_id
);
1755 *session_id
= msg
.payload
.session
.session_id
;
1761 vhost_user_crypto_close_session(struct vhost_dev
*dev
, uint64_t session_id
)
1763 bool crypto_session
= virtio_has_feature(dev
->protocol_features
,
1764 VHOST_USER_PROTOCOL_F_CRYPTO_SESSION
);
1765 VhostUserMsg msg
= {
1766 .hdr
.request
= VHOST_USER_CLOSE_CRYPTO_SESSION
,
1767 .hdr
.flags
= VHOST_USER_VERSION
,
1768 .hdr
.size
= sizeof(msg
.payload
.u64
),
1770 msg
.payload
.u64
= session_id
;
1772 if (!crypto_session
) {
1773 error_report("vhost-user trying to send unhandled ioctl");
1777 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
1778 error_report("vhost_user_write() return -1, close session failed");
1785 static bool vhost_user_mem_section_filter(struct vhost_dev
*dev
,
1786 MemoryRegionSection
*section
)
1790 result
= memory_region_get_fd(section
->mr
) >= 0;
1795 static int vhost_user_get_inflight_fd(struct vhost_dev
*dev
,
1796 uint16_t queue_size
,
1797 struct vhost_inflight
*inflight
)
1801 struct vhost_user
*u
= dev
->opaque
;
1802 CharBackend
*chr
= u
->user
->chr
;
1803 VhostUserMsg msg
= {
1804 .hdr
.request
= VHOST_USER_GET_INFLIGHT_FD
,
1805 .hdr
.flags
= VHOST_USER_VERSION
,
1806 .payload
.inflight
.num_queues
= dev
->nvqs
,
1807 .payload
.inflight
.queue_size
= queue_size
,
1808 .hdr
.size
= sizeof(msg
.payload
.inflight
),
1811 if (!virtio_has_feature(dev
->protocol_features
,
1812 VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD
)) {
1816 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
1820 if (vhost_user_read(dev
, &msg
) < 0) {
1824 if (msg
.hdr
.request
!= VHOST_USER_GET_INFLIGHT_FD
) {
1825 error_report("Received unexpected msg type. "
1826 "Expected %d received %d",
1827 VHOST_USER_GET_INFLIGHT_FD
, msg
.hdr
.request
);
1831 if (msg
.hdr
.size
!= sizeof(msg
.payload
.inflight
)) {
1832 error_report("Received bad msg size.");
1836 if (!msg
.payload
.inflight
.mmap_size
) {
1840 fd
= qemu_chr_fe_get_msgfd(chr
);
1842 error_report("Failed to get mem fd");
1846 addr
= mmap(0, msg
.payload
.inflight
.mmap_size
, PROT_READ
| PROT_WRITE
,
1847 MAP_SHARED
, fd
, msg
.payload
.inflight
.mmap_offset
);
1849 if (addr
== MAP_FAILED
) {
1850 error_report("Failed to mmap mem fd");
1855 inflight
->addr
= addr
;
1857 inflight
->size
= msg
.payload
.inflight
.mmap_size
;
1858 inflight
->offset
= msg
.payload
.inflight
.mmap_offset
;
1859 inflight
->queue_size
= queue_size
;
1864 static int vhost_user_set_inflight_fd(struct vhost_dev
*dev
,
1865 struct vhost_inflight
*inflight
)
1867 VhostUserMsg msg
= {
1868 .hdr
.request
= VHOST_USER_SET_INFLIGHT_FD
,
1869 .hdr
.flags
= VHOST_USER_VERSION
,
1870 .payload
.inflight
.mmap_size
= inflight
->size
,
1871 .payload
.inflight
.mmap_offset
= inflight
->offset
,
1872 .payload
.inflight
.num_queues
= dev
->nvqs
,
1873 .payload
.inflight
.queue_size
= inflight
->queue_size
,
1874 .hdr
.size
= sizeof(msg
.payload
.inflight
),
1877 if (!virtio_has_feature(dev
->protocol_features
,
1878 VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD
)) {
1882 if (vhost_user_write(dev
, &msg
, &inflight
->fd
, 1) < 0) {
1889 bool vhost_user_init(VhostUserState
*user
, CharBackend
*chr
, Error
**errp
)
1892 error_setg(errp
, "Cannot initialize vhost-user state");
1899 void vhost_user_cleanup(VhostUserState
*user
)
1907 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
1908 if (user
->notifier
[i
].addr
) {
1909 object_unparent(OBJECT(&user
->notifier
[i
].mr
));
1910 munmap(user
->notifier
[i
].addr
, qemu_real_host_page_size
);
1911 user
->notifier
[i
].addr
= NULL
;
1917 const VhostOps user_ops
= {
1918 .backend_type
= VHOST_BACKEND_TYPE_USER
,
1919 .vhost_backend_init
= vhost_user_backend_init
,
1920 .vhost_backend_cleanup
= vhost_user_backend_cleanup
,
1921 .vhost_backend_memslots_limit
= vhost_user_memslots_limit
,
1922 .vhost_set_log_base
= vhost_user_set_log_base
,
1923 .vhost_set_mem_table
= vhost_user_set_mem_table
,
1924 .vhost_set_vring_addr
= vhost_user_set_vring_addr
,
1925 .vhost_set_vring_endian
= vhost_user_set_vring_endian
,
1926 .vhost_set_vring_num
= vhost_user_set_vring_num
,
1927 .vhost_set_vring_base
= vhost_user_set_vring_base
,
1928 .vhost_get_vring_base
= vhost_user_get_vring_base
,
1929 .vhost_set_vring_kick
= vhost_user_set_vring_kick
,
1930 .vhost_set_vring_call
= vhost_user_set_vring_call
,
1931 .vhost_set_features
= vhost_user_set_features
,
1932 .vhost_get_features
= vhost_user_get_features
,
1933 .vhost_set_owner
= vhost_user_set_owner
,
1934 .vhost_reset_device
= vhost_user_reset_device
,
1935 .vhost_get_vq_index
= vhost_user_get_vq_index
,
1936 .vhost_set_vring_enable
= vhost_user_set_vring_enable
,
1937 .vhost_requires_shm_log
= vhost_user_requires_shm_log
,
1938 .vhost_migration_done
= vhost_user_migration_done
,
1939 .vhost_backend_can_merge
= vhost_user_can_merge
,
1940 .vhost_net_set_mtu
= vhost_user_net_set_mtu
,
1941 .vhost_set_iotlb_callback
= vhost_user_set_iotlb_callback
,
1942 .vhost_send_device_iotlb_msg
= vhost_user_send_device_iotlb_msg
,
1943 .vhost_get_config
= vhost_user_get_config
,
1944 .vhost_set_config
= vhost_user_set_config
,
1945 .vhost_crypto_create_session
= vhost_user_crypto_create_session
,
1946 .vhost_crypto_close_session
= vhost_user_crypto_close_session
,
1947 .vhost_backend_mem_section_filter
= vhost_user_mem_section_filter
,
1948 .vhost_get_inflight_fd
= vhost_user_get_inflight_fd
,
1949 .vhost_set_inflight_fd
= vhost_user_set_inflight_fd
,