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 */
40 static int multifd_zstd_send_setup(MultiFDSendParams
*p
, Error
**errp
)
42 struct zstd_data
*z
= g_new0(struct zstd_data
, 1);
45 z
->zcs
= ZSTD_createCStream();
48 error_setg(errp
, "multifd %u: zstd createCStream failed", p
->id
);
52 res
= ZSTD_initCStream(z
->zcs
, migrate_multifd_zstd_level());
53 if (ZSTD_isError(res
)) {
54 ZSTD_freeCStream(z
->zcs
);
56 error_setg(errp
, "multifd %u: initCStream failed with error %s",
57 p
->id
, ZSTD_getErrorName(res
));
60 /* This is the maximum size of the compressed buffer */
61 z
->zbuff_len
= ZSTD_compressBound(MULTIFD_PACKET_SIZE
);
62 z
->zbuff
= g_try_malloc(z
->zbuff_len
);
64 ZSTD_freeCStream(z
->zcs
);
66 error_setg(errp
, "multifd %u: out of memory for zbuff", p
->id
);
71 /* Needs 2 IOVs, one for packet header and one for compressed data */
72 p
->iov
= g_new0(struct iovec
, 2);
76 static void multifd_zstd_send_cleanup(MultiFDSendParams
*p
, Error
**errp
)
78 struct zstd_data
*z
= p
->compress_data
;
80 ZSTD_freeCStream(z
->zcs
);
84 g_free(p
->compress_data
);
85 p
->compress_data
= NULL
;
91 static int multifd_zstd_send_prepare(MultiFDSendParams
*p
, Error
**errp
)
93 MultiFDPages_t
*pages
= &p
->data
->u
.ram
;
94 struct zstd_data
*z
= p
->compress_data
;
98 if (!multifd_send_prepare_common(p
)) {
102 z
->out
.dst
= z
->zbuff
;
103 z
->out
.size
= z
->zbuff_len
;
106 for (i
= 0; i
< pages
->normal_num
; i
++) {
107 ZSTD_EndDirective flush
= ZSTD_e_continue
;
109 if (i
== pages
->normal_num
- 1) {
110 flush
= ZSTD_e_flush
;
112 z
->in
.src
= pages
->block
->host
+ pages
->offset
[i
];
113 z
->in
.size
= multifd_ram_page_size();
117 * Welcome to compressStream2 semantics
119 * We need to loop while:
121 * - there is input available
122 * - there is output space free
125 ret
= ZSTD_compressStream2(z
->zcs
, &z
->out
, &z
->in
, flush
);
126 } while (ret
> 0 && (z
->in
.size
> z
->in
.pos
)
127 && (z
->out
.size
> z
->out
.pos
));
128 if (ret
> 0 && (z
->in
.size
> z
->in
.pos
)) {
129 error_setg(errp
, "multifd %u: compressStream buffer too small",
133 if (ZSTD_isError(ret
)) {
134 error_setg(errp
, "multifd %u: compressStream error %s",
135 p
->id
, ZSTD_getErrorName(ret
));
139 p
->iov
[p
->iovs_num
].iov_base
= z
->zbuff
;
140 p
->iov
[p
->iovs_num
].iov_len
= z
->out
.pos
;
142 p
->next_packet_size
= z
->out
.pos
;
145 p
->flags
|= MULTIFD_FLAG_ZSTD
;
146 multifd_send_fill_packet(p
);
150 static int multifd_zstd_recv_setup(MultiFDRecvParams
*p
, Error
**errp
)
152 struct zstd_data
*z
= g_new0(struct zstd_data
, 1);
155 p
->compress_data
= z
;
156 z
->zds
= ZSTD_createDStream();
159 error_setg(errp
, "multifd %u: zstd createDStream failed", p
->id
);
163 ret
= ZSTD_initDStream(z
->zds
);
164 if (ZSTD_isError(ret
)) {
165 ZSTD_freeDStream(z
->zds
);
167 error_setg(errp
, "multifd %u: initDStream failed with error %s",
168 p
->id
, ZSTD_getErrorName(ret
));
172 /* To be safe, we reserve twice the size of the packet */
173 z
->zbuff_len
= MULTIFD_PACKET_SIZE
* 2;
174 z
->zbuff
= g_try_malloc(z
->zbuff_len
);
176 ZSTD_freeDStream(z
->zds
);
178 error_setg(errp
, "multifd %u: out of memory for zbuff", p
->id
);
184 static void multifd_zstd_recv_cleanup(MultiFDRecvParams
*p
)
186 struct zstd_data
*z
= p
->compress_data
;
188 ZSTD_freeDStream(z
->zds
);
192 g_free(p
->compress_data
);
193 p
->compress_data
= NULL
;
196 static int multifd_zstd_recv(MultiFDRecvParams
*p
, Error
**errp
)
198 uint32_t in_size
= p
->next_packet_size
;
199 uint32_t out_size
= 0;
200 uint32_t page_size
= multifd_ram_page_size();
201 uint32_t expected_size
= p
->normal_num
* page_size
;
202 uint32_t flags
= p
->flags
& MULTIFD_FLAG_COMPRESSION_MASK
;
203 struct zstd_data
*z
= p
->compress_data
;
207 if (flags
!= MULTIFD_FLAG_ZSTD
) {
208 error_setg(errp
, "multifd %u: flags received %x flags expected %x",
209 p
->id
, flags
, MULTIFD_FLAG_ZSTD
);
213 multifd_recv_zero_page_process(p
);
215 if (!p
->normal_num
) {
216 assert(in_size
== 0);
220 ret
= qio_channel_read_all(p
->c
, (void *)z
->zbuff
, in_size
, errp
);
226 z
->in
.src
= z
->zbuff
;
227 z
->in
.size
= in_size
;
230 for (i
= 0; i
< p
->normal_num
; i
++) {
231 ramblock_recv_bitmap_set_offset(p
->block
, p
->normal
[i
]);
232 z
->out
.dst
= p
->host
+ p
->normal
[i
];
233 z
->out
.size
= page_size
;
237 * Welcome to decompressStream semantics
239 * We need to loop while:
241 * - there is input available
242 * - we haven't put out a full page
245 ret
= ZSTD_decompressStream(z
->zds
, &z
->out
, &z
->in
);
246 } while (ret
> 0 && (z
->in
.size
> z
->in
.pos
)
247 && (z
->out
.pos
< page_size
));
248 if (ret
> 0 && (z
->out
.pos
< page_size
)) {
249 error_setg(errp
, "multifd %u: decompressStream buffer too small",
253 if (ZSTD_isError(ret
)) {
254 error_setg(errp
, "multifd %u: decompressStream returned %s",
255 p
->id
, ZSTD_getErrorName(ret
));
258 out_size
+= z
->out
.pos
;
260 if (out_size
!= expected_size
) {
261 error_setg(errp
, "multifd %u: packet size received %u size expected %u",
262 p
->id
, out_size
, expected_size
);
268 static const MultiFDMethods multifd_zstd_ops
= {
269 .send_setup
= multifd_zstd_send_setup
,
270 .send_cleanup
= multifd_zstd_send_cleanup
,
271 .send_prepare
= multifd_zstd_send_prepare
,
272 .recv_setup
= multifd_zstd_recv_setup
,
273 .recv_cleanup
= multifd_zstd_recv_cleanup
,
274 .recv
= multifd_zstd_recv
277 static void multifd_zstd_register(void)
279 multifd_register_ops(MULTIFD_COMPRESSION_ZSTD
, &multifd_zstd_ops
);
282 migration_init(multifd_zstd_register
);