xref: /openbmc/qemu/migration/multifd-zlib.c (revision 6016b7b4)
1 /*
2  * Multifd zlib compression implementation
3  *
4  * Copyright (c) 2020 Red Hat Inc
5  *
6  * Authors:
7  *  Juan Quintela <quintela@redhat.com>
8  *
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.
11  */
12 
13 #include "qemu/osdep.h"
14 #include <zlib.h>
15 #include "qemu/rcu.h"
16 #include "exec/ramblock.h"
17 #include "exec/target_page.h"
18 #include "qapi/error.h"
19 #include "migration.h"
20 #include "trace.h"
21 #include "multifd.h"
22 
23 struct zlib_data {
24     /* stream for compression */
25     z_stream zs;
26     /* compressed buffer */
27     uint8_t *zbuff;
28     /* size of compressed buffer */
29     uint32_t zbuff_len;
30 };
31 
32 /* Multifd zlib compression */
33 
34 /**
35  * zlib_send_setup: setup send side
36  *
37  * Setup each channel with zlib compression.
38  *
39  * Returns 0 for success or -1 for error
40  *
41  * @p: Params for the channel that we are using
42  * @errp: pointer to an error
43  */
44 static int zlib_send_setup(MultiFDSendParams *p, Error **errp)
45 {
46     struct zlib_data *z = g_malloc0(sizeof(struct zlib_data));
47     z_stream *zs = &z->zs;
48 
49     zs->zalloc = Z_NULL;
50     zs->zfree = Z_NULL;
51     zs->opaque = Z_NULL;
52     if (deflateInit(zs, migrate_multifd_zlib_level()) != Z_OK) {
53         g_free(z);
54         error_setg(errp, "multifd %d: deflate init failed", p->id);
55         return -1;
56     }
57     /* To be safe, we reserve twice the size of the packet */
58     z->zbuff_len = MULTIFD_PACKET_SIZE * 2;
59     z->zbuff = g_try_malloc(z->zbuff_len);
60     if (!z->zbuff) {
61         deflateEnd(&z->zs);
62         g_free(z);
63         error_setg(errp, "multifd %d: out of memory for zbuff", p->id);
64         return -1;
65     }
66     p->data = z;
67     return 0;
68 }
69 
70 /**
71  * zlib_send_cleanup: cleanup send side
72  *
73  * Close the channel and return memory.
74  *
75  * @p: Params for the channel that we are using
76  * @errp: pointer to an error
77  */
78 static void zlib_send_cleanup(MultiFDSendParams *p, Error **errp)
79 {
80     struct zlib_data *z = p->data;
81 
82     deflateEnd(&z->zs);
83     g_free(z->zbuff);
84     z->zbuff = NULL;
85     g_free(p->data);
86     p->data = NULL;
87 }
88 
89 /**
90  * zlib_send_prepare: prepare date to be able to send
91  *
92  * Create a compressed buffer with all the pages that we are going to
93  * send.
94  *
95  * Returns 0 for success or -1 for error
96  *
97  * @p: Params for the channel that we are using
98  * @errp: pointer to an error
99  */
100 static int zlib_send_prepare(MultiFDSendParams *p, Error **errp)
101 {
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;
106     int ret;
107     uint32_t i;
108 
109     for (i = 0; i < p->pages->num; i++) {
110         uint32_t available = z->zbuff_len - out_size;
111         int flush = Z_NO_FLUSH;
112 
113         if (i == p->pages->num - 1) {
114             flush = Z_SYNC_FLUSH;
115         }
116 
117         zs->avail_in = page_size;
118         zs->next_in = p->pages->block->host + p->pages->offset[i];
119 
120         zs->avail_out = available;
121         zs->next_out = z->zbuff + out_size;
122 
123         /*
124          * Welcome to deflate semantics
125          *
126          * We need to loop while:
127          * - return is Z_OK
128          * - there are stuff to be compressed
129          * - there are output space free
130          */
131         do {
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 %d: deflate failed to compress all input",
136                        p->id);
137             return -1;
138         }
139         if (ret != Z_OK) {
140             error_setg(errp, "multifd %d: deflate returned %d instead of Z_OK",
141                        p->id, ret);
142             return -1;
143         }
144         out_size += available - zs->avail_out;
145     }
146     p->next_packet_size = out_size;
147     p->flags |= MULTIFD_FLAG_ZLIB;
148 
149     return 0;
150 }
151 
152 /**
153  * zlib_send_write: do the actual write of the data
154  *
155  * Do the actual write of the comprresed buffer.
156  *
157  * Returns 0 for success or -1 for error
158  *
159  * @p: Params for the channel that we are using
160  * @used: number of pages used
161  * @errp: pointer to an error
162  */
163 static int zlib_send_write(MultiFDSendParams *p, uint32_t used, Error **errp)
164 {
165     struct zlib_data *z = p->data;
166 
167     return qio_channel_write_all(p->c, (void *)z->zbuff, p->next_packet_size,
168                                  errp);
169 }
170 
171 /**
172  * zlib_recv_setup: setup receive side
173  *
174  * Create the compressed channel and buffer.
175  *
176  * Returns 0 for success or -1 for error
177  *
178  * @p: Params for the channel that we are using
179  * @errp: pointer to an error
180  */
181 static int zlib_recv_setup(MultiFDRecvParams *p, Error **errp)
182 {
183     struct zlib_data *z = g_malloc0(sizeof(struct zlib_data));
184     z_stream *zs = &z->zs;
185 
186     p->data = z;
187     zs->zalloc = Z_NULL;
188     zs->zfree = Z_NULL;
189     zs->opaque = Z_NULL;
190     zs->avail_in = 0;
191     zs->next_in = Z_NULL;
192     if (inflateInit(zs) != Z_OK) {
193         error_setg(errp, "multifd %d: inflate init failed", p->id);
194         return -1;
195     }
196     /* To be safe, we reserve twice the size of the packet */
197     z->zbuff_len = MULTIFD_PACKET_SIZE * 2;
198     z->zbuff = g_try_malloc(z->zbuff_len);
199     if (!z->zbuff) {
200         inflateEnd(zs);
201         error_setg(errp, "multifd %d: out of memory for zbuff", p->id);
202         return -1;
203     }
204     return 0;
205 }
206 
207 /**
208  * zlib_recv_cleanup: setup receive side
209  *
210  * For no compression this function does nothing.
211  *
212  * @p: Params for the channel that we are using
213  */
214 static void zlib_recv_cleanup(MultiFDRecvParams *p)
215 {
216     struct zlib_data *z = p->data;
217 
218     inflateEnd(&z->zs);
219     g_free(z->zbuff);
220     z->zbuff = NULL;
221     g_free(p->data);
222     p->data = NULL;
223 }
224 
225 /**
226  * zlib_recv_pages: read the data from the channel into actual pages
227  *
228  * Read the compressed buffer, and uncompress it into the actual
229  * pages.
230  *
231  * Returns 0 for success or -1 for error
232  *
233  * @p: Params for the channel that we are using
234  * @errp: pointer to an error
235  */
236 static int zlib_recv_pages(MultiFDRecvParams *p, Error **errp)
237 {
238     struct zlib_data *z = p->data;
239     size_t page_size = qemu_target_page_size();
240     z_stream *zs = &z->zs;
241     uint32_t in_size = p->next_packet_size;
242     /* we measure the change of total_out */
243     uint32_t out_size = zs->total_out;
244     uint32_t expected_size = p->pages->num * qemu_target_page_size();
245     uint32_t flags = p->flags & MULTIFD_FLAG_COMPRESSION_MASK;
246     int ret;
247     int i;
248 
249     if (flags != MULTIFD_FLAG_ZLIB) {
250         error_setg(errp, "multifd %d: flags received %x flags expected %x",
251                    p->id, flags, MULTIFD_FLAG_ZLIB);
252         return -1;
253     }
254     ret = qio_channel_read_all(p->c, (void *)z->zbuff, in_size, errp);
255 
256     if (ret != 0) {
257         return ret;
258     }
259 
260     zs->avail_in = in_size;
261     zs->next_in = z->zbuff;
262 
263     for (i = 0; i < p->pages->num; i++) {
264         int flush = Z_NO_FLUSH;
265         unsigned long start = zs->total_out;
266 
267         if (i == p->pages->num - 1) {
268             flush = Z_SYNC_FLUSH;
269         }
270 
271         zs->avail_out = page_size;
272         zs->next_out = p->pages->block->host + p->pages->offset[i];
273 
274         /*
275          * Welcome to inflate semantics
276          *
277          * We need to loop while:
278          * - return is Z_OK
279          * - there are input available
280          * - we haven't completed a full page
281          */
282         do {
283             ret = inflate(zs, flush);
284         } while (ret == Z_OK && zs->avail_in
285                              && (zs->total_out - start) < page_size);
286         if (ret == Z_OK && (zs->total_out - start) < page_size) {
287             error_setg(errp, "multifd %d: inflate generated too few output",
288                        p->id);
289             return -1;
290         }
291         if (ret != Z_OK) {
292             error_setg(errp, "multifd %d: inflate returned %d instead of Z_OK",
293                        p->id, ret);
294             return -1;
295         }
296     }
297     out_size = zs->total_out - out_size;
298     if (out_size != expected_size) {
299         error_setg(errp, "multifd %d: packet size received %d size expected %d",
300                    p->id, out_size, expected_size);
301         return -1;
302     }
303     return 0;
304 }
305 
306 static MultiFDMethods multifd_zlib_ops = {
307     .send_setup = zlib_send_setup,
308     .send_cleanup = zlib_send_cleanup,
309     .send_prepare = zlib_send_prepare,
310     .send_write = zlib_send_write,
311     .recv_setup = zlib_recv_setup,
312     .recv_cleanup = zlib_recv_cleanup,
313     .recv_pages = zlib_recv_pages
314 };
315 
316 static void multifd_zlib_register(void)
317 {
318     multifd_register_ops(MULTIFD_COMPRESSION_ZLIB, &multifd_zlib_ops);
319 }
320 
321 migration_init(multifd_zlib_register);
322