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-backend.h"
15 #include "hw/virtio/virtio-net.h"
16 #include "sysemu/char.h"
17 #include "sysemu/kvm.h"
18 #include "qemu/error-report.h"
19 #include "qemu/sockets.h"
20 #include "migration/migration.h"
22 #include <sys/ioctl.h>
23 #include <sys/socket.h>
25 #include <linux/vhost.h>
27 #define VHOST_MEMORY_MAX_NREGIONS 8
28 #define VHOST_USER_F_PROTOCOL_FEATURES 30
30 enum VhostUserProtocolFeature
{
31 VHOST_USER_PROTOCOL_F_MQ
= 0,
32 VHOST_USER_PROTOCOL_F_LOG_SHMFD
= 1,
33 VHOST_USER_PROTOCOL_F_RARP
= 2,
34 VHOST_USER_PROTOCOL_F_REPLY_ACK
= 3,
36 VHOST_USER_PROTOCOL_F_MAX
39 #define VHOST_USER_PROTOCOL_FEATURE_MASK ((1 << VHOST_USER_PROTOCOL_F_MAX) - 1)
41 typedef enum VhostUserRequest
{
43 VHOST_USER_GET_FEATURES
= 1,
44 VHOST_USER_SET_FEATURES
= 2,
45 VHOST_USER_SET_OWNER
= 3,
46 VHOST_USER_RESET_OWNER
= 4,
47 VHOST_USER_SET_MEM_TABLE
= 5,
48 VHOST_USER_SET_LOG_BASE
= 6,
49 VHOST_USER_SET_LOG_FD
= 7,
50 VHOST_USER_SET_VRING_NUM
= 8,
51 VHOST_USER_SET_VRING_ADDR
= 9,
52 VHOST_USER_SET_VRING_BASE
= 10,
53 VHOST_USER_GET_VRING_BASE
= 11,
54 VHOST_USER_SET_VRING_KICK
= 12,
55 VHOST_USER_SET_VRING_CALL
= 13,
56 VHOST_USER_SET_VRING_ERR
= 14,
57 VHOST_USER_GET_PROTOCOL_FEATURES
= 15,
58 VHOST_USER_SET_PROTOCOL_FEATURES
= 16,
59 VHOST_USER_GET_QUEUE_NUM
= 17,
60 VHOST_USER_SET_VRING_ENABLE
= 18,
61 VHOST_USER_SEND_RARP
= 19,
65 typedef struct VhostUserMemoryRegion
{
66 uint64_t guest_phys_addr
;
68 uint64_t userspace_addr
;
70 } VhostUserMemoryRegion
;
72 typedef struct VhostUserMemory
{
75 VhostUserMemoryRegion regions
[VHOST_MEMORY_MAX_NREGIONS
];
78 typedef struct VhostUserLog
{
83 typedef struct VhostUserMsg
{
84 VhostUserRequest request
;
86 #define VHOST_USER_VERSION_MASK (0x3)
87 #define VHOST_USER_REPLY_MASK (0x1<<2)
88 #define VHOST_USER_NEED_REPLY_MASK (0x1 << 3)
90 uint32_t size
; /* the following payload size */
92 #define VHOST_USER_VRING_IDX_MASK (0xff)
93 #define VHOST_USER_VRING_NOFD_MASK (0x1<<8)
95 struct vhost_vring_state state
;
96 struct vhost_vring_addr addr
;
97 VhostUserMemory memory
;
100 } QEMU_PACKED VhostUserMsg
;
102 static VhostUserMsg m
__attribute__ ((unused
));
103 #define VHOST_USER_HDR_SIZE (sizeof(m.request) \
107 #define VHOST_USER_PAYLOAD_SIZE (sizeof(m) - VHOST_USER_HDR_SIZE)
109 /* The version of the protocol we support */
110 #define VHOST_USER_VERSION (0x1)
112 static bool ioeventfd_enabled(void)
114 return kvm_enabled() && kvm_eventfds_enabled();
117 static int vhost_user_read(struct vhost_dev
*dev
, VhostUserMsg
*msg
)
119 CharDriverState
*chr
= dev
->opaque
;
120 uint8_t *p
= (uint8_t *) msg
;
121 int r
, size
= VHOST_USER_HDR_SIZE
;
123 r
= qemu_chr_fe_read_all(chr
, p
, size
);
125 error_report("Failed to read msg header. Read %d instead of %d."
126 " Original request %d.", r
, size
, msg
->request
);
130 /* validate received flags */
131 if (msg
->flags
!= (VHOST_USER_REPLY_MASK
| VHOST_USER_VERSION
)) {
132 error_report("Failed to read msg header."
133 " Flags 0x%x instead of 0x%x.", msg
->flags
,
134 VHOST_USER_REPLY_MASK
| VHOST_USER_VERSION
);
138 /* validate message size is sane */
139 if (msg
->size
> VHOST_USER_PAYLOAD_SIZE
) {
140 error_report("Failed to read msg header."
141 " Size %d exceeds the maximum %zu.", msg
->size
,
142 VHOST_USER_PAYLOAD_SIZE
);
147 p
+= VHOST_USER_HDR_SIZE
;
149 r
= qemu_chr_fe_read_all(chr
, p
, size
);
151 error_report("Failed to read msg payload."
152 " Read %d instead of %d.", r
, msg
->size
);
163 static int process_message_reply(struct vhost_dev
*dev
,
164 VhostUserRequest request
)
168 if (vhost_user_read(dev
, &msg
) < 0) {
172 if (msg
.request
!= request
) {
173 error_report("Received unexpected msg type."
174 "Expected %d received %d",
175 request
, msg
.request
);
179 return msg
.payload
.u64
? -1 : 0;
182 static bool vhost_user_one_time_request(VhostUserRequest request
)
185 case VHOST_USER_SET_OWNER
:
186 case VHOST_USER_RESET_OWNER
:
187 case VHOST_USER_SET_MEM_TABLE
:
188 case VHOST_USER_GET_QUEUE_NUM
:
195 /* most non-init callers ignore the error */
196 static int vhost_user_write(struct vhost_dev
*dev
, VhostUserMsg
*msg
,
197 int *fds
, int fd_num
)
199 CharDriverState
*chr
= dev
->opaque
;
200 int ret
, size
= VHOST_USER_HDR_SIZE
+ msg
->size
;
203 * For non-vring specific requests, like VHOST_USER_SET_MEM_TABLE,
204 * we just need send it once in the first time. For later such
205 * request, we just ignore it.
207 if (vhost_user_one_time_request(msg
->request
) && dev
->vq_index
!= 0) {
211 if (qemu_chr_fe_set_msgfds(chr
, fds
, fd_num
) < 0) {
212 error_report("Failed to set msg fds.");
216 ret
= qemu_chr_fe_write_all(chr
, (const uint8_t *) msg
, size
);
218 error_report("Failed to write msg."
219 " Wrote %d instead of %d.", ret
, size
);
226 static int vhost_user_set_log_base(struct vhost_dev
*dev
, uint64_t base
,
227 struct vhost_log
*log
)
229 int fds
[VHOST_MEMORY_MAX_NREGIONS
];
231 bool shmfd
= virtio_has_feature(dev
->protocol_features
,
232 VHOST_USER_PROTOCOL_F_LOG_SHMFD
);
234 .request
= VHOST_USER_SET_LOG_BASE
,
235 .flags
= VHOST_USER_VERSION
,
236 .payload
.log
.mmap_size
= log
->size
* sizeof(*(log
->log
)),
237 .payload
.log
.mmap_offset
= 0,
238 .size
= sizeof(msg
.payload
.log
),
241 if (shmfd
&& log
->fd
!= -1) {
242 fds
[fd_num
++] = log
->fd
;
245 if (vhost_user_write(dev
, &msg
, fds
, fd_num
) < 0) {
251 if (vhost_user_read(dev
, &msg
) < 0) {
255 if (msg
.request
!= VHOST_USER_SET_LOG_BASE
) {
256 error_report("Received unexpected msg type. "
257 "Expected %d received %d",
258 VHOST_USER_SET_LOG_BASE
, msg
.request
);
266 static int vhost_user_set_mem_table(struct vhost_dev
*dev
,
267 struct vhost_memory
*mem
)
269 int fds
[VHOST_MEMORY_MAX_NREGIONS
];
272 bool reply_supported
= virtio_has_feature(dev
->protocol_features
,
273 VHOST_USER_PROTOCOL_F_REPLY_ACK
);
276 .request
= VHOST_USER_SET_MEM_TABLE
,
277 .flags
= VHOST_USER_VERSION
,
280 if (reply_supported
) {
281 msg
.flags
|= VHOST_USER_NEED_REPLY_MASK
;
284 for (i
= 0; i
< dev
->mem
->nregions
; ++i
) {
285 struct vhost_memory_region
*reg
= dev
->mem
->regions
+ i
;
289 assert((uintptr_t)reg
->userspace_addr
== reg
->userspace_addr
);
290 mr
= memory_region_from_host((void *)(uintptr_t)reg
->userspace_addr
,
292 fd
= memory_region_get_fd(mr
);
294 msg
.payload
.memory
.regions
[fd_num
].userspace_addr
= reg
->userspace_addr
;
295 msg
.payload
.memory
.regions
[fd_num
].memory_size
= reg
->memory_size
;
296 msg
.payload
.memory
.regions
[fd_num
].guest_phys_addr
= reg
->guest_phys_addr
;
297 msg
.payload
.memory
.regions
[fd_num
].mmap_offset
= offset
;
298 assert(fd_num
< VHOST_MEMORY_MAX_NREGIONS
);
303 msg
.payload
.memory
.nregions
= fd_num
;
306 error_report("Failed initializing vhost-user memory map, "
307 "consider using -object memory-backend-file share=on");
311 msg
.size
= sizeof(msg
.payload
.memory
.nregions
);
312 msg
.size
+= sizeof(msg
.payload
.memory
.padding
);
313 msg
.size
+= fd_num
* sizeof(VhostUserMemoryRegion
);
315 if (vhost_user_write(dev
, &msg
, fds
, fd_num
) < 0) {
319 if (reply_supported
) {
320 return process_message_reply(dev
, msg
.request
);
326 static int vhost_user_set_vring_addr(struct vhost_dev
*dev
,
327 struct vhost_vring_addr
*addr
)
330 .request
= VHOST_USER_SET_VRING_ADDR
,
331 .flags
= VHOST_USER_VERSION
,
332 .payload
.addr
= *addr
,
333 .size
= sizeof(msg
.payload
.addr
),
336 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
343 static int vhost_user_set_vring_endian(struct vhost_dev
*dev
,
344 struct vhost_vring_state
*ring
)
346 error_report("vhost-user trying to send unhandled ioctl");
350 static int vhost_set_vring(struct vhost_dev
*dev
,
351 unsigned long int request
,
352 struct vhost_vring_state
*ring
)
356 .flags
= VHOST_USER_VERSION
,
357 .payload
.state
= *ring
,
358 .size
= sizeof(msg
.payload
.state
),
361 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
368 static int vhost_user_set_vring_num(struct vhost_dev
*dev
,
369 struct vhost_vring_state
*ring
)
371 return vhost_set_vring(dev
, VHOST_USER_SET_VRING_NUM
, ring
);
374 static int vhost_user_set_vring_base(struct vhost_dev
*dev
,
375 struct vhost_vring_state
*ring
)
377 return vhost_set_vring(dev
, VHOST_USER_SET_VRING_BASE
, ring
);
380 static int vhost_user_set_vring_enable(struct vhost_dev
*dev
, int enable
)
384 if (!virtio_has_feature(dev
->features
, VHOST_USER_F_PROTOCOL_FEATURES
)) {
388 for (i
= 0; i
< dev
->nvqs
; ++i
) {
389 struct vhost_vring_state state
= {
390 .index
= dev
->vq_index
+ i
,
394 vhost_set_vring(dev
, VHOST_USER_SET_VRING_ENABLE
, &state
);
400 static int vhost_user_get_vring_base(struct vhost_dev
*dev
,
401 struct vhost_vring_state
*ring
)
404 .request
= VHOST_USER_GET_VRING_BASE
,
405 .flags
= VHOST_USER_VERSION
,
406 .payload
.state
= *ring
,
407 .size
= sizeof(msg
.payload
.state
),
410 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
414 if (vhost_user_read(dev
, &msg
) < 0) {
418 if (msg
.request
!= VHOST_USER_GET_VRING_BASE
) {
419 error_report("Received unexpected msg type. Expected %d received %d",
420 VHOST_USER_GET_VRING_BASE
, msg
.request
);
424 if (msg
.size
!= sizeof(msg
.payload
.state
)) {
425 error_report("Received bad msg size.");
429 *ring
= msg
.payload
.state
;
434 static int vhost_set_vring_file(struct vhost_dev
*dev
,
435 VhostUserRequest request
,
436 struct vhost_vring_file
*file
)
438 int fds
[VHOST_MEMORY_MAX_NREGIONS
];
442 .flags
= VHOST_USER_VERSION
,
443 .payload
.u64
= file
->index
& VHOST_USER_VRING_IDX_MASK
,
444 .size
= sizeof(msg
.payload
.u64
),
447 if (ioeventfd_enabled() && file
->fd
> 0) {
448 fds
[fd_num
++] = file
->fd
;
450 msg
.payload
.u64
|= VHOST_USER_VRING_NOFD_MASK
;
453 if (vhost_user_write(dev
, &msg
, fds
, fd_num
) < 0) {
460 static int vhost_user_set_vring_kick(struct vhost_dev
*dev
,
461 struct vhost_vring_file
*file
)
463 return vhost_set_vring_file(dev
, VHOST_USER_SET_VRING_KICK
, file
);
466 static int vhost_user_set_vring_call(struct vhost_dev
*dev
,
467 struct vhost_vring_file
*file
)
469 return vhost_set_vring_file(dev
, VHOST_USER_SET_VRING_CALL
, file
);
472 static int vhost_user_set_u64(struct vhost_dev
*dev
, int request
, uint64_t u64
)
476 .flags
= VHOST_USER_VERSION
,
478 .size
= sizeof(msg
.payload
.u64
),
481 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
488 static int vhost_user_set_features(struct vhost_dev
*dev
,
491 return vhost_user_set_u64(dev
, VHOST_USER_SET_FEATURES
, features
);
494 static int vhost_user_set_protocol_features(struct vhost_dev
*dev
,
497 return vhost_user_set_u64(dev
, VHOST_USER_SET_PROTOCOL_FEATURES
, features
);
500 static int vhost_user_get_u64(struct vhost_dev
*dev
, int request
, uint64_t *u64
)
504 .flags
= VHOST_USER_VERSION
,
507 if (vhost_user_one_time_request(request
) && dev
->vq_index
!= 0) {
511 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
515 if (vhost_user_read(dev
, &msg
) < 0) {
519 if (msg
.request
!= request
) {
520 error_report("Received unexpected msg type. Expected %d received %d",
521 request
, msg
.request
);
525 if (msg
.size
!= sizeof(msg
.payload
.u64
)) {
526 error_report("Received bad msg size.");
530 *u64
= msg
.payload
.u64
;
535 static int vhost_user_get_features(struct vhost_dev
*dev
, uint64_t *features
)
537 return vhost_user_get_u64(dev
, VHOST_USER_GET_FEATURES
, features
);
540 static int vhost_user_set_owner(struct vhost_dev
*dev
)
543 .request
= VHOST_USER_SET_OWNER
,
544 .flags
= VHOST_USER_VERSION
,
547 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
554 static int vhost_user_reset_device(struct vhost_dev
*dev
)
557 .request
= VHOST_USER_RESET_OWNER
,
558 .flags
= VHOST_USER_VERSION
,
561 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
568 static int vhost_user_init(struct vhost_dev
*dev
, void *opaque
)
573 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
575 dev
->opaque
= opaque
;
577 err
= vhost_user_get_features(dev
, &features
);
582 if (virtio_has_feature(features
, VHOST_USER_F_PROTOCOL_FEATURES
)) {
583 dev
->backend_features
|= 1ULL << VHOST_USER_F_PROTOCOL_FEATURES
;
585 err
= vhost_user_get_u64(dev
, VHOST_USER_GET_PROTOCOL_FEATURES
,
591 dev
->protocol_features
= features
& VHOST_USER_PROTOCOL_FEATURE_MASK
;
592 err
= vhost_user_set_protocol_features(dev
, dev
->protocol_features
);
597 /* query the max queues we support if backend supports Multiple Queue */
598 if (dev
->protocol_features
& (1ULL << VHOST_USER_PROTOCOL_F_MQ
)) {
599 err
= vhost_user_get_u64(dev
, VHOST_USER_GET_QUEUE_NUM
,
607 if (dev
->migration_blocker
== NULL
&&
608 !virtio_has_feature(dev
->protocol_features
,
609 VHOST_USER_PROTOCOL_F_LOG_SHMFD
)) {
610 error_setg(&dev
->migration_blocker
,
611 "Migration disabled: vhost-user backend lacks "
612 "VHOST_USER_PROTOCOL_F_LOG_SHMFD feature.");
618 static int vhost_user_cleanup(struct vhost_dev
*dev
)
620 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
627 static int vhost_user_get_vq_index(struct vhost_dev
*dev
, int idx
)
629 assert(idx
>= dev
->vq_index
&& idx
< dev
->vq_index
+ dev
->nvqs
);
634 static int vhost_user_memslots_limit(struct vhost_dev
*dev
)
636 return VHOST_MEMORY_MAX_NREGIONS
;
639 static bool vhost_user_requires_shm_log(struct vhost_dev
*dev
)
641 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
643 return virtio_has_feature(dev
->protocol_features
,
644 VHOST_USER_PROTOCOL_F_LOG_SHMFD
);
647 static int vhost_user_migration_done(struct vhost_dev
*dev
, char* mac_addr
)
649 VhostUserMsg msg
= { 0 };
651 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
653 /* If guest supports GUEST_ANNOUNCE do nothing */
654 if (virtio_has_feature(dev
->acked_features
, VIRTIO_NET_F_GUEST_ANNOUNCE
)) {
658 /* if backend supports VHOST_USER_PROTOCOL_F_RARP ask it to send the RARP */
659 if (virtio_has_feature(dev
->protocol_features
,
660 VHOST_USER_PROTOCOL_F_RARP
)) {
661 msg
.request
= VHOST_USER_SEND_RARP
;
662 msg
.flags
= VHOST_USER_VERSION
;
663 memcpy((char *)&msg
.payload
.u64
, mac_addr
, 6);
664 msg
.size
= sizeof(msg
.payload
.u64
);
666 return vhost_user_write(dev
, &msg
, NULL
, 0);
671 static bool vhost_user_can_merge(struct vhost_dev
*dev
,
672 uint64_t start1
, uint64_t size1
,
673 uint64_t start2
, uint64_t size2
)
679 mr
= memory_region_from_host((void *)(uintptr_t)start1
, &offset
);
680 mfd
= memory_region_get_fd(mr
);
682 mr
= memory_region_from_host((void *)(uintptr_t)start2
, &offset
);
683 rfd
= memory_region_get_fd(mr
);
688 const VhostOps user_ops
= {
689 .backend_type
= VHOST_BACKEND_TYPE_USER
,
690 .vhost_backend_init
= vhost_user_init
,
691 .vhost_backend_cleanup
= vhost_user_cleanup
,
692 .vhost_backend_memslots_limit
= vhost_user_memslots_limit
,
693 .vhost_set_log_base
= vhost_user_set_log_base
,
694 .vhost_set_mem_table
= vhost_user_set_mem_table
,
695 .vhost_set_vring_addr
= vhost_user_set_vring_addr
,
696 .vhost_set_vring_endian
= vhost_user_set_vring_endian
,
697 .vhost_set_vring_num
= vhost_user_set_vring_num
,
698 .vhost_set_vring_base
= vhost_user_set_vring_base
,
699 .vhost_get_vring_base
= vhost_user_get_vring_base
,
700 .vhost_set_vring_kick
= vhost_user_set_vring_kick
,
701 .vhost_set_vring_call
= vhost_user_set_vring_call
,
702 .vhost_set_features
= vhost_user_set_features
,
703 .vhost_get_features
= vhost_user_get_features
,
704 .vhost_set_owner
= vhost_user_set_owner
,
705 .vhost_reset_device
= vhost_user_reset_device
,
706 .vhost_get_vq_index
= vhost_user_get_vq_index
,
707 .vhost_set_vring_enable
= vhost_user_set_vring_enable
,
708 .vhost_requires_shm_log
= vhost_user_requires_shm_log
,
709 .vhost_migration_done
= vhost_user_migration_done
,
710 .vhost_backend_can_merge
= vhost_user_can_merge
,