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 | 183 +++++++++++++++++++++++++++++++
20 migration/channel-savevm-async.h | 51 +++++++++
21 migration/meson.build | 1 +
22 3 files changed, 235 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..aab081ce07
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,
179 + IOHandler *io_read,
180 + IOHandler *io_write,
183 + // if channel-block starts doing something, check if this needs adaptation
188 +qio_channel_savevm_async_class_init(ObjectClass *klass,
189 + void *class_data G_GNUC_UNUSED)
191 + QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass);
193 + ioc_klass->io_writev = qio_channel_savevm_async_writev;
194 + ioc_klass->io_readv = qio_channel_savevm_async_readv;
195 + ioc_klass->io_set_blocking = qio_channel_savevm_async_set_blocking;
196 + ioc_klass->io_close = qio_channel_savevm_async_close;
197 + ioc_klass->io_set_aio_fd_handler = qio_channel_savevm_async_set_aio_fd_handler;
200 +static const TypeInfo qio_channel_savevm_async_info = {
201 + .parent = TYPE_QIO_CHANNEL,
202 + .name = TYPE_QIO_CHANNEL_SAVEVM_ASYNC,
203 + .instance_size = sizeof(QIOChannelSavevmAsync),
204 + .instance_finalize = qio_channel_savevm_async_finalize,
205 + .class_init = qio_channel_savevm_async_class_init,
209 +qio_channel_savevm_async_register_types(void)
211 + type_register_static(&qio_channel_savevm_async_info);
214 +type_init(qio_channel_savevm_async_register_types);
215 diff --git a/migration/channel-savevm-async.h b/migration/channel-savevm-async.h
217 index 0000000000..17ae2cb261
219 +++ b/migration/channel-savevm-async.h
222 + * QEMU I/O channels driver for savevm-async.c
224 + * Copyright (c) 2022 Proxmox Server Solutions
227 + * Fiona Ebner (f.ebner@proxmox.com)
229 + * This work is licensed under the terms of the GNU GPL, version 2 or later.
230 + * See the COPYING file in the top-level directory.
233 +#ifndef QIO_CHANNEL_SAVEVM_ASYNC_H
234 +#define QIO_CHANNEL_SAVEVM_ASYNC_H
236 +#include "io/channel.h"
237 +#include "qom/object.h"
239 +#define TYPE_QIO_CHANNEL_SAVEVM_ASYNC "qio-channel-savevm-async"
240 +OBJECT_DECLARE_SIMPLE_TYPE(QIOChannelSavevmAsync, QIO_CHANNEL_SAVEVM_ASYNC)
244 + * QIOChannelSavevmAsync:
246 + * The QIOChannelBlock object provides a channel implementation that is able to
247 + * perform I/O on any BlockBackend whose BlockDriverState directly contains a
248 + * VMState (as opposed to indirectly, like qcow2). It allows tracking the
249 + * current position from the outside.
251 +struct QIOChannelSavevmAsync {
259 + * qio_channel_savevm_async_new:
260 + * @be: the block backend
261 + * @bs_pos: used to keep track of the IOChannels current position
263 + * Create a new IO channel object that can perform I/O on a BlockBackend object
264 + * whose BlockDriverState directly contains a VMState.
266 + * Returns: the new channel object
268 +QIOChannelSavevmAsync *
269 +qio_channel_savevm_async_new(BlockBackend *be, size_t *bs_pos);
271 +#endif /* QIO_CHANNEL_SAVEVM_ASYNC_H */
272 diff --git a/migration/meson.build b/migration/meson.build
273 index 1ae28523a1..37ddcb5d60 100644
274 --- a/migration/meson.build
275 +++ b/migration/meson.build
276 @@ -13,6 +13,7 @@ system_ss.add(files(
277 'block-dirty-bitmap.c',
280 + 'channel-savevm-async.c',