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"
24 /* stream for compression */
26 /* compressed buffer */
28 /* size of compressed buffer */
32 /* Multifd zlib compression */
35 * zlib_send_setup: setup send side
37 * Setup each channel with zlib compression.
39 * Returns 0 for success or -1 for error
41 * @p: Params for the channel that we are using
42 * @errp: pointer to an error
44 static int zlib_send_setup(MultiFDSendParams
*p
, Error
**errp
)
46 struct zlib_data
*z
= g_new0(struct zlib_data
, 1);
47 z_stream
*zs
= &z
->zs
;
52 if (deflateInit(zs
, migrate_multifd_zlib_level()) != Z_OK
) {
54 error_setg(errp
, "multifd %u: deflate init failed", p
->id
);
57 /* This is the maxium size of the compressed buffer */
58 z
->zbuff_len
= compressBound(MULTIFD_PACKET_SIZE
);
59 z
->zbuff
= g_try_malloc(z
->zbuff_len
);
63 error_setg(errp
, "multifd %u: out of memory for zbuff", p
->id
);
71 * zlib_send_cleanup: cleanup send side
73 * Close the channel and return memory.
75 * @p: Params for the channel that we are using
76 * @errp: pointer to an error
78 static void zlib_send_cleanup(MultiFDSendParams
*p
, Error
**errp
)
80 struct zlib_data
*z
= p
->data
;
90 * zlib_send_prepare: prepare date to be able to send
92 * Create a compressed buffer with all the pages that we are going to
95 * Returns 0 for success or -1 for error
97 * @p: Params for the channel that we are using
98 * @errp: pointer to an error
100 static int zlib_send_prepare(MultiFDSendParams
*p
, Error
**errp
)
102 struct zlib_data
*z
= p
->data
;
103 size_t page_size
= qemu_target_page_size();
104 z_stream
*zs
= &z
->zs
;
105 uint32_t out_size
= 0;
109 for (i
= 0; i
< p
->normal_num
; i
++) {
110 uint32_t available
= z
->zbuff_len
- out_size
;
111 int flush
= Z_NO_FLUSH
;
113 if (i
== p
->normal_num
- 1) {
114 flush
= Z_SYNC_FLUSH
;
117 zs
->avail_in
= page_size
;
118 zs
->next_in
= p
->pages
->block
->host
+ p
->normal
[i
];
120 zs
->avail_out
= available
;
121 zs
->next_out
= z
->zbuff
+ out_size
;
124 * Welcome to deflate semantics
126 * We need to loop while:
128 * - there are stuff to be compressed
129 * - there are output space free
132 ret
= deflate(zs
, flush
);
133 } while (ret
== Z_OK
&& zs
->avail_in
&& zs
->avail_out
);
134 if (ret
== Z_OK
&& zs
->avail_in
) {
135 error_setg(errp
, "multifd %u: deflate failed to compress all input",
140 error_setg(errp
, "multifd %u: deflate returned %d instead of Z_OK",
144 out_size
+= available
- zs
->avail_out
;
146 p
->iov
[p
->iovs_num
].iov_base
= z
->zbuff
;
147 p
->iov
[p
->iovs_num
].iov_len
= out_size
;
149 p
->next_packet_size
= out_size
;
150 p
->flags
|= MULTIFD_FLAG_ZLIB
;
156 * zlib_recv_setup: setup receive side
158 * Create the compressed channel and buffer.
160 * Returns 0 for success or -1 for error
162 * @p: Params for the channel that we are using
163 * @errp: pointer to an error
165 static int zlib_recv_setup(MultiFDRecvParams
*p
, Error
**errp
)
167 struct zlib_data
*z
= g_new0(struct zlib_data
, 1);
168 z_stream
*zs
= &z
->zs
;
175 zs
->next_in
= Z_NULL
;
176 if (inflateInit(zs
) != Z_OK
) {
177 error_setg(errp
, "multifd %u: inflate init failed", p
->id
);
180 /* To be safe, we reserve twice the size of the packet */
181 z
->zbuff_len
= MULTIFD_PACKET_SIZE
* 2;
182 z
->zbuff
= g_try_malloc(z
->zbuff_len
);
185 error_setg(errp
, "multifd %u: out of memory for zbuff", p
->id
);
192 * zlib_recv_cleanup: setup receive side
194 * For no compression this function does nothing.
196 * @p: Params for the channel that we are using
198 static void zlib_recv_cleanup(MultiFDRecvParams
*p
)
200 struct zlib_data
*z
= p
->data
;
210 * zlib_recv_pages: read the data from the channel into actual pages
212 * Read the compressed buffer, and uncompress it into the actual
215 * Returns 0 for success or -1 for error
217 * @p: Params for the channel that we are using
218 * @errp: pointer to an error
220 static int zlib_recv_pages(MultiFDRecvParams
*p
, Error
**errp
)
222 struct zlib_data
*z
= p
->data
;
223 size_t page_size
= qemu_target_page_size();
224 z_stream
*zs
= &z
->zs
;
225 uint32_t in_size
= p
->next_packet_size
;
226 /* we measure the change of total_out */
227 uint32_t out_size
= zs
->total_out
;
228 uint32_t expected_size
= p
->normal_num
* page_size
;
229 uint32_t flags
= p
->flags
& MULTIFD_FLAG_COMPRESSION_MASK
;
233 if (flags
!= MULTIFD_FLAG_ZLIB
) {
234 error_setg(errp
, "multifd %u: flags received %x flags expected %x",
235 p
->id
, flags
, MULTIFD_FLAG_ZLIB
);
238 ret
= qio_channel_read_all(p
->c
, (void *)z
->zbuff
, in_size
, errp
);
244 zs
->avail_in
= in_size
;
245 zs
->next_in
= z
->zbuff
;
247 for (i
= 0; i
< p
->normal_num
; i
++) {
248 int flush
= Z_NO_FLUSH
;
249 unsigned long start
= zs
->total_out
;
251 if (i
== p
->normal_num
- 1) {
252 flush
= Z_SYNC_FLUSH
;
255 zs
->avail_out
= page_size
;
256 zs
->next_out
= p
->host
+ p
->normal
[i
];
259 * Welcome to inflate semantics
261 * We need to loop while:
263 * - there are input available
264 * - we haven't completed a full page
267 ret
= inflate(zs
, flush
);
268 } while (ret
== Z_OK
&& zs
->avail_in
269 && (zs
->total_out
- start
) < page_size
);
270 if (ret
== Z_OK
&& (zs
->total_out
- start
) < page_size
) {
271 error_setg(errp
, "multifd %u: inflate generated too few output",
276 error_setg(errp
, "multifd %u: inflate returned %d instead of Z_OK",
281 out_size
= zs
->total_out
- out_size
;
282 if (out_size
!= expected_size
) {
283 error_setg(errp
, "multifd %u: packet size received %u size expected %u",
284 p
->id
, out_size
, expected_size
);
290 static MultiFDMethods multifd_zlib_ops
= {
291 .send_setup
= zlib_send_setup
,
292 .send_cleanup
= zlib_send_cleanup
,
293 .send_prepare
= zlib_send_prepare
,
294 .recv_setup
= zlib_recv_setup
,
295 .recv_cleanup
= zlib_recv_cleanup
,
296 .recv_pages
= zlib_recv_pages
299 static void multifd_zlib_register(void)
301 multifd_register_ops(MULTIFD_COMPRESSION_ZLIB
, &multifd_zlib_ops
);
304 migration_init(multifd_zlib_register
);