vhost: add vhost_set_log_base op
[qemu/ar7.git] / hw / virtio / vhost-user.c
blob5d512898cf4384f6d3d8339ea617ac9164eabe7e
1 /*
2 * vhost-user
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.
9 */
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"
19 #include <fcntl.h>
20 #include <unistd.h>
21 #include <sys/ioctl.h>
22 #include <sys/socket.h>
23 #include <sys/un.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 {
33 VHOST_USER_NONE = 0,
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,
52 VHOST_USER_MAX
53 } VhostUserRequest;
55 typedef struct VhostUserMemoryRegion {
56 uint64_t guest_phys_addr;
57 uint64_t memory_size;
58 uint64_t userspace_addr;
59 uint64_t mmap_offset;
60 } VhostUserMemoryRegion;
62 typedef struct VhostUserMemory {
63 uint32_t nregions;
64 uint32_t padding;
65 VhostUserMemoryRegion regions[VHOST_MEMORY_MAX_NREGIONS];
66 } VhostUserMemory;
68 typedef struct VhostUserMsg {
69 VhostUserRequest request;
71 #define VHOST_USER_VERSION_MASK (0x3)
72 #define VHOST_USER_REPLY_MASK (0x1<<2)
73 uint32_t flags;
74 uint32_t size; /* the following payload size */
75 union {
76 #define VHOST_USER_VRING_IDX_MASK (0xff)
77 #define VHOST_USER_VRING_NOFD_MASK (0x1<<8)
78 uint64_t u64;
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) \
87 + sizeof(m.flags) \
88 + sizeof(m.size))
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) {
124 break;
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);
138 if (r != size) {
139 error_report("Failed to read msg header. Read %d instead of %d.", r,
140 size);
141 goto fail;
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);
149 goto fail;
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);
157 goto fail;
160 if (msg->size) {
161 p += VHOST_USER_HDR_SIZE;
162 size = msg->size;
163 r = qemu_chr_fe_read_all(chr, p, size);
164 if (r != size) {
165 error_report("Failed to read msg payload."
166 " Read %d instead of %d.", r, msg->size);
167 goto fail;
171 return 0;
173 fail:
174 return -1;
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;
183 if (fd_num) {
184 qemu_chr_fe_set_msgfds(chr, fds, fd_num);
187 return qemu_chr_fe_write_all(chr, (const uint8_t *) msg, size) == size ?
188 0 : -1;
191 static bool vhost_user_one_time_request(VhostUserRequest request)
193 switch (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:
198 return true;
199 default:
200 return false;
204 static int vhost_user_call(struct vhost_dev *dev, unsigned long int request,
205 void *arg)
207 VhostUserMsg msg;
208 VhostUserRequest msg_request;
209 struct vhost_vring_file *file = 0;
210 int need_reply = 0;
211 int fds[VHOST_MEMORY_MAX_NREGIONS];
212 int i, fd;
213 size_t fd_num = 0;
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);
220 } else {
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) {
230 return 0;
233 msg.request = msg_request;
234 msg.flags = VHOST_USER_VERSION;
235 msg.size = 0;
237 switch (msg_request) {
238 case VHOST_USER_GET_FEATURES:
239 case VHOST_USER_GET_PROTOCOL_FEATURES:
240 case VHOST_USER_GET_QUEUE_NUM:
241 need_reply = 1;
242 break;
244 case VHOST_USER_SET_FEATURES:
245 case VHOST_USER_SET_PROTOCOL_FEATURES:
246 msg.u64 = *((__u64 *) arg);
247 msg.size = sizeof(m.u64);
248 break;
250 case VHOST_USER_SET_OWNER:
251 case VHOST_USER_RESET_DEVICE:
252 break;
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;
257 ram_addr_t ram_addr;
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);
262 if (fd > 0) {
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);
269 fds[fd_num++] = fd;
273 msg.memory.nregions = fd_num;
275 if (!fd_num) {
276 error_report("Failed initializing vhost-user memory map, "
277 "consider using -object memory-backend-file share=on");
278 return -1;
281 msg.size = sizeof(m.memory.nregions);
282 msg.size += sizeof(m.memory.padding);
283 msg.size += fd_num * sizeof(VhostUserMemoryRegion);
285 break;
287 case VHOST_USER_SET_LOG_FD:
288 fds[fd_num++] = *((int *) arg);
289 break;
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);
296 break;
298 case VHOST_USER_GET_VRING_BASE:
299 memcpy(&msg.state, arg, sizeof(struct vhost_vring_state));
300 msg.size = sizeof(m.state);
301 need_reply = 1;
302 break;
304 case VHOST_USER_SET_VRING_ADDR:
305 memcpy(&msg.addr, arg, sizeof(struct vhost_vring_addr));
306 msg.size = sizeof(m.addr);
307 break;
309 case VHOST_USER_SET_VRING_KICK:
310 case VHOST_USER_SET_VRING_CALL:
311 case VHOST_USER_SET_VRING_ERR:
312 file = arg;
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;
317 } else {
318 msg.u64 |= VHOST_USER_VRING_NOFD_MASK;
320 break;
321 default:
322 error_report("vhost-user trying to send unhandled ioctl");
323 return -1;
324 break;
327 if (vhost_user_write(dev, &msg, fds, fd_num) < 0) {
328 return 0;
331 if (need_reply) {
332 if (vhost_user_read(dev, &msg) < 0) {
333 return 0;
336 if (msg_request != msg.request) {
337 error_report("Received unexpected msg type."
338 " Expected %d received %d", msg_request, msg.request);
339 return -1;
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.");
348 return -1;
350 *((__u64 *) arg) = msg.u64;
351 break;
352 case VHOST_USER_GET_VRING_BASE:
353 if (msg.size != sizeof(m.state)) {
354 error_report("Received bad msg size.");
355 return -1;
357 memcpy(arg, &msg.state, sizeof(struct vhost_vring_state));
358 break;
359 default:
360 error_report("Received unexpected msg type.");
361 return -1;
362 break;
366 return 0;
369 static int vhost_set_log_base(struct vhost_dev *dev, uint64_t base)
371 VhostUserMsg msg = {
372 .request = VHOST_USER_SET_LOG_BASE,
373 .flags = VHOST_USER_VERSION,
374 .u64 = base,
375 .size = sizeof(m.u64),
378 vhost_user_write(dev, &msg, NULL, 0);
380 return 0;
383 static int vhost_user_init(struct vhost_dev *dev, void *opaque)
385 unsigned long long features;
386 int err;
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);
393 if (err < 0) {
394 return err;
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);
401 if (err < 0) {
402 return err;
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);
408 if (err < 0) {
409 return err;
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);
415 if (err < 0) {
416 return err;
421 return 0;
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,
428 .num = enable,
431 assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER);
433 if (!(dev->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_MQ))) {
434 return -1;
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);
444 dev->opaque = 0;
446 return 0;
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);
453 return idx;
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,