vhost-user: call set_msgfds unconditionally
[qemu/kevin.git] / hw / virtio / vhost-user.c
blobf01b92f51271feac2e82e8370e12fb699fb2c560
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 "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"
20 #include "migration/migration.h"
22 #include <sys/ioctl.h>
23 #include <sys/socket.h>
24 #include <sys/un.h>
25 #include <linux/vhost.h>
27 #define VHOST_MEMORY_MAX_NREGIONS 8
28 #define VHOST_USER_F_PROTOCOL_FEATURES 30
30 enum VhostUserProtocolFeature {
31 VHOST_USER_PROTOCOL_F_MQ = 0,
32 VHOST_USER_PROTOCOL_F_LOG_SHMFD = 1,
33 VHOST_USER_PROTOCOL_F_RARP = 2,
35 VHOST_USER_PROTOCOL_F_MAX
38 #define VHOST_USER_PROTOCOL_FEATURE_MASK ((1 << VHOST_USER_PROTOCOL_F_MAX) - 1)
40 typedef enum VhostUserRequest {
41 VHOST_USER_NONE = 0,
42 VHOST_USER_GET_FEATURES = 1,
43 VHOST_USER_SET_FEATURES = 2,
44 VHOST_USER_SET_OWNER = 3,
45 VHOST_USER_RESET_OWNER = 4,
46 VHOST_USER_SET_MEM_TABLE = 5,
47 VHOST_USER_SET_LOG_BASE = 6,
48 VHOST_USER_SET_LOG_FD = 7,
49 VHOST_USER_SET_VRING_NUM = 8,
50 VHOST_USER_SET_VRING_ADDR = 9,
51 VHOST_USER_SET_VRING_BASE = 10,
52 VHOST_USER_GET_VRING_BASE = 11,
53 VHOST_USER_SET_VRING_KICK = 12,
54 VHOST_USER_SET_VRING_CALL = 13,
55 VHOST_USER_SET_VRING_ERR = 14,
56 VHOST_USER_GET_PROTOCOL_FEATURES = 15,
57 VHOST_USER_SET_PROTOCOL_FEATURES = 16,
58 VHOST_USER_GET_QUEUE_NUM = 17,
59 VHOST_USER_SET_VRING_ENABLE = 18,
60 VHOST_USER_SEND_RARP = 19,
61 VHOST_USER_MAX
62 } VhostUserRequest;
64 typedef struct VhostUserMemoryRegion {
65 uint64_t guest_phys_addr;
66 uint64_t memory_size;
67 uint64_t userspace_addr;
68 uint64_t mmap_offset;
69 } VhostUserMemoryRegion;
71 typedef struct VhostUserMemory {
72 uint32_t nregions;
73 uint32_t padding;
74 VhostUserMemoryRegion regions[VHOST_MEMORY_MAX_NREGIONS];
75 } VhostUserMemory;
77 typedef struct VhostUserLog {
78 uint64_t mmap_size;
79 uint64_t mmap_offset;
80 } VhostUserLog;
82 typedef struct VhostUserMsg {
83 VhostUserRequest request;
85 #define VHOST_USER_VERSION_MASK (0x3)
86 #define VHOST_USER_REPLY_MASK (0x1<<2)
87 uint32_t flags;
88 uint32_t size; /* the following payload size */
89 union {
90 #define VHOST_USER_VRING_IDX_MASK (0xff)
91 #define VHOST_USER_VRING_NOFD_MASK (0x1<<8)
92 uint64_t u64;
93 struct vhost_vring_state state;
94 struct vhost_vring_addr addr;
95 VhostUserMemory memory;
96 VhostUserLog log;
97 } payload;
98 } QEMU_PACKED VhostUserMsg;
100 static VhostUserMsg m __attribute__ ((unused));
101 #define VHOST_USER_HDR_SIZE (sizeof(m.request) \
102 + sizeof(m.flags) \
103 + sizeof(m.size))
105 #define VHOST_USER_PAYLOAD_SIZE (sizeof(m) - VHOST_USER_HDR_SIZE)
107 /* The version of the protocol we support */
108 #define VHOST_USER_VERSION (0x1)
110 static bool ioeventfd_enabled(void)
112 return kvm_enabled() && kvm_eventfds_enabled();
115 static int vhost_user_read(struct vhost_dev *dev, VhostUserMsg *msg)
117 CharDriverState *chr = dev->opaque;
118 uint8_t *p = (uint8_t *) msg;
119 int r, size = VHOST_USER_HDR_SIZE;
121 r = qemu_chr_fe_read_all(chr, p, size);
122 if (r != size) {
123 error_report("Failed to read msg header. Read %d instead of %d."
124 " Original request %d.", r, size, msg->request);
125 goto fail;
128 /* validate received flags */
129 if (msg->flags != (VHOST_USER_REPLY_MASK | VHOST_USER_VERSION)) {
130 error_report("Failed to read msg header."
131 " Flags 0x%x instead of 0x%x.", msg->flags,
132 VHOST_USER_REPLY_MASK | VHOST_USER_VERSION);
133 goto fail;
136 /* validate message size is sane */
137 if (msg->size > VHOST_USER_PAYLOAD_SIZE) {
138 error_report("Failed to read msg header."
139 " Size %d exceeds the maximum %zu.", msg->size,
140 VHOST_USER_PAYLOAD_SIZE);
141 goto fail;
144 if (msg->size) {
145 p += VHOST_USER_HDR_SIZE;
146 size = msg->size;
147 r = qemu_chr_fe_read_all(chr, p, size);
148 if (r != size) {
149 error_report("Failed to read msg payload."
150 " Read %d instead of %d.", r, msg->size);
151 goto fail;
155 return 0;
157 fail:
158 return -1;
161 static bool vhost_user_one_time_request(VhostUserRequest request)
163 switch (request) {
164 case VHOST_USER_SET_OWNER:
165 case VHOST_USER_RESET_OWNER:
166 case VHOST_USER_SET_MEM_TABLE:
167 case VHOST_USER_GET_QUEUE_NUM:
168 return true;
169 default:
170 return false;
174 /* most non-init callers ignore the error */
175 static int vhost_user_write(struct vhost_dev *dev, VhostUserMsg *msg,
176 int *fds, int fd_num)
178 CharDriverState *chr = dev->opaque;
179 int size = VHOST_USER_HDR_SIZE + msg->size;
182 * For non-vring specific requests, like VHOST_USER_SET_MEM_TABLE,
183 * we just need send it once in the first time. For later such
184 * request, we just ignore it.
186 if (vhost_user_one_time_request(msg->request) && dev->vq_index != 0) {
187 return 0;
190 qemu_chr_fe_set_msgfds(chr, fds, fd_num);
192 return qemu_chr_fe_write_all(chr, (const uint8_t *) msg, size) == size ?
193 0 : -1;
196 static int vhost_user_set_log_base(struct vhost_dev *dev, uint64_t base,
197 struct vhost_log *log)
199 int fds[VHOST_MEMORY_MAX_NREGIONS];
200 size_t fd_num = 0;
201 bool shmfd = virtio_has_feature(dev->protocol_features,
202 VHOST_USER_PROTOCOL_F_LOG_SHMFD);
203 VhostUserMsg msg = {
204 .request = VHOST_USER_SET_LOG_BASE,
205 .flags = VHOST_USER_VERSION,
206 .payload.log.mmap_size = log->size * sizeof(*(log->log)),
207 .payload.log.mmap_offset = 0,
208 .size = sizeof(msg.payload.log),
211 if (shmfd && log->fd != -1) {
212 fds[fd_num++] = log->fd;
215 vhost_user_write(dev, &msg, fds, fd_num);
217 if (shmfd) {
218 msg.size = 0;
219 if (vhost_user_read(dev, &msg) < 0) {
220 return 0;
223 if (msg.request != VHOST_USER_SET_LOG_BASE) {
224 error_report("Received unexpected msg type. "
225 "Expected %d received %d",
226 VHOST_USER_SET_LOG_BASE, msg.request);
227 return -1;
231 return 0;
234 static int vhost_user_set_mem_table(struct vhost_dev *dev,
235 struct vhost_memory *mem)
237 int fds[VHOST_MEMORY_MAX_NREGIONS];
238 int i, fd;
239 size_t fd_num = 0;
240 VhostUserMsg msg = {
241 .request = VHOST_USER_SET_MEM_TABLE,
242 .flags = VHOST_USER_VERSION,
245 for (i = 0; i < dev->mem->nregions; ++i) {
246 struct vhost_memory_region *reg = dev->mem->regions + i;
247 ram_addr_t offset;
248 MemoryRegion *mr;
250 assert((uintptr_t)reg->userspace_addr == reg->userspace_addr);
251 mr = memory_region_from_host((void *)(uintptr_t)reg->userspace_addr,
252 &offset);
253 fd = memory_region_get_fd(mr);
254 if (fd > 0) {
255 msg.payload.memory.regions[fd_num].userspace_addr = reg->userspace_addr;
256 msg.payload.memory.regions[fd_num].memory_size = reg->memory_size;
257 msg.payload.memory.regions[fd_num].guest_phys_addr = reg->guest_phys_addr;
258 msg.payload.memory.regions[fd_num].mmap_offset = offset;
259 assert(fd_num < VHOST_MEMORY_MAX_NREGIONS);
260 fds[fd_num++] = fd;
264 msg.payload.memory.nregions = fd_num;
266 if (!fd_num) {
267 error_report("Failed initializing vhost-user memory map, "
268 "consider using -object memory-backend-file share=on");
269 return -1;
272 msg.size = sizeof(msg.payload.memory.nregions);
273 msg.size += sizeof(msg.payload.memory.padding);
274 msg.size += fd_num * sizeof(VhostUserMemoryRegion);
276 vhost_user_write(dev, &msg, fds, fd_num);
278 return 0;
281 static int vhost_user_set_vring_addr(struct vhost_dev *dev,
282 struct vhost_vring_addr *addr)
284 VhostUserMsg msg = {
285 .request = VHOST_USER_SET_VRING_ADDR,
286 .flags = VHOST_USER_VERSION,
287 .payload.addr = *addr,
288 .size = sizeof(msg.payload.addr),
291 vhost_user_write(dev, &msg, NULL, 0);
293 return 0;
296 static int vhost_user_set_vring_endian(struct vhost_dev *dev,
297 struct vhost_vring_state *ring)
299 error_report("vhost-user trying to send unhandled ioctl");
300 return -1;
303 static int vhost_set_vring(struct vhost_dev *dev,
304 unsigned long int request,
305 struct vhost_vring_state *ring)
307 VhostUserMsg msg = {
308 .request = request,
309 .flags = VHOST_USER_VERSION,
310 .payload.state = *ring,
311 .size = sizeof(msg.payload.state),
314 vhost_user_write(dev, &msg, NULL, 0);
316 return 0;
319 static int vhost_user_set_vring_num(struct vhost_dev *dev,
320 struct vhost_vring_state *ring)
322 return vhost_set_vring(dev, VHOST_USER_SET_VRING_NUM, ring);
325 static int vhost_user_set_vring_base(struct vhost_dev *dev,
326 struct vhost_vring_state *ring)
328 return vhost_set_vring(dev, VHOST_USER_SET_VRING_BASE, ring);
331 static int vhost_user_set_vring_enable(struct vhost_dev *dev, int enable)
333 int i;
335 if (!virtio_has_feature(dev->features, VHOST_USER_F_PROTOCOL_FEATURES)) {
336 return -1;
339 for (i = 0; i < dev->nvqs; ++i) {
340 struct vhost_vring_state state = {
341 .index = dev->vq_index + i,
342 .num = enable,
345 vhost_set_vring(dev, VHOST_USER_SET_VRING_ENABLE, &state);
348 return 0;
351 static int vhost_user_get_vring_base(struct vhost_dev *dev,
352 struct vhost_vring_state *ring)
354 VhostUserMsg msg = {
355 .request = VHOST_USER_GET_VRING_BASE,
356 .flags = VHOST_USER_VERSION,
357 .payload.state = *ring,
358 .size = sizeof(msg.payload.state),
361 vhost_user_write(dev, &msg, NULL, 0);
363 if (vhost_user_read(dev, &msg) < 0) {
364 return 0;
367 if (msg.request != VHOST_USER_GET_VRING_BASE) {
368 error_report("Received unexpected msg type. Expected %d received %d",
369 VHOST_USER_GET_VRING_BASE, msg.request);
370 return -1;
373 if (msg.size != sizeof(msg.payload.state)) {
374 error_report("Received bad msg size.");
375 return -1;
378 *ring = msg.payload.state;
380 return 0;
383 static int vhost_set_vring_file(struct vhost_dev *dev,
384 VhostUserRequest request,
385 struct vhost_vring_file *file)
387 int fds[VHOST_MEMORY_MAX_NREGIONS];
388 size_t fd_num = 0;
389 VhostUserMsg msg = {
390 .request = request,
391 .flags = VHOST_USER_VERSION,
392 .payload.u64 = file->index & VHOST_USER_VRING_IDX_MASK,
393 .size = sizeof(msg.payload.u64),
396 if (ioeventfd_enabled() && file->fd > 0) {
397 fds[fd_num++] = file->fd;
398 } else {
399 msg.payload.u64 |= VHOST_USER_VRING_NOFD_MASK;
402 vhost_user_write(dev, &msg, fds, fd_num);
404 return 0;
407 static int vhost_user_set_vring_kick(struct vhost_dev *dev,
408 struct vhost_vring_file *file)
410 return vhost_set_vring_file(dev, VHOST_USER_SET_VRING_KICK, file);
413 static int vhost_user_set_vring_call(struct vhost_dev *dev,
414 struct vhost_vring_file *file)
416 return vhost_set_vring_file(dev, VHOST_USER_SET_VRING_CALL, file);
419 static int vhost_user_set_u64(struct vhost_dev *dev, int request, uint64_t u64)
421 VhostUserMsg msg = {
422 .request = request,
423 .flags = VHOST_USER_VERSION,
424 .payload.u64 = u64,
425 .size = sizeof(msg.payload.u64),
428 vhost_user_write(dev, &msg, NULL, 0);
430 return 0;
433 static int vhost_user_set_features(struct vhost_dev *dev,
434 uint64_t features)
436 return vhost_user_set_u64(dev, VHOST_USER_SET_FEATURES, features);
439 static int vhost_user_set_protocol_features(struct vhost_dev *dev,
440 uint64_t features)
442 return vhost_user_set_u64(dev, VHOST_USER_SET_PROTOCOL_FEATURES, features);
445 static int vhost_user_get_u64(struct vhost_dev *dev, int request, uint64_t *u64)
447 VhostUserMsg msg = {
448 .request = request,
449 .flags = VHOST_USER_VERSION,
452 if (vhost_user_one_time_request(request) && dev->vq_index != 0) {
453 return 0;
456 vhost_user_write(dev, &msg, NULL, 0);
458 if (vhost_user_read(dev, &msg) < 0) {
459 return 0;
462 if (msg.request != request) {
463 error_report("Received unexpected msg type. Expected %d received %d",
464 request, msg.request);
465 return -1;
468 if (msg.size != sizeof(msg.payload.u64)) {
469 error_report("Received bad msg size.");
470 return -1;
473 *u64 = msg.payload.u64;
475 return 0;
478 static int vhost_user_get_features(struct vhost_dev *dev, uint64_t *features)
480 return vhost_user_get_u64(dev, VHOST_USER_GET_FEATURES, features);
483 static int vhost_user_set_owner(struct vhost_dev *dev)
485 VhostUserMsg msg = {
486 .request = VHOST_USER_SET_OWNER,
487 .flags = VHOST_USER_VERSION,
490 vhost_user_write(dev, &msg, NULL, 0);
492 return 0;
495 static int vhost_user_reset_device(struct vhost_dev *dev)
497 VhostUserMsg msg = {
498 .request = VHOST_USER_RESET_OWNER,
499 .flags = VHOST_USER_VERSION,
502 vhost_user_write(dev, &msg, NULL, 0);
504 return 0;
507 static int vhost_user_init(struct vhost_dev *dev, void *opaque)
509 uint64_t features;
510 int err;
512 assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER);
514 dev->opaque = opaque;
516 err = vhost_user_get_features(dev, &features);
517 if (err < 0) {
518 return err;
521 if (virtio_has_feature(features, VHOST_USER_F_PROTOCOL_FEATURES)) {
522 dev->backend_features |= 1ULL << VHOST_USER_F_PROTOCOL_FEATURES;
524 err = vhost_user_get_u64(dev, VHOST_USER_GET_PROTOCOL_FEATURES,
525 &features);
526 if (err < 0) {
527 return err;
530 dev->protocol_features = features & VHOST_USER_PROTOCOL_FEATURE_MASK;
531 err = vhost_user_set_protocol_features(dev, dev->protocol_features);
532 if (err < 0) {
533 return err;
536 /* query the max queues we support if backend supports Multiple Queue */
537 if (dev->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_MQ)) {
538 err = vhost_user_get_u64(dev, VHOST_USER_GET_QUEUE_NUM,
539 &dev->max_queues);
540 if (err < 0) {
541 return err;
546 if (dev->migration_blocker == NULL &&
547 !virtio_has_feature(dev->protocol_features,
548 VHOST_USER_PROTOCOL_F_LOG_SHMFD)) {
549 error_setg(&dev->migration_blocker,
550 "Migration disabled: vhost-user backend lacks "
551 "VHOST_USER_PROTOCOL_F_LOG_SHMFD feature.");
554 return 0;
557 static int vhost_user_cleanup(struct vhost_dev *dev)
559 assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER);
561 dev->opaque = 0;
563 return 0;
566 static int vhost_user_get_vq_index(struct vhost_dev *dev, int idx)
568 assert(idx >= dev->vq_index && idx < dev->vq_index + dev->nvqs);
570 return idx;
573 static int vhost_user_memslots_limit(struct vhost_dev *dev)
575 return VHOST_MEMORY_MAX_NREGIONS;
578 static bool vhost_user_requires_shm_log(struct vhost_dev *dev)
580 assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER);
582 return virtio_has_feature(dev->protocol_features,
583 VHOST_USER_PROTOCOL_F_LOG_SHMFD);
586 static int vhost_user_migration_done(struct vhost_dev *dev, char* mac_addr)
588 VhostUserMsg msg = { 0 };
589 int err;
591 assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER);
593 /* If guest supports GUEST_ANNOUNCE do nothing */
594 if (virtio_has_feature(dev->acked_features, VIRTIO_NET_F_GUEST_ANNOUNCE)) {
595 return 0;
598 /* if backend supports VHOST_USER_PROTOCOL_F_RARP ask it to send the RARP */
599 if (virtio_has_feature(dev->protocol_features,
600 VHOST_USER_PROTOCOL_F_RARP)) {
601 msg.request = VHOST_USER_SEND_RARP;
602 msg.flags = VHOST_USER_VERSION;
603 memcpy((char *)&msg.payload.u64, mac_addr, 6);
604 msg.size = sizeof(msg.payload.u64);
606 err = vhost_user_write(dev, &msg, NULL, 0);
607 return err;
609 return -1;
612 static bool vhost_user_can_merge(struct vhost_dev *dev,
613 uint64_t start1, uint64_t size1,
614 uint64_t start2, uint64_t size2)
616 ram_addr_t offset;
617 int mfd, rfd;
618 MemoryRegion *mr;
620 mr = memory_region_from_host((void *)(uintptr_t)start1, &offset);
621 mfd = memory_region_get_fd(mr);
623 mr = memory_region_from_host((void *)(uintptr_t)start2, &offset);
624 rfd = memory_region_get_fd(mr);
626 return mfd == rfd;
629 const VhostOps user_ops = {
630 .backend_type = VHOST_BACKEND_TYPE_USER,
631 .vhost_backend_init = vhost_user_init,
632 .vhost_backend_cleanup = vhost_user_cleanup,
633 .vhost_backend_memslots_limit = vhost_user_memslots_limit,
634 .vhost_set_log_base = vhost_user_set_log_base,
635 .vhost_set_mem_table = vhost_user_set_mem_table,
636 .vhost_set_vring_addr = vhost_user_set_vring_addr,
637 .vhost_set_vring_endian = vhost_user_set_vring_endian,
638 .vhost_set_vring_num = vhost_user_set_vring_num,
639 .vhost_set_vring_base = vhost_user_set_vring_base,
640 .vhost_get_vring_base = vhost_user_get_vring_base,
641 .vhost_set_vring_kick = vhost_user_set_vring_kick,
642 .vhost_set_vring_call = vhost_user_set_vring_call,
643 .vhost_set_features = vhost_user_set_features,
644 .vhost_get_features = vhost_user_get_features,
645 .vhost_set_owner = vhost_user_set_owner,
646 .vhost_reset_device = vhost_user_reset_device,
647 .vhost_get_vq_index = vhost_user_get_vq_index,
648 .vhost_set_vring_enable = vhost_user_set_vring_enable,
649 .vhost_requires_shm_log = vhost_user_requires_shm_log,
650 .vhost_migration_done = vhost_user_migration_done,
651 .vhost_backend_can_merge = vhost_user_can_merge,