2 * Multifd zlib compression implementation
4 * Copyright (c) 2020 Red Hat Inc
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"
16 #include "exec/ramblock.h"
17 #include "exec/target_page.h"
18 #include "qapi/error.h"
19 #include "migration.h"
25 /* stream for compression */
27 /* compressed buffer */
29 /* size of compressed buffer */
31 /* uncompressed buffer of size qemu_target_page_size() */
35 /* Multifd zlib compression */
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
;
56 if (deflateInit(zs
, migrate_multifd_zlib_level()) != Z_OK
) {
57 err_msg
= "deflate init failed";
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
);
64 err_msg
= "out of memory for zbuff";
67 z
->buf
= g_try_malloc(qemu_target_page_size());
69 err_msg
= "out of memory for buf";
81 error_setg(errp
, "multifd %u: %s", p
->id
, err_msg
);
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
->compress_data
;
102 g_free(p
->compress_data
);
103 p
->compress_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
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 MultiFDPages_t
*pages
= p
->pages
;
120 struct zlib_data
*z
= p
->compress_data
;
121 z_stream
*zs
= &z
->zs
;
122 uint32_t out_size
= 0;
126 if (!multifd_send_prepare_common(p
)) {
130 for (i
= 0; i
< pages
->normal_num
; i
++) {
131 uint32_t available
= z
->zbuff_len
- out_size
;
132 int flush
= Z_NO_FLUSH
;
134 if (i
== pages
->normal_num
- 1) {
135 flush
= Z_SYNC_FLUSH
;
139 * Since the VM might be running, the page may be changing concurrently
140 * with compression. zlib does not guarantee that this is safe,
141 * therefore copy the page before calling deflate().
143 memcpy(z
->buf
, p
->pages
->block
->host
+ pages
->offset
[i
], p
->page_size
);
144 zs
->avail_in
= p
->page_size
;
145 zs
->next_in
= z
->buf
;
147 zs
->avail_out
= available
;
148 zs
->next_out
= z
->zbuff
+ out_size
;
151 * Welcome to deflate semantics
153 * We need to loop while:
155 * - there are stuff to be compressed
156 * - there are output space free
159 ret
= deflate(zs
, flush
);
160 } while (ret
== Z_OK
&& zs
->avail_in
&& zs
->avail_out
);
161 if (ret
== Z_OK
&& zs
->avail_in
) {
162 error_setg(errp
, "multifd %u: deflate failed to compress all input",
167 error_setg(errp
, "multifd %u: deflate returned %d instead of Z_OK",
171 out_size
+= available
- zs
->avail_out
;
173 p
->iov
[p
->iovs_num
].iov_base
= z
->zbuff
;
174 p
->iov
[p
->iovs_num
].iov_len
= out_size
;
176 p
->next_packet_size
= out_size
;
179 p
->flags
|= MULTIFD_FLAG_ZLIB
;
180 multifd_send_fill_packet(p
);
185 * zlib_recv_setup: setup receive side
187 * Create the compressed channel and buffer.
189 * Returns 0 for success or -1 for error
191 * @p: Params for the channel that we are using
192 * @errp: pointer to an error
194 static int zlib_recv_setup(MultiFDRecvParams
*p
, Error
**errp
)
196 struct zlib_data
*z
= g_new0(struct zlib_data
, 1);
197 z_stream
*zs
= &z
->zs
;
199 p
->compress_data
= z
;
204 zs
->next_in
= Z_NULL
;
205 if (inflateInit(zs
) != Z_OK
) {
206 error_setg(errp
, "multifd %u: inflate init failed", p
->id
);
209 /* To be safe, we reserve twice the size of the packet */
210 z
->zbuff_len
= MULTIFD_PACKET_SIZE
* 2;
211 z
->zbuff
= g_try_malloc(z
->zbuff_len
);
214 error_setg(errp
, "multifd %u: out of memory for zbuff", p
->id
);
221 * zlib_recv_cleanup: setup receive side
223 * For no compression this function does nothing.
225 * @p: Params for the channel that we are using
227 static void zlib_recv_cleanup(MultiFDRecvParams
*p
)
229 struct zlib_data
*z
= p
->compress_data
;
234 g_free(p
->compress_data
);
235 p
->compress_data
= NULL
;
239 * zlib_recv: read the data from the channel into actual pages
241 * Read the compressed buffer, and uncompress it into the actual
244 * Returns 0 for success or -1 for error
246 * @p: Params for the channel that we are using
247 * @errp: pointer to an error
249 static int zlib_recv(MultiFDRecvParams
*p
, Error
**errp
)
251 struct zlib_data
*z
= p
->compress_data
;
252 z_stream
*zs
= &z
->zs
;
253 uint32_t in_size
= p
->next_packet_size
;
254 /* we measure the change of total_out */
255 uint32_t out_size
= zs
->total_out
;
256 uint32_t expected_size
= p
->normal_num
* p
->page_size
;
257 uint32_t flags
= p
->flags
& MULTIFD_FLAG_COMPRESSION_MASK
;
261 if (flags
!= MULTIFD_FLAG_ZLIB
) {
262 error_setg(errp
, "multifd %u: flags received %x flags expected %x",
263 p
->id
, flags
, MULTIFD_FLAG_ZLIB
);
267 multifd_recv_zero_page_process(p
);
269 if (!p
->normal_num
) {
270 assert(in_size
== 0);
274 ret
= qio_channel_read_all(p
->c
, (void *)z
->zbuff
, in_size
, errp
);
280 zs
->avail_in
= in_size
;
281 zs
->next_in
= z
->zbuff
;
283 for (i
= 0; i
< p
->normal_num
; i
++) {
284 int flush
= Z_NO_FLUSH
;
285 unsigned long start
= zs
->total_out
;
287 ramblock_recv_bitmap_set_offset(p
->block
, p
->normal
[i
]);
288 if (i
== p
->normal_num
- 1) {
289 flush
= Z_SYNC_FLUSH
;
292 zs
->avail_out
= p
->page_size
;
293 zs
->next_out
= p
->host
+ p
->normal
[i
];
296 * Welcome to inflate semantics
298 * We need to loop while:
300 * - there are input available
301 * - we haven't completed a full page
304 ret
= inflate(zs
, flush
);
305 } while (ret
== Z_OK
&& zs
->avail_in
306 && (zs
->total_out
- start
) < p
->page_size
);
307 if (ret
== Z_OK
&& (zs
->total_out
- start
) < p
->page_size
) {
308 error_setg(errp
, "multifd %u: inflate generated too few output",
313 error_setg(errp
, "multifd %u: inflate returned %d instead of Z_OK",
318 out_size
= zs
->total_out
- out_size
;
319 if (out_size
!= expected_size
) {
320 error_setg(errp
, "multifd %u: packet size received %u size expected %u",
321 p
->id
, out_size
, expected_size
);
328 static MultiFDMethods multifd_zlib_ops
= {
329 .send_setup
= zlib_send_setup
,
330 .send_cleanup
= zlib_send_cleanup
,
331 .send_prepare
= zlib_send_prepare
,
332 .recv_setup
= zlib_recv_setup
,
333 .recv_cleanup
= zlib_recv_cleanup
,
337 static void multifd_zlib_register(void)
339 multifd_register_ops(MULTIFD_COMPRESSION_ZLIB
, &multifd_zlib_ops
);
342 migration_init(multifd_zlib_register
);