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>
17 Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
19 migration/channel-savevm-async.c | 184 +++++++++++++++++++++++++++++++
20 migration/channel-savevm-async.h | 51 +++++++++
21 migration/meson.build | 1 +
22 3 files changed, 236 insertions(+)
23 create mode 100644 migration/channel-savevm-async.c
24 create mode 100644 migration/channel-savevm-async.h
26 diff --git a/migration/channel-savevm-async.c b/migration/channel-savevm-async.c
28 index 0000000000..081a192f49
30 +++ b/migration/channel-savevm-async.c
33 + * QIO Channel implementation to be used by savevm-async QMP calls
35 +#include "qemu/osdep.h"
36 +#include "migration/channel-savevm-async.h"
37 +#include "qapi/error.h"
38 +#include "sysemu/block-backend.h"
41 +QIOChannelSavevmAsync *
42 +qio_channel_savevm_async_new(BlockBackend *be, size_t *bs_pos)
44 + QIOChannelSavevmAsync *ioc;
46 + ioc = QIO_CHANNEL_SAVEVM_ASYNC(object_new(TYPE_QIO_CHANNEL_SAVEVM_ASYNC));
48 + bdrv_ref(blk_bs(be));
50 + ioc->bs_pos = bs_pos;
57 +qio_channel_savevm_async_finalize(Object *obj)
59 + QIOChannelSavevmAsync *ioc = QIO_CHANNEL_SAVEVM_ASYNC(obj);
62 + bdrv_unref(blk_bs(ioc->be));
70 +qio_channel_savevm_async_readv(QIOChannel *ioc,
71 + const struct iovec *iov,
78 + QIOChannelSavevmAsync *saioc = QIO_CHANNEL_SAVEVM_ASYNC(ioc);
79 + BlockBackend *be = saioc->be;
80 + int64_t maxlen = blk_getlength(be);
85 + qemu_iovec_init_external(&qiov, (struct iovec *)iov, niov);
87 + if (*saioc->bs_pos >= maxlen) {
88 + error_setg(errp, "cannot read beyond maxlen");
92 + if (maxlen - *saioc->bs_pos < qiov.size) {
93 + size = maxlen - *saioc->bs_pos;
98 + // returns 0 on success
99 + ret = blk_preadv(be, *saioc->bs_pos, size, &qiov, 0);
101 + error_setg_errno(errp, -ret, "blk_preadv failed");
105 + *saioc->bs_pos += size;
111 +qio_channel_savevm_async_writev(QIOChannel *ioc,
112 + const struct iovec *iov,
119 + QIOChannelSavevmAsync *saioc = QIO_CHANNEL_SAVEVM_ASYNC(ioc);
120 + BlockBackend *be = saioc->be;
124 + qemu_iovec_init_external(&qiov, (struct iovec *)iov, niov);
126 + if (qemu_in_coroutine()) {
127 + ret = blk_co_pwritev(be, *saioc->bs_pos, qiov.size, &qiov, 0);
130 + ret = blk_pwritev(be, *saioc->bs_pos, qiov.size, &qiov, 0);
134 + error_setg_errno(errp, -ret, "blk(_co)_pwritev failed");
138 + *saioc->bs_pos += qiov.size;
144 +qio_channel_savevm_async_set_blocking(QIOChannel *ioc,
149 + error_setg(errp, "Non-blocking mode not supported for savevm-async");
157 +qio_channel_savevm_async_close(QIOChannel *ioc,
160 + QIOChannelSavevmAsync *saioc = QIO_CHANNEL_SAVEVM_ASYNC(ioc);
161 + int rv = bdrv_flush(blk_bs(saioc->be));
164 + error_setg_errno(errp, -rv, "Unable to flush VMState");
168 + bdrv_unref(blk_bs(saioc->be));
170 + saioc->bs_pos = NULL;
177 +qio_channel_savevm_async_set_aio_fd_handler(QIOChannel *ioc,
178 + AioContext *read_ctx,
179 + IOHandler *io_read,
180 + AioContext *write_ctx,
181 + IOHandler *io_write,
184 + // if channel-block starts doing something, check if this needs adaptation
189 +qio_channel_savevm_async_class_init(ObjectClass *klass,
190 + void *class_data G_GNUC_UNUSED)
192 + QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass);
194 + ioc_klass->io_writev = qio_channel_savevm_async_writev;
195 + ioc_klass->io_readv = qio_channel_savevm_async_readv;
196 + ioc_klass->io_set_blocking = qio_channel_savevm_async_set_blocking;
197 + ioc_klass->io_close = qio_channel_savevm_async_close;
198 + ioc_klass->io_set_aio_fd_handler = qio_channel_savevm_async_set_aio_fd_handler;
201 +static const TypeInfo qio_channel_savevm_async_info = {
202 + .parent = TYPE_QIO_CHANNEL,
203 + .name = TYPE_QIO_CHANNEL_SAVEVM_ASYNC,
204 + .instance_size = sizeof(QIOChannelSavevmAsync),
205 + .instance_finalize = qio_channel_savevm_async_finalize,
206 + .class_init = qio_channel_savevm_async_class_init,
210 +qio_channel_savevm_async_register_types(void)
212 + type_register_static(&qio_channel_savevm_async_info);
215 +type_init(qio_channel_savevm_async_register_types);
216 diff --git a/migration/channel-savevm-async.h b/migration/channel-savevm-async.h
218 index 0000000000..17ae2cb261
220 +++ b/migration/channel-savevm-async.h
223 + * QEMU I/O channels driver for savevm-async.c
225 + * Copyright (c) 2022 Proxmox Server Solutions
228 + * Fiona Ebner (f.ebner@proxmox.com)
230 + * This work is licensed under the terms of the GNU GPL, version 2 or later.
231 + * See the COPYING file in the top-level directory.
234 +#ifndef QIO_CHANNEL_SAVEVM_ASYNC_H
235 +#define QIO_CHANNEL_SAVEVM_ASYNC_H
237 +#include "io/channel.h"
238 +#include "qom/object.h"
240 +#define TYPE_QIO_CHANNEL_SAVEVM_ASYNC "qio-channel-savevm-async"
241 +OBJECT_DECLARE_SIMPLE_TYPE(QIOChannelSavevmAsync, QIO_CHANNEL_SAVEVM_ASYNC)
245 + * QIOChannelSavevmAsync:
247 + * The QIOChannelBlock object provides a channel implementation that is able to
248 + * perform I/O on any BlockBackend whose BlockDriverState directly contains a
249 + * VMState (as opposed to indirectly, like qcow2). It allows tracking the
250 + * current position from the outside.
252 +struct QIOChannelSavevmAsync {
260 + * qio_channel_savevm_async_new:
261 + * @be: the block backend
262 + * @bs_pos: used to keep track of the IOChannels current position
264 + * Create a new IO channel object that can perform I/O on a BlockBackend object
265 + * whose BlockDriverState directly contains a VMState.
267 + * Returns: the new channel object
269 +QIOChannelSavevmAsync *
270 +qio_channel_savevm_async_new(BlockBackend *be, size_t *bs_pos);
272 +#endif /* QIO_CHANNEL_SAVEVM_ASYNC_H */
273 diff --git a/migration/meson.build b/migration/meson.build
274 index 1eeb915ff6..95d1cf2250 100644
275 --- a/migration/meson.build
276 +++ b/migration/meson.build
277 @@ -13,6 +13,7 @@ system_ss.add(files(
278 'block-dirty-bitmap.c',
281 + 'channel-savevm-async.c',