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_PROTOCOL_FEATURES
:
246 msg
.u64
= *((__u64
*) arg
);
247 msg
.size
= sizeof(m
.u64
);
250 case VHOST_USER_SET_OWNER
:
251 case VHOST_USER_RESET_DEVICE
:
254 case VHOST_USER_SET_MEM_TABLE
:
255 for (i
= 0; i
< dev
->mem
->nregions
; ++i
) {
256 struct vhost_memory_region
*reg
= dev
->mem
->regions
+ i
;
259 assert((uintptr_t)reg
->userspace_addr
== reg
->userspace_addr
);
260 qemu_ram_addr_from_host((void *)(uintptr_t)reg
->userspace_addr
, &ram_addr
);
261 fd
= qemu_get_ram_fd(ram_addr
);
263 msg
.memory
.regions
[fd_num
].userspace_addr
= reg
->userspace_addr
;
264 msg
.memory
.regions
[fd_num
].memory_size
= reg
->memory_size
;
265 msg
.memory
.regions
[fd_num
].guest_phys_addr
= reg
->guest_phys_addr
;
266 msg
.memory
.regions
[fd_num
].mmap_offset
= reg
->userspace_addr
-
267 (uintptr_t) qemu_get_ram_block_host_ptr(ram_addr
);
268 assert(fd_num
< VHOST_MEMORY_MAX_NREGIONS
);
273 msg
.memory
.nregions
= fd_num
;
276 error_report("Failed initializing vhost-user memory map, "
277 "consider using -object memory-backend-file share=on");
281 msg
.size
= sizeof(m
.memory
.nregions
);
282 msg
.size
+= sizeof(m
.memory
.padding
);
283 msg
.size
+= fd_num
* sizeof(VhostUserMemoryRegion
);
287 case VHOST_USER_SET_LOG_FD
:
288 fds
[fd_num
++] = *((int *) arg
);
291 case VHOST_USER_SET_VRING_NUM
:
292 case VHOST_USER_SET_VRING_BASE
:
293 case VHOST_USER_SET_VRING_ENABLE
:
294 memcpy(&msg
.state
, arg
, sizeof(struct vhost_vring_state
));
295 msg
.size
= sizeof(m
.state
);
298 case VHOST_USER_GET_VRING_BASE
:
299 memcpy(&msg
.state
, arg
, sizeof(struct vhost_vring_state
));
300 msg
.size
= sizeof(m
.state
);
304 case VHOST_USER_SET_VRING_ADDR
:
305 memcpy(&msg
.addr
, arg
, sizeof(struct vhost_vring_addr
));
306 msg
.size
= sizeof(m
.addr
);
309 case VHOST_USER_SET_VRING_KICK
:
310 case VHOST_USER_SET_VRING_CALL
:
311 case VHOST_USER_SET_VRING_ERR
:
313 msg
.u64
= file
->index
& VHOST_USER_VRING_IDX_MASK
;
314 msg
.size
= sizeof(m
.u64
);
315 if (ioeventfd_enabled() && file
->fd
> 0) {
316 fds
[fd_num
++] = file
->fd
;
318 msg
.u64
|= VHOST_USER_VRING_NOFD_MASK
;
322 error_report("vhost-user trying to send unhandled ioctl");
327 if (vhost_user_write(dev
, &msg
, fds
, fd_num
) < 0) {
332 if (vhost_user_read(dev
, &msg
) < 0) {
336 if (msg_request
!= msg
.request
) {
337 error_report("Received unexpected msg type."
338 " Expected %d received %d", msg_request
, msg
.request
);
342 switch (msg_request
) {
343 case VHOST_USER_GET_FEATURES
:
344 case VHOST_USER_GET_PROTOCOL_FEATURES
:
345 case VHOST_USER_GET_QUEUE_NUM
:
346 if (msg
.size
!= sizeof(m
.u64
)) {
347 error_report("Received bad msg size.");
350 *((__u64
*) arg
) = msg
.u64
;
352 case VHOST_USER_GET_VRING_BASE
:
353 if (msg
.size
!= sizeof(m
.state
)) {
354 error_report("Received bad msg size.");
357 memcpy(arg
, &msg
.state
, sizeof(struct vhost_vring_state
));
360 error_report("Received unexpected msg type.");
369 static int vhost_set_log_base(struct vhost_dev
*dev
, uint64_t base
)
372 .request
= VHOST_USER_SET_LOG_BASE
,
373 .flags
= VHOST_USER_VERSION
,
375 .size
= sizeof(m
.u64
),
378 vhost_user_write(dev
, &msg
, NULL
, 0);
383 static int vhost_user_init(struct vhost_dev
*dev
, void *opaque
)
385 unsigned long long features
;
388 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
390 dev
->opaque
= opaque
;
392 err
= vhost_user_call(dev
, VHOST_USER_GET_FEATURES
, &features
);
397 if (virtio_has_feature(features
, VHOST_USER_F_PROTOCOL_FEATURES
)) {
398 dev
->backend_features
|= 1ULL << VHOST_USER_F_PROTOCOL_FEATURES
;
400 err
= vhost_user_call(dev
, VHOST_USER_GET_PROTOCOL_FEATURES
, &features
);
405 dev
->protocol_features
= features
& VHOST_USER_PROTOCOL_FEATURE_MASK
;
406 err
= vhost_user_call(dev
, VHOST_USER_SET_PROTOCOL_FEATURES
,
407 &dev
->protocol_features
);
412 /* query the max queues we support if backend supports Multiple Queue */
413 if (dev
->protocol_features
& (1ULL << VHOST_USER_PROTOCOL_F_MQ
)) {
414 err
= vhost_user_call(dev
, VHOST_USER_GET_QUEUE_NUM
, &dev
->max_queues
);
424 static int vhost_user_set_vring_enable(struct vhost_dev
*dev
, int enable
)
426 struct vhost_vring_state state
= {
427 .index
= dev
->vq_index
,
431 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
433 if (!(dev
->protocol_features
& (1ULL << VHOST_USER_PROTOCOL_F_MQ
))) {
437 return vhost_user_call(dev
, VHOST_USER_SET_VRING_ENABLE
, &state
);
440 static int vhost_user_cleanup(struct vhost_dev
*dev
)
442 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
449 static int vhost_user_get_vq_index(struct vhost_dev
*dev
, int idx
)
451 assert(idx
>= dev
->vq_index
&& idx
< dev
->vq_index
+ dev
->nvqs
);
456 static int vhost_user_memslots_limit(struct vhost_dev
*dev
)
458 return VHOST_MEMORY_MAX_NREGIONS
;
461 const VhostOps user_ops
= {
462 .backend_type
= VHOST_BACKEND_TYPE_USER
,
463 .vhost_call
= vhost_user_call
,
464 .vhost_backend_init
= vhost_user_init
,
465 .vhost_backend_cleanup
= vhost_user_cleanup
,
466 .vhost_backend_get_vq_index
= vhost_user_get_vq_index
,
467 .vhost_backend_set_vring_enable
= vhost_user_set_vring_enable
,
468 .vhost_backend_memslots_limit
= vhost_user_memslots_limit
,
469 .vhost_set_log_base
= vhost_set_log_base
,