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_vring_addr(struct vhost_dev
*dev
,
267 struct vhost_vring_addr
*addr
)
270 .request
= VHOST_USER_SET_VRING_ADDR
,
271 .flags
= VHOST_USER_VERSION
,
272 .payload
.addr
= *addr
,
273 .size
= sizeof(msg
.payload
.addr
),
276 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
283 static int vhost_user_set_vring_endian(struct vhost_dev
*dev
,
284 struct vhost_vring_state
*ring
)
286 error_report("vhost-user trying to send unhandled ioctl");
290 static int vhost_set_vring(struct vhost_dev
*dev
,
291 unsigned long int request
,
292 struct vhost_vring_state
*ring
)
296 .flags
= VHOST_USER_VERSION
,
297 .payload
.state
= *ring
,
298 .size
= sizeof(msg
.payload
.state
),
301 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
308 static int vhost_user_set_vring_num(struct vhost_dev
*dev
,
309 struct vhost_vring_state
*ring
)
311 return vhost_set_vring(dev
, VHOST_USER_SET_VRING_NUM
, ring
);
314 static int vhost_user_set_vring_base(struct vhost_dev
*dev
,
315 struct vhost_vring_state
*ring
)
317 return vhost_set_vring(dev
, VHOST_USER_SET_VRING_BASE
, ring
);
320 static int vhost_user_set_vring_enable(struct vhost_dev
*dev
, int enable
)
324 if (!virtio_has_feature(dev
->features
, VHOST_USER_F_PROTOCOL_FEATURES
)) {
328 for (i
= 0; i
< dev
->nvqs
; ++i
) {
329 struct vhost_vring_state state
= {
330 .index
= dev
->vq_index
+ i
,
334 vhost_set_vring(dev
, VHOST_USER_SET_VRING_ENABLE
, &state
);
340 static int vhost_user_get_vring_base(struct vhost_dev
*dev
,
341 struct vhost_vring_state
*ring
)
344 .request
= VHOST_USER_GET_VRING_BASE
,
345 .flags
= VHOST_USER_VERSION
,
346 .payload
.state
= *ring
,
347 .size
= sizeof(msg
.payload
.state
),
350 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
354 if (vhost_user_read(dev
, &msg
) < 0) {
358 if (msg
.request
!= VHOST_USER_GET_VRING_BASE
) {
359 error_report("Received unexpected msg type. Expected %d received %d",
360 VHOST_USER_GET_VRING_BASE
, msg
.request
);
364 if (msg
.size
!= sizeof(msg
.payload
.state
)) {
365 error_report("Received bad msg size.");
369 *ring
= msg
.payload
.state
;
374 static int vhost_set_vring_file(struct vhost_dev
*dev
,
375 VhostUserRequest request
,
376 struct vhost_vring_file
*file
)
378 int fds
[VHOST_MEMORY_MAX_NREGIONS
];
382 .flags
= VHOST_USER_VERSION
,
383 .payload
.u64
= file
->index
& VHOST_USER_VRING_IDX_MASK
,
384 .size
= sizeof(msg
.payload
.u64
),
387 if (ioeventfd_enabled() && file
->fd
> 0) {
388 fds
[fd_num
++] = file
->fd
;
390 msg
.payload
.u64
|= VHOST_USER_VRING_NOFD_MASK
;
393 if (vhost_user_write(dev
, &msg
, fds
, fd_num
) < 0) {
400 static int vhost_user_set_vring_kick(struct vhost_dev
*dev
,
401 struct vhost_vring_file
*file
)
403 return vhost_set_vring_file(dev
, VHOST_USER_SET_VRING_KICK
, file
);
406 static int vhost_user_set_vring_call(struct vhost_dev
*dev
,
407 struct vhost_vring_file
*file
)
409 return vhost_set_vring_file(dev
, VHOST_USER_SET_VRING_CALL
, file
);
412 static int vhost_user_set_u64(struct vhost_dev
*dev
, int request
, uint64_t u64
)
416 .flags
= VHOST_USER_VERSION
,
418 .size
= sizeof(msg
.payload
.u64
),
421 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
428 static int vhost_user_set_features(struct vhost_dev
*dev
,
431 return vhost_user_set_u64(dev
, VHOST_USER_SET_FEATURES
, features
);
434 static int vhost_user_set_protocol_features(struct vhost_dev
*dev
,
437 return vhost_user_set_u64(dev
, VHOST_USER_SET_PROTOCOL_FEATURES
, features
);
440 static int vhost_user_get_u64(struct vhost_dev
*dev
, int request
, uint64_t *u64
)
444 .flags
= VHOST_USER_VERSION
,
447 if (vhost_user_one_time_request(request
) && dev
->vq_index
!= 0) {
451 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
455 if (vhost_user_read(dev
, &msg
) < 0) {
459 if (msg
.request
!= request
) {
460 error_report("Received unexpected msg type. Expected %d received %d",
461 request
, msg
.request
);
465 if (msg
.size
!= sizeof(msg
.payload
.u64
)) {
466 error_report("Received bad msg size.");
470 *u64
= msg
.payload
.u64
;
475 static int vhost_user_get_features(struct vhost_dev
*dev
, uint64_t *features
)
477 return vhost_user_get_u64(dev
, VHOST_USER_GET_FEATURES
, features
);
480 static int vhost_user_set_mem_table(struct vhost_dev
*dev
,
481 struct vhost_memory
*mem
)
483 int fds
[VHOST_MEMORY_MAX_NREGIONS
];
487 bool reply_supported
= virtio_has_feature(dev
->protocol_features
,
488 VHOST_USER_PROTOCOL_F_REPLY_ACK
);
491 .request
= VHOST_USER_SET_MEM_TABLE
,
492 .flags
= VHOST_USER_VERSION
,
495 if (reply_supported
) {
496 msg
.flags
|= VHOST_USER_NEED_REPLY_MASK
;
499 for (i
= 0; i
< dev
->mem
->nregions
; ++i
) {
500 struct vhost_memory_region
*reg
= dev
->mem
->regions
+ i
;
504 assert((uintptr_t)reg
->userspace_addr
== reg
->userspace_addr
);
505 mr
= memory_region_from_host((void *)(uintptr_t)reg
->userspace_addr
,
507 fd
= memory_region_get_fd(mr
);
509 msg
.payload
.memory
.regions
[fd_num
].userspace_addr
510 = reg
->userspace_addr
;
511 msg
.payload
.memory
.regions
[fd_num
].memory_size
= reg
->memory_size
;
512 msg
.payload
.memory
.regions
[fd_num
].guest_phys_addr
513 = reg
->guest_phys_addr
;
514 msg
.payload
.memory
.regions
[fd_num
].mmap_offset
= offset
;
515 assert(fd_num
< VHOST_MEMORY_MAX_NREGIONS
);
520 msg
.payload
.memory
.nregions
= fd_num
;
523 error_report("Failed initializing vhost-user memory map, "
524 "consider using -object memory-backend-file share=on");
528 msg
.size
= sizeof(msg
.payload
.memory
.nregions
);
529 msg
.size
+= sizeof(msg
.payload
.memory
.padding
);
530 msg
.size
+= fd_num
* sizeof(VhostUserMemoryRegion
);
532 vhost_user_write(dev
, &msg
, fds
, fd_num
);
534 if (reply_supported
) {
535 return process_message_reply(dev
, msg
.request
);
537 /* Note: It is (yet) unknown when the client application has finished
539 * Attempt to prevent a race by sending a command that requires a reply.
541 vhost_user_get_features(dev
, &features
);
547 static int vhost_user_set_owner(struct vhost_dev
*dev
)
550 .request
= VHOST_USER_SET_OWNER
,
551 .flags
= VHOST_USER_VERSION
,
554 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
561 static int vhost_user_reset_device(struct vhost_dev
*dev
)
564 .request
= VHOST_USER_RESET_OWNER
,
565 .flags
= VHOST_USER_VERSION
,
568 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
575 static int vhost_user_init(struct vhost_dev
*dev
, void *opaque
)
580 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
582 dev
->opaque
= opaque
;
584 err
= vhost_user_get_features(dev
, &features
);
589 if (virtio_has_feature(features
, VHOST_USER_F_PROTOCOL_FEATURES
)) {
590 dev
->backend_features
|= 1ULL << VHOST_USER_F_PROTOCOL_FEATURES
;
592 err
= vhost_user_get_u64(dev
, VHOST_USER_GET_PROTOCOL_FEATURES
,
598 dev
->protocol_features
= features
& VHOST_USER_PROTOCOL_FEATURE_MASK
;
599 err
= vhost_user_set_protocol_features(dev
, dev
->protocol_features
);
604 /* query the max queues we support if backend supports Multiple Queue */
605 if (dev
->protocol_features
& (1ULL << VHOST_USER_PROTOCOL_F_MQ
)) {
606 err
= vhost_user_get_u64(dev
, VHOST_USER_GET_QUEUE_NUM
,
614 if (dev
->migration_blocker
== NULL
&&
615 !virtio_has_feature(dev
->protocol_features
,
616 VHOST_USER_PROTOCOL_F_LOG_SHMFD
)) {
617 error_setg(&dev
->migration_blocker
,
618 "Migration disabled: vhost-user backend lacks "
619 "VHOST_USER_PROTOCOL_F_LOG_SHMFD feature.");
625 static int vhost_user_cleanup(struct vhost_dev
*dev
)
627 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
634 static int vhost_user_get_vq_index(struct vhost_dev
*dev
, int idx
)
636 assert(idx
>= dev
->vq_index
&& idx
< dev
->vq_index
+ dev
->nvqs
);
641 static int vhost_user_memslots_limit(struct vhost_dev
*dev
)
643 return VHOST_MEMORY_MAX_NREGIONS
;
646 static bool vhost_user_requires_shm_log(struct vhost_dev
*dev
)
648 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
650 return virtio_has_feature(dev
->protocol_features
,
651 VHOST_USER_PROTOCOL_F_LOG_SHMFD
);
654 static int vhost_user_migration_done(struct vhost_dev
*dev
, char* mac_addr
)
656 VhostUserMsg msg
= { 0 };
658 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
660 /* If guest supports GUEST_ANNOUNCE do nothing */
661 if (virtio_has_feature(dev
->acked_features
, VIRTIO_NET_F_GUEST_ANNOUNCE
)) {
665 /* if backend supports VHOST_USER_PROTOCOL_F_RARP ask it to send the RARP */
666 if (virtio_has_feature(dev
->protocol_features
,
667 VHOST_USER_PROTOCOL_F_RARP
)) {
668 msg
.request
= VHOST_USER_SEND_RARP
;
669 msg
.flags
= VHOST_USER_VERSION
;
670 memcpy((char *)&msg
.payload
.u64
, mac_addr
, 6);
671 msg
.size
= sizeof(msg
.payload
.u64
);
673 return vhost_user_write(dev
, &msg
, NULL
, 0);
678 static bool vhost_user_can_merge(struct vhost_dev
*dev
,
679 uint64_t start1
, uint64_t size1
,
680 uint64_t start2
, uint64_t size2
)
686 mr
= memory_region_from_host((void *)(uintptr_t)start1
, &offset
);
687 mfd
= memory_region_get_fd(mr
);
689 mr
= memory_region_from_host((void *)(uintptr_t)start2
, &offset
);
690 rfd
= memory_region_get_fd(mr
);
695 const VhostOps user_ops
= {
696 .backend_type
= VHOST_BACKEND_TYPE_USER
,
697 .vhost_backend_init
= vhost_user_init
,
698 .vhost_backend_cleanup
= vhost_user_cleanup
,
699 .vhost_backend_memslots_limit
= vhost_user_memslots_limit
,
700 .vhost_set_log_base
= vhost_user_set_log_base
,
701 .vhost_set_mem_table
= vhost_user_set_mem_table
,
702 .vhost_set_vring_addr
= vhost_user_set_vring_addr
,
703 .vhost_set_vring_endian
= vhost_user_set_vring_endian
,
704 .vhost_set_vring_num
= vhost_user_set_vring_num
,
705 .vhost_set_vring_base
= vhost_user_set_vring_base
,
706 .vhost_get_vring_base
= vhost_user_get_vring_base
,
707 .vhost_set_vring_kick
= vhost_user_set_vring_kick
,
708 .vhost_set_vring_call
= vhost_user_set_vring_call
,
709 .vhost_set_features
= vhost_user_set_features
,
710 .vhost_get_features
= vhost_user_get_features
,
711 .vhost_set_owner
= vhost_user_set_owner
,
712 .vhost_reset_device
= vhost_user_reset_device
,
713 .vhost_get_vq_index
= vhost_user_get_vq_index
,
714 .vhost_set_vring_enable
= vhost_user_set_vring_enable
,
715 .vhost_requires_shm_log
= vhost_user_requires_shm_log
,
716 .vhost_migration_done
= vhost_user_migration_done
,
717 .vhost_backend_can_merge
= vhost_user_can_merge
,