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_MAX
64 #define VHOST_USER_PROTOCOL_FEATURE_MASK ((1 << VHOST_USER_PROTOCOL_F_MAX) - 1)
66 typedef enum VhostUserRequest
{
68 VHOST_USER_GET_FEATURES
= 1,
69 VHOST_USER_SET_FEATURES
= 2,
70 VHOST_USER_SET_OWNER
= 3,
71 VHOST_USER_RESET_OWNER
= 4,
72 VHOST_USER_SET_MEM_TABLE
= 5,
73 VHOST_USER_SET_LOG_BASE
= 6,
74 VHOST_USER_SET_LOG_FD
= 7,
75 VHOST_USER_SET_VRING_NUM
= 8,
76 VHOST_USER_SET_VRING_ADDR
= 9,
77 VHOST_USER_SET_VRING_BASE
= 10,
78 VHOST_USER_GET_VRING_BASE
= 11,
79 VHOST_USER_SET_VRING_KICK
= 12,
80 VHOST_USER_SET_VRING_CALL
= 13,
81 VHOST_USER_SET_VRING_ERR
= 14,
82 VHOST_USER_GET_PROTOCOL_FEATURES
= 15,
83 VHOST_USER_SET_PROTOCOL_FEATURES
= 16,
84 VHOST_USER_GET_QUEUE_NUM
= 17,
85 VHOST_USER_SET_VRING_ENABLE
= 18,
86 VHOST_USER_SEND_RARP
= 19,
87 VHOST_USER_NET_SET_MTU
= 20,
88 VHOST_USER_SET_SLAVE_REQ_FD
= 21,
89 VHOST_USER_IOTLB_MSG
= 22,
90 VHOST_USER_SET_VRING_ENDIAN
= 23,
91 VHOST_USER_GET_CONFIG
= 24,
92 VHOST_USER_SET_CONFIG
= 25,
93 VHOST_USER_CREATE_CRYPTO_SESSION
= 26,
94 VHOST_USER_CLOSE_CRYPTO_SESSION
= 27,
95 VHOST_USER_POSTCOPY_ADVISE
= 28,
96 VHOST_USER_POSTCOPY_LISTEN
= 29,
97 VHOST_USER_POSTCOPY_END
= 30,
98 VHOST_USER_GET_INFLIGHT_FD
= 31,
99 VHOST_USER_SET_INFLIGHT_FD
= 32,
100 VHOST_USER_GPU_SET_SOCKET
= 33,
104 typedef enum VhostUserSlaveRequest
{
105 VHOST_USER_SLAVE_NONE
= 0,
106 VHOST_USER_SLAVE_IOTLB_MSG
= 1,
107 VHOST_USER_SLAVE_CONFIG_CHANGE_MSG
= 2,
108 VHOST_USER_SLAVE_VRING_HOST_NOTIFIER_MSG
= 3,
110 } VhostUserSlaveRequest
;
112 typedef struct VhostUserMemoryRegion
{
113 uint64_t guest_phys_addr
;
114 uint64_t memory_size
;
115 uint64_t userspace_addr
;
116 uint64_t mmap_offset
;
117 } VhostUserMemoryRegion
;
119 typedef struct VhostUserMemory
{
122 VhostUserMemoryRegion regions
[VHOST_MEMORY_MAX_NREGIONS
];
125 typedef struct VhostUserLog
{
127 uint64_t mmap_offset
;
130 typedef struct VhostUserConfig
{
134 uint8_t region
[VHOST_USER_MAX_CONFIG_SIZE
];
137 #define VHOST_CRYPTO_SYM_HMAC_MAX_KEY_LEN 512
138 #define VHOST_CRYPTO_SYM_CIPHER_MAX_KEY_LEN 64
140 typedef struct VhostUserCryptoSession
{
141 /* session id for success, -1 on errors */
143 CryptoDevBackendSymSessionInfo session_setup_data
;
144 uint8_t key
[VHOST_CRYPTO_SYM_CIPHER_MAX_KEY_LEN
];
145 uint8_t auth_key
[VHOST_CRYPTO_SYM_HMAC_MAX_KEY_LEN
];
146 } VhostUserCryptoSession
;
148 static VhostUserConfig c
__attribute__ ((unused
));
149 #define VHOST_USER_CONFIG_HDR_SIZE (sizeof(c.offset) \
153 typedef struct VhostUserVringArea
{
157 } VhostUserVringArea
;
159 typedef struct VhostUserInflight
{
161 uint64_t mmap_offset
;
167 VhostUserRequest request
;
169 #define VHOST_USER_VERSION_MASK (0x3)
170 #define VHOST_USER_REPLY_MASK (0x1<<2)
171 #define VHOST_USER_NEED_REPLY_MASK (0x1 << 3)
173 uint32_t size
; /* the following payload size */
174 } QEMU_PACKED VhostUserHeader
;
177 #define VHOST_USER_VRING_IDX_MASK (0xff)
178 #define VHOST_USER_VRING_NOFD_MASK (0x1<<8)
180 struct vhost_vring_state state
;
181 struct vhost_vring_addr addr
;
182 VhostUserMemory memory
;
184 struct vhost_iotlb_msg iotlb
;
185 VhostUserConfig config
;
186 VhostUserCryptoSession session
;
187 VhostUserVringArea area
;
188 VhostUserInflight inflight
;
191 typedef struct VhostUserMsg
{
193 VhostUserPayload payload
;
194 } QEMU_PACKED VhostUserMsg
;
196 static VhostUserMsg m
__attribute__ ((unused
));
197 #define VHOST_USER_HDR_SIZE (sizeof(VhostUserHeader))
199 #define VHOST_USER_PAYLOAD_SIZE (sizeof(VhostUserPayload))
201 /* The version of the protocol we support */
202 #define VHOST_USER_VERSION (0x1)
205 struct vhost_dev
*dev
;
206 /* Shared between vhost devs of the same virtio device */
207 VhostUserState
*user
;
209 NotifierWithReturn postcopy_notifier
;
210 struct PostCopyFD postcopy_fd
;
211 uint64_t postcopy_client_bases
[VHOST_MEMORY_MAX_NREGIONS
];
212 /* Length of the region_rb and region_rb_offset arrays */
213 size_t region_rb_len
;
214 /* RAMBlock associated with a given region */
215 RAMBlock
**region_rb
;
216 /* The offset from the start of the RAMBlock to the start of the
219 ram_addr_t
*region_rb_offset
;
221 /* True once we've entered postcopy_listen */
222 bool postcopy_listen
;
225 static bool ioeventfd_enabled(void)
227 return !kvm_enabled() || kvm_eventfds_enabled();
230 static int vhost_user_read_header(struct vhost_dev
*dev
, VhostUserMsg
*msg
)
232 struct vhost_user
*u
= dev
->opaque
;
233 CharBackend
*chr
= u
->user
->chr
;
234 uint8_t *p
= (uint8_t *) msg
;
235 int r
, size
= VHOST_USER_HDR_SIZE
;
237 r
= qemu_chr_fe_read_all(chr
, p
, size
);
239 error_report("Failed to read msg header. Read %d instead of %d."
240 " Original request %d.", r
, size
, msg
->hdr
.request
);
244 /* validate received flags */
245 if (msg
->hdr
.flags
!= (VHOST_USER_REPLY_MASK
| VHOST_USER_VERSION
)) {
246 error_report("Failed to read msg header."
247 " Flags 0x%x instead of 0x%x.", msg
->hdr
.flags
,
248 VHOST_USER_REPLY_MASK
| VHOST_USER_VERSION
);
255 static int vhost_user_read(struct vhost_dev
*dev
, VhostUserMsg
*msg
)
257 struct vhost_user
*u
= dev
->opaque
;
258 CharBackend
*chr
= u
->user
->chr
;
259 uint8_t *p
= (uint8_t *) msg
;
262 if (vhost_user_read_header(dev
, msg
) < 0) {
266 /* validate message size is sane */
267 if (msg
->hdr
.size
> VHOST_USER_PAYLOAD_SIZE
) {
268 error_report("Failed to read msg header."
269 " Size %d exceeds the maximum %zu.", msg
->hdr
.size
,
270 VHOST_USER_PAYLOAD_SIZE
);
275 p
+= VHOST_USER_HDR_SIZE
;
276 size
= msg
->hdr
.size
;
277 r
= qemu_chr_fe_read_all(chr
, p
, size
);
279 error_report("Failed to read msg payload."
280 " Read %d instead of %d.", r
, msg
->hdr
.size
);
288 static int process_message_reply(struct vhost_dev
*dev
,
289 const VhostUserMsg
*msg
)
291 VhostUserMsg msg_reply
;
293 if ((msg
->hdr
.flags
& VHOST_USER_NEED_REPLY_MASK
) == 0) {
297 if (vhost_user_read(dev
, &msg_reply
) < 0) {
301 if (msg_reply
.hdr
.request
!= msg
->hdr
.request
) {
302 error_report("Received unexpected msg type."
303 "Expected %d received %d",
304 msg
->hdr
.request
, msg_reply
.hdr
.request
);
308 return msg_reply
.payload
.u64
? -1 : 0;
311 static bool vhost_user_one_time_request(VhostUserRequest request
)
314 case VHOST_USER_SET_OWNER
:
315 case VHOST_USER_RESET_OWNER
:
316 case VHOST_USER_SET_MEM_TABLE
:
317 case VHOST_USER_GET_QUEUE_NUM
:
318 case VHOST_USER_NET_SET_MTU
:
325 /* most non-init callers ignore the error */
326 static int vhost_user_write(struct vhost_dev
*dev
, VhostUserMsg
*msg
,
327 int *fds
, int fd_num
)
329 struct vhost_user
*u
= dev
->opaque
;
330 CharBackend
*chr
= u
->user
->chr
;
331 int ret
, size
= VHOST_USER_HDR_SIZE
+ msg
->hdr
.size
;
334 * For non-vring specific requests, like VHOST_USER_SET_MEM_TABLE,
335 * we just need send it once in the first time. For later such
336 * request, we just ignore it.
338 if (vhost_user_one_time_request(msg
->hdr
.request
) && dev
->vq_index
!= 0) {
339 msg
->hdr
.flags
&= ~VHOST_USER_NEED_REPLY_MASK
;
343 if (qemu_chr_fe_set_msgfds(chr
, fds
, fd_num
) < 0) {
344 error_report("Failed to set msg fds.");
348 ret
= qemu_chr_fe_write_all(chr
, (const uint8_t *) msg
, size
);
350 error_report("Failed to write msg."
351 " Wrote %d instead of %d.", ret
, size
);
358 int vhost_user_gpu_set_socket(struct vhost_dev
*dev
, int fd
)
361 .hdr
.request
= VHOST_USER_GPU_SET_SOCKET
,
362 .hdr
.flags
= VHOST_USER_VERSION
,
365 return vhost_user_write(dev
, &msg
, &fd
, 1);
368 static int vhost_user_set_log_base(struct vhost_dev
*dev
, uint64_t base
,
369 struct vhost_log
*log
)
371 int fds
[VHOST_MEMORY_MAX_NREGIONS
];
373 bool shmfd
= virtio_has_feature(dev
->protocol_features
,
374 VHOST_USER_PROTOCOL_F_LOG_SHMFD
);
376 .hdr
.request
= VHOST_USER_SET_LOG_BASE
,
377 .hdr
.flags
= VHOST_USER_VERSION
,
378 .payload
.log
.mmap_size
= log
->size
* sizeof(*(log
->log
)),
379 .payload
.log
.mmap_offset
= 0,
380 .hdr
.size
= sizeof(msg
.payload
.log
),
383 if (shmfd
&& log
->fd
!= -1) {
384 fds
[fd_num
++] = log
->fd
;
387 if (vhost_user_write(dev
, &msg
, fds
, fd_num
) < 0) {
393 if (vhost_user_read(dev
, &msg
) < 0) {
397 if (msg
.hdr
.request
!= VHOST_USER_SET_LOG_BASE
) {
398 error_report("Received unexpected msg type. "
399 "Expected %d received %d",
400 VHOST_USER_SET_LOG_BASE
, msg
.hdr
.request
);
408 static int vhost_user_set_mem_table_postcopy(struct vhost_dev
*dev
,
409 struct vhost_memory
*mem
)
411 struct vhost_user
*u
= dev
->opaque
;
412 int fds
[VHOST_MEMORY_MAX_NREGIONS
];
415 VhostUserMsg msg_reply
;
419 .hdr
.request
= VHOST_USER_SET_MEM_TABLE
,
420 .hdr
.flags
= VHOST_USER_VERSION
,
423 if (u
->region_rb_len
< dev
->mem
->nregions
) {
424 u
->region_rb
= g_renew(RAMBlock
*, u
->region_rb
, dev
->mem
->nregions
);
425 u
->region_rb_offset
= g_renew(ram_addr_t
, u
->region_rb_offset
,
427 memset(&(u
->region_rb
[u
->region_rb_len
]), '\0',
428 sizeof(RAMBlock
*) * (dev
->mem
->nregions
- u
->region_rb_len
));
429 memset(&(u
->region_rb_offset
[u
->region_rb_len
]), '\0',
430 sizeof(ram_addr_t
) * (dev
->mem
->nregions
- u
->region_rb_len
));
431 u
->region_rb_len
= dev
->mem
->nregions
;
434 for (i
= 0; i
< dev
->mem
->nregions
; ++i
) {
435 struct vhost_memory_region
*reg
= dev
->mem
->regions
+ i
;
439 assert((uintptr_t)reg
->userspace_addr
== reg
->userspace_addr
);
440 mr
= memory_region_from_host((void *)(uintptr_t)reg
->userspace_addr
,
442 fd
= memory_region_get_fd(mr
);
444 trace_vhost_user_set_mem_table_withfd(fd_num
, mr
->name
,
446 reg
->guest_phys_addr
,
447 reg
->userspace_addr
, offset
);
448 u
->region_rb_offset
[i
] = offset
;
449 u
->region_rb
[i
] = mr
->ram_block
;
450 msg
.payload
.memory
.regions
[fd_num
].userspace_addr
=
452 msg
.payload
.memory
.regions
[fd_num
].memory_size
= reg
->memory_size
;
453 msg
.payload
.memory
.regions
[fd_num
].guest_phys_addr
=
454 reg
->guest_phys_addr
;
455 msg
.payload
.memory
.regions
[fd_num
].mmap_offset
= offset
;
456 assert(fd_num
< VHOST_MEMORY_MAX_NREGIONS
);
459 u
->region_rb_offset
[i
] = 0;
460 u
->region_rb
[i
] = NULL
;
464 msg
.payload
.memory
.nregions
= fd_num
;
467 error_report("Failed initializing vhost-user memory map, "
468 "consider using -object memory-backend-file share=on");
472 msg
.hdr
.size
= sizeof(msg
.payload
.memory
.nregions
);
473 msg
.hdr
.size
+= sizeof(msg
.payload
.memory
.padding
);
474 msg
.hdr
.size
+= fd_num
* sizeof(VhostUserMemoryRegion
);
476 if (vhost_user_write(dev
, &msg
, fds
, fd_num
) < 0) {
480 if (vhost_user_read(dev
, &msg_reply
) < 0) {
484 if (msg_reply
.hdr
.request
!= VHOST_USER_SET_MEM_TABLE
) {
485 error_report("%s: Received unexpected msg type."
486 "Expected %d received %d", __func__
,
487 VHOST_USER_SET_MEM_TABLE
, msg_reply
.hdr
.request
);
490 /* We're using the same structure, just reusing one of the
491 * fields, so it should be the same size.
493 if (msg_reply
.hdr
.size
!= msg
.hdr
.size
) {
494 error_report("%s: Unexpected size for postcopy reply "
495 "%d vs %d", __func__
, msg_reply
.hdr
.size
, msg
.hdr
.size
);
499 memset(u
->postcopy_client_bases
, 0,
500 sizeof(uint64_t) * VHOST_MEMORY_MAX_NREGIONS
);
502 /* They're in the same order as the regions that were sent
503 * but some of the regions were skipped (above) if they
506 for (msg_i
= 0, region_i
= 0;
507 region_i
< dev
->mem
->nregions
;
509 if (msg_i
< fd_num
&&
510 msg_reply
.payload
.memory
.regions
[msg_i
].guest_phys_addr
==
511 dev
->mem
->regions
[region_i
].guest_phys_addr
) {
512 u
->postcopy_client_bases
[region_i
] =
513 msg_reply
.payload
.memory
.regions
[msg_i
].userspace_addr
;
514 trace_vhost_user_set_mem_table_postcopy(
515 msg_reply
.payload
.memory
.regions
[msg_i
].userspace_addr
,
516 msg
.payload
.memory
.regions
[msg_i
].userspace_addr
,
521 if (msg_i
!= fd_num
) {
522 error_report("%s: postcopy reply not fully consumed "
524 __func__
, msg_i
, fd_num
);
527 /* Now we've registered this with the postcopy code, we ack to the client,
528 * because now we're in the position to be able to deal with any faults
531 /* TODO: Use this for failure cases as well with a bad value */
532 msg
.hdr
.size
= sizeof(msg
.payload
.u64
);
533 msg
.payload
.u64
= 0; /* OK */
534 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
541 static int vhost_user_set_mem_table(struct vhost_dev
*dev
,
542 struct vhost_memory
*mem
)
544 struct vhost_user
*u
= dev
->opaque
;
545 int fds
[VHOST_MEMORY_MAX_NREGIONS
];
548 bool do_postcopy
= u
->postcopy_listen
&& u
->postcopy_fd
.handler
;
549 bool reply_supported
= virtio_has_feature(dev
->protocol_features
,
550 VHOST_USER_PROTOCOL_F_REPLY_ACK
);
553 /* Postcopy has enough differences that it's best done in it's own
556 return vhost_user_set_mem_table_postcopy(dev
, mem
);
560 .hdr
.request
= VHOST_USER_SET_MEM_TABLE
,
561 .hdr
.flags
= VHOST_USER_VERSION
,
564 if (reply_supported
) {
565 msg
.hdr
.flags
|= VHOST_USER_NEED_REPLY_MASK
;
568 for (i
= 0; i
< dev
->mem
->nregions
; ++i
) {
569 struct vhost_memory_region
*reg
= dev
->mem
->regions
+ i
;
573 assert((uintptr_t)reg
->userspace_addr
== reg
->userspace_addr
);
574 mr
= memory_region_from_host((void *)(uintptr_t)reg
->userspace_addr
,
576 fd
= memory_region_get_fd(mr
);
578 if (fd_num
== VHOST_MEMORY_MAX_NREGIONS
) {
579 error_report("Failed preparing vhost-user memory table msg");
582 msg
.payload
.memory
.regions
[fd_num
].userspace_addr
=
584 msg
.payload
.memory
.regions
[fd_num
].memory_size
= reg
->memory_size
;
585 msg
.payload
.memory
.regions
[fd_num
].guest_phys_addr
=
586 reg
->guest_phys_addr
;
587 msg
.payload
.memory
.regions
[fd_num
].mmap_offset
= offset
;
592 msg
.payload
.memory
.nregions
= fd_num
;
595 error_report("Failed initializing vhost-user memory map, "
596 "consider using -object memory-backend-file share=on");
600 msg
.hdr
.size
= sizeof(msg
.payload
.memory
.nregions
);
601 msg
.hdr
.size
+= sizeof(msg
.payload
.memory
.padding
);
602 msg
.hdr
.size
+= fd_num
* sizeof(VhostUserMemoryRegion
);
604 if (vhost_user_write(dev
, &msg
, fds
, fd_num
) < 0) {
608 if (reply_supported
) {
609 return process_message_reply(dev
, &msg
);
615 static int vhost_user_set_vring_addr(struct vhost_dev
*dev
,
616 struct vhost_vring_addr
*addr
)
619 .hdr
.request
= VHOST_USER_SET_VRING_ADDR
,
620 .hdr
.flags
= VHOST_USER_VERSION
,
621 .payload
.addr
= *addr
,
622 .hdr
.size
= sizeof(msg
.payload
.addr
),
625 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
632 static int vhost_user_set_vring_endian(struct vhost_dev
*dev
,
633 struct vhost_vring_state
*ring
)
635 bool cross_endian
= virtio_has_feature(dev
->protocol_features
,
636 VHOST_USER_PROTOCOL_F_CROSS_ENDIAN
);
638 .hdr
.request
= VHOST_USER_SET_VRING_ENDIAN
,
639 .hdr
.flags
= VHOST_USER_VERSION
,
640 .payload
.state
= *ring
,
641 .hdr
.size
= sizeof(msg
.payload
.state
),
645 error_report("vhost-user trying to send unhandled ioctl");
649 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
656 static int vhost_set_vring(struct vhost_dev
*dev
,
657 unsigned long int request
,
658 struct vhost_vring_state
*ring
)
661 .hdr
.request
= request
,
662 .hdr
.flags
= VHOST_USER_VERSION
,
663 .payload
.state
= *ring
,
664 .hdr
.size
= sizeof(msg
.payload
.state
),
667 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
674 static int vhost_user_set_vring_num(struct vhost_dev
*dev
,
675 struct vhost_vring_state
*ring
)
677 return vhost_set_vring(dev
, VHOST_USER_SET_VRING_NUM
, ring
);
680 static void vhost_user_host_notifier_restore(struct vhost_dev
*dev
,
683 struct vhost_user
*u
= dev
->opaque
;
684 VhostUserHostNotifier
*n
= &u
->user
->notifier
[queue_idx
];
685 VirtIODevice
*vdev
= dev
->vdev
;
687 if (n
->addr
&& !n
->set
) {
688 virtio_queue_set_host_notifier_mr(vdev
, queue_idx
, &n
->mr
, true);
693 static void vhost_user_host_notifier_remove(struct vhost_dev
*dev
,
696 struct vhost_user
*u
= dev
->opaque
;
697 VhostUserHostNotifier
*n
= &u
->user
->notifier
[queue_idx
];
698 VirtIODevice
*vdev
= dev
->vdev
;
700 if (n
->addr
&& n
->set
) {
701 virtio_queue_set_host_notifier_mr(vdev
, queue_idx
, &n
->mr
, false);
706 static int vhost_user_set_vring_base(struct vhost_dev
*dev
,
707 struct vhost_vring_state
*ring
)
709 vhost_user_host_notifier_restore(dev
, ring
->index
);
711 return vhost_set_vring(dev
, VHOST_USER_SET_VRING_BASE
, ring
);
714 static int vhost_user_set_vring_enable(struct vhost_dev
*dev
, int enable
)
718 if (!virtio_has_feature(dev
->features
, VHOST_USER_F_PROTOCOL_FEATURES
)) {
722 for (i
= 0; i
< dev
->nvqs
; ++i
) {
723 struct vhost_vring_state state
= {
724 .index
= dev
->vq_index
+ i
,
728 vhost_set_vring(dev
, VHOST_USER_SET_VRING_ENABLE
, &state
);
734 static int vhost_user_get_vring_base(struct vhost_dev
*dev
,
735 struct vhost_vring_state
*ring
)
738 .hdr
.request
= VHOST_USER_GET_VRING_BASE
,
739 .hdr
.flags
= VHOST_USER_VERSION
,
740 .payload
.state
= *ring
,
741 .hdr
.size
= sizeof(msg
.payload
.state
),
744 vhost_user_host_notifier_remove(dev
, ring
->index
);
746 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
750 if (vhost_user_read(dev
, &msg
) < 0) {
754 if (msg
.hdr
.request
!= VHOST_USER_GET_VRING_BASE
) {
755 error_report("Received unexpected msg type. Expected %d received %d",
756 VHOST_USER_GET_VRING_BASE
, msg
.hdr
.request
);
760 if (msg
.hdr
.size
!= sizeof(msg
.payload
.state
)) {
761 error_report("Received bad msg size.");
765 *ring
= msg
.payload
.state
;
770 static int vhost_set_vring_file(struct vhost_dev
*dev
,
771 VhostUserRequest request
,
772 struct vhost_vring_file
*file
)
774 int fds
[VHOST_MEMORY_MAX_NREGIONS
];
777 .hdr
.request
= request
,
778 .hdr
.flags
= VHOST_USER_VERSION
,
779 .payload
.u64
= file
->index
& VHOST_USER_VRING_IDX_MASK
,
780 .hdr
.size
= sizeof(msg
.payload
.u64
),
783 if (ioeventfd_enabled() && file
->fd
> 0) {
784 fds
[fd_num
++] = file
->fd
;
786 msg
.payload
.u64
|= VHOST_USER_VRING_NOFD_MASK
;
789 if (vhost_user_write(dev
, &msg
, fds
, fd_num
) < 0) {
796 static int vhost_user_set_vring_kick(struct vhost_dev
*dev
,
797 struct vhost_vring_file
*file
)
799 return vhost_set_vring_file(dev
, VHOST_USER_SET_VRING_KICK
, file
);
802 static int vhost_user_set_vring_call(struct vhost_dev
*dev
,
803 struct vhost_vring_file
*file
)
805 return vhost_set_vring_file(dev
, VHOST_USER_SET_VRING_CALL
, file
);
808 static int vhost_user_set_u64(struct vhost_dev
*dev
, int request
, uint64_t u64
)
811 .hdr
.request
= request
,
812 .hdr
.flags
= VHOST_USER_VERSION
,
814 .hdr
.size
= sizeof(msg
.payload
.u64
),
817 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
824 static int vhost_user_set_features(struct vhost_dev
*dev
,
827 return vhost_user_set_u64(dev
, VHOST_USER_SET_FEATURES
, features
);
830 static int vhost_user_set_protocol_features(struct vhost_dev
*dev
,
833 return vhost_user_set_u64(dev
, VHOST_USER_SET_PROTOCOL_FEATURES
, features
);
836 static int vhost_user_get_u64(struct vhost_dev
*dev
, int request
, uint64_t *u64
)
839 .hdr
.request
= request
,
840 .hdr
.flags
= VHOST_USER_VERSION
,
843 if (vhost_user_one_time_request(request
) && dev
->vq_index
!= 0) {
847 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
851 if (vhost_user_read(dev
, &msg
) < 0) {
855 if (msg
.hdr
.request
!= request
) {
856 error_report("Received unexpected msg type. Expected %d received %d",
857 request
, msg
.hdr
.request
);
861 if (msg
.hdr
.size
!= sizeof(msg
.payload
.u64
)) {
862 error_report("Received bad msg size.");
866 *u64
= msg
.payload
.u64
;
871 static int vhost_user_get_features(struct vhost_dev
*dev
, uint64_t *features
)
873 return vhost_user_get_u64(dev
, VHOST_USER_GET_FEATURES
, features
);
876 static int vhost_user_set_owner(struct vhost_dev
*dev
)
879 .hdr
.request
= VHOST_USER_SET_OWNER
,
880 .hdr
.flags
= VHOST_USER_VERSION
,
883 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
890 static int vhost_user_reset_device(struct vhost_dev
*dev
)
893 .hdr
.request
= VHOST_USER_RESET_OWNER
,
894 .hdr
.flags
= VHOST_USER_VERSION
,
897 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
904 static int vhost_user_slave_handle_config_change(struct vhost_dev
*dev
)
908 if (!dev
->config_ops
) {
912 if (dev
->config_ops
->vhost_dev_config_notifier
) {
913 ret
= dev
->config_ops
->vhost_dev_config_notifier(dev
);
919 static int vhost_user_slave_handle_vring_host_notifier(struct vhost_dev
*dev
,
920 VhostUserVringArea
*area
,
923 int queue_idx
= area
->u64
& VHOST_USER_VRING_IDX_MASK
;
924 size_t page_size
= qemu_real_host_page_size
;
925 struct vhost_user
*u
= dev
->opaque
;
926 VhostUserState
*user
= u
->user
;
927 VirtIODevice
*vdev
= dev
->vdev
;
928 VhostUserHostNotifier
*n
;
932 if (!virtio_has_feature(dev
->protocol_features
,
933 VHOST_USER_PROTOCOL_F_HOST_NOTIFIER
) ||
934 vdev
== NULL
|| queue_idx
>= virtio_get_num_queues(vdev
)) {
938 n
= &user
->notifier
[queue_idx
];
941 virtio_queue_set_host_notifier_mr(vdev
, queue_idx
, &n
->mr
, false);
942 object_unparent(OBJECT(&n
->mr
));
943 munmap(n
->addr
, page_size
);
947 if (area
->u64
& VHOST_USER_VRING_NOFD_MASK
) {
952 if (area
->size
!= page_size
) {
956 addr
= mmap(NULL
, page_size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
,
958 if (addr
== MAP_FAILED
) {
962 name
= g_strdup_printf("vhost-user/host-notifier@%p mmaps[%d]",
964 memory_region_init_ram_device_ptr(&n
->mr
, OBJECT(vdev
), name
,
968 if (virtio_queue_set_host_notifier_mr(vdev
, queue_idx
, &n
->mr
, true)) {
969 munmap(addr
, page_size
);
979 static void slave_read(void *opaque
)
981 struct vhost_dev
*dev
= opaque
;
982 struct vhost_user
*u
= dev
->opaque
;
983 VhostUserHeader hdr
= { 0, };
984 VhostUserPayload payload
= { 0, };
988 int fd
[VHOST_USER_SLAVE_MAX_FDS
];
989 char control
[CMSG_SPACE(sizeof(fd
))];
990 struct cmsghdr
*cmsg
;
993 memset(&msgh
, 0, sizeof(msgh
));
996 msgh
.msg_control
= control
;
997 msgh
.msg_controllen
= sizeof(control
);
999 memset(fd
, -1, sizeof(fd
));
1002 iov
.iov_base
= &hdr
;
1003 iov
.iov_len
= VHOST_USER_HDR_SIZE
;
1006 size
= recvmsg(u
->slave_fd
, &msgh
, 0);
1007 } while (size
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
1009 if (size
!= VHOST_USER_HDR_SIZE
) {
1010 error_report("Failed to read from slave.");
1014 if (msgh
.msg_flags
& MSG_CTRUNC
) {
1015 error_report("Truncated message.");
1019 for (cmsg
= CMSG_FIRSTHDR(&msgh
); cmsg
!= NULL
;
1020 cmsg
= CMSG_NXTHDR(&msgh
, cmsg
)) {
1021 if (cmsg
->cmsg_level
== SOL_SOCKET
&&
1022 cmsg
->cmsg_type
== SCM_RIGHTS
) {
1023 fdsize
= cmsg
->cmsg_len
- CMSG_LEN(0);
1024 memcpy(fd
, CMSG_DATA(cmsg
), fdsize
);
1029 if (hdr
.size
> VHOST_USER_PAYLOAD_SIZE
) {
1030 error_report("Failed to read msg header."
1031 " Size %d exceeds the maximum %zu.", hdr
.size
,
1032 VHOST_USER_PAYLOAD_SIZE
);
1038 size
= read(u
->slave_fd
, &payload
, hdr
.size
);
1039 } while (size
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
1041 if (size
!= hdr
.size
) {
1042 error_report("Failed to read payload from slave.");
1046 switch (hdr
.request
) {
1047 case VHOST_USER_SLAVE_IOTLB_MSG
:
1048 ret
= vhost_backend_handle_iotlb_msg(dev
, &payload
.iotlb
);
1050 case VHOST_USER_SLAVE_CONFIG_CHANGE_MSG
:
1051 ret
= vhost_user_slave_handle_config_change(dev
);
1053 case VHOST_USER_SLAVE_VRING_HOST_NOTIFIER_MSG
:
1054 ret
= vhost_user_slave_handle_vring_host_notifier(dev
, &payload
.area
,
1058 error_report("Received unexpected msg type.");
1062 /* Close the remaining file descriptors. */
1063 for (i
= 0; i
< fdsize
; i
++) {
1070 * REPLY_ACK feature handling. Other reply types has to be managed
1071 * directly in their request handlers.
1073 if (hdr
.flags
& VHOST_USER_NEED_REPLY_MASK
) {
1074 struct iovec iovec
[2];
1077 hdr
.flags
&= ~VHOST_USER_NEED_REPLY_MASK
;
1078 hdr
.flags
|= VHOST_USER_REPLY_MASK
;
1080 payload
.u64
= !!ret
;
1081 hdr
.size
= sizeof(payload
.u64
);
1083 iovec
[0].iov_base
= &hdr
;
1084 iovec
[0].iov_len
= VHOST_USER_HDR_SIZE
;
1085 iovec
[1].iov_base
= &payload
;
1086 iovec
[1].iov_len
= hdr
.size
;
1089 size
= writev(u
->slave_fd
, iovec
, ARRAY_SIZE(iovec
));
1090 } while (size
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
1092 if (size
!= VHOST_USER_HDR_SIZE
+ hdr
.size
) {
1093 error_report("Failed to send msg reply to slave.");
1101 qemu_set_fd_handler(u
->slave_fd
, NULL
, NULL
, NULL
);
1104 for (i
= 0; i
< fdsize
; i
++) {
1112 static int vhost_setup_slave_channel(struct vhost_dev
*dev
)
1114 VhostUserMsg msg
= {
1115 .hdr
.request
= VHOST_USER_SET_SLAVE_REQ_FD
,
1116 .hdr
.flags
= VHOST_USER_VERSION
,
1118 struct vhost_user
*u
= dev
->opaque
;
1120 bool reply_supported
= virtio_has_feature(dev
->protocol_features
,
1121 VHOST_USER_PROTOCOL_F_REPLY_ACK
);
1123 if (!virtio_has_feature(dev
->protocol_features
,
1124 VHOST_USER_PROTOCOL_F_SLAVE_REQ
)) {
1128 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, sv
) == -1) {
1129 error_report("socketpair() failed");
1133 u
->slave_fd
= sv
[0];
1134 qemu_set_fd_handler(u
->slave_fd
, slave_read
, NULL
, dev
);
1136 if (reply_supported
) {
1137 msg
.hdr
.flags
|= VHOST_USER_NEED_REPLY_MASK
;
1140 ret
= vhost_user_write(dev
, &msg
, &sv
[1], 1);
1145 if (reply_supported
) {
1146 ret
= process_message_reply(dev
, &msg
);
1152 qemu_set_fd_handler(u
->slave_fd
, NULL
, NULL
, NULL
);
1162 * Called back from the postcopy fault thread when a fault is received on our
1164 * TODO: This is Linux specific
1166 static int vhost_user_postcopy_fault_handler(struct PostCopyFD
*pcfd
,
1169 struct vhost_dev
*dev
= pcfd
->data
;
1170 struct vhost_user
*u
= dev
->opaque
;
1171 struct uffd_msg
*msg
= ufd
;
1172 uint64_t faultaddr
= msg
->arg
.pagefault
.address
;
1173 RAMBlock
*rb
= NULL
;
1177 trace_vhost_user_postcopy_fault_handler(pcfd
->idstr
, faultaddr
,
1178 dev
->mem
->nregions
);
1179 for (i
= 0; i
< MIN(dev
->mem
->nregions
, u
->region_rb_len
); i
++) {
1180 trace_vhost_user_postcopy_fault_handler_loop(i
,
1181 u
->postcopy_client_bases
[i
], dev
->mem
->regions
[i
].memory_size
);
1182 if (faultaddr
>= u
->postcopy_client_bases
[i
]) {
1183 /* Ofset of the fault address in the vhost region */
1184 uint64_t region_offset
= faultaddr
- u
->postcopy_client_bases
[i
];
1185 if (region_offset
< dev
->mem
->regions
[i
].memory_size
) {
1186 rb_offset
= region_offset
+ u
->region_rb_offset
[i
];
1187 trace_vhost_user_postcopy_fault_handler_found(i
,
1188 region_offset
, rb_offset
);
1189 rb
= u
->region_rb
[i
];
1190 return postcopy_request_shared_page(pcfd
, rb
, faultaddr
,
1195 error_report("%s: Failed to find region for fault %" PRIx64
,
1196 __func__
, faultaddr
);
1200 static int vhost_user_postcopy_waker(struct PostCopyFD
*pcfd
, RAMBlock
*rb
,
1203 struct vhost_dev
*dev
= pcfd
->data
;
1204 struct vhost_user
*u
= dev
->opaque
;
1207 trace_vhost_user_postcopy_waker(qemu_ram_get_idstr(rb
), offset
);
1212 /* Translate the offset into an address in the clients address space */
1213 for (i
= 0; i
< MIN(dev
->mem
->nregions
, u
->region_rb_len
); i
++) {
1214 if (u
->region_rb
[i
] == rb
&&
1215 offset
>= u
->region_rb_offset
[i
] &&
1216 offset
< (u
->region_rb_offset
[i
] +
1217 dev
->mem
->regions
[i
].memory_size
)) {
1218 uint64_t client_addr
= (offset
- u
->region_rb_offset
[i
]) +
1219 u
->postcopy_client_bases
[i
];
1220 trace_vhost_user_postcopy_waker_found(client_addr
);
1221 return postcopy_wake_shared(pcfd
, client_addr
, rb
);
1225 trace_vhost_user_postcopy_waker_nomatch(qemu_ram_get_idstr(rb
), offset
);
1231 * Called at the start of an inbound postcopy on reception of the
1234 static int vhost_user_postcopy_advise(struct vhost_dev
*dev
, Error
**errp
)
1237 struct vhost_user
*u
= dev
->opaque
;
1238 CharBackend
*chr
= u
->user
->chr
;
1240 VhostUserMsg msg
= {
1241 .hdr
.request
= VHOST_USER_POSTCOPY_ADVISE
,
1242 .hdr
.flags
= VHOST_USER_VERSION
,
1245 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
1246 error_setg(errp
, "Failed to send postcopy_advise to vhost");
1250 if (vhost_user_read(dev
, &msg
) < 0) {
1251 error_setg(errp
, "Failed to get postcopy_advise reply from vhost");
1255 if (msg
.hdr
.request
!= VHOST_USER_POSTCOPY_ADVISE
) {
1256 error_setg(errp
, "Unexpected msg type. Expected %d received %d",
1257 VHOST_USER_POSTCOPY_ADVISE
, msg
.hdr
.request
);
1262 error_setg(errp
, "Received bad msg size.");
1265 ufd
= qemu_chr_fe_get_msgfd(chr
);
1267 error_setg(errp
, "%s: Failed to get ufd", __func__
);
1270 qemu_set_nonblock(ufd
);
1272 /* register ufd with userfault thread */
1273 u
->postcopy_fd
.fd
= ufd
;
1274 u
->postcopy_fd
.data
= dev
;
1275 u
->postcopy_fd
.handler
= vhost_user_postcopy_fault_handler
;
1276 u
->postcopy_fd
.waker
= vhost_user_postcopy_waker
;
1277 u
->postcopy_fd
.idstr
= "vhost-user"; /* Need to find unique name */
1278 postcopy_register_shared_ufd(&u
->postcopy_fd
);
1281 error_setg(errp
, "Postcopy not supported on non-Linux systems");
1287 * Called at the switch to postcopy on reception of the 'listen' command.
1289 static int vhost_user_postcopy_listen(struct vhost_dev
*dev
, Error
**errp
)
1291 struct vhost_user
*u
= dev
->opaque
;
1293 VhostUserMsg msg
= {
1294 .hdr
.request
= VHOST_USER_POSTCOPY_LISTEN
,
1295 .hdr
.flags
= VHOST_USER_VERSION
| VHOST_USER_NEED_REPLY_MASK
,
1297 u
->postcopy_listen
= true;
1298 trace_vhost_user_postcopy_listen();
1299 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
1300 error_setg(errp
, "Failed to send postcopy_listen to vhost");
1304 ret
= process_message_reply(dev
, &msg
);
1306 error_setg(errp
, "Failed to receive reply to postcopy_listen");
1314 * Called at the end of postcopy
1316 static int vhost_user_postcopy_end(struct vhost_dev
*dev
, Error
**errp
)
1318 VhostUserMsg msg
= {
1319 .hdr
.request
= VHOST_USER_POSTCOPY_END
,
1320 .hdr
.flags
= VHOST_USER_VERSION
| VHOST_USER_NEED_REPLY_MASK
,
1323 struct vhost_user
*u
= dev
->opaque
;
1325 trace_vhost_user_postcopy_end_entry();
1326 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
1327 error_setg(errp
, "Failed to send postcopy_end to vhost");
1331 ret
= process_message_reply(dev
, &msg
);
1333 error_setg(errp
, "Failed to receive reply to postcopy_end");
1336 postcopy_unregister_shared_ufd(&u
->postcopy_fd
);
1337 close(u
->postcopy_fd
.fd
);
1338 u
->postcopy_fd
.handler
= NULL
;
1340 trace_vhost_user_postcopy_end_exit();
1345 static int vhost_user_postcopy_notifier(NotifierWithReturn
*notifier
,
1348 struct PostcopyNotifyData
*pnd
= opaque
;
1349 struct vhost_user
*u
= container_of(notifier
, struct vhost_user
,
1351 struct vhost_dev
*dev
= u
->dev
;
1353 switch (pnd
->reason
) {
1354 case POSTCOPY_NOTIFY_PROBE
:
1355 if (!virtio_has_feature(dev
->protocol_features
,
1356 VHOST_USER_PROTOCOL_F_PAGEFAULT
)) {
1357 /* TODO: Get the device name into this error somehow */
1358 error_setg(pnd
->errp
,
1359 "vhost-user backend not capable of postcopy");
1364 case POSTCOPY_NOTIFY_INBOUND_ADVISE
:
1365 return vhost_user_postcopy_advise(dev
, pnd
->errp
);
1367 case POSTCOPY_NOTIFY_INBOUND_LISTEN
:
1368 return vhost_user_postcopy_listen(dev
, pnd
->errp
);
1370 case POSTCOPY_NOTIFY_INBOUND_END
:
1371 return vhost_user_postcopy_end(dev
, pnd
->errp
);
1374 /* We ignore notifications we don't know */
1381 static int vhost_user_backend_init(struct vhost_dev
*dev
, void *opaque
)
1383 uint64_t features
, protocol_features
;
1384 struct vhost_user
*u
;
1387 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
1389 u
= g_new0(struct vhost_user
, 1);
1395 err
= vhost_user_get_features(dev
, &features
);
1400 if (virtio_has_feature(features
, VHOST_USER_F_PROTOCOL_FEATURES
)) {
1401 dev
->backend_features
|= 1ULL << VHOST_USER_F_PROTOCOL_FEATURES
;
1403 err
= vhost_user_get_u64(dev
, VHOST_USER_GET_PROTOCOL_FEATURES
,
1404 &protocol_features
);
1409 dev
->protocol_features
=
1410 protocol_features
& VHOST_USER_PROTOCOL_FEATURE_MASK
;
1412 if (!dev
->config_ops
|| !dev
->config_ops
->vhost_dev_config_notifier
) {
1413 /* Don't acknowledge CONFIG feature if device doesn't support it */
1414 dev
->protocol_features
&= ~(1ULL << VHOST_USER_PROTOCOL_F_CONFIG
);
1415 } else if (!(protocol_features
&
1416 (1ULL << VHOST_USER_PROTOCOL_F_CONFIG
))) {
1417 error_report("Device expects VHOST_USER_PROTOCOL_F_CONFIG "
1418 "but backend does not support it.");
1422 err
= vhost_user_set_protocol_features(dev
, dev
->protocol_features
);
1427 /* query the max queues we support if backend supports Multiple Queue */
1428 if (dev
->protocol_features
& (1ULL << VHOST_USER_PROTOCOL_F_MQ
)) {
1429 err
= vhost_user_get_u64(dev
, VHOST_USER_GET_QUEUE_NUM
,
1436 if (virtio_has_feature(features
, VIRTIO_F_IOMMU_PLATFORM
) &&
1437 !(virtio_has_feature(dev
->protocol_features
,
1438 VHOST_USER_PROTOCOL_F_SLAVE_REQ
) &&
1439 virtio_has_feature(dev
->protocol_features
,
1440 VHOST_USER_PROTOCOL_F_REPLY_ACK
))) {
1441 error_report("IOMMU support requires reply-ack and "
1442 "slave-req protocol features.");
1447 if (dev
->migration_blocker
== NULL
&&
1448 !virtio_has_feature(dev
->protocol_features
,
1449 VHOST_USER_PROTOCOL_F_LOG_SHMFD
)) {
1450 error_setg(&dev
->migration_blocker
,
1451 "Migration disabled: vhost-user backend lacks "
1452 "VHOST_USER_PROTOCOL_F_LOG_SHMFD feature.");
1455 err
= vhost_setup_slave_channel(dev
);
1460 u
->postcopy_notifier
.notify
= vhost_user_postcopy_notifier
;
1461 postcopy_add_notifier(&u
->postcopy_notifier
);
1466 static int vhost_user_backend_cleanup(struct vhost_dev
*dev
)
1468 struct vhost_user
*u
;
1470 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
1473 if (u
->postcopy_notifier
.notify
) {
1474 postcopy_remove_notifier(&u
->postcopy_notifier
);
1475 u
->postcopy_notifier
.notify
= NULL
;
1477 u
->postcopy_listen
= false;
1478 if (u
->postcopy_fd
.handler
) {
1479 postcopy_unregister_shared_ufd(&u
->postcopy_fd
);
1480 close(u
->postcopy_fd
.fd
);
1481 u
->postcopy_fd
.handler
= NULL
;
1483 if (u
->slave_fd
>= 0) {
1484 qemu_set_fd_handler(u
->slave_fd
, NULL
, NULL
, NULL
);
1488 g_free(u
->region_rb
);
1489 u
->region_rb
= NULL
;
1490 g_free(u
->region_rb_offset
);
1491 u
->region_rb_offset
= NULL
;
1492 u
->region_rb_len
= 0;
1499 static int vhost_user_get_vq_index(struct vhost_dev
*dev
, int idx
)
1501 assert(idx
>= dev
->vq_index
&& idx
< dev
->vq_index
+ dev
->nvqs
);
1506 static int vhost_user_memslots_limit(struct vhost_dev
*dev
)
1508 return VHOST_MEMORY_MAX_NREGIONS
;
1511 static bool vhost_user_requires_shm_log(struct vhost_dev
*dev
)
1513 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
1515 return virtio_has_feature(dev
->protocol_features
,
1516 VHOST_USER_PROTOCOL_F_LOG_SHMFD
);
1519 static int vhost_user_migration_done(struct vhost_dev
*dev
, char* mac_addr
)
1521 VhostUserMsg msg
= { };
1523 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
1525 /* If guest supports GUEST_ANNOUNCE do nothing */
1526 if (virtio_has_feature(dev
->acked_features
, VIRTIO_NET_F_GUEST_ANNOUNCE
)) {
1530 /* if backend supports VHOST_USER_PROTOCOL_F_RARP ask it to send the RARP */
1531 if (virtio_has_feature(dev
->protocol_features
,
1532 VHOST_USER_PROTOCOL_F_RARP
)) {
1533 msg
.hdr
.request
= VHOST_USER_SEND_RARP
;
1534 msg
.hdr
.flags
= VHOST_USER_VERSION
;
1535 memcpy((char *)&msg
.payload
.u64
, mac_addr
, 6);
1536 msg
.hdr
.size
= sizeof(msg
.payload
.u64
);
1538 return vhost_user_write(dev
, &msg
, NULL
, 0);
1543 static bool vhost_user_can_merge(struct vhost_dev
*dev
,
1544 uint64_t start1
, uint64_t size1
,
1545 uint64_t start2
, uint64_t size2
)
1551 mr
= memory_region_from_host((void *)(uintptr_t)start1
, &offset
);
1552 mfd
= memory_region_get_fd(mr
);
1554 mr
= memory_region_from_host((void *)(uintptr_t)start2
, &offset
);
1555 rfd
= memory_region_get_fd(mr
);
1560 static int vhost_user_net_set_mtu(struct vhost_dev
*dev
, uint16_t mtu
)
1563 bool reply_supported
= virtio_has_feature(dev
->protocol_features
,
1564 VHOST_USER_PROTOCOL_F_REPLY_ACK
);
1566 if (!(dev
->protocol_features
& (1ULL << VHOST_USER_PROTOCOL_F_NET_MTU
))) {
1570 msg
.hdr
.request
= VHOST_USER_NET_SET_MTU
;
1571 msg
.payload
.u64
= mtu
;
1572 msg
.hdr
.size
= sizeof(msg
.payload
.u64
);
1573 msg
.hdr
.flags
= VHOST_USER_VERSION
;
1574 if (reply_supported
) {
1575 msg
.hdr
.flags
|= VHOST_USER_NEED_REPLY_MASK
;
1578 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
1582 /* If reply_ack supported, slave has to ack specified MTU is valid */
1583 if (reply_supported
) {
1584 return process_message_reply(dev
, &msg
);
1590 static int vhost_user_send_device_iotlb_msg(struct vhost_dev
*dev
,
1591 struct vhost_iotlb_msg
*imsg
)
1593 VhostUserMsg msg
= {
1594 .hdr
.request
= VHOST_USER_IOTLB_MSG
,
1595 .hdr
.size
= sizeof(msg
.payload
.iotlb
),
1596 .hdr
.flags
= VHOST_USER_VERSION
| VHOST_USER_NEED_REPLY_MASK
,
1597 .payload
.iotlb
= *imsg
,
1600 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
1604 return process_message_reply(dev
, &msg
);
1608 static void vhost_user_set_iotlb_callback(struct vhost_dev
*dev
, int enabled
)
1610 /* No-op as the receive channel is not dedicated to IOTLB messages. */
1613 static int vhost_user_get_config(struct vhost_dev
*dev
, uint8_t *config
,
1614 uint32_t config_len
)
1616 VhostUserMsg msg
= {
1617 .hdr
.request
= VHOST_USER_GET_CONFIG
,
1618 .hdr
.flags
= VHOST_USER_VERSION
,
1619 .hdr
.size
= VHOST_USER_CONFIG_HDR_SIZE
+ config_len
,
1622 if (!virtio_has_feature(dev
->protocol_features
,
1623 VHOST_USER_PROTOCOL_F_CONFIG
)) {
1627 if (config_len
> VHOST_USER_MAX_CONFIG_SIZE
) {
1631 msg
.payload
.config
.offset
= 0;
1632 msg
.payload
.config
.size
= config_len
;
1633 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
1637 if (vhost_user_read(dev
, &msg
) < 0) {
1641 if (msg
.hdr
.request
!= VHOST_USER_GET_CONFIG
) {
1642 error_report("Received unexpected msg type. Expected %d received %d",
1643 VHOST_USER_GET_CONFIG
, msg
.hdr
.request
);
1647 if (msg
.hdr
.size
!= VHOST_USER_CONFIG_HDR_SIZE
+ config_len
) {
1648 error_report("Received bad msg size.");
1652 memcpy(config
, msg
.payload
.config
.region
, config_len
);
1657 static int vhost_user_set_config(struct vhost_dev
*dev
, const uint8_t *data
,
1658 uint32_t offset
, uint32_t size
, uint32_t flags
)
1661 bool reply_supported
= virtio_has_feature(dev
->protocol_features
,
1662 VHOST_USER_PROTOCOL_F_REPLY_ACK
);
1664 VhostUserMsg msg
= {
1665 .hdr
.request
= VHOST_USER_SET_CONFIG
,
1666 .hdr
.flags
= VHOST_USER_VERSION
,
1667 .hdr
.size
= VHOST_USER_CONFIG_HDR_SIZE
+ size
,
1670 if (!virtio_has_feature(dev
->protocol_features
,
1671 VHOST_USER_PROTOCOL_F_CONFIG
)) {
1675 if (reply_supported
) {
1676 msg
.hdr
.flags
|= VHOST_USER_NEED_REPLY_MASK
;
1679 if (size
> VHOST_USER_MAX_CONFIG_SIZE
) {
1683 msg
.payload
.config
.offset
= offset
,
1684 msg
.payload
.config
.size
= size
,
1685 msg
.payload
.config
.flags
= flags
,
1686 p
= msg
.payload
.config
.region
;
1687 memcpy(p
, data
, size
);
1689 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
1693 if (reply_supported
) {
1694 return process_message_reply(dev
, &msg
);
1700 static int vhost_user_crypto_create_session(struct vhost_dev
*dev
,
1702 uint64_t *session_id
)
1704 bool crypto_session
= virtio_has_feature(dev
->protocol_features
,
1705 VHOST_USER_PROTOCOL_F_CRYPTO_SESSION
);
1706 CryptoDevBackendSymSessionInfo
*sess_info
= session_info
;
1707 VhostUserMsg msg
= {
1708 .hdr
.request
= VHOST_USER_CREATE_CRYPTO_SESSION
,
1709 .hdr
.flags
= VHOST_USER_VERSION
,
1710 .hdr
.size
= sizeof(msg
.payload
.session
),
1713 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
1715 if (!crypto_session
) {
1716 error_report("vhost-user trying to send unhandled ioctl");
1720 memcpy(&msg
.payload
.session
.session_setup_data
, sess_info
,
1721 sizeof(CryptoDevBackendSymSessionInfo
));
1722 if (sess_info
->key_len
) {
1723 memcpy(&msg
.payload
.session
.key
, sess_info
->cipher_key
,
1724 sess_info
->key_len
);
1726 if (sess_info
->auth_key_len
> 0) {
1727 memcpy(&msg
.payload
.session
.auth_key
, sess_info
->auth_key
,
1728 sess_info
->auth_key_len
);
1730 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
1731 error_report("vhost_user_write() return -1, create session failed");
1735 if (vhost_user_read(dev
, &msg
) < 0) {
1736 error_report("vhost_user_read() return -1, create session failed");
1740 if (msg
.hdr
.request
!= VHOST_USER_CREATE_CRYPTO_SESSION
) {
1741 error_report("Received unexpected msg type. Expected %d received %d",
1742 VHOST_USER_CREATE_CRYPTO_SESSION
, msg
.hdr
.request
);
1746 if (msg
.hdr
.size
!= sizeof(msg
.payload
.session
)) {
1747 error_report("Received bad msg size.");
1751 if (msg
.payload
.session
.session_id
< 0) {
1752 error_report("Bad session id: %" PRId64
"",
1753 msg
.payload
.session
.session_id
);
1756 *session_id
= msg
.payload
.session
.session_id
;
1762 vhost_user_crypto_close_session(struct vhost_dev
*dev
, uint64_t session_id
)
1764 bool crypto_session
= virtio_has_feature(dev
->protocol_features
,
1765 VHOST_USER_PROTOCOL_F_CRYPTO_SESSION
);
1766 VhostUserMsg msg
= {
1767 .hdr
.request
= VHOST_USER_CLOSE_CRYPTO_SESSION
,
1768 .hdr
.flags
= VHOST_USER_VERSION
,
1769 .hdr
.size
= sizeof(msg
.payload
.u64
),
1771 msg
.payload
.u64
= session_id
;
1773 if (!crypto_session
) {
1774 error_report("vhost-user trying to send unhandled ioctl");
1778 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
1779 error_report("vhost_user_write() return -1, close session failed");
1786 static bool vhost_user_mem_section_filter(struct vhost_dev
*dev
,
1787 MemoryRegionSection
*section
)
1791 result
= memory_region_get_fd(section
->mr
) >= 0;
1796 static int vhost_user_get_inflight_fd(struct vhost_dev
*dev
,
1797 uint16_t queue_size
,
1798 struct vhost_inflight
*inflight
)
1802 struct vhost_user
*u
= dev
->opaque
;
1803 CharBackend
*chr
= u
->user
->chr
;
1804 VhostUserMsg msg
= {
1805 .hdr
.request
= VHOST_USER_GET_INFLIGHT_FD
,
1806 .hdr
.flags
= VHOST_USER_VERSION
,
1807 .payload
.inflight
.num_queues
= dev
->nvqs
,
1808 .payload
.inflight
.queue_size
= queue_size
,
1809 .hdr
.size
= sizeof(msg
.payload
.inflight
),
1812 if (!virtio_has_feature(dev
->protocol_features
,
1813 VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD
)) {
1817 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
1821 if (vhost_user_read(dev
, &msg
) < 0) {
1825 if (msg
.hdr
.request
!= VHOST_USER_GET_INFLIGHT_FD
) {
1826 error_report("Received unexpected msg type. "
1827 "Expected %d received %d",
1828 VHOST_USER_GET_INFLIGHT_FD
, msg
.hdr
.request
);
1832 if (msg
.hdr
.size
!= sizeof(msg
.payload
.inflight
)) {
1833 error_report("Received bad msg size.");
1837 if (!msg
.payload
.inflight
.mmap_size
) {
1841 fd
= qemu_chr_fe_get_msgfd(chr
);
1843 error_report("Failed to get mem fd");
1847 addr
= mmap(0, msg
.payload
.inflight
.mmap_size
, PROT_READ
| PROT_WRITE
,
1848 MAP_SHARED
, fd
, msg
.payload
.inflight
.mmap_offset
);
1850 if (addr
== MAP_FAILED
) {
1851 error_report("Failed to mmap mem fd");
1856 inflight
->addr
= addr
;
1858 inflight
->size
= msg
.payload
.inflight
.mmap_size
;
1859 inflight
->offset
= msg
.payload
.inflight
.mmap_offset
;
1860 inflight
->queue_size
= queue_size
;
1865 static int vhost_user_set_inflight_fd(struct vhost_dev
*dev
,
1866 struct vhost_inflight
*inflight
)
1868 VhostUserMsg msg
= {
1869 .hdr
.request
= VHOST_USER_SET_INFLIGHT_FD
,
1870 .hdr
.flags
= VHOST_USER_VERSION
,
1871 .payload
.inflight
.mmap_size
= inflight
->size
,
1872 .payload
.inflight
.mmap_offset
= inflight
->offset
,
1873 .payload
.inflight
.num_queues
= dev
->nvqs
,
1874 .payload
.inflight
.queue_size
= inflight
->queue_size
,
1875 .hdr
.size
= sizeof(msg
.payload
.inflight
),
1878 if (!virtio_has_feature(dev
->protocol_features
,
1879 VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD
)) {
1883 if (vhost_user_write(dev
, &msg
, &inflight
->fd
, 1) < 0) {
1890 bool vhost_user_init(VhostUserState
*user
, CharBackend
*chr
, Error
**errp
)
1893 error_setg(errp
, "Cannot initialize vhost-user state");
1900 void vhost_user_cleanup(VhostUserState
*user
)
1908 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
1909 if (user
->notifier
[i
].addr
) {
1910 object_unparent(OBJECT(&user
->notifier
[i
].mr
));
1911 munmap(user
->notifier
[i
].addr
, qemu_real_host_page_size
);
1912 user
->notifier
[i
].addr
= NULL
;
1918 const VhostOps user_ops
= {
1919 .backend_type
= VHOST_BACKEND_TYPE_USER
,
1920 .vhost_backend_init
= vhost_user_backend_init
,
1921 .vhost_backend_cleanup
= vhost_user_backend_cleanup
,
1922 .vhost_backend_memslots_limit
= vhost_user_memslots_limit
,
1923 .vhost_set_log_base
= vhost_user_set_log_base
,
1924 .vhost_set_mem_table
= vhost_user_set_mem_table
,
1925 .vhost_set_vring_addr
= vhost_user_set_vring_addr
,
1926 .vhost_set_vring_endian
= vhost_user_set_vring_endian
,
1927 .vhost_set_vring_num
= vhost_user_set_vring_num
,
1928 .vhost_set_vring_base
= vhost_user_set_vring_base
,
1929 .vhost_get_vring_base
= vhost_user_get_vring_base
,
1930 .vhost_set_vring_kick
= vhost_user_set_vring_kick
,
1931 .vhost_set_vring_call
= vhost_user_set_vring_call
,
1932 .vhost_set_features
= vhost_user_set_features
,
1933 .vhost_get_features
= vhost_user_get_features
,
1934 .vhost_set_owner
= vhost_user_set_owner
,
1935 .vhost_reset_device
= vhost_user_reset_device
,
1936 .vhost_get_vq_index
= vhost_user_get_vq_index
,
1937 .vhost_set_vring_enable
= vhost_user_set_vring_enable
,
1938 .vhost_requires_shm_log
= vhost_user_requires_shm_log
,
1939 .vhost_migration_done
= vhost_user_migration_done
,
1940 .vhost_backend_can_merge
= vhost_user_can_merge
,
1941 .vhost_net_set_mtu
= vhost_user_net_set_mtu
,
1942 .vhost_set_iotlb_callback
= vhost_user_set_iotlb_callback
,
1943 .vhost_send_device_iotlb_msg
= vhost_user_send_device_iotlb_msg
,
1944 .vhost_get_config
= vhost_user_get_config
,
1945 .vhost_set_config
= vhost_user_set_config
,
1946 .vhost_crypto_create_session
= vhost_user_crypto_create_session
,
1947 .vhost_crypto_close_session
= vhost_user_crypto_close_session
,
1948 .vhost_backend_mem_section_filter
= vhost_user_mem_section_filter
,
1949 .vhost_get_inflight_fd
= vhost_user_get_inflight_fd
,
1950 .vhost_set_inflight_fd
= vhost_user_set_inflight_fd
,