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)
113 static bool ioeventfd_enabled(void)
115 return kvm_enabled() && kvm_eventfds_enabled();
118 static int vhost_user_read(struct vhost_dev
*dev
, VhostUserMsg
*msg
)
120 CharBackend
*chr
= dev
->opaque
;
121 uint8_t *p
= (uint8_t *) msg
;
122 int r
, size
= VHOST_USER_HDR_SIZE
;
124 r
= qemu_chr_fe_read_all(chr
, p
, size
);
126 error_report("Failed to read msg header. Read %d instead of %d."
127 " Original request %d.", r
, size
, msg
->request
);
131 /* validate received flags */
132 if (msg
->flags
!= (VHOST_USER_REPLY_MASK
| VHOST_USER_VERSION
)) {
133 error_report("Failed to read msg header."
134 " Flags 0x%x instead of 0x%x.", msg
->flags
,
135 VHOST_USER_REPLY_MASK
| VHOST_USER_VERSION
);
139 /* validate message size is sane */
140 if (msg
->size
> VHOST_USER_PAYLOAD_SIZE
) {
141 error_report("Failed to read msg header."
142 " Size %d exceeds the maximum %zu.", msg
->size
,
143 VHOST_USER_PAYLOAD_SIZE
);
148 p
+= VHOST_USER_HDR_SIZE
;
150 r
= qemu_chr_fe_read_all(chr
, p
, size
);
152 error_report("Failed to read msg payload."
153 " Read %d instead of %d.", r
, msg
->size
);
164 static int process_message_reply(struct vhost_dev
*dev
,
165 const VhostUserMsg
*msg
)
167 VhostUserMsg msg_reply
;
169 if ((msg
->flags
& VHOST_USER_NEED_REPLY_MASK
) == 0) {
173 if (vhost_user_read(dev
, &msg_reply
) < 0) {
177 if (msg_reply
.request
!= msg
->request
) {
178 error_report("Received unexpected msg type."
179 "Expected %d received %d",
180 msg
->request
, msg_reply
.request
);
184 return msg_reply
.payload
.u64
? -1 : 0;
187 static bool vhost_user_one_time_request(VhostUserRequest request
)
190 case VHOST_USER_SET_OWNER
:
191 case VHOST_USER_RESET_OWNER
:
192 case VHOST_USER_SET_MEM_TABLE
:
193 case VHOST_USER_GET_QUEUE_NUM
:
194 case VHOST_USER_NET_SET_MTU
:
201 /* most non-init callers ignore the error */
202 static int vhost_user_write(struct vhost_dev
*dev
, VhostUserMsg
*msg
,
203 int *fds
, int fd_num
)
205 CharBackend
*chr
= dev
->opaque
;
206 int ret
, size
= VHOST_USER_HDR_SIZE
+ msg
->size
;
209 * For non-vring specific requests, like VHOST_USER_SET_MEM_TABLE,
210 * we just need send it once in the first time. For later such
211 * request, we just ignore it.
213 if (vhost_user_one_time_request(msg
->request
) && dev
->vq_index
!= 0) {
214 msg
->flags
&= ~VHOST_USER_NEED_REPLY_MASK
;
218 if (qemu_chr_fe_set_msgfds(chr
, fds
, fd_num
) < 0) {
219 error_report("Failed to set msg fds.");
223 ret
= qemu_chr_fe_write_all(chr
, (const uint8_t *) msg
, size
);
225 error_report("Failed to write msg."
226 " Wrote %d instead of %d.", ret
, size
);
233 static int vhost_user_set_log_base(struct vhost_dev
*dev
, uint64_t base
,
234 struct vhost_log
*log
)
236 int fds
[VHOST_MEMORY_MAX_NREGIONS
];
238 bool shmfd
= virtio_has_feature(dev
->protocol_features
,
239 VHOST_USER_PROTOCOL_F_LOG_SHMFD
);
241 .request
= VHOST_USER_SET_LOG_BASE
,
242 .flags
= VHOST_USER_VERSION
,
243 .payload
.log
.mmap_size
= log
->size
* sizeof(*(log
->log
)),
244 .payload
.log
.mmap_offset
= 0,
245 .size
= sizeof(msg
.payload
.log
),
248 if (shmfd
&& log
->fd
!= -1) {
249 fds
[fd_num
++] = log
->fd
;
252 if (vhost_user_write(dev
, &msg
, fds
, fd_num
) < 0) {
258 if (vhost_user_read(dev
, &msg
) < 0) {
262 if (msg
.request
!= VHOST_USER_SET_LOG_BASE
) {
263 error_report("Received unexpected msg type. "
264 "Expected %d received %d",
265 VHOST_USER_SET_LOG_BASE
, msg
.request
);
273 static int vhost_user_set_mem_table(struct vhost_dev
*dev
,
274 struct vhost_memory
*mem
)
276 int fds
[VHOST_MEMORY_MAX_NREGIONS
];
279 bool reply_supported
= virtio_has_feature(dev
->protocol_features
,
280 VHOST_USER_PROTOCOL_F_REPLY_ACK
);
283 .request
= VHOST_USER_SET_MEM_TABLE
,
284 .flags
= VHOST_USER_VERSION
,
287 if (reply_supported
) {
288 msg
.flags
|= VHOST_USER_NEED_REPLY_MASK
;
291 for (i
= 0; i
< dev
->mem
->nregions
; ++i
) {
292 struct vhost_memory_region
*reg
= dev
->mem
->regions
+ i
;
296 assert((uintptr_t)reg
->userspace_addr
== reg
->userspace_addr
);
297 mr
= memory_region_from_host((void *)(uintptr_t)reg
->userspace_addr
,
299 fd
= memory_region_get_fd(mr
);
301 msg
.payload
.memory
.regions
[fd_num
].userspace_addr
= reg
->userspace_addr
;
302 msg
.payload
.memory
.regions
[fd_num
].memory_size
= reg
->memory_size
;
303 msg
.payload
.memory
.regions
[fd_num
].guest_phys_addr
= reg
->guest_phys_addr
;
304 msg
.payload
.memory
.regions
[fd_num
].mmap_offset
= offset
;
305 assert(fd_num
< VHOST_MEMORY_MAX_NREGIONS
);
310 msg
.payload
.memory
.nregions
= fd_num
;
313 error_report("Failed initializing vhost-user memory map, "
314 "consider using -object memory-backend-file share=on");
318 msg
.size
= sizeof(msg
.payload
.memory
.nregions
);
319 msg
.size
+= sizeof(msg
.payload
.memory
.padding
);
320 msg
.size
+= fd_num
* sizeof(VhostUserMemoryRegion
);
322 if (vhost_user_write(dev
, &msg
, fds
, fd_num
) < 0) {
326 if (reply_supported
) {
327 return process_message_reply(dev
, &msg
);
333 static int vhost_user_set_vring_addr(struct vhost_dev
*dev
,
334 struct vhost_vring_addr
*addr
)
337 .request
= VHOST_USER_SET_VRING_ADDR
,
338 .flags
= VHOST_USER_VERSION
,
339 .payload
.addr
= *addr
,
340 .size
= sizeof(msg
.payload
.addr
),
343 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
350 static int vhost_user_set_vring_endian(struct vhost_dev
*dev
,
351 struct vhost_vring_state
*ring
)
353 error_report("vhost-user trying to send unhandled ioctl");
357 static int vhost_set_vring(struct vhost_dev
*dev
,
358 unsigned long int request
,
359 struct vhost_vring_state
*ring
)
363 .flags
= VHOST_USER_VERSION
,
364 .payload
.state
= *ring
,
365 .size
= sizeof(msg
.payload
.state
),
368 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
375 static int vhost_user_set_vring_num(struct vhost_dev
*dev
,
376 struct vhost_vring_state
*ring
)
378 return vhost_set_vring(dev
, VHOST_USER_SET_VRING_NUM
, ring
);
381 static int vhost_user_set_vring_base(struct vhost_dev
*dev
,
382 struct vhost_vring_state
*ring
)
384 return vhost_set_vring(dev
, VHOST_USER_SET_VRING_BASE
, ring
);
387 static int vhost_user_set_vring_enable(struct vhost_dev
*dev
, int enable
)
391 if (!virtio_has_feature(dev
->features
, VHOST_USER_F_PROTOCOL_FEATURES
)) {
395 for (i
= 0; i
< dev
->nvqs
; ++i
) {
396 struct vhost_vring_state state
= {
397 .index
= dev
->vq_index
+ i
,
401 vhost_set_vring(dev
, VHOST_USER_SET_VRING_ENABLE
, &state
);
407 static int vhost_user_get_vring_base(struct vhost_dev
*dev
,
408 struct vhost_vring_state
*ring
)
411 .request
= VHOST_USER_GET_VRING_BASE
,
412 .flags
= VHOST_USER_VERSION
,
413 .payload
.state
= *ring
,
414 .size
= sizeof(msg
.payload
.state
),
417 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
421 if (vhost_user_read(dev
, &msg
) < 0) {
425 if (msg
.request
!= VHOST_USER_GET_VRING_BASE
) {
426 error_report("Received unexpected msg type. Expected %d received %d",
427 VHOST_USER_GET_VRING_BASE
, msg
.request
);
431 if (msg
.size
!= sizeof(msg
.payload
.state
)) {
432 error_report("Received bad msg size.");
436 *ring
= msg
.payload
.state
;
441 static int vhost_set_vring_file(struct vhost_dev
*dev
,
442 VhostUserRequest request
,
443 struct vhost_vring_file
*file
)
445 int fds
[VHOST_MEMORY_MAX_NREGIONS
];
449 .flags
= VHOST_USER_VERSION
,
450 .payload
.u64
= file
->index
& VHOST_USER_VRING_IDX_MASK
,
451 .size
= sizeof(msg
.payload
.u64
),
454 if (ioeventfd_enabled() && file
->fd
> 0) {
455 fds
[fd_num
++] = file
->fd
;
457 msg
.payload
.u64
|= VHOST_USER_VRING_NOFD_MASK
;
460 if (vhost_user_write(dev
, &msg
, fds
, fd_num
) < 0) {
467 static int vhost_user_set_vring_kick(struct vhost_dev
*dev
,
468 struct vhost_vring_file
*file
)
470 return vhost_set_vring_file(dev
, VHOST_USER_SET_VRING_KICK
, file
);
473 static int vhost_user_set_vring_call(struct vhost_dev
*dev
,
474 struct vhost_vring_file
*file
)
476 return vhost_set_vring_file(dev
, VHOST_USER_SET_VRING_CALL
, file
);
479 static int vhost_user_set_u64(struct vhost_dev
*dev
, int request
, uint64_t u64
)
483 .flags
= VHOST_USER_VERSION
,
485 .size
= sizeof(msg
.payload
.u64
),
488 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
495 static int vhost_user_set_features(struct vhost_dev
*dev
,
498 return vhost_user_set_u64(dev
, VHOST_USER_SET_FEATURES
, features
);
501 static int vhost_user_set_protocol_features(struct vhost_dev
*dev
,
504 return vhost_user_set_u64(dev
, VHOST_USER_SET_PROTOCOL_FEATURES
, features
);
507 static int vhost_user_get_u64(struct vhost_dev
*dev
, int request
, uint64_t *u64
)
511 .flags
= VHOST_USER_VERSION
,
514 if (vhost_user_one_time_request(request
) && dev
->vq_index
!= 0) {
518 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
522 if (vhost_user_read(dev
, &msg
) < 0) {
526 if (msg
.request
!= request
) {
527 error_report("Received unexpected msg type. Expected %d received %d",
528 request
, msg
.request
);
532 if (msg
.size
!= sizeof(msg
.payload
.u64
)) {
533 error_report("Received bad msg size.");
537 *u64
= msg
.payload
.u64
;
542 static int vhost_user_get_features(struct vhost_dev
*dev
, uint64_t *features
)
544 return vhost_user_get_u64(dev
, VHOST_USER_GET_FEATURES
, 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 static int vhost_user_net_set_mtu(struct vhost_dev
*dev
, uint16_t mtu
)
698 bool reply_supported
= virtio_has_feature(dev
->protocol_features
,
699 VHOST_USER_PROTOCOL_F_REPLY_ACK
);
701 if (!(dev
->protocol_features
& (1ULL << VHOST_USER_PROTOCOL_F_NET_MTU
))) {
705 msg
.request
= VHOST_USER_NET_SET_MTU
;
706 msg
.payload
.u64
= mtu
;
707 msg
.size
= sizeof(msg
.payload
.u64
);
708 msg
.flags
= VHOST_USER_VERSION
;
709 if (reply_supported
) {
710 msg
.flags
|= VHOST_USER_NEED_REPLY_MASK
;
713 if (vhost_user_write(dev
, &msg
, NULL
, 0) < 0) {
717 /* If reply_ack supported, slave has to ack specified MTU is valid */
718 if (reply_supported
) {
719 return process_message_reply(dev
, &msg
);
725 const VhostOps user_ops
= {
726 .backend_type
= VHOST_BACKEND_TYPE_USER
,
727 .vhost_backend_init
= vhost_user_init
,
728 .vhost_backend_cleanup
= vhost_user_cleanup
,
729 .vhost_backend_memslots_limit
= vhost_user_memslots_limit
,
730 .vhost_set_log_base
= vhost_user_set_log_base
,
731 .vhost_set_mem_table
= vhost_user_set_mem_table
,
732 .vhost_set_vring_addr
= vhost_user_set_vring_addr
,
733 .vhost_set_vring_endian
= vhost_user_set_vring_endian
,
734 .vhost_set_vring_num
= vhost_user_set_vring_num
,
735 .vhost_set_vring_base
= vhost_user_set_vring_base
,
736 .vhost_get_vring_base
= vhost_user_get_vring_base
,
737 .vhost_set_vring_kick
= vhost_user_set_vring_kick
,
738 .vhost_set_vring_call
= vhost_user_set_vring_call
,
739 .vhost_set_features
= vhost_user_set_features
,
740 .vhost_get_features
= vhost_user_get_features
,
741 .vhost_set_owner
= vhost_user_set_owner
,
742 .vhost_reset_device
= vhost_user_reset_device
,
743 .vhost_get_vq_index
= vhost_user_get_vq_index
,
744 .vhost_set_vring_enable
= vhost_user_set_vring_enable
,
745 .vhost_requires_shm_log
= vhost_user_requires_shm_log
,
746 .vhost_migration_done
= vhost_user_migration_done
,
747 .vhost_backend_can_merge
= vhost_user_can_merge
,
748 .vhost_net_set_mtu
= vhost_user_net_set_mtu
,