Merge tag 'pull-trivial-patches' of https://gitlab.com/mjt0k/qemu into staging
[qemu/kevin.git] / migration / multifd-zlib.c
blob2ced69487e508ddb53ac261f73deeb44d951ed21
1 /*
2 * Multifd zlib compression implementation
4 * Copyright (c) 2020 Red Hat Inc
6 * Authors:
7 * Juan Quintela <quintela@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
13 #include "qemu/osdep.h"
14 #include <zlib.h>
15 #include "qemu/rcu.h"
16 #include "exec/ramblock.h"
17 #include "exec/target_page.h"
18 #include "qapi/error.h"
19 #include "migration.h"
20 #include "trace.h"
21 #include "options.h"
22 #include "multifd.h"
24 struct zlib_data {
25 /* stream for compression */
26 z_stream zs;
27 /* compressed buffer */
28 uint8_t *zbuff;
29 /* size of compressed buffer */
30 uint32_t zbuff_len;
31 /* uncompressed buffer of size qemu_target_page_size() */
32 uint8_t *buf;
35 /* Multifd zlib compression */
37 /**
38 * zlib_send_setup: setup send side
40 * Setup each channel with zlib compression.
42 * Returns 0 for success or -1 for error
44 * @p: Params for the channel that we are using
45 * @errp: pointer to an error
47 static int zlib_send_setup(MultiFDSendParams *p, Error **errp)
49 struct zlib_data *z = g_new0(struct zlib_data, 1);
50 z_stream *zs = &z->zs;
51 const char *err_msg;
53 zs->zalloc = Z_NULL;
54 zs->zfree = Z_NULL;
55 zs->opaque = Z_NULL;
56 if (deflateInit(zs, migrate_multifd_zlib_level()) != Z_OK) {
57 err_msg = "deflate init failed";
58 goto err_free_z;
60 /* This is the maximum size of the compressed buffer */
61 z->zbuff_len = compressBound(MULTIFD_PACKET_SIZE);
62 z->zbuff = g_try_malloc(z->zbuff_len);
63 if (!z->zbuff) {
64 err_msg = "out of memory for zbuff";
65 goto err_deflate_end;
67 z->buf = g_try_malloc(qemu_target_page_size());
68 if (!z->buf) {
69 err_msg = "out of memory for buf";
70 goto err_free_zbuff;
72 p->compress_data = z;
74 /* Needs 2 IOVs, one for packet header and one for compressed data */
75 p->iov = g_new0(struct iovec, 2);
77 return 0;
79 err_free_zbuff:
80 g_free(z->zbuff);
81 err_deflate_end:
82 deflateEnd(zs);
83 err_free_z:
84 g_free(z);
85 error_setg(errp, "multifd %u: %s", p->id, err_msg);
86 return -1;
89 /**
90 * zlib_send_cleanup: cleanup send side
92 * Close the channel and return memory.
94 * @p: Params for the channel that we are using
95 * @errp: pointer to an error
97 static void zlib_send_cleanup(MultiFDSendParams *p, Error **errp)
99 struct zlib_data *z = p->compress_data;
101 deflateEnd(&z->zs);
102 g_free(z->zbuff);
103 z->zbuff = NULL;
104 g_free(z->buf);
105 z->buf = NULL;
106 g_free(p->compress_data);
107 p->compress_data = NULL;
109 g_free(p->iov);
110 p->iov = NULL;
114 * zlib_send_prepare: prepare date to be able to send
116 * Create a compressed buffer with all the pages that we are going to
117 * send.
119 * Returns 0 for success or -1 for error
121 * @p: Params for the channel that we are using
122 * @errp: pointer to an error
124 static int zlib_send_prepare(MultiFDSendParams *p, Error **errp)
126 MultiFDPages_t *pages = p->pages;
127 struct zlib_data *z = p->compress_data;
128 z_stream *zs = &z->zs;
129 uint32_t out_size = 0;
130 int ret;
131 uint32_t i;
133 if (!multifd_send_prepare_common(p)) {
134 goto out;
137 for (i = 0; i < pages->normal_num; i++) {
138 uint32_t available = z->zbuff_len - out_size;
139 int flush = Z_NO_FLUSH;
141 if (i == pages->normal_num - 1) {
142 flush = Z_SYNC_FLUSH;
146 * Since the VM might be running, the page may be changing concurrently
147 * with compression. zlib does not guarantee that this is safe,
148 * therefore copy the page before calling deflate().
150 memcpy(z->buf, p->pages->block->host + pages->offset[i], p->page_size);
151 zs->avail_in = p->page_size;
152 zs->next_in = z->buf;
154 zs->avail_out = available;
155 zs->next_out = z->zbuff + out_size;
158 * Welcome to deflate semantics
160 * We need to loop while:
161 * - return is Z_OK
162 * - there are stuff to be compressed
163 * - there are output space free
165 do {
166 ret = deflate(zs, flush);
167 } while (ret == Z_OK && zs->avail_in && zs->avail_out);
168 if (ret == Z_OK && zs->avail_in) {
169 error_setg(errp, "multifd %u: deflate failed to compress all input",
170 p->id);
171 return -1;
173 if (ret != Z_OK) {
174 error_setg(errp, "multifd %u: deflate returned %d instead of Z_OK",
175 p->id, ret);
176 return -1;
178 out_size += available - zs->avail_out;
180 p->iov[p->iovs_num].iov_base = z->zbuff;
181 p->iov[p->iovs_num].iov_len = out_size;
182 p->iovs_num++;
183 p->next_packet_size = out_size;
185 out:
186 p->flags |= MULTIFD_FLAG_ZLIB;
187 multifd_send_fill_packet(p);
188 return 0;
192 * zlib_recv_setup: setup receive side
194 * Create the compressed channel and buffer.
196 * Returns 0 for success or -1 for error
198 * @p: Params for the channel that we are using
199 * @errp: pointer to an error
201 static int zlib_recv_setup(MultiFDRecvParams *p, Error **errp)
203 struct zlib_data *z = g_new0(struct zlib_data, 1);
204 z_stream *zs = &z->zs;
206 p->compress_data = z;
207 zs->zalloc = Z_NULL;
208 zs->zfree = Z_NULL;
209 zs->opaque = Z_NULL;
210 zs->avail_in = 0;
211 zs->next_in = Z_NULL;
212 if (inflateInit(zs) != Z_OK) {
213 error_setg(errp, "multifd %u: inflate init failed", p->id);
214 return -1;
216 /* To be safe, we reserve twice the size of the packet */
217 z->zbuff_len = MULTIFD_PACKET_SIZE * 2;
218 z->zbuff = g_try_malloc(z->zbuff_len);
219 if (!z->zbuff) {
220 inflateEnd(zs);
221 error_setg(errp, "multifd %u: out of memory for zbuff", p->id);
222 return -1;
224 return 0;
228 * zlib_recv_cleanup: setup receive side
230 * For no compression this function does nothing.
232 * @p: Params for the channel that we are using
234 static void zlib_recv_cleanup(MultiFDRecvParams *p)
236 struct zlib_data *z = p->compress_data;
238 inflateEnd(&z->zs);
239 g_free(z->zbuff);
240 z->zbuff = NULL;
241 g_free(p->compress_data);
242 p->compress_data = NULL;
246 * zlib_recv: read the data from the channel into actual pages
248 * Read the compressed buffer, and uncompress it into the actual
249 * pages.
251 * Returns 0 for success or -1 for error
253 * @p: Params for the channel that we are using
254 * @errp: pointer to an error
256 static int zlib_recv(MultiFDRecvParams *p, Error **errp)
258 struct zlib_data *z = p->compress_data;
259 z_stream *zs = &z->zs;
260 uint32_t in_size = p->next_packet_size;
261 /* we measure the change of total_out */
262 uint32_t out_size = zs->total_out;
263 uint32_t expected_size = p->normal_num * p->page_size;
264 uint32_t flags = p->flags & MULTIFD_FLAG_COMPRESSION_MASK;
265 int ret;
266 int i;
268 if (flags != MULTIFD_FLAG_ZLIB) {
269 error_setg(errp, "multifd %u: flags received %x flags expected %x",
270 p->id, flags, MULTIFD_FLAG_ZLIB);
271 return -1;
274 multifd_recv_zero_page_process(p);
276 if (!p->normal_num) {
277 assert(in_size == 0);
278 return 0;
281 ret = qio_channel_read_all(p->c, (void *)z->zbuff, in_size, errp);
283 if (ret != 0) {
284 return ret;
287 zs->avail_in = in_size;
288 zs->next_in = z->zbuff;
290 for (i = 0; i < p->normal_num; i++) {
291 int flush = Z_NO_FLUSH;
292 unsigned long start = zs->total_out;
294 ramblock_recv_bitmap_set_offset(p->block, p->normal[i]);
295 if (i == p->normal_num - 1) {
296 flush = Z_SYNC_FLUSH;
299 zs->avail_out = p->page_size;
300 zs->next_out = p->host + p->normal[i];
303 * Welcome to inflate semantics
305 * We need to loop while:
306 * - return is Z_OK
307 * - there are input available
308 * - we haven't completed a full page
310 do {
311 ret = inflate(zs, flush);
312 } while (ret == Z_OK && zs->avail_in
313 && (zs->total_out - start) < p->page_size);
314 if (ret == Z_OK && (zs->total_out - start) < p->page_size) {
315 error_setg(errp, "multifd %u: inflate generated too few output",
316 p->id);
317 return -1;
319 if (ret != Z_OK) {
320 error_setg(errp, "multifd %u: inflate returned %d instead of Z_OK",
321 p->id, ret);
322 return -1;
325 out_size = zs->total_out - out_size;
326 if (out_size != expected_size) {
327 error_setg(errp, "multifd %u: packet size received %u size expected %u",
328 p->id, out_size, expected_size);
329 return -1;
332 return 0;
335 static MultiFDMethods multifd_zlib_ops = {
336 .send_setup = zlib_send_setup,
337 .send_cleanup = zlib_send_cleanup,
338 .send_prepare = zlib_send_prepare,
339 .recv_setup = zlib_recv_setup,
340 .recv_cleanup = zlib_recv_cleanup,
341 .recv = zlib_recv
344 static void multifd_zlib_register(void)
346 multifd_register_ops(MULTIFD_COMPRESSION_ZLIB, &multifd_zlib_ops);
349 migration_init(multifd_zlib_register);