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";
74 /* Needs 2 IOVs, one for packet header and one for compressed data */
75 p
->iov
= g_new0(struct iovec
, 2);
85 error_setg(errp
, "multifd %u: %s", p
->id
, err_msg
);
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
;
106 g_free(p
->compress_data
);
107 p
->compress_data
= 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
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;
133 if (!multifd_send_prepare_common(p
)) {
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:
162 * - there are stuff to be compressed
163 * - there are output space free
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",
174 error_setg(errp
, "multifd %u: deflate returned %d instead of Z_OK",
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
;
183 p
->next_packet_size
= out_size
;
186 p
->flags
|= MULTIFD_FLAG_ZLIB
;
187 multifd_send_fill_packet(p
);
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
;
211 zs
->next_in
= Z_NULL
;
212 if (inflateInit(zs
) != Z_OK
) {
213 error_setg(errp
, "multifd %u: inflate init failed", p
->id
);
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
);
221 error_setg(errp
, "multifd %u: out of memory for zbuff", p
->id
);
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
;
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
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
;
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
);
274 multifd_recv_zero_page_process(p
);
276 if (!p
->normal_num
) {
277 assert(in_size
== 0);
281 ret
= qio_channel_read_all(p
->c
, (void *)z
->zbuff
, in_size
, errp
);
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:
307 * - there are input available
308 * - we haven't completed a full page
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",
320 error_setg(errp
, "multifd %u: inflate returned %d instead of Z_OK",
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
);
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
,
344 static void multifd_zlib_register(void)
346 multifd_register_ops(MULTIFD_COMPRESSION_ZLIB
, &multifd_zlib_ops
);
349 migration_init(multifd_zlib_register
);