]> git.proxmox.com Git - pve-qemu.git/blob - debian/patches/pve/0016-PVE-add-IOChannel-implementation-for-savevm-async.patch
update submodule and patches to 7.1.0
[pve-qemu.git] / debian / patches / pve / 0016-PVE-add-IOChannel-implementation-for-savevm-async.patch
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
5
6 based on migration/channel-block.c and the implementation that was
7 present in migration/savevm-async.c before QEMU 7.1.
8
9 Passes along read/write requests to the given BlockBackend, while
10 ensuring that a read request going beyond the end results in a
11 graceful short read.
12
13 Additionally, allows tracking the current position from the outside
14 (intended to be used for progress tracking).
15
16 Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
17 ---
18 migration/channel-savevm-async.c | 181 +++++++++++++++++++++++++++++++
19 migration/channel-savevm-async.h | 51 +++++++++
20 migration/meson.build | 1 +
21 3 files changed, 233 insertions(+)
22 create mode 100644 migration/channel-savevm-async.c
23 create mode 100644 migration/channel-savevm-async.h
24
25 diff --git a/migration/channel-savevm-async.c b/migration/channel-savevm-async.c
26 new file mode 100644
27 index 0000000000..efea3842cf
28 --- /dev/null
29 +++ b/migration/channel-savevm-async.c
30 @@ -0,0 +1,181 @@
31 +/*
32 + * QIO Channel implementation to be used by savevm-async QMP calls
33 + */
34 +#include "qemu/osdep.h"
35 +#include "migration/channel-savevm-async.h"
36 +#include "qapi/error.h"
37 +#include "sysemu/block-backend.h"
38 +#include "trace.h"
39 +
40 +QIOChannelSavevmAsync *
41 +qio_channel_savevm_async_new(BlockBackend *be, size_t *bs_pos)
42 +{
43 + QIOChannelSavevmAsync *ioc;
44 +
45 + ioc = QIO_CHANNEL_SAVEVM_ASYNC(object_new(TYPE_QIO_CHANNEL_SAVEVM_ASYNC));
46 +
47 + bdrv_ref(blk_bs(be));
48 + ioc->be = be;
49 + ioc->bs_pos = bs_pos;
50 +
51 + return ioc;
52 +}
53 +
54 +
55 +static void
56 +qio_channel_savevm_async_finalize(Object *obj)
57 +{
58 + QIOChannelSavevmAsync *ioc = QIO_CHANNEL_SAVEVM_ASYNC(obj);
59 +
60 + if (ioc->be) {
61 + bdrv_unref(blk_bs(ioc->be));
62 + ioc->be = NULL;
63 + }
64 + ioc->bs_pos = NULL;
65 +}
66 +
67 +
68 +static ssize_t
69 +qio_channel_savevm_async_readv(QIOChannel *ioc,
70 + const struct iovec *iov,
71 + size_t niov,
72 + int **fds,
73 + size_t *nfds,
74 + Error **errp)
75 +{
76 + QIOChannelSavevmAsync *saioc = QIO_CHANNEL_SAVEVM_ASYNC(ioc);
77 + BlockBackend *be = saioc->be;
78 + int64_t maxlen = blk_getlength(be);
79 + QEMUIOVector qiov;
80 + size_t size;
81 + int ret;
82 +
83 + qemu_iovec_init_external(&qiov, (struct iovec *)iov, niov);
84 +
85 + if (*saioc->bs_pos >= maxlen) {
86 + error_setg(errp, "cannot read beyond maxlen");
87 + return -1;
88 + }
89 +
90 + if (maxlen - *saioc->bs_pos < qiov.size) {
91 + size = maxlen - *saioc->bs_pos;
92 + } else {
93 + size = qiov.size;
94 + }
95 +
96 + // returns 0 on success
97 + ret = blk_preadv(be, *saioc->bs_pos, size, &qiov, 0);
98 + if (ret < 0) {
99 + error_setg(errp, "blk_preadv returned %d", ret);
100 + return -1;
101 + }
102 +
103 + *saioc->bs_pos += size;
104 + return size;
105 +}
106 +
107 +
108 +static ssize_t
109 +qio_channel_savevm_async_writev(QIOChannel *ioc,
110 + const struct iovec *iov,
111 + size_t niov,
112 + int *fds,
113 + size_t nfds,
114 + int flags,
115 + Error **errp)
116 +{
117 + QIOChannelSavevmAsync *saioc = QIO_CHANNEL_SAVEVM_ASYNC(ioc);
118 + BlockBackend *be = saioc->be;
119 + QEMUIOVector qiov;
120 + int ret;
121 +
122 + qemu_iovec_init_external(&qiov, (struct iovec *)iov, niov);
123 +
124 + if (qemu_in_coroutine()) {
125 + ret = blk_co_pwritev(be, *saioc->bs_pos, qiov.size, &qiov, 0);
126 + aio_wait_kick();
127 + } else {
128 + ret = blk_pwritev(be, *saioc->bs_pos, qiov.size, &qiov, 0);
129 + }
130 +
131 + if (ret < 0) {
132 + return ret;
133 + }
134 +
135 + *saioc->bs_pos += qiov.size;
136 + return qiov.size;
137 +}
138 +
139 +
140 +static int
141 +qio_channel_savevm_async_set_blocking(QIOChannel *ioc,
142 + bool enabled,
143 + Error **errp)
144 +{
145 + if (!enabled) {
146 + error_setg(errp, "Non-blocking mode not supported for savevm-async");
147 + return -1;
148 + }
149 + return 0;
150 +}
151 +
152 +
153 +static int
154 +qio_channel_savevm_async_close(QIOChannel *ioc,
155 + Error **errp)
156 +{
157 + QIOChannelSavevmAsync *saioc = QIO_CHANNEL_SAVEVM_ASYNC(ioc);
158 + int rv = bdrv_flush(blk_bs(saioc->be));
159 +
160 + if (rv < 0) {
161 + error_setg_errno(errp, -rv, "Unable to flush VMState");
162 + return -1;
163 + }
164 +
165 + bdrv_unref(blk_bs(saioc->be));
166 + saioc->be = NULL;
167 + saioc->bs_pos = NULL;
168 +
169 + return 0;
170 +}
171 +
172 +
173 +static void
174 +qio_channel_savevm_async_set_aio_fd_handler(QIOChannel *ioc,
175 + AioContext *ctx,
176 + IOHandler *io_read,
177 + IOHandler *io_write,
178 + void *opaque)
179 +{
180 + // if channel-block starts doing something, check if this needs adaptation
181 +}
182 +
183 +
184 +static void
185 +qio_channel_savevm_async_class_init(ObjectClass *klass,
186 + void *class_data G_GNUC_UNUSED)
187 +{
188 + QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass);
189 +
190 + ioc_klass->io_writev = qio_channel_savevm_async_writev;
191 + ioc_klass->io_readv = qio_channel_savevm_async_readv;
192 + ioc_klass->io_set_blocking = qio_channel_savevm_async_set_blocking;
193 + ioc_klass->io_close = qio_channel_savevm_async_close;
194 + ioc_klass->io_set_aio_fd_handler = qio_channel_savevm_async_set_aio_fd_handler;
195 +}
196 +
197 +static const TypeInfo qio_channel_savevm_async_info = {
198 + .parent = TYPE_QIO_CHANNEL,
199 + .name = TYPE_QIO_CHANNEL_SAVEVM_ASYNC,
200 + .instance_size = sizeof(QIOChannelSavevmAsync),
201 + .instance_finalize = qio_channel_savevm_async_finalize,
202 + .class_init = qio_channel_savevm_async_class_init,
203 +};
204 +
205 +static void
206 +qio_channel_savevm_async_register_types(void)
207 +{
208 + type_register_static(&qio_channel_savevm_async_info);
209 +}
210 +
211 +type_init(qio_channel_savevm_async_register_types);
212 diff --git a/migration/channel-savevm-async.h b/migration/channel-savevm-async.h
213 new file mode 100644
214 index 0000000000..17ae2cb261
215 --- /dev/null
216 +++ b/migration/channel-savevm-async.h
217 @@ -0,0 +1,51 @@
218 +/*
219 + * QEMU I/O channels driver for savevm-async.c
220 + *
221 + * Copyright (c) 2022 Proxmox Server Solutions
222 + *
223 + * Authors:
224 + * Fiona Ebner (f.ebner@proxmox.com)
225 + *
226 + * This work is licensed under the terms of the GNU GPL, version 2 or later.
227 + * See the COPYING file in the top-level directory.
228 + */
229 +
230 +#ifndef QIO_CHANNEL_SAVEVM_ASYNC_H
231 +#define QIO_CHANNEL_SAVEVM_ASYNC_H
232 +
233 +#include "io/channel.h"
234 +#include "qom/object.h"
235 +
236 +#define TYPE_QIO_CHANNEL_SAVEVM_ASYNC "qio-channel-savevm-async"
237 +OBJECT_DECLARE_SIMPLE_TYPE(QIOChannelSavevmAsync, QIO_CHANNEL_SAVEVM_ASYNC)
238 +
239 +
240 +/**
241 + * QIOChannelSavevmAsync:
242 + *
243 + * The QIOChannelBlock object provides a channel implementation that is able to
244 + * perform I/O on any BlockBackend whose BlockDriverState directly contains a
245 + * VMState (as opposed to indirectly, like qcow2). It allows tracking the
246 + * current position from the outside.
247 + */
248 +struct QIOChannelSavevmAsync {
249 + QIOChannel parent;
250 + BlockBackend *be;
251 + size_t *bs_pos;
252 +};
253 +
254 +
255 +/**
256 + * qio_channel_savevm_async_new:
257 + * @be: the block backend
258 + * @bs_pos: used to keep track of the IOChannels current position
259 + *
260 + * Create a new IO channel object that can perform I/O on a BlockBackend object
261 + * whose BlockDriverState directly contains a VMState.
262 + *
263 + * Returns: the new channel object
264 + */
265 +QIOChannelSavevmAsync *
266 +qio_channel_savevm_async_new(BlockBackend *be, size_t *bs_pos);
267 +
268 +#endif /* QIO_CHANNEL_SAVEVM_ASYNC_H */
269 diff --git a/migration/meson.build b/migration/meson.build
270 index 690487cf1a..8cac83c06c 100644
271 --- a/migration/meson.build
272 +++ b/migration/meson.build
273 @@ -13,6 +13,7 @@ softmmu_ss.add(files(
274 'block-dirty-bitmap.c',
275 'channel.c',
276 'channel-block.c',
277 + 'channel-savevm-async.c',
278 'colo-failover.c',
279 'colo.c',
280 'exec.c',