MAINTAINERS: Add include/hw/xtensa/mx_pic.h to the XTFPGA machine section
[qemu/kevin.git] / migration / multifd-zlib.c
blob37ce48621e7866da0995a44be6977daa3fde591a
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->data = z;
73 return 0;
75 err_free_zbuff:
76 g_free(z->zbuff);
77 err_deflate_end:
78 deflateEnd(&z->zs);
79 err_free_z:
80 g_free(z);
81 error_setg(errp, "multifd %u: %s", p->id, err_msg);
82 return -1;
85 /**
86 * zlib_send_cleanup: cleanup send side
88 * Close the channel and return memory.
90 * @p: Params for the channel that we are using
91 * @errp: pointer to an error
93 static void zlib_send_cleanup(MultiFDSendParams *p, Error **errp)
95 struct zlib_data *z = p->data;
97 deflateEnd(&z->zs);
98 g_free(z->zbuff);
99 z->zbuff = NULL;
100 g_free(z->buf);
101 z->buf = NULL;
102 g_free(p->data);
103 p->data = NULL;
107 * zlib_send_prepare: prepare date to be able to send
109 * Create a compressed buffer with all the pages that we are going to
110 * send.
112 * Returns 0 for success or -1 for error
114 * @p: Params for the channel that we are using
115 * @errp: pointer to an error
117 static int zlib_send_prepare(MultiFDSendParams *p, Error **errp)
119 struct zlib_data *z = p->data;
120 z_stream *zs = &z->zs;
121 uint32_t out_size = 0;
122 int ret;
123 uint32_t i;
125 for (i = 0; i < p->normal_num; i++) {
126 uint32_t available = z->zbuff_len - out_size;
127 int flush = Z_NO_FLUSH;
129 if (i == p->normal_num - 1) {
130 flush = Z_SYNC_FLUSH;
134 * Since the VM might be running, the page may be changing concurrently
135 * with compression. zlib does not guarantee that this is safe,
136 * therefore copy the page before calling deflate().
138 memcpy(z->buf, p->pages->block->host + p->normal[i], p->page_size);
139 zs->avail_in = p->page_size;
140 zs->next_in = z->buf;
142 zs->avail_out = available;
143 zs->next_out = z->zbuff + out_size;
146 * Welcome to deflate semantics
148 * We need to loop while:
149 * - return is Z_OK
150 * - there are stuff to be compressed
151 * - there are output space free
153 do {
154 ret = deflate(zs, flush);
155 } while (ret == Z_OK && zs->avail_in && zs->avail_out);
156 if (ret == Z_OK && zs->avail_in) {
157 error_setg(errp, "multifd %u: deflate failed to compress all input",
158 p->id);
159 return -1;
161 if (ret != Z_OK) {
162 error_setg(errp, "multifd %u: deflate returned %d instead of Z_OK",
163 p->id, ret);
164 return -1;
166 out_size += available - zs->avail_out;
168 p->iov[p->iovs_num].iov_base = z->zbuff;
169 p->iov[p->iovs_num].iov_len = out_size;
170 p->iovs_num++;
171 p->next_packet_size = out_size;
172 p->flags |= MULTIFD_FLAG_ZLIB;
174 return 0;
178 * zlib_recv_setup: setup receive side
180 * Create the compressed channel and buffer.
182 * Returns 0 for success or -1 for error
184 * @p: Params for the channel that we are using
185 * @errp: pointer to an error
187 static int zlib_recv_setup(MultiFDRecvParams *p, Error **errp)
189 struct zlib_data *z = g_new0(struct zlib_data, 1);
190 z_stream *zs = &z->zs;
192 p->data = z;
193 zs->zalloc = Z_NULL;
194 zs->zfree = Z_NULL;
195 zs->opaque = Z_NULL;
196 zs->avail_in = 0;
197 zs->next_in = Z_NULL;
198 if (inflateInit(zs) != Z_OK) {
199 error_setg(errp, "multifd %u: inflate init failed", p->id);
200 return -1;
202 /* To be safe, we reserve twice the size of the packet */
203 z->zbuff_len = MULTIFD_PACKET_SIZE * 2;
204 z->zbuff = g_try_malloc(z->zbuff_len);
205 if (!z->zbuff) {
206 inflateEnd(zs);
207 error_setg(errp, "multifd %u: out of memory for zbuff", p->id);
208 return -1;
210 return 0;
214 * zlib_recv_cleanup: setup receive side
216 * For no compression this function does nothing.
218 * @p: Params for the channel that we are using
220 static void zlib_recv_cleanup(MultiFDRecvParams *p)
222 struct zlib_data *z = p->data;
224 inflateEnd(&z->zs);
225 g_free(z->zbuff);
226 z->zbuff = NULL;
227 g_free(p->data);
228 p->data = NULL;
232 * zlib_recv_pages: read the data from the channel into actual pages
234 * Read the compressed buffer, and uncompress it into the actual
235 * pages.
237 * Returns 0 for success or -1 for error
239 * @p: Params for the channel that we are using
240 * @errp: pointer to an error
242 static int zlib_recv_pages(MultiFDRecvParams *p, Error **errp)
244 struct zlib_data *z = p->data;
245 z_stream *zs = &z->zs;
246 uint32_t in_size = p->next_packet_size;
247 /* we measure the change of total_out */
248 uint32_t out_size = zs->total_out;
249 uint32_t expected_size = p->normal_num * p->page_size;
250 uint32_t flags = p->flags & MULTIFD_FLAG_COMPRESSION_MASK;
251 int ret;
252 int i;
254 if (flags != MULTIFD_FLAG_ZLIB) {
255 error_setg(errp, "multifd %u: flags received %x flags expected %x",
256 p->id, flags, MULTIFD_FLAG_ZLIB);
257 return -1;
259 ret = qio_channel_read_all(p->c, (void *)z->zbuff, in_size, errp);
261 if (ret != 0) {
262 return ret;
265 zs->avail_in = in_size;
266 zs->next_in = z->zbuff;
268 for (i = 0; i < p->normal_num; i++) {
269 int flush = Z_NO_FLUSH;
270 unsigned long start = zs->total_out;
272 if (i == p->normal_num - 1) {
273 flush = Z_SYNC_FLUSH;
276 zs->avail_out = p->page_size;
277 zs->next_out = p->host + p->normal[i];
280 * Welcome to inflate semantics
282 * We need to loop while:
283 * - return is Z_OK
284 * - there are input available
285 * - we haven't completed a full page
287 do {
288 ret = inflate(zs, flush);
289 } while (ret == Z_OK && zs->avail_in
290 && (zs->total_out - start) < p->page_size);
291 if (ret == Z_OK && (zs->total_out - start) < p->page_size) {
292 error_setg(errp, "multifd %u: inflate generated too few output",
293 p->id);
294 return -1;
296 if (ret != Z_OK) {
297 error_setg(errp, "multifd %u: inflate returned %d instead of Z_OK",
298 p->id, ret);
299 return -1;
302 out_size = zs->total_out - out_size;
303 if (out_size != expected_size) {
304 error_setg(errp, "multifd %u: packet size received %u size expected %u",
305 p->id, out_size, expected_size);
306 return -1;
308 return 0;
311 static MultiFDMethods multifd_zlib_ops = {
312 .send_setup = zlib_send_setup,
313 .send_cleanup = zlib_send_cleanup,
314 .send_prepare = zlib_send_prepare,
315 .recv_setup = zlib_recv_setup,
316 .recv_cleanup = zlib_recv_cleanup,
317 .recv_pages = zlib_recv_pages
320 static void multifd_zlib_register(void)
322 multifd_register_ops(MULTIFD_COMPRESSION_ZLIB, &multifd_zlib_ops);
325 migration_init(multifd_zlib_register);