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 /* stream for decompression */
32 /* compressed buffer */
34 /* size of compressed buffer */
38 /* Multifd zstd compression */
41 * zstd_send_setup: setup send side
43 * Setup each channel with zstd compression.
45 * Returns 0 for success or -1 for error
47 * @p: Params for the channel that we are using
48 * @errp: pointer to an error
50 static int zstd_send_setup(MultiFDSendParams
*p
, Error
**errp
)
52 struct zstd_data
*z
= g_new0(struct zstd_data
, 1);
55 z
->zcs
= ZSTD_createCStream();
58 error_setg(errp
, "multifd %u: zstd createCStream failed", p
->id
);
62 res
= ZSTD_initCStream(z
->zcs
, migrate_multifd_zstd_level());
63 if (ZSTD_isError(res
)) {
64 ZSTD_freeCStream(z
->zcs
);
66 error_setg(errp
, "multifd %u: initCStream failed with error %s",
67 p
->id
, ZSTD_getErrorName(res
));
70 /* This is the maximum size of the compressed buffer */
71 z
->zbuff_len
= ZSTD_compressBound(MULTIFD_PACKET_SIZE
);
72 z
->zbuff
= g_try_malloc(z
->zbuff_len
);
74 ZSTD_freeCStream(z
->zcs
);
76 error_setg(errp
, "multifd %u: out of memory for zbuff", p
->id
);
81 /* Needs 2 IOVs, one for packet header and one for compressed data */
82 p
->iov
= g_new0(struct iovec
, 2);
87 * zstd_send_cleanup: cleanup send side
89 * Close the channel and return memory.
91 * @p: Params for the channel that we are using
92 * @errp: pointer to an error
94 static void zstd_send_cleanup(MultiFDSendParams
*p
, Error
**errp
)
96 struct zstd_data
*z
= p
->compress_data
;
98 ZSTD_freeCStream(z
->zcs
);
102 g_free(p
->compress_data
);
103 p
->compress_data
= NULL
;
110 * zstd_send_prepare: prepare date to be able to send
112 * Create a compressed buffer with all the pages that we are going to
115 * Returns 0 for success or -1 for error
117 * @p: Params for the channel that we are using
118 * @errp: pointer to an error
120 static int zstd_send_prepare(MultiFDSendParams
*p
, Error
**errp
)
122 MultiFDPages_t
*pages
= p
->pages
;
123 struct zstd_data
*z
= p
->compress_data
;
127 if (!multifd_send_prepare_common(p
)) {
131 z
->out
.dst
= z
->zbuff
;
132 z
->out
.size
= z
->zbuff_len
;
135 for (i
= 0; i
< pages
->normal_num
; i
++) {
136 ZSTD_EndDirective flush
= ZSTD_e_continue
;
138 if (i
== pages
->normal_num
- 1) {
139 flush
= ZSTD_e_flush
;
141 z
->in
.src
= p
->pages
->block
->host
+ pages
->offset
[i
];
142 z
->in
.size
= p
->page_size
;
146 * Welcome to compressStream2 semantics
148 * We need to loop while:
150 * - there is input available
151 * - there is output space free
154 ret
= ZSTD_compressStream2(z
->zcs
, &z
->out
, &z
->in
, flush
);
155 } while (ret
> 0 && (z
->in
.size
- z
->in
.pos
> 0)
156 && (z
->out
.size
- z
->out
.pos
> 0));
157 if (ret
> 0 && (z
->in
.size
- z
->in
.pos
> 0)) {
158 error_setg(errp
, "multifd %u: compressStream buffer too small",
162 if (ZSTD_isError(ret
)) {
163 error_setg(errp
, "multifd %u: compressStream error %s",
164 p
->id
, ZSTD_getErrorName(ret
));
168 p
->iov
[p
->iovs_num
].iov_base
= z
->zbuff
;
169 p
->iov
[p
->iovs_num
].iov_len
= z
->out
.pos
;
171 p
->next_packet_size
= z
->out
.pos
;
174 p
->flags
|= MULTIFD_FLAG_ZSTD
;
175 multifd_send_fill_packet(p
);
180 * zstd_recv_setup: setup receive side
182 * Create the compressed channel and buffer.
184 * Returns 0 for success or -1 for error
186 * @p: Params for the channel that we are using
187 * @errp: pointer to an error
189 static int zstd_recv_setup(MultiFDRecvParams
*p
, Error
**errp
)
191 struct zstd_data
*z
= g_new0(struct zstd_data
, 1);
194 p
->compress_data
= z
;
195 z
->zds
= ZSTD_createDStream();
198 error_setg(errp
, "multifd %u: zstd createDStream failed", p
->id
);
202 ret
= ZSTD_initDStream(z
->zds
);
203 if (ZSTD_isError(ret
)) {
204 ZSTD_freeDStream(z
->zds
);
206 error_setg(errp
, "multifd %u: initDStream failed with error %s",
207 p
->id
, ZSTD_getErrorName(ret
));
211 /* To be safe, we reserve twice the size of the packet */
212 z
->zbuff_len
= MULTIFD_PACKET_SIZE
* 2;
213 z
->zbuff
= g_try_malloc(z
->zbuff_len
);
215 ZSTD_freeDStream(z
->zds
);
217 error_setg(errp
, "multifd %u: out of memory for zbuff", p
->id
);
224 * zstd_recv_cleanup: setup receive side
226 * For no compression this function does nothing.
228 * @p: Params for the channel that we are using
230 static void zstd_recv_cleanup(MultiFDRecvParams
*p
)
232 struct zstd_data
*z
= p
->compress_data
;
234 ZSTD_freeDStream(z
->zds
);
238 g_free(p
->compress_data
);
239 p
->compress_data
= NULL
;
243 * zstd_recv: read the data from the channel into actual pages
245 * Read the compressed buffer, and uncompress it into the actual
248 * Returns 0 for success or -1 for error
250 * @p: Params for the channel that we are using
251 * @errp: pointer to an error
253 static int zstd_recv(MultiFDRecvParams
*p
, Error
**errp
)
255 uint32_t in_size
= p
->next_packet_size
;
256 uint32_t out_size
= 0;
257 uint32_t expected_size
= p
->normal_num
* p
->page_size
;
258 uint32_t flags
= p
->flags
& MULTIFD_FLAG_COMPRESSION_MASK
;
259 struct zstd_data
*z
= p
->compress_data
;
263 if (flags
!= MULTIFD_FLAG_ZSTD
) {
264 error_setg(errp
, "multifd %u: flags received %x flags expected %x",
265 p
->id
, flags
, MULTIFD_FLAG_ZSTD
);
269 multifd_recv_zero_page_process(p
);
271 if (!p
->normal_num
) {
272 assert(in_size
== 0);
276 ret
= qio_channel_read_all(p
->c
, (void *)z
->zbuff
, in_size
, errp
);
282 z
->in
.src
= z
->zbuff
;
283 z
->in
.size
= in_size
;
286 for (i
= 0; i
< p
->normal_num
; i
++) {
287 ramblock_recv_bitmap_set_offset(p
->block
, p
->normal
[i
]);
288 z
->out
.dst
= p
->host
+ p
->normal
[i
];
289 z
->out
.size
= p
->page_size
;
293 * Welcome to decompressStream semantics
295 * We need to loop while:
297 * - there is input available
298 * - we haven't put out a full page
301 ret
= ZSTD_decompressStream(z
->zds
, &z
->out
, &z
->in
);
302 } while (ret
> 0 && (z
->in
.size
- z
->in
.pos
> 0)
303 && (z
->out
.pos
< p
->page_size
));
304 if (ret
> 0 && (z
->out
.pos
< p
->page_size
)) {
305 error_setg(errp
, "multifd %u: decompressStream buffer too small",
309 if (ZSTD_isError(ret
)) {
310 error_setg(errp
, "multifd %u: decompressStream returned %s",
311 p
->id
, ZSTD_getErrorName(ret
));
314 out_size
+= z
->out
.pos
;
316 if (out_size
!= expected_size
) {
317 error_setg(errp
, "multifd %u: packet size received %u size expected %u",
318 p
->id
, out_size
, expected_size
);
324 static MultiFDMethods multifd_zstd_ops
= {
325 .send_setup
= zstd_send_setup
,
326 .send_cleanup
= zstd_send_cleanup
,
327 .send_prepare
= zstd_send_prepare
,
328 .recv_setup
= zstd_recv_setup
,
329 .recv_cleanup
= zstd_recv_cleanup
,
333 static void multifd_zstd_register(void)
335 multifd_register_ops(MULTIFD_COMPRESSION_ZSTD
, &multifd_zstd_ops
);
338 migration_init(multifd_zstd_register
);