1 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2 From: Fiona Ebner <f.ebner@proxmox.com>
3 Date: Thu, 13 Oct 2022 11:33:50 +0200
4 Subject: [PATCH] PVE: add IOChannel implementation for savevm-async
6 based on migration/channel-block.c and the implementation that was
7 present in migration/savevm-async.c before QEMU 7.1.
9 Passes along read/write requests to the given BlockBackend, while
10 ensuring that a read request going beyond the end results in a
13 Additionally, allows tracking the current position from the outside
14 (intended to be used for progress tracking).
16 Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
18 migration/channel-savevm-async.c | 183 +++++++++++++++++++++++++++++++
19 migration/channel-savevm-async.h | 51 +++++++++
20 migration/meson.build | 1 +
21 3 files changed, 235 insertions(+)
22 create mode 100644 migration/channel-savevm-async.c
23 create mode 100644 migration/channel-savevm-async.h
25 diff --git a/migration/channel-savevm-async.c b/migration/channel-savevm-async.c
27 index 0000000000..aab081ce07
29 +++ b/migration/channel-savevm-async.c
32 + * QIO Channel implementation to be used by savevm-async QMP calls
34 +#include "qemu/osdep.h"
35 +#include "migration/channel-savevm-async.h"
36 +#include "qapi/error.h"
37 +#include "sysemu/block-backend.h"
40 +QIOChannelSavevmAsync *
41 +qio_channel_savevm_async_new(BlockBackend *be, size_t *bs_pos)
43 + QIOChannelSavevmAsync *ioc;
45 + ioc = QIO_CHANNEL_SAVEVM_ASYNC(object_new(TYPE_QIO_CHANNEL_SAVEVM_ASYNC));
47 + bdrv_ref(blk_bs(be));
49 + ioc->bs_pos = bs_pos;
56 +qio_channel_savevm_async_finalize(Object *obj)
58 + QIOChannelSavevmAsync *ioc = QIO_CHANNEL_SAVEVM_ASYNC(obj);
61 + bdrv_unref(blk_bs(ioc->be));
69 +qio_channel_savevm_async_readv(QIOChannel *ioc,
70 + const struct iovec *iov,
77 + QIOChannelSavevmAsync *saioc = QIO_CHANNEL_SAVEVM_ASYNC(ioc);
78 + BlockBackend *be = saioc->be;
79 + int64_t maxlen = blk_getlength(be);
84 + qemu_iovec_init_external(&qiov, (struct iovec *)iov, niov);
86 + if (*saioc->bs_pos >= maxlen) {
87 + error_setg(errp, "cannot read beyond maxlen");
91 + if (maxlen - *saioc->bs_pos < qiov.size) {
92 + size = maxlen - *saioc->bs_pos;
97 + // returns 0 on success
98 + ret = blk_preadv(be, *saioc->bs_pos, size, &qiov, 0);
100 + error_setg_errno(errp, -ret, "blk_preadv failed");
104 + *saioc->bs_pos += size;
110 +qio_channel_savevm_async_writev(QIOChannel *ioc,
111 + const struct iovec *iov,
118 + QIOChannelSavevmAsync *saioc = QIO_CHANNEL_SAVEVM_ASYNC(ioc);
119 + BlockBackend *be = saioc->be;
123 + qemu_iovec_init_external(&qiov, (struct iovec *)iov, niov);
125 + if (qemu_in_coroutine()) {
126 + ret = blk_co_pwritev(be, *saioc->bs_pos, qiov.size, &qiov, 0);
129 + ret = blk_pwritev(be, *saioc->bs_pos, qiov.size, &qiov, 0);
133 + error_setg_errno(errp, -ret, "blk(_co)_pwritev failed");
137 + *saioc->bs_pos += qiov.size;
143 +qio_channel_savevm_async_set_blocking(QIOChannel *ioc,
148 + error_setg(errp, "Non-blocking mode not supported for savevm-async");
156 +qio_channel_savevm_async_close(QIOChannel *ioc,
159 + QIOChannelSavevmAsync *saioc = QIO_CHANNEL_SAVEVM_ASYNC(ioc);
160 + int rv = bdrv_flush(blk_bs(saioc->be));
163 + error_setg_errno(errp, -rv, "Unable to flush VMState");
167 + bdrv_unref(blk_bs(saioc->be));
169 + saioc->bs_pos = NULL;
176 +qio_channel_savevm_async_set_aio_fd_handler(QIOChannel *ioc,
178 + IOHandler *io_read,
179 + IOHandler *io_write,
182 + // if channel-block starts doing something, check if this needs adaptation
187 +qio_channel_savevm_async_class_init(ObjectClass *klass,
188 + void *class_data G_GNUC_UNUSED)
190 + QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass);
192 + ioc_klass->io_writev = qio_channel_savevm_async_writev;
193 + ioc_klass->io_readv = qio_channel_savevm_async_readv;
194 + ioc_klass->io_set_blocking = qio_channel_savevm_async_set_blocking;
195 + ioc_klass->io_close = qio_channel_savevm_async_close;
196 + ioc_klass->io_set_aio_fd_handler = qio_channel_savevm_async_set_aio_fd_handler;
199 +static const TypeInfo qio_channel_savevm_async_info = {
200 + .parent = TYPE_QIO_CHANNEL,
201 + .name = TYPE_QIO_CHANNEL_SAVEVM_ASYNC,
202 + .instance_size = sizeof(QIOChannelSavevmAsync),
203 + .instance_finalize = qio_channel_savevm_async_finalize,
204 + .class_init = qio_channel_savevm_async_class_init,
208 +qio_channel_savevm_async_register_types(void)
210 + type_register_static(&qio_channel_savevm_async_info);
213 +type_init(qio_channel_savevm_async_register_types);
214 diff --git a/migration/channel-savevm-async.h b/migration/channel-savevm-async.h
216 index 0000000000..17ae2cb261
218 +++ b/migration/channel-savevm-async.h
221 + * QEMU I/O channels driver for savevm-async.c
223 + * Copyright (c) 2022 Proxmox Server Solutions
226 + * Fiona Ebner (f.ebner@proxmox.com)
228 + * This work is licensed under the terms of the GNU GPL, version 2 or later.
229 + * See the COPYING file in the top-level directory.
232 +#ifndef QIO_CHANNEL_SAVEVM_ASYNC_H
233 +#define QIO_CHANNEL_SAVEVM_ASYNC_H
235 +#include "io/channel.h"
236 +#include "qom/object.h"
238 +#define TYPE_QIO_CHANNEL_SAVEVM_ASYNC "qio-channel-savevm-async"
239 +OBJECT_DECLARE_SIMPLE_TYPE(QIOChannelSavevmAsync, QIO_CHANNEL_SAVEVM_ASYNC)
243 + * QIOChannelSavevmAsync:
245 + * The QIOChannelBlock object provides a channel implementation that is able to
246 + * perform I/O on any BlockBackend whose BlockDriverState directly contains a
247 + * VMState (as opposed to indirectly, like qcow2). It allows tracking the
248 + * current position from the outside.
250 +struct QIOChannelSavevmAsync {
258 + * qio_channel_savevm_async_new:
259 + * @be: the block backend
260 + * @bs_pos: used to keep track of the IOChannels current position
262 + * Create a new IO channel object that can perform I/O on a BlockBackend object
263 + * whose BlockDriverState directly contains a VMState.
265 + * Returns: the new channel object
267 +QIOChannelSavevmAsync *
268 +qio_channel_savevm_async_new(BlockBackend *be, size_t *bs_pos);
270 +#endif /* QIO_CHANNEL_SAVEVM_ASYNC_H */
271 diff --git a/migration/meson.build b/migration/meson.build
272 index 0d1bb9f96e..8a142fc7a9 100644
273 --- a/migration/meson.build
274 +++ b/migration/meson.build
275 @@ -13,6 +13,7 @@ softmmu_ss.add(files(
276 'block-dirty-bitmap.c',
279 + 'channel-savevm-async.c',