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"
21 #include <sys/ioctl.h>
22 #include <sys/socket.h>
24 #include <linux/vhost.h>
26 #define VHOST_MEMORY_MAX_NREGIONS 8
27 #define VHOST_USER_F_PROTOCOL_FEATURES 30
29 enum VhostUserProtocolFeature
{
30 VHOST_USER_PROTOCOL_F_MQ
= 0,
31 VHOST_USER_PROTOCOL_F_LOG_SHMFD
= 1,
32 VHOST_USER_PROTOCOL_F_RARP
= 2,
33 VHOST_USER_PROTOCOL_F_REPLY_ACK
= 3,
34 VHOST_USER_PROTOCOL_F_NET_MTU
= 4,
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,
62 VHOST_USER_NET_SET_MTU
= 20,
66 typedef struct VhostUserMemoryRegion
{
67 uint64_t guest_phys_addr
;
69 uint64_t userspace_addr
;
71 } VhostUserMemoryRegion
;
73 typedef struct VhostUserMemory
{
76 VhostUserMemoryRegion regions
[VHOST_MEMORY_MAX_NREGIONS
];
79 typedef struct VhostUserLog
{
84 typedef struct VhostUserMsg
{
85 VhostUserRequest request
;
87 #define VHOST_USER_VERSION_MASK (0x3)
88 #define VHOST_USER_REPLY_MASK (0x1<<2)
89 #define VHOST_USER_NEED_REPLY_MASK (0x1 << 3)
91 uint32_t size
; /* the following payload size */
93 #define VHOST_USER_VRING_IDX_MASK (0xff)
94 #define VHOST_USER_VRING_NOFD_MASK (0x1<<8)
96 struct vhost_vring_state state
;
97 struct vhost_vring_addr addr
;
98 VhostUserMemory memory
;
101 } QEMU_PACKED VhostUserMsg
;
103 static VhostUserMsg m
__attribute__ ((unused
));
104 #define VHOST_USER_HDR_SIZE (sizeof(m.request) \
108 #define VHOST_USER_PAYLOAD_SIZE (sizeof(m) - VHOST_USER_HDR_SIZE)
110 /* The version of the protocol we support */
111 #define VHOST_USER_VERSION (0x1)
117 static bool ioeventfd_enabled(void)
119 return kvm_enabled() && kvm_eventfds_enabled();
122 static int vhost_user_read(struct vhost_dev
*dev
, VhostUserMsg
*msg
)
124 struct vhost_user
*u
= dev
->opaque
;
125 CharBackend
*chr
= u
->chr
;
126 uint8_t *p
= (uint8_t *) msg
;
127 int r
, size
= VHOST_USER_HDR_SIZE
;
129 r
= qemu_chr_fe_read_all(chr
, p
, size
);
131 error_report("Failed to read msg header. Read %d instead of %d."
132 " Original request %d.", r
, size
, msg
->request
);
136 /* validate received flags */
137 if (msg
->flags
!= (VHOST_USER_REPLY_MASK
| VHOST_USER_VERSION
)) {
138 error_report("Failed to read msg header."
139 " Flags 0x%x instead of 0x%x.", msg
->flags
,
140 VHOST_USER_REPLY_MASK
| VHOST_USER_VERSION
);
144 /* validate message size is sane */
145 if (msg
->size
> VHOST_USER_PAYLOAD_SIZE
) {
146 error_report("Failed to read msg header."
147 " Size %d exceeds the maximum %zu.", msg
->size
,
148 VHOST_USER_PAYLOAD_SIZE
);
153 p
+= VHOST_USER_HDR_SIZE
;
155 r
= qemu_chr_fe_read_all(chr
, p
, size
);
157 error_report("Failed to read msg payload."
158 " Read %d instead of %d.", r
, msg
->size
);
169 static int process_message_reply(struct vhost_dev
*dev
,
170 const VhostUserMsg
*msg
)
172 VhostUserMsg msg_reply
;
174 if ((msg
->flags
& VHOST_USER_NEED_REPLY_MASK
) == 0) {
178 if (vhost_user_read(dev
, &msg_reply
) < 0) {
182 if (msg_reply
.request
!= msg
->request
) {
183 error_report("Received unexpected msg type."
184 "Expected %d received %d",
185 msg
->request
, msg_reply
.request
);
189 return msg_reply
.payload
.u64
? -1 : 0;
192 static bool vhost_user_one_time_request(VhostUserRequest request
)
195 case VHOST_USER_SET_OWNER
:
196 case VHOST_USER_RESET_OWNER
:
197 case VHOST_USER_SET_MEM_TABLE
:
198 case VHOST_USER_GET_QUEUE_NUM
:
199 case VHOST_USER_NET_SET_MTU
:
206 /* most non-init callers ignore the error */
207 static int vhost_user_write(struct vhost_dev
*dev
, VhostUserMsg
*msg
,
208 int *fds
, int fd_num
)
210 struct vhost_user
*u
= dev
->opaque
;
211 CharBackend
*chr
= u
->chr
;
212 int ret
, size
= VHOST_USER_HDR_SIZE
+ msg
->size
;
215 * For non-vring specific requests, like VHOST_USER_SET_MEM_TABLE,
216 * we just need send it once in the first time. For later such
217 * request, we just ignore it.
219 if (vhost_user_one_time_request(msg
->request
) && dev
->vq_index
!= 0) {
220 msg
->flags
&= ~VHOST_USER_NEED_REPLY_MASK
;
224 if (qemu_chr_fe_set_msgfds(chr
, fds
, fd_num
) < 0) {
225 error_report("Failed to set msg fds.");
229 ret
= qemu_chr_fe_write_all(chr
, (const uint8_t *) msg
, size
);
231 error_report("Failed to write msg."
232 " Wrote %d instead of %d.", ret
, size
);
239 static int vhost_user_set_log_base(struct vhost_dev
*dev
, uint64_t base
,
240 struct vhost_log
*log
)
242 int fds
[VHOST_MEMORY_MAX_NREGIONS
];
244 bool shmfd
= virtio_has_feature(dev
->protocol_features
,
245 VHOST_USER_PROTOCOL_F_LOG_SHMFD
);
247 .request
= VHOST_USER_SET_LOG_BASE
,
248 .flags
= VHOST_USER_VERSION
,
249 .payload
.log
.mmap_size
= log
->size
* sizeof(*(log
->log
)),
250 .payload
.log
.mmap_offset
= 0,
251 .size
= sizeof(msg
.payload
.log
),
254 if (shmfd
&& log
->fd
!= -1) {
255 fds
[fd_num
++] = log
->fd
;
258 if (vhost_user_write(dev
, &msg
, fds
, fd_num
) < 0) {
264 if (vhost_user_read(dev
, &msg
) < 0) {
268 if (msg
.request
!= VHOST_USER_SET_LOG_BASE
) {
269 error_report("Received unexpected msg type. "
270 "Expected %d received %d",
271 VHOST_USER_SET_LOG_BASE
, msg
.request
);
279 static int vhost_user_set_mem_table(struct vhost_dev
*dev
,
280 struct vhost_memory
*mem
)
282 int fds
[VHOST_MEMORY_MAX_NREGIONS
];
285 bool reply_supported
= virtio_has_feature(dev
->protocol_features
,
286 VHOST_USER_PROTOCOL_F_REPLY_ACK
);
289 .request
= VHOST_USER_SET_MEM_TABLE
,
290 .flags
= VHOST_USER_VERSION
,
293 if (reply_supported
) {
294 msg
.flags
|= VHOST_USER_NEED_REPLY_MASK
;
297 for (i
= 0; i
< dev
->mem
->nregions
; ++i
) {
298 struct vhost_memory_region
*reg
= dev
->mem
->regions
+ i
;
302 assert((uintptr_t)reg
->userspace_addr
== reg
->userspace_addr
);
303 mr
= memory_region_from_host((void *)(uintptr_t)reg
->userspace_addr
,
305 fd
= memory_region_get_fd(mr
);
307 msg
.payload
.memory
.regions
[fd_num
].userspace_addr
= reg
->userspace_addr
;
308 msg
.payload
.memory
.regions
[fd_num
].memory_size
= reg
->memory_size
;
309 msg
.payload
.memory
.regions
[fd_num
].guest_phys_addr
= reg
->guest_phys_addr
;
310 msg
.payload
.memory
.regions
[fd_num
].mmap_offset
= offset
;
311 assert(fd_num
< VHOST_MEMORY_MAX_NREGIONS
);
316 msg
.payload
.memory
.nregions
= fd_num
;
319 error_report("Failed initializing vhost-user memory map, "
320 "consider using -object memory-backend-file share=on");
324 msg
.size
= sizeof(msg
.payload
.memory
.nregions
);
325 msg
.size
+= sizeof(msg
.payload
.memory
.padding
);
326 msg
.size
+= fd_num
* sizeof(VhostUserMemoryRegion
);
328 if (vhost_user_write(dev
, &msg
, fds
, fd_num
) < 0) {
332 if (reply_supported
) {
333 return process_message_reply(dev
, &msg
);
339 static int vhost_user_set_vring_addr(struct vhost_dev
*dev
,
340 struct vhost_vring_addr
*addr
)
343 .request
= VHOST_USER_SET_VRING_ADDR
,
344 .flags
= VHOST_USER_VERSION
,
345 .payload
.addr
= *addr
,
346 .size
= sizeof(msg
.payload
.addr
),
349 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
356 static int vhost_user_set_vring_endian(struct vhost_dev
*dev
,
357 struct vhost_vring_state
*ring
)
359 error_report("vhost-user trying to send unhandled ioctl");
363 static int vhost_set_vring(struct vhost_dev
*dev
,
364 unsigned long int request
,
365 struct vhost_vring_state
*ring
)
369 .flags
= VHOST_USER_VERSION
,
370 .payload
.state
= *ring
,
371 .size
= sizeof(msg
.payload
.state
),
374 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
381 static int vhost_user_set_vring_num(struct vhost_dev
*dev
,
382 struct vhost_vring_state
*ring
)
384 return vhost_set_vring(dev
, VHOST_USER_SET_VRING_NUM
, ring
);
387 static int vhost_user_set_vring_base(struct vhost_dev
*dev
,
388 struct vhost_vring_state
*ring
)
390 return vhost_set_vring(dev
, VHOST_USER_SET_VRING_BASE
, ring
);
393 static int vhost_user_set_vring_enable(struct vhost_dev
*dev
, int enable
)
397 if (!virtio_has_feature(dev
->features
, VHOST_USER_F_PROTOCOL_FEATURES
)) {
401 for (i
= 0; i
< dev
->nvqs
; ++i
) {
402 struct vhost_vring_state state
= {
403 .index
= dev
->vq_index
+ i
,
407 vhost_set_vring(dev
, VHOST_USER_SET_VRING_ENABLE
, &state
);
413 static int vhost_user_get_vring_base(struct vhost_dev
*dev
,
414 struct vhost_vring_state
*ring
)
417 .request
= VHOST_USER_GET_VRING_BASE
,
418 .flags
= VHOST_USER_VERSION
,
419 .payload
.state
= *ring
,
420 .size
= sizeof(msg
.payload
.state
),
423 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
427 if (vhost_user_read(dev
, &msg
) < 0) {
431 if (msg
.request
!= VHOST_USER_GET_VRING_BASE
) {
432 error_report("Received unexpected msg type. Expected %d received %d",
433 VHOST_USER_GET_VRING_BASE
, msg
.request
);
437 if (msg
.size
!= sizeof(msg
.payload
.state
)) {
438 error_report("Received bad msg size.");
442 *ring
= msg
.payload
.state
;
447 static int vhost_set_vring_file(struct vhost_dev
*dev
,
448 VhostUserRequest request
,
449 struct vhost_vring_file
*file
)
451 int fds
[VHOST_MEMORY_MAX_NREGIONS
];
455 .flags
= VHOST_USER_VERSION
,
456 .payload
.u64
= file
->index
& VHOST_USER_VRING_IDX_MASK
,
457 .size
= sizeof(msg
.payload
.u64
),
460 if (ioeventfd_enabled() && file
->fd
> 0) {
461 fds
[fd_num
++] = file
->fd
;
463 msg
.payload
.u64
|= VHOST_USER_VRING_NOFD_MASK
;
466 if (vhost_user_write(dev
, &msg
, fds
, fd_num
) < 0) {
473 static int vhost_user_set_vring_kick(struct vhost_dev
*dev
,
474 struct vhost_vring_file
*file
)
476 return vhost_set_vring_file(dev
, VHOST_USER_SET_VRING_KICK
, file
);
479 static int vhost_user_set_vring_call(struct vhost_dev
*dev
,
480 struct vhost_vring_file
*file
)
482 return vhost_set_vring_file(dev
, VHOST_USER_SET_VRING_CALL
, file
);
485 static int vhost_user_set_u64(struct vhost_dev
*dev
, int request
, uint64_t u64
)
489 .flags
= VHOST_USER_VERSION
,
491 .size
= sizeof(msg
.payload
.u64
),
494 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
501 static int vhost_user_set_features(struct vhost_dev
*dev
,
504 return vhost_user_set_u64(dev
, VHOST_USER_SET_FEATURES
, features
);
507 static int vhost_user_set_protocol_features(struct vhost_dev
*dev
,
510 return vhost_user_set_u64(dev
, VHOST_USER_SET_PROTOCOL_FEATURES
, features
);
513 static int vhost_user_get_u64(struct vhost_dev
*dev
, int request
, uint64_t *u64
)
517 .flags
= VHOST_USER_VERSION
,
520 if (vhost_user_one_time_request(request
) && dev
->vq_index
!= 0) {
524 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
528 if (vhost_user_read(dev
, &msg
) < 0) {
532 if (msg
.request
!= request
) {
533 error_report("Received unexpected msg type. Expected %d received %d",
534 request
, msg
.request
);
538 if (msg
.size
!= sizeof(msg
.payload
.u64
)) {
539 error_report("Received bad msg size.");
543 *u64
= msg
.payload
.u64
;
548 static int vhost_user_get_features(struct vhost_dev
*dev
, uint64_t *features
)
550 return vhost_user_get_u64(dev
, VHOST_USER_GET_FEATURES
, features
);
553 static int vhost_user_set_owner(struct vhost_dev
*dev
)
556 .request
= VHOST_USER_SET_OWNER
,
557 .flags
= VHOST_USER_VERSION
,
560 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
567 static int vhost_user_reset_device(struct vhost_dev
*dev
)
570 .request
= VHOST_USER_RESET_OWNER
,
571 .flags
= VHOST_USER_VERSION
,
574 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
581 static int vhost_user_init(struct vhost_dev
*dev
, void *opaque
)
584 struct vhost_user
*u
;
587 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
589 u
= g_new0(struct vhost_user
, 1);
593 err
= vhost_user_get_features(dev
, &features
);
598 if (virtio_has_feature(features
, VHOST_USER_F_PROTOCOL_FEATURES
)) {
599 dev
->backend_features
|= 1ULL << VHOST_USER_F_PROTOCOL_FEATURES
;
601 err
= vhost_user_get_u64(dev
, VHOST_USER_GET_PROTOCOL_FEATURES
,
607 dev
->protocol_features
= features
& VHOST_USER_PROTOCOL_FEATURE_MASK
;
608 err
= vhost_user_set_protocol_features(dev
, dev
->protocol_features
);
613 /* query the max queues we support if backend supports Multiple Queue */
614 if (dev
->protocol_features
& (1ULL << VHOST_USER_PROTOCOL_F_MQ
)) {
615 err
= vhost_user_get_u64(dev
, VHOST_USER_GET_QUEUE_NUM
,
623 if (dev
->migration_blocker
== NULL
&&
624 !virtio_has_feature(dev
->protocol_features
,
625 VHOST_USER_PROTOCOL_F_LOG_SHMFD
)) {
626 error_setg(&dev
->migration_blocker
,
627 "Migration disabled: vhost-user backend lacks "
628 "VHOST_USER_PROTOCOL_F_LOG_SHMFD feature.");
634 static int vhost_user_cleanup(struct vhost_dev
*dev
)
636 struct vhost_user
*u
;
638 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
647 static int vhost_user_get_vq_index(struct vhost_dev
*dev
, int idx
)
649 assert(idx
>= dev
->vq_index
&& idx
< dev
->vq_index
+ dev
->nvqs
);
654 static int vhost_user_memslots_limit(struct vhost_dev
*dev
)
656 return VHOST_MEMORY_MAX_NREGIONS
;
659 static bool vhost_user_requires_shm_log(struct vhost_dev
*dev
)
661 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
663 return virtio_has_feature(dev
->protocol_features
,
664 VHOST_USER_PROTOCOL_F_LOG_SHMFD
);
667 static int vhost_user_migration_done(struct vhost_dev
*dev
, char* mac_addr
)
669 VhostUserMsg msg
= { 0 };
671 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
673 /* If guest supports GUEST_ANNOUNCE do nothing */
674 if (virtio_has_feature(dev
->acked_features
, VIRTIO_NET_F_GUEST_ANNOUNCE
)) {
678 /* if backend supports VHOST_USER_PROTOCOL_F_RARP ask it to send the RARP */
679 if (virtio_has_feature(dev
->protocol_features
,
680 VHOST_USER_PROTOCOL_F_RARP
)) {
681 msg
.request
= VHOST_USER_SEND_RARP
;
682 msg
.flags
= VHOST_USER_VERSION
;
683 memcpy((char *)&msg
.payload
.u64
, mac_addr
, 6);
684 msg
.size
= sizeof(msg
.payload
.u64
);
686 return vhost_user_write(dev
, &msg
, NULL
, 0);
691 static bool vhost_user_can_merge(struct vhost_dev
*dev
,
692 uint64_t start1
, uint64_t size1
,
693 uint64_t start2
, uint64_t size2
)
699 mr
= memory_region_from_host((void *)(uintptr_t)start1
, &offset
);
700 mfd
= memory_region_get_fd(mr
);
702 mr
= memory_region_from_host((void *)(uintptr_t)start2
, &offset
);
703 rfd
= memory_region_get_fd(mr
);
708 static int vhost_user_net_set_mtu(struct vhost_dev
*dev
, uint16_t mtu
)
711 bool reply_supported
= virtio_has_feature(dev
->protocol_features
,
712 VHOST_USER_PROTOCOL_F_REPLY_ACK
);
714 if (!(dev
->protocol_features
& (1ULL << VHOST_USER_PROTOCOL_F_NET_MTU
))) {
718 msg
.request
= VHOST_USER_NET_SET_MTU
;
719 msg
.payload
.u64
= mtu
;
720 msg
.size
= sizeof(msg
.payload
.u64
);
721 msg
.flags
= VHOST_USER_VERSION
;
722 if (reply_supported
) {
723 msg
.flags
|= VHOST_USER_NEED_REPLY_MASK
;
726 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
730 /* If reply_ack supported, slave has to ack specified MTU is valid */
731 if (reply_supported
) {
732 return process_message_reply(dev
, &msg
);
738 const VhostOps user_ops
= {
739 .backend_type
= VHOST_BACKEND_TYPE_USER
,
740 .vhost_backend_init
= vhost_user_init
,
741 .vhost_backend_cleanup
= vhost_user_cleanup
,
742 .vhost_backend_memslots_limit
= vhost_user_memslots_limit
,
743 .vhost_set_log_base
= vhost_user_set_log_base
,
744 .vhost_set_mem_table
= vhost_user_set_mem_table
,
745 .vhost_set_vring_addr
= vhost_user_set_vring_addr
,
746 .vhost_set_vring_endian
= vhost_user_set_vring_endian
,
747 .vhost_set_vring_num
= vhost_user_set_vring_num
,
748 .vhost_set_vring_base
= vhost_user_set_vring_base
,
749 .vhost_get_vring_base
= vhost_user_get_vring_base
,
750 .vhost_set_vring_kick
= vhost_user_set_vring_kick
,
751 .vhost_set_vring_call
= vhost_user_set_vring_call
,
752 .vhost_set_features
= vhost_user_set_features
,
753 .vhost_get_features
= vhost_user_get_features
,
754 .vhost_set_owner
= vhost_user_set_owner
,
755 .vhost_reset_device
= vhost_user_reset_device
,
756 .vhost_get_vq_index
= vhost_user_get_vq_index
,
757 .vhost_set_vring_enable
= vhost_user_set_vring_enable
,
758 .vhost_requires_shm_log
= vhost_user_requires_shm_log
,
759 .vhost_migration_done
= vhost_user_migration_done
,
760 .vhost_backend_can_merge
= vhost_user_can_merge
,
761 .vhost_net_set_mtu
= vhost_user_net_set_mtu
,