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 "hw/virtio/vhost.h"
12 #include "hw/virtio/vhost-backend.h"
13 #include "sysemu/char.h"
14 #include "sysemu/kvm.h"
15 #include "qemu/error-report.h"
16 #include "qemu/sockets.h"
17 #include "exec/ram_addr.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
28 #define VHOST_USER_PROTOCOL_FEATURE_MASK 0x1ULL
30 #define VHOST_USER_PROTOCOL_F_MQ 0
32 typedef enum VhostUserRequest
{
34 VHOST_USER_GET_FEATURES
= 1,
35 VHOST_USER_SET_FEATURES
= 2,
36 VHOST_USER_SET_OWNER
= 3,
37 VHOST_USER_RESET_DEVICE
= 4,
38 VHOST_USER_SET_MEM_TABLE
= 5,
39 VHOST_USER_SET_LOG_BASE
= 6,
40 VHOST_USER_SET_LOG_FD
= 7,
41 VHOST_USER_SET_VRING_NUM
= 8,
42 VHOST_USER_SET_VRING_ADDR
= 9,
43 VHOST_USER_SET_VRING_BASE
= 10,
44 VHOST_USER_GET_VRING_BASE
= 11,
45 VHOST_USER_SET_VRING_KICK
= 12,
46 VHOST_USER_SET_VRING_CALL
= 13,
47 VHOST_USER_SET_VRING_ERR
= 14,
48 VHOST_USER_GET_PROTOCOL_FEATURES
= 15,
49 VHOST_USER_SET_PROTOCOL_FEATURES
= 16,
50 VHOST_USER_GET_QUEUE_NUM
= 17,
51 VHOST_USER_SET_VRING_ENABLE
= 18,
55 typedef struct VhostUserMemoryRegion
{
56 uint64_t guest_phys_addr
;
58 uint64_t userspace_addr
;
60 } VhostUserMemoryRegion
;
62 typedef struct VhostUserMemory
{
65 VhostUserMemoryRegion regions
[VHOST_MEMORY_MAX_NREGIONS
];
68 typedef struct VhostUserMsg
{
69 VhostUserRequest request
;
71 #define VHOST_USER_VERSION_MASK (0x3)
72 #define VHOST_USER_REPLY_MASK (0x1<<2)
74 uint32_t size
; /* the following payload size */
76 #define VHOST_USER_VRING_IDX_MASK (0xff)
77 #define VHOST_USER_VRING_NOFD_MASK (0x1<<8)
79 struct vhost_vring_state state
;
80 struct vhost_vring_addr addr
;
81 VhostUserMemory memory
;
83 } QEMU_PACKED VhostUserMsg
;
85 static VhostUserMsg m
__attribute__ ((unused
));
86 #define VHOST_USER_HDR_SIZE (sizeof(m.request) \
90 #define VHOST_USER_PAYLOAD_SIZE (sizeof(m) - VHOST_USER_HDR_SIZE)
92 /* The version of the protocol we support */
93 #define VHOST_USER_VERSION (0x1)
95 static bool ioeventfd_enabled(void)
97 return kvm_enabled() && kvm_eventfds_enabled();
100 static unsigned long int ioctl_to_vhost_user_request
[VHOST_USER_MAX
] = {
101 -1, /* VHOST_USER_NONE */
102 VHOST_GET_FEATURES
, /* VHOST_USER_GET_FEATURES */
103 VHOST_SET_FEATURES
, /* VHOST_USER_SET_FEATURES */
104 VHOST_SET_OWNER
, /* VHOST_USER_SET_OWNER */
105 VHOST_RESET_DEVICE
, /* VHOST_USER_RESET_DEVICE */
106 VHOST_SET_MEM_TABLE
, /* VHOST_USER_SET_MEM_TABLE */
107 VHOST_SET_LOG_BASE
, /* VHOST_USER_SET_LOG_BASE */
108 VHOST_SET_LOG_FD
, /* VHOST_USER_SET_LOG_FD */
109 VHOST_SET_VRING_NUM
, /* VHOST_USER_SET_VRING_NUM */
110 VHOST_SET_VRING_ADDR
, /* VHOST_USER_SET_VRING_ADDR */
111 VHOST_SET_VRING_BASE
, /* VHOST_USER_SET_VRING_BASE */
112 VHOST_GET_VRING_BASE
, /* VHOST_USER_GET_VRING_BASE */
113 VHOST_SET_VRING_KICK
, /* VHOST_USER_SET_VRING_KICK */
114 VHOST_SET_VRING_CALL
, /* VHOST_USER_SET_VRING_CALL */
115 VHOST_SET_VRING_ERR
/* VHOST_USER_SET_VRING_ERR */
118 static VhostUserRequest
vhost_user_request_translate(unsigned long int request
)
120 VhostUserRequest idx
;
122 for (idx
= 0; idx
< VHOST_USER_MAX
; idx
++) {
123 if (ioctl_to_vhost_user_request
[idx
] == request
) {
128 return (idx
== VHOST_USER_MAX
) ? VHOST_USER_NONE
: idx
;
131 static int vhost_user_read(struct vhost_dev
*dev
, VhostUserMsg
*msg
)
133 CharDriverState
*chr
= dev
->opaque
;
134 uint8_t *p
= (uint8_t *) msg
;
135 int r
, size
= VHOST_USER_HDR_SIZE
;
137 r
= qemu_chr_fe_read_all(chr
, p
, size
);
139 error_report("Failed to read msg header. Read %d instead of %d.", r
,
144 /* validate received flags */
145 if (msg
->flags
!= (VHOST_USER_REPLY_MASK
| VHOST_USER_VERSION
)) {
146 error_report("Failed to read msg header."
147 " Flags 0x%x instead of 0x%x.", msg
->flags
,
148 VHOST_USER_REPLY_MASK
| VHOST_USER_VERSION
);
152 /* validate message size is sane */
153 if (msg
->size
> VHOST_USER_PAYLOAD_SIZE
) {
154 error_report("Failed to read msg header."
155 " Size %d exceeds the maximum %zu.", msg
->size
,
156 VHOST_USER_PAYLOAD_SIZE
);
161 p
+= VHOST_USER_HDR_SIZE
;
163 r
= qemu_chr_fe_read_all(chr
, p
, size
);
165 error_report("Failed to read msg payload."
166 " Read %d instead of %d.", r
, msg
->size
);
177 static int vhost_user_write(struct vhost_dev
*dev
, VhostUserMsg
*msg
,
178 int *fds
, int fd_num
)
180 CharDriverState
*chr
= dev
->opaque
;
181 int size
= VHOST_USER_HDR_SIZE
+ msg
->size
;
184 qemu_chr_fe_set_msgfds(chr
, fds
, fd_num
);
187 return qemu_chr_fe_write_all(chr
, (const uint8_t *) msg
, size
) == size
?
191 static bool vhost_user_one_time_request(VhostUserRequest request
)
194 case VHOST_USER_SET_OWNER
:
195 case VHOST_USER_RESET_DEVICE
:
196 case VHOST_USER_SET_MEM_TABLE
:
197 case VHOST_USER_GET_QUEUE_NUM
:
204 static int vhost_user_call(struct vhost_dev
*dev
, unsigned long int request
,
208 VhostUserRequest msg_request
;
209 struct vhost_vring_file
*file
= 0;
211 int fds
[VHOST_MEMORY_MAX_NREGIONS
];
215 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
217 /* only translate vhost ioctl requests */
218 if (request
> VHOST_USER_MAX
) {
219 msg_request
= vhost_user_request_translate(request
);
221 msg_request
= request
;
225 * For non-vring specific requests, like VHOST_USER_SET_MEM_TABLE,
226 * we just need send it once in the first time. For later such
227 * request, we just ignore it.
229 if (vhost_user_one_time_request(msg_request
) && dev
->vq_index
!= 0) {
233 msg
.request
= msg_request
;
234 msg
.flags
= VHOST_USER_VERSION
;
237 switch (msg_request
) {
238 case VHOST_USER_GET_FEATURES
:
239 case VHOST_USER_GET_PROTOCOL_FEATURES
:
240 case VHOST_USER_GET_QUEUE_NUM
:
244 case VHOST_USER_SET_FEATURES
:
245 case VHOST_USER_SET_LOG_BASE
:
246 case VHOST_USER_SET_PROTOCOL_FEATURES
:
247 msg
.u64
= *((__u64
*) arg
);
248 msg
.size
= sizeof(m
.u64
);
251 case VHOST_USER_SET_OWNER
:
252 case VHOST_USER_RESET_DEVICE
:
255 case VHOST_USER_SET_MEM_TABLE
:
256 for (i
= 0; i
< dev
->mem
->nregions
; ++i
) {
257 struct vhost_memory_region
*reg
= dev
->mem
->regions
+ i
;
260 assert((uintptr_t)reg
->userspace_addr
== reg
->userspace_addr
);
261 qemu_ram_addr_from_host((void *)(uintptr_t)reg
->userspace_addr
, &ram_addr
);
262 fd
= qemu_get_ram_fd(ram_addr
);
264 msg
.memory
.regions
[fd_num
].userspace_addr
= reg
->userspace_addr
;
265 msg
.memory
.regions
[fd_num
].memory_size
= reg
->memory_size
;
266 msg
.memory
.regions
[fd_num
].guest_phys_addr
= reg
->guest_phys_addr
;
267 msg
.memory
.regions
[fd_num
].mmap_offset
= reg
->userspace_addr
-
268 (uintptr_t) qemu_get_ram_block_host_ptr(ram_addr
);
269 assert(fd_num
< VHOST_MEMORY_MAX_NREGIONS
);
274 msg
.memory
.nregions
= fd_num
;
277 error_report("Failed initializing vhost-user memory map, "
278 "consider using -object memory-backend-file share=on");
282 msg
.size
= sizeof(m
.memory
.nregions
);
283 msg
.size
+= sizeof(m
.memory
.padding
);
284 msg
.size
+= fd_num
* sizeof(VhostUserMemoryRegion
);
288 case VHOST_USER_SET_LOG_FD
:
289 fds
[fd_num
++] = *((int *) arg
);
292 case VHOST_USER_SET_VRING_NUM
:
293 case VHOST_USER_SET_VRING_BASE
:
294 case VHOST_USER_SET_VRING_ENABLE
:
295 memcpy(&msg
.state
, arg
, sizeof(struct vhost_vring_state
));
296 msg
.size
= sizeof(m
.state
);
299 case VHOST_USER_GET_VRING_BASE
:
300 memcpy(&msg
.state
, arg
, sizeof(struct vhost_vring_state
));
301 msg
.size
= sizeof(m
.state
);
305 case VHOST_USER_SET_VRING_ADDR
:
306 memcpy(&msg
.addr
, arg
, sizeof(struct vhost_vring_addr
));
307 msg
.size
= sizeof(m
.addr
);
310 case VHOST_USER_SET_VRING_KICK
:
311 case VHOST_USER_SET_VRING_CALL
:
312 case VHOST_USER_SET_VRING_ERR
:
314 msg
.u64
= file
->index
& VHOST_USER_VRING_IDX_MASK
;
315 msg
.size
= sizeof(m
.u64
);
316 if (ioeventfd_enabled() && file
->fd
> 0) {
317 fds
[fd_num
++] = file
->fd
;
319 msg
.u64
|= VHOST_USER_VRING_NOFD_MASK
;
323 error_report("vhost-user trying to send unhandled ioctl");
328 if (vhost_user_write(dev
, &msg
, fds
, fd_num
) < 0) {
333 if (vhost_user_read(dev
, &msg
) < 0) {
337 if (msg_request
!= msg
.request
) {
338 error_report("Received unexpected msg type."
339 " Expected %d received %d", msg_request
, msg
.request
);
343 switch (msg_request
) {
344 case VHOST_USER_GET_FEATURES
:
345 case VHOST_USER_GET_PROTOCOL_FEATURES
:
346 case VHOST_USER_GET_QUEUE_NUM
:
347 if (msg
.size
!= sizeof(m
.u64
)) {
348 error_report("Received bad msg size.");
351 *((__u64
*) arg
) = msg
.u64
;
353 case VHOST_USER_GET_VRING_BASE
:
354 if (msg
.size
!= sizeof(m
.state
)) {
355 error_report("Received bad msg size.");
358 memcpy(arg
, &msg
.state
, sizeof(struct vhost_vring_state
));
361 error_report("Received unexpected msg type.");
370 static int vhost_user_init(struct vhost_dev
*dev
, void *opaque
)
372 unsigned long long features
;
375 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
377 dev
->opaque
= opaque
;
379 err
= vhost_user_call(dev
, VHOST_USER_GET_FEATURES
, &features
);
384 if (virtio_has_feature(features
, VHOST_USER_F_PROTOCOL_FEATURES
)) {
385 dev
->backend_features
|= 1ULL << VHOST_USER_F_PROTOCOL_FEATURES
;
387 err
= vhost_user_call(dev
, VHOST_USER_GET_PROTOCOL_FEATURES
, &features
);
392 dev
->protocol_features
= features
& VHOST_USER_PROTOCOL_FEATURE_MASK
;
393 err
= vhost_user_call(dev
, VHOST_USER_SET_PROTOCOL_FEATURES
,
394 &dev
->protocol_features
);
399 /* query the max queues we support if backend supports Multiple Queue */
400 if (dev
->protocol_features
& (1ULL << VHOST_USER_PROTOCOL_F_MQ
)) {
401 err
= vhost_user_call(dev
, VHOST_USER_GET_QUEUE_NUM
, &dev
->max_queues
);
411 static int vhost_user_set_vring_enable(struct vhost_dev
*dev
, int enable
)
413 struct vhost_vring_state state
= {
414 .index
= dev
->vq_index
,
418 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
420 if (!(dev
->protocol_features
& (1ULL << VHOST_USER_PROTOCOL_F_MQ
))) {
424 return vhost_user_call(dev
, VHOST_USER_SET_VRING_ENABLE
, &state
);
427 static int vhost_user_cleanup(struct vhost_dev
*dev
)
429 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
436 static int vhost_user_get_vq_index(struct vhost_dev
*dev
, int idx
)
438 assert(idx
>= dev
->vq_index
&& idx
< dev
->vq_index
+ dev
->nvqs
);
443 const VhostOps user_ops
= {
444 .backend_type
= VHOST_BACKEND_TYPE_USER
,
445 .vhost_call
= vhost_user_call
,
446 .vhost_backend_init
= vhost_user_init
,
447 .vhost_backend_cleanup
= vhost_user_cleanup
,
448 .vhost_backend_get_vq_index
= vhost_user_get_vq_index
,
449 .vhost_backend_set_vring_enable
= vhost_user_set_vring_enable
,