target-arm: Fix warn about implicit conversion
[qemu/cris-port.git] / hw / virtio / vhost-user.c
blob1a7d53c8f4e4f96ee7975d42ab8f1c991c83f6ef
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,
34 VHOST_USER_PROTOCOL_F_REPLY_ACK = 3,
36 VHOST_USER_PROTOCOL_F_MAX
39 #define VHOST_USER_PROTOCOL_FEATURE_MASK ((1 << VHOST_USER_PROTOCOL_F_MAX) - 1)
41 typedef enum VhostUserRequest {
42 VHOST_USER_NONE = 0,
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_MAX
63 } VhostUserRequest;
65 typedef struct VhostUserMemoryRegion {
66 uint64_t guest_phys_addr;
67 uint64_t memory_size;
68 uint64_t userspace_addr;
69 uint64_t mmap_offset;
70 } VhostUserMemoryRegion;
72 typedef struct VhostUserMemory {
73 uint32_t nregions;
74 uint32_t padding;
75 VhostUserMemoryRegion regions[VHOST_MEMORY_MAX_NREGIONS];
76 } VhostUserMemory;
78 typedef struct VhostUserLog {
79 uint64_t mmap_size;
80 uint64_t mmap_offset;
81 } VhostUserLog;
83 typedef struct VhostUserMsg {
84 VhostUserRequest request;
86 #define VHOST_USER_VERSION_MASK (0x3)
87 #define VHOST_USER_REPLY_MASK (0x1<<2)
88 #define VHOST_USER_NEED_REPLY_MASK (0x1 << 3)
89 uint32_t flags;
90 uint32_t size; /* the following payload size */
91 union {
92 #define VHOST_USER_VRING_IDX_MASK (0xff)
93 #define VHOST_USER_VRING_NOFD_MASK (0x1<<8)
94 uint64_t u64;
95 struct vhost_vring_state state;
96 struct vhost_vring_addr addr;
97 VhostUserMemory memory;
98 VhostUserLog log;
99 } payload;
100 } QEMU_PACKED VhostUserMsg;
102 static VhostUserMsg m __attribute__ ((unused));
103 #define VHOST_USER_HDR_SIZE (sizeof(m.request) \
104 + sizeof(m.flags) \
105 + sizeof(m.size))
107 #define VHOST_USER_PAYLOAD_SIZE (sizeof(m) - VHOST_USER_HDR_SIZE)
109 /* The version of the protocol we support */
110 #define VHOST_USER_VERSION (0x1)
112 static bool ioeventfd_enabled(void)
114 return kvm_enabled() && kvm_eventfds_enabled();
117 static int vhost_user_read(struct vhost_dev *dev, VhostUserMsg *msg)
119 CharDriverState *chr = dev->opaque;
120 uint8_t *p = (uint8_t *) msg;
121 int r, size = VHOST_USER_HDR_SIZE;
123 r = qemu_chr_fe_read_all(chr, p, size);
124 if (r != size) {
125 error_report("Failed to read msg header. Read %d instead of %d."
126 " Original request %d.", r, size, msg->request);
127 goto fail;
130 /* validate received flags */
131 if (msg->flags != (VHOST_USER_REPLY_MASK | VHOST_USER_VERSION)) {
132 error_report("Failed to read msg header."
133 " Flags 0x%x instead of 0x%x.", msg->flags,
134 VHOST_USER_REPLY_MASK | VHOST_USER_VERSION);
135 goto fail;
138 /* validate message size is sane */
139 if (msg->size > VHOST_USER_PAYLOAD_SIZE) {
140 error_report("Failed to read msg header."
141 " Size %d exceeds the maximum %zu.", msg->size,
142 VHOST_USER_PAYLOAD_SIZE);
143 goto fail;
146 if (msg->size) {
147 p += VHOST_USER_HDR_SIZE;
148 size = msg->size;
149 r = qemu_chr_fe_read_all(chr, p, size);
150 if (r != size) {
151 error_report("Failed to read msg payload."
152 " Read %d instead of %d.", r, msg->size);
153 goto fail;
157 return 0;
159 fail:
160 return -1;
163 static int process_message_reply(struct vhost_dev *dev,
164 VhostUserRequest request)
166 VhostUserMsg msg;
168 if (vhost_user_read(dev, &msg) < 0) {
169 return -1;
172 if (msg.request != request) {
173 error_report("Received unexpected msg type."
174 "Expected %d received %d",
175 request, msg.request);
176 return -1;
179 return msg.payload.u64 ? -1 : 0;
182 static bool vhost_user_one_time_request(VhostUserRequest request)
184 switch (request) {
185 case VHOST_USER_SET_OWNER:
186 case VHOST_USER_RESET_OWNER:
187 case VHOST_USER_SET_MEM_TABLE:
188 case VHOST_USER_GET_QUEUE_NUM:
189 return true;
190 default:
191 return false;
195 /* most non-init callers ignore the error */
196 static int vhost_user_write(struct vhost_dev *dev, VhostUserMsg *msg,
197 int *fds, int fd_num)
199 CharDriverState *chr = dev->opaque;
200 int ret, size = VHOST_USER_HDR_SIZE + msg->size;
203 * For non-vring specific requests, like VHOST_USER_SET_MEM_TABLE,
204 * we just need send it once in the first time. For later such
205 * request, we just ignore it.
207 if (vhost_user_one_time_request(msg->request) && dev->vq_index != 0) {
208 return 0;
211 if (qemu_chr_fe_set_msgfds(chr, fds, fd_num) < 0) {
212 error_report("Failed to set msg fds.");
213 return -1;
216 ret = qemu_chr_fe_write_all(chr, (const uint8_t *) msg, size);
217 if (ret != size) {
218 error_report("Failed to write msg."
219 " Wrote %d instead of %d.", ret, size);
220 return -1;
223 return 0;
226 static int vhost_user_set_log_base(struct vhost_dev *dev, uint64_t base,
227 struct vhost_log *log)
229 int fds[VHOST_MEMORY_MAX_NREGIONS];
230 size_t fd_num = 0;
231 bool shmfd = virtio_has_feature(dev->protocol_features,
232 VHOST_USER_PROTOCOL_F_LOG_SHMFD);
233 VhostUserMsg msg = {
234 .request = VHOST_USER_SET_LOG_BASE,
235 .flags = VHOST_USER_VERSION,
236 .payload.log.mmap_size = log->size * sizeof(*(log->log)),
237 .payload.log.mmap_offset = 0,
238 .size = sizeof(msg.payload.log),
241 if (shmfd && log->fd != -1) {
242 fds[fd_num++] = log->fd;
245 if (vhost_user_write(dev, &msg, fds, fd_num) < 0) {
246 return -1;
249 if (shmfd) {
250 msg.size = 0;
251 if (vhost_user_read(dev, &msg) < 0) {
252 return -1;
255 if (msg.request != VHOST_USER_SET_LOG_BASE) {
256 error_report("Received unexpected msg type. "
257 "Expected %d received %d",
258 VHOST_USER_SET_LOG_BASE, msg.request);
259 return -1;
263 return 0;
266 static int vhost_user_set_vring_addr(struct vhost_dev *dev,
267 struct vhost_vring_addr *addr)
269 VhostUserMsg msg = {
270 .request = VHOST_USER_SET_VRING_ADDR,
271 .flags = VHOST_USER_VERSION,
272 .payload.addr = *addr,
273 .size = sizeof(msg.payload.addr),
276 if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
277 return -1;
280 return 0;
283 static int vhost_user_set_vring_endian(struct vhost_dev *dev,
284 struct vhost_vring_state *ring)
286 error_report("vhost-user trying to send unhandled ioctl");
287 return -1;
290 static int vhost_set_vring(struct vhost_dev *dev,
291 unsigned long int request,
292 struct vhost_vring_state *ring)
294 VhostUserMsg msg = {
295 .request = request,
296 .flags = VHOST_USER_VERSION,
297 .payload.state = *ring,
298 .size = sizeof(msg.payload.state),
301 if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
302 return -1;
305 return 0;
308 static int vhost_user_set_vring_num(struct vhost_dev *dev,
309 struct vhost_vring_state *ring)
311 return vhost_set_vring(dev, VHOST_USER_SET_VRING_NUM, ring);
314 static int vhost_user_set_vring_base(struct vhost_dev *dev,
315 struct vhost_vring_state *ring)
317 return vhost_set_vring(dev, VHOST_USER_SET_VRING_BASE, ring);
320 static int vhost_user_set_vring_enable(struct vhost_dev *dev, int enable)
322 int i;
324 if (!virtio_has_feature(dev->features, VHOST_USER_F_PROTOCOL_FEATURES)) {
325 return -1;
328 for (i = 0; i < dev->nvqs; ++i) {
329 struct vhost_vring_state state = {
330 .index = dev->vq_index + i,
331 .num = enable,
334 vhost_set_vring(dev, VHOST_USER_SET_VRING_ENABLE, &state);
337 return 0;
340 static int vhost_user_get_vring_base(struct vhost_dev *dev,
341 struct vhost_vring_state *ring)
343 VhostUserMsg msg = {
344 .request = VHOST_USER_GET_VRING_BASE,
345 .flags = VHOST_USER_VERSION,
346 .payload.state = *ring,
347 .size = sizeof(msg.payload.state),
350 if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
351 return -1;
354 if (vhost_user_read(dev, &msg) < 0) {
355 return -1;
358 if (msg.request != VHOST_USER_GET_VRING_BASE) {
359 error_report("Received unexpected msg type. Expected %d received %d",
360 VHOST_USER_GET_VRING_BASE, msg.request);
361 return -1;
364 if (msg.size != sizeof(msg.payload.state)) {
365 error_report("Received bad msg size.");
366 return -1;
369 *ring = msg.payload.state;
371 return 0;
374 static int vhost_set_vring_file(struct vhost_dev *dev,
375 VhostUserRequest request,
376 struct vhost_vring_file *file)
378 int fds[VHOST_MEMORY_MAX_NREGIONS];
379 size_t fd_num = 0;
380 VhostUserMsg msg = {
381 .request = request,
382 .flags = VHOST_USER_VERSION,
383 .payload.u64 = file->index & VHOST_USER_VRING_IDX_MASK,
384 .size = sizeof(msg.payload.u64),
387 if (ioeventfd_enabled() && file->fd > 0) {
388 fds[fd_num++] = file->fd;
389 } else {
390 msg.payload.u64 |= VHOST_USER_VRING_NOFD_MASK;
393 if (vhost_user_write(dev, &msg, fds, fd_num) < 0) {
394 return -1;
397 return 0;
400 static int vhost_user_set_vring_kick(struct vhost_dev *dev,
401 struct vhost_vring_file *file)
403 return vhost_set_vring_file(dev, VHOST_USER_SET_VRING_KICK, file);
406 static int vhost_user_set_vring_call(struct vhost_dev *dev,
407 struct vhost_vring_file *file)
409 return vhost_set_vring_file(dev, VHOST_USER_SET_VRING_CALL, file);
412 static int vhost_user_set_u64(struct vhost_dev *dev, int request, uint64_t u64)
414 VhostUserMsg msg = {
415 .request = request,
416 .flags = VHOST_USER_VERSION,
417 .payload.u64 = u64,
418 .size = sizeof(msg.payload.u64),
421 if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
422 return -1;
425 return 0;
428 static int vhost_user_set_features(struct vhost_dev *dev,
429 uint64_t features)
431 return vhost_user_set_u64(dev, VHOST_USER_SET_FEATURES, features);
434 static int vhost_user_set_protocol_features(struct vhost_dev *dev,
435 uint64_t features)
437 return vhost_user_set_u64(dev, VHOST_USER_SET_PROTOCOL_FEATURES, features);
440 static int vhost_user_get_u64(struct vhost_dev *dev, int request, uint64_t *u64)
442 VhostUserMsg msg = {
443 .request = request,
444 .flags = VHOST_USER_VERSION,
447 if (vhost_user_one_time_request(request) && dev->vq_index != 0) {
448 return 0;
451 if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
452 return -1;
455 if (vhost_user_read(dev, &msg) < 0) {
456 return -1;
459 if (msg.request != request) {
460 error_report("Received unexpected msg type. Expected %d received %d",
461 request, msg.request);
462 return -1;
465 if (msg.size != sizeof(msg.payload.u64)) {
466 error_report("Received bad msg size.");
467 return -1;
470 *u64 = msg.payload.u64;
472 return 0;
475 static int vhost_user_get_features(struct vhost_dev *dev, uint64_t *features)
477 return vhost_user_get_u64(dev, VHOST_USER_GET_FEATURES, features);
480 static int vhost_user_set_mem_table(struct vhost_dev *dev,
481 struct vhost_memory *mem)
483 int fds[VHOST_MEMORY_MAX_NREGIONS];
484 int i, fd;
485 size_t fd_num = 0;
486 uint64_t features;
487 bool reply_supported = virtio_has_feature(dev->protocol_features,
488 VHOST_USER_PROTOCOL_F_REPLY_ACK);
490 VhostUserMsg msg = {
491 .request = VHOST_USER_SET_MEM_TABLE,
492 .flags = VHOST_USER_VERSION,
495 if (reply_supported) {
496 msg.flags |= VHOST_USER_NEED_REPLY_MASK;
499 for (i = 0; i < dev->mem->nregions; ++i) {
500 struct vhost_memory_region *reg = dev->mem->regions + i;
501 ram_addr_t offset;
502 MemoryRegion *mr;
504 assert((uintptr_t)reg->userspace_addr == reg->userspace_addr);
505 mr = memory_region_from_host((void *)(uintptr_t)reg->userspace_addr,
506 &offset);
507 fd = memory_region_get_fd(mr);
508 if (fd > 0) {
509 msg.payload.memory.regions[fd_num].userspace_addr
510 = reg->userspace_addr;
511 msg.payload.memory.regions[fd_num].memory_size = reg->memory_size;
512 msg.payload.memory.regions[fd_num].guest_phys_addr
513 = reg->guest_phys_addr;
514 msg.payload.memory.regions[fd_num].mmap_offset = offset;
515 assert(fd_num < VHOST_MEMORY_MAX_NREGIONS);
516 fds[fd_num++] = fd;
520 msg.payload.memory.nregions = fd_num;
522 if (!fd_num) {
523 error_report("Failed initializing vhost-user memory map, "
524 "consider using -object memory-backend-file share=on");
525 return -1;
528 msg.size = sizeof(msg.payload.memory.nregions);
529 msg.size += sizeof(msg.payload.memory.padding);
530 msg.size += fd_num * sizeof(VhostUserMemoryRegion);
532 vhost_user_write(dev, &msg, fds, fd_num);
534 if (reply_supported) {
535 return process_message_reply(dev, msg.request);
536 } else {
537 /* Note: It is (yet) unknown when the client application has finished
538 * remapping the GPA.
539 * Attempt to prevent a race by sending a command that requires a reply.
541 vhost_user_get_features(dev, &features);
544 return 0;
547 static int vhost_user_set_owner(struct vhost_dev *dev)
549 VhostUserMsg msg = {
550 .request = VHOST_USER_SET_OWNER,
551 .flags = VHOST_USER_VERSION,
554 if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
555 return -1;
558 return 0;
561 static int vhost_user_reset_device(struct vhost_dev *dev)
563 VhostUserMsg msg = {
564 .request = VHOST_USER_RESET_OWNER,
565 .flags = VHOST_USER_VERSION,
568 if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
569 return -1;
572 return 0;
575 static int vhost_user_init(struct vhost_dev *dev, void *opaque)
577 uint64_t features;
578 int err;
580 assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER);
582 dev->opaque = opaque;
584 err = vhost_user_get_features(dev, &features);
585 if (err < 0) {
586 return err;
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,
593 &features);
594 if (err < 0) {
595 return err;
598 dev->protocol_features = features & VHOST_USER_PROTOCOL_FEATURE_MASK;
599 err = vhost_user_set_protocol_features(dev, dev->protocol_features);
600 if (err < 0) {
601 return err;
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,
607 &dev->max_queues);
608 if (err < 0) {
609 return err;
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.");
622 return 0;
625 static int vhost_user_cleanup(struct vhost_dev *dev)
627 assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER);
629 dev->opaque = 0;
631 return 0;
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);
638 return idx;
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)) {
662 return 0;
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);
675 return -1;
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)
682 ram_addr_t offset;
683 int mfd, rfd;
684 MemoryRegion *mr;
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);
692 return mfd == rfd;
695 const VhostOps user_ops = {
696 .backend_type = VHOST_BACKEND_TYPE_USER,
697 .vhost_backend_init = vhost_user_init,
698 .vhost_backend_cleanup = vhost_user_cleanup,
699 .vhost_backend_memslots_limit = vhost_user_memslots_limit,
700 .vhost_set_log_base = vhost_user_set_log_base,
701 .vhost_set_mem_table = vhost_user_set_mem_table,
702 .vhost_set_vring_addr = vhost_user_set_vring_addr,
703 .vhost_set_vring_endian = vhost_user_set_vring_endian,
704 .vhost_set_vring_num = vhost_user_set_vring_num,
705 .vhost_set_vring_base = vhost_user_set_vring_base,
706 .vhost_get_vring_base = vhost_user_get_vring_base,
707 .vhost_set_vring_kick = vhost_user_set_vring_kick,
708 .vhost_set_vring_call = vhost_user_set_vring_call,
709 .vhost_set_features = vhost_user_set_features,
710 .vhost_get_features = vhost_user_get_features,
711 .vhost_set_owner = vhost_user_set_owner,
712 .vhost_reset_device = vhost_user_reset_device,
713 .vhost_get_vq_index = vhost_user_get_vq_index,
714 .vhost_set_vring_enable = vhost_user_set_vring_enable,
715 .vhost_requires_shm_log = vhost_user_requires_shm_log,
716 .vhost_migration_done = vhost_user_migration_done,
717 .vhost_backend_can_merge = vhost_user_can_merge,