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