vhost-user: Introduce a new protocol feature REPLY_ACK.
[qemu/cris-port.git] / hw / virtio / vhost-user.c
blobb57454a4b737e6a86fc8dd651bc97aa1a6ae929a
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_mem_table(struct vhost_dev *dev,
267 struct vhost_memory *mem)
269 int fds[VHOST_MEMORY_MAX_NREGIONS];
270 int i, fd;
271 size_t fd_num = 0;
272 bool reply_supported = virtio_has_feature(dev->protocol_features,
273 VHOST_USER_PROTOCOL_F_REPLY_ACK);
275 VhostUserMsg msg = {
276 .request = VHOST_USER_SET_MEM_TABLE,
277 .flags = VHOST_USER_VERSION,
280 if (reply_supported) {
281 msg.flags |= VHOST_USER_NEED_REPLY_MASK;
284 for (i = 0; i < dev->mem->nregions; ++i) {
285 struct vhost_memory_region *reg = dev->mem->regions + i;
286 ram_addr_t offset;
287 MemoryRegion *mr;
289 assert((uintptr_t)reg->userspace_addr == reg->userspace_addr);
290 mr = memory_region_from_host((void *)(uintptr_t)reg->userspace_addr,
291 &offset);
292 fd = memory_region_get_fd(mr);
293 if (fd > 0) {
294 msg.payload.memory.regions[fd_num].userspace_addr = reg->userspace_addr;
295 msg.payload.memory.regions[fd_num].memory_size = reg->memory_size;
296 msg.payload.memory.regions[fd_num].guest_phys_addr = reg->guest_phys_addr;
297 msg.payload.memory.regions[fd_num].mmap_offset = offset;
298 assert(fd_num < VHOST_MEMORY_MAX_NREGIONS);
299 fds[fd_num++] = fd;
303 msg.payload.memory.nregions = fd_num;
305 if (!fd_num) {
306 error_report("Failed initializing vhost-user memory map, "
307 "consider using -object memory-backend-file share=on");
308 return -1;
311 msg.size = sizeof(msg.payload.memory.nregions);
312 msg.size += sizeof(msg.payload.memory.padding);
313 msg.size += fd_num * sizeof(VhostUserMemoryRegion);
315 if (vhost_user_write(dev, &msg, fds, fd_num) < 0) {
316 return -1;
319 if (reply_supported) {
320 return process_message_reply(dev, msg.request);
323 return 0;
326 static int vhost_user_set_vring_addr(struct vhost_dev *dev,
327 struct vhost_vring_addr *addr)
329 VhostUserMsg msg = {
330 .request = VHOST_USER_SET_VRING_ADDR,
331 .flags = VHOST_USER_VERSION,
332 .payload.addr = *addr,
333 .size = sizeof(msg.payload.addr),
336 if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
337 return -1;
340 return 0;
343 static int vhost_user_set_vring_endian(struct vhost_dev *dev,
344 struct vhost_vring_state *ring)
346 error_report("vhost-user trying to send unhandled ioctl");
347 return -1;
350 static int vhost_set_vring(struct vhost_dev *dev,
351 unsigned long int request,
352 struct vhost_vring_state *ring)
354 VhostUserMsg msg = {
355 .request = request,
356 .flags = VHOST_USER_VERSION,
357 .payload.state = *ring,
358 .size = sizeof(msg.payload.state),
361 if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
362 return -1;
365 return 0;
368 static int vhost_user_set_vring_num(struct vhost_dev *dev,
369 struct vhost_vring_state *ring)
371 return vhost_set_vring(dev, VHOST_USER_SET_VRING_NUM, ring);
374 static int vhost_user_set_vring_base(struct vhost_dev *dev,
375 struct vhost_vring_state *ring)
377 return vhost_set_vring(dev, VHOST_USER_SET_VRING_BASE, ring);
380 static int vhost_user_set_vring_enable(struct vhost_dev *dev, int enable)
382 int i;
384 if (!virtio_has_feature(dev->features, VHOST_USER_F_PROTOCOL_FEATURES)) {
385 return -1;
388 for (i = 0; i < dev->nvqs; ++i) {
389 struct vhost_vring_state state = {
390 .index = dev->vq_index + i,
391 .num = enable,
394 vhost_set_vring(dev, VHOST_USER_SET_VRING_ENABLE, &state);
397 return 0;
400 static int vhost_user_get_vring_base(struct vhost_dev *dev,
401 struct vhost_vring_state *ring)
403 VhostUserMsg msg = {
404 .request = VHOST_USER_GET_VRING_BASE,
405 .flags = VHOST_USER_VERSION,
406 .payload.state = *ring,
407 .size = sizeof(msg.payload.state),
410 if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
411 return -1;
414 if (vhost_user_read(dev, &msg) < 0) {
415 return -1;
418 if (msg.request != VHOST_USER_GET_VRING_BASE) {
419 error_report("Received unexpected msg type. Expected %d received %d",
420 VHOST_USER_GET_VRING_BASE, msg.request);
421 return -1;
424 if (msg.size != sizeof(msg.payload.state)) {
425 error_report("Received bad msg size.");
426 return -1;
429 *ring = msg.payload.state;
431 return 0;
434 static int vhost_set_vring_file(struct vhost_dev *dev,
435 VhostUserRequest request,
436 struct vhost_vring_file *file)
438 int fds[VHOST_MEMORY_MAX_NREGIONS];
439 size_t fd_num = 0;
440 VhostUserMsg msg = {
441 .request = request,
442 .flags = VHOST_USER_VERSION,
443 .payload.u64 = file->index & VHOST_USER_VRING_IDX_MASK,
444 .size = sizeof(msg.payload.u64),
447 if (ioeventfd_enabled() && file->fd > 0) {
448 fds[fd_num++] = file->fd;
449 } else {
450 msg.payload.u64 |= VHOST_USER_VRING_NOFD_MASK;
453 if (vhost_user_write(dev, &msg, fds, fd_num) < 0) {
454 return -1;
457 return 0;
460 static int vhost_user_set_vring_kick(struct vhost_dev *dev,
461 struct vhost_vring_file *file)
463 return vhost_set_vring_file(dev, VHOST_USER_SET_VRING_KICK, file);
466 static int vhost_user_set_vring_call(struct vhost_dev *dev,
467 struct vhost_vring_file *file)
469 return vhost_set_vring_file(dev, VHOST_USER_SET_VRING_CALL, file);
472 static int vhost_user_set_u64(struct vhost_dev *dev, int request, uint64_t u64)
474 VhostUserMsg msg = {
475 .request = request,
476 .flags = VHOST_USER_VERSION,
477 .payload.u64 = u64,
478 .size = sizeof(msg.payload.u64),
481 if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
482 return -1;
485 return 0;
488 static int vhost_user_set_features(struct vhost_dev *dev,
489 uint64_t features)
491 return vhost_user_set_u64(dev, VHOST_USER_SET_FEATURES, features);
494 static int vhost_user_set_protocol_features(struct vhost_dev *dev,
495 uint64_t features)
497 return vhost_user_set_u64(dev, VHOST_USER_SET_PROTOCOL_FEATURES, features);
500 static int vhost_user_get_u64(struct vhost_dev *dev, int request, uint64_t *u64)
502 VhostUserMsg msg = {
503 .request = request,
504 .flags = VHOST_USER_VERSION,
507 if (vhost_user_one_time_request(request) && dev->vq_index != 0) {
508 return 0;
511 if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
512 return -1;
515 if (vhost_user_read(dev, &msg) < 0) {
516 return -1;
519 if (msg.request != request) {
520 error_report("Received unexpected msg type. Expected %d received %d",
521 request, msg.request);
522 return -1;
525 if (msg.size != sizeof(msg.payload.u64)) {
526 error_report("Received bad msg size.");
527 return -1;
530 *u64 = msg.payload.u64;
532 return 0;
535 static int vhost_user_get_features(struct vhost_dev *dev, uint64_t *features)
537 return vhost_user_get_u64(dev, VHOST_USER_GET_FEATURES, features);
540 static int vhost_user_set_owner(struct vhost_dev *dev)
542 VhostUserMsg msg = {
543 .request = VHOST_USER_SET_OWNER,
544 .flags = VHOST_USER_VERSION,
547 if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
548 return -1;
551 return 0;
554 static int vhost_user_reset_device(struct vhost_dev *dev)
556 VhostUserMsg msg = {
557 .request = VHOST_USER_RESET_OWNER,
558 .flags = VHOST_USER_VERSION,
561 if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
562 return -1;
565 return 0;
568 static int vhost_user_init(struct vhost_dev *dev, void *opaque)
570 uint64_t features;
571 int err;
573 assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER);
575 dev->opaque = opaque;
577 err = vhost_user_get_features(dev, &features);
578 if (err < 0) {
579 return err;
582 if (virtio_has_feature(features, VHOST_USER_F_PROTOCOL_FEATURES)) {
583 dev->backend_features |= 1ULL << VHOST_USER_F_PROTOCOL_FEATURES;
585 err = vhost_user_get_u64(dev, VHOST_USER_GET_PROTOCOL_FEATURES,
586 &features);
587 if (err < 0) {
588 return err;
591 dev->protocol_features = features & VHOST_USER_PROTOCOL_FEATURE_MASK;
592 err = vhost_user_set_protocol_features(dev, dev->protocol_features);
593 if (err < 0) {
594 return err;
597 /* query the max queues we support if backend supports Multiple Queue */
598 if (dev->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_MQ)) {
599 err = vhost_user_get_u64(dev, VHOST_USER_GET_QUEUE_NUM,
600 &dev->max_queues);
601 if (err < 0) {
602 return err;
607 if (dev->migration_blocker == NULL &&
608 !virtio_has_feature(dev->protocol_features,
609 VHOST_USER_PROTOCOL_F_LOG_SHMFD)) {
610 error_setg(&dev->migration_blocker,
611 "Migration disabled: vhost-user backend lacks "
612 "VHOST_USER_PROTOCOL_F_LOG_SHMFD feature.");
615 return 0;
618 static int vhost_user_cleanup(struct vhost_dev *dev)
620 assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER);
622 dev->opaque = 0;
624 return 0;
627 static int vhost_user_get_vq_index(struct vhost_dev *dev, int idx)
629 assert(idx >= dev->vq_index && idx < dev->vq_index + dev->nvqs);
631 return idx;
634 static int vhost_user_memslots_limit(struct vhost_dev *dev)
636 return VHOST_MEMORY_MAX_NREGIONS;
639 static bool vhost_user_requires_shm_log(struct vhost_dev *dev)
641 assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER);
643 return virtio_has_feature(dev->protocol_features,
644 VHOST_USER_PROTOCOL_F_LOG_SHMFD);
647 static int vhost_user_migration_done(struct vhost_dev *dev, char* mac_addr)
649 VhostUserMsg msg = { 0 };
651 assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER);
653 /* If guest supports GUEST_ANNOUNCE do nothing */
654 if (virtio_has_feature(dev->acked_features, VIRTIO_NET_F_GUEST_ANNOUNCE)) {
655 return 0;
658 /* if backend supports VHOST_USER_PROTOCOL_F_RARP ask it to send the RARP */
659 if (virtio_has_feature(dev->protocol_features,
660 VHOST_USER_PROTOCOL_F_RARP)) {
661 msg.request = VHOST_USER_SEND_RARP;
662 msg.flags = VHOST_USER_VERSION;
663 memcpy((char *)&msg.payload.u64, mac_addr, 6);
664 msg.size = sizeof(msg.payload.u64);
666 return vhost_user_write(dev, &msg, NULL, 0);
668 return -1;
671 static bool vhost_user_can_merge(struct vhost_dev *dev,
672 uint64_t start1, uint64_t size1,
673 uint64_t start2, uint64_t size2)
675 ram_addr_t offset;
676 int mfd, rfd;
677 MemoryRegion *mr;
679 mr = memory_region_from_host((void *)(uintptr_t)start1, &offset);
680 mfd = memory_region_get_fd(mr);
682 mr = memory_region_from_host((void *)(uintptr_t)start2, &offset);
683 rfd = memory_region_get_fd(mr);
685 return mfd == rfd;
688 const VhostOps user_ops = {
689 .backend_type = VHOST_BACKEND_TYPE_USER,
690 .vhost_backend_init = vhost_user_init,
691 .vhost_backend_cleanup = vhost_user_cleanup,
692 .vhost_backend_memslots_limit = vhost_user_memslots_limit,
693 .vhost_set_log_base = vhost_user_set_log_base,
694 .vhost_set_mem_table = vhost_user_set_mem_table,
695 .vhost_set_vring_addr = vhost_user_set_vring_addr,
696 .vhost_set_vring_endian = vhost_user_set_vring_endian,
697 .vhost_set_vring_num = vhost_user_set_vring_num,
698 .vhost_set_vring_base = vhost_user_set_vring_base,
699 .vhost_get_vring_base = vhost_user_get_vring_base,
700 .vhost_set_vring_kick = vhost_user_set_vring_kick,
701 .vhost_set_vring_call = vhost_user_set_vring_call,
702 .vhost_set_features = vhost_user_set_features,
703 .vhost_get_features = vhost_user_get_features,
704 .vhost_set_owner = vhost_user_set_owner,
705 .vhost_reset_device = vhost_user_reset_device,
706 .vhost_get_vq_index = vhost_user_get_vq_index,
707 .vhost_set_vring_enable = vhost_user_set_vring_enable,
708 .vhost_requires_shm_log = vhost_user_requires_shm_log,
709 .vhost_migration_done = vhost_user_migration_done,
710 .vhost_backend_can_merge = vhost_user_can_merge,