xref: /openbmc/qemu/migration/multifd-qatzip.c (revision 6a0e10b7)
1 /*
2  * Multifd QATzip compression implementation
3  *
4  * Copyright (c) Bytedance
5  *
6  * Authors:
7  *  Bryan Zhang <bryan.zhang@bytedance.com>
8  *  Hao Xiang <hao.xiang@bytedance.com>
9  *  Yichen Wang <yichen.wang@bytedance.com>
10  *
11  * This work is licensed under the terms of the GNU GPL, version 2 or later.
12  * See the COPYING file in the top-level directory.
13  */
14 
15 #include "qemu/osdep.h"
16 #include "exec/ramblock.h"
17 #include "qapi/error.h"
18 #include "qemu/error-report.h"
19 #include "qapi/qapi-types-migration.h"
20 #include "options.h"
21 #include "multifd.h"
22 #include <qatzip.h>
23 
24 typedef struct {
25     /*
26      * Unique session for use with QATzip API
27      */
28     QzSession_T sess;
29 
30     /*
31      * For compression: Buffer for pages to compress
32      * For decompression: Buffer for data to decompress
33      */
34     uint8_t *in_buf;
35     uint32_t in_len;
36 
37     /*
38      * For compression: Output buffer of compressed data
39      * For decompression: Output buffer of decompressed data
40      */
41     uint8_t *out_buf;
42     uint32_t out_len;
43 } QatzipData;
44 
45 /**
46  * qatzip_send_setup: Set up QATzip session and private buffers.
47  *
48  * @param p    Multifd channel params
49  * @param errp Pointer to error, which will be set in case of error
50  * @return     0 on success, -1 on error (and *errp will be set)
51  */
52 static int qatzip_send_setup(MultiFDSendParams *p, Error **errp)
53 {
54     QatzipData *q;
55     QzSessionParamsDeflate_T params;
56     const char *err_msg;
57     int ret;
58 
59     q = g_new0(QatzipData, 1);
60     p->compress_data = q;
61     /* We need one extra place for the packet header */
62     p->iov = g_new0(struct iovec, 2);
63 
64     /*
65      * Initialize QAT device with software fallback by default. This allows
66      * QATzip to use CPU path when QAT hardware reaches maximum throughput.
67      */
68     ret = qzInit(&q->sess, true);
69     if (ret != QZ_OK && ret != QZ_DUPLICATE) {
70         err_msg = "qzInit failed";
71         goto err;
72     }
73 
74     ret = qzGetDefaultsDeflate(&params);
75     if (ret != QZ_OK) {
76         err_msg = "qzGetDefaultsDeflate failed";
77         goto err;
78     }
79 
80     /* Make sure to use configured QATzip compression level. */
81     params.common_params.comp_lvl = migrate_multifd_qatzip_level();
82     ret = qzSetupSessionDeflate(&q->sess, &params);
83     if (ret != QZ_OK && ret != QZ_DUPLICATE) {
84         err_msg = "qzSetupSessionDeflate failed";
85         goto err;
86     }
87 
88     if (MULTIFD_PACKET_SIZE > UINT32_MAX) {
89         err_msg = "packet size too large for QAT";
90         goto err;
91     }
92 
93     q->in_len = MULTIFD_PACKET_SIZE;
94     /*
95      * PINNED_MEM is an enum from qatzip headers, which means to use
96      * kzalloc_node() to allocate memory for QAT DMA purposes. When QAT device
97      * is not available or software fallback is used, the malloc flag needs to
98      * be set as COMMON_MEM.
99      */
100     q->in_buf = qzMalloc(q->in_len, 0, PINNED_MEM);
101     if (!q->in_buf) {
102         q->in_buf = qzMalloc(q->in_len, 0, COMMON_MEM);
103         if (!q->in_buf) {
104             err_msg = "qzMalloc failed";
105             goto err;
106         }
107     }
108 
109     q->out_len = qzMaxCompressedLength(MULTIFD_PACKET_SIZE, &q->sess);
110     q->out_buf = qzMalloc(q->out_len, 0, PINNED_MEM);
111     if (!q->out_buf) {
112         q->out_buf = qzMalloc(q->out_len, 0, COMMON_MEM);
113         if (!q->out_buf) {
114             err_msg = "qzMalloc failed";
115             goto err;
116         }
117     }
118 
119     return 0;
120 
121 err:
122     error_setg(errp, "multifd %u: [sender] %s", p->id, err_msg);
123     return -1;
124 }
125 
126 /**
127  * qatzip_send_cleanup: Tear down QATzip session and release private buffers.
128  *
129  * @param p    Multifd channel params
130  * @param errp Pointer to error, which will be set in case of error
131  * @return     None
132  */
133 static void qatzip_send_cleanup(MultiFDSendParams *p, Error **errp)
134 {
135     QatzipData *q = p->compress_data;
136 
137     if (q) {
138         if (q->in_buf) {
139             qzFree(q->in_buf);
140         }
141         if (q->out_buf) {
142             qzFree(q->out_buf);
143         }
144         (void)qzTeardownSession(&q->sess);
145         (void)qzClose(&q->sess);
146         g_free(q);
147     }
148 
149     g_free(p->iov);
150     p->iov = NULL;
151     p->compress_data = NULL;
152 }
153 
154 /**
155  * qatzip_send_prepare: Compress pages and update IO channel info.
156  *
157  * @param p    Multifd channel params
158  * @param errp Pointer to error, which will be set in case of error
159  * @return     0 on success, -1 on error (and *errp will be set)
160  */
161 static int qatzip_send_prepare(MultiFDSendParams *p, Error **errp)
162 {
163     MultiFDPages_t *pages = p->pages;
164     QatzipData *q = p->compress_data;
165     int ret;
166     unsigned int in_len, out_len;
167 
168     if (!multifd_send_prepare_common(p)) {
169         goto out;
170     }
171 
172     /*
173      * Unlike other multifd compression implementations, we use a non-streaming
174      * API and place all the data into one buffer, rather than sending each
175      * page to the compression API at a time. Based on initial benchmarks, the
176      * non-streaming API outperforms the streaming API. Plus, the logic in QEMU
177      * is friendly to using the non-streaming API anyway. If either of these
178      * statements becomes no longer true, we can revisit adding a streaming
179      * implementation.
180      */
181     for (int i = 0; i < pages->normal_num; i++) {
182         memcpy(q->in_buf + (i * p->page_size),
183                pages->block->host + pages->offset[i],
184                p->page_size);
185     }
186 
187     in_len = pages->normal_num * p->page_size;
188     if (in_len > q->in_len) {
189         error_setg(errp, "multifd %u: unexpectedly large input", p->id);
190         return -1;
191     }
192     out_len = q->out_len;
193 
194     ret = qzCompress(&q->sess, q->in_buf, &in_len, q->out_buf, &out_len, 1);
195     if (ret != QZ_OK) {
196         error_setg(errp, "multifd %u: QATzip returned %d instead of QZ_OK",
197                    p->id, ret);
198         return -1;
199     }
200     if (in_len != pages->normal_num * p->page_size) {
201         error_setg(errp, "multifd %u: QATzip failed to compress all input",
202                    p->id);
203         return -1;
204     }
205 
206     p->iov[p->iovs_num].iov_base = q->out_buf;
207     p->iov[p->iovs_num].iov_len = out_len;
208     p->iovs_num++;
209     p->next_packet_size = out_len;
210 
211 out:
212     p->flags |= MULTIFD_FLAG_QATZIP;
213     multifd_send_fill_packet(p);
214     return 0;
215 }
216 
217 /**
218  * qatzip_recv_setup: Set up QATzip session and allocate private buffers.
219  *
220  * @param p    Multifd channel params
221  * @param errp Pointer to error, which will be set in case of error
222  * @return     0 on success, -1 on error (and *errp will be set)
223  */
224 static int qatzip_recv_setup(MultiFDRecvParams *p, Error **errp)
225 {
226     QatzipData *q;
227     QzSessionParamsDeflate_T params;
228     const char *err_msg;
229     int ret;
230 
231     q = g_new0(QatzipData, 1);
232     p->compress_data = q;
233 
234     /*
235      * Initialize QAT device with software fallback by default. This allows
236      * QATzip to use CPU path when QAT hardware reaches maximum throughput.
237      */
238     ret = qzInit(&q->sess, true);
239     if (ret != QZ_OK && ret != QZ_DUPLICATE) {
240         err_msg = "qzInit failed";
241         goto err;
242     }
243 
244     ret = qzGetDefaultsDeflate(&params);
245     if (ret != QZ_OK) {
246         err_msg = "qzGetDefaultsDeflate failed";
247         goto err;
248     }
249 
250     ret = qzSetupSessionDeflate(&q->sess, &params);
251     if (ret != QZ_OK && ret != QZ_DUPLICATE) {
252         err_msg = "qzSetupSessionDeflate failed";
253         goto err;
254     }
255 
256     /*
257      * Reserve extra spaces for the incoming packets. Current implementation
258      * doesn't send uncompressed pages in case the compression gets too big.
259      */
260     q->in_len = MULTIFD_PACKET_SIZE * 2;
261     /*
262      * PINNED_MEM is an enum from qatzip headers, which means to use
263      * kzalloc_node() to allocate memory for QAT DMA purposes. When QAT device
264      * is not available or software fallback is used, the malloc flag needs to
265      * be set as COMMON_MEM.
266      */
267     q->in_buf = qzMalloc(q->in_len, 0, PINNED_MEM);
268     if (!q->in_buf) {
269         q->in_buf = qzMalloc(q->in_len, 0, COMMON_MEM);
270         if (!q->in_buf) {
271             err_msg = "qzMalloc failed";
272             goto err;
273         }
274     }
275 
276     q->out_len = MULTIFD_PACKET_SIZE;
277     q->out_buf = qzMalloc(q->out_len, 0, PINNED_MEM);
278     if (!q->out_buf) {
279         q->out_buf = qzMalloc(q->out_len, 0, COMMON_MEM);
280         if (!q->out_buf) {
281             err_msg = "qzMalloc failed";
282             goto err;
283         }
284     }
285 
286     return 0;
287 
288 err:
289     error_setg(errp, "multifd %u: [receiver] %s", p->id, err_msg);
290     return -1;
291 }
292 
293 /**
294  * qatzip_recv_cleanup: Tear down QATzip session and release private buffers.
295  *
296  * @param p    Multifd channel params
297  * @return     None
298  */
299 static void qatzip_recv_cleanup(MultiFDRecvParams *p)
300 {
301     QatzipData *q = p->compress_data;
302 
303     if (q) {
304         if (q->in_buf) {
305             qzFree(q->in_buf);
306         }
307         if (q->out_buf) {
308             qzFree(q->out_buf);
309         }
310         (void)qzTeardownSession(&q->sess);
311         (void)qzClose(&q->sess);
312         g_free(q);
313     }
314     p->compress_data = NULL;
315 }
316 
317 
318 /**
319  * qatzip_recv: Decompress pages and copy them to the appropriate
320  * locations.
321  *
322  * @param p    Multifd channel params
323  * @param errp Pointer to error, which will be set in case of error
324  * @return     0 on success, -1 on error (and *errp will be set)
325  */
326 static int qatzip_recv(MultiFDRecvParams *p, Error **errp)
327 {
328     QatzipData *q = p->compress_data;
329     int ret;
330     unsigned int in_len, out_len;
331     uint32_t in_size = p->next_packet_size;
332     uint32_t expected_size = p->normal_num * p->page_size;
333     uint32_t flags = p->flags & MULTIFD_FLAG_COMPRESSION_MASK;
334 
335     if (in_size > q->in_len) {
336         error_setg(errp, "multifd %u: received unexpectedly large packet",
337                    p->id);
338         return -1;
339     }
340 
341     if (flags != MULTIFD_FLAG_QATZIP) {
342         error_setg(errp, "multifd %u: flags received %x flags expected %x",
343                    p->id, flags, MULTIFD_FLAG_QATZIP);
344         return -1;
345     }
346 
347     multifd_recv_zero_page_process(p);
348     if (!p->normal_num) {
349         assert(in_size == 0);
350         return 0;
351     }
352 
353     ret = qio_channel_read_all(p->c, (void *)q->in_buf, in_size, errp);
354     if (ret != 0) {
355         return ret;
356     }
357 
358     in_len = in_size;
359     out_len = q->out_len;
360     ret = qzDecompress(&q->sess, q->in_buf, &in_len, q->out_buf, &out_len);
361     if (ret != QZ_OK) {
362         error_setg(errp, "multifd %u: qzDecompress failed", p->id);
363         return -1;
364     }
365     if (out_len != expected_size) {
366         error_setg(errp, "multifd %u: packet size received %u size expected %u",
367                    p->id, out_len, expected_size);
368         return -1;
369     }
370 
371     /* Copy each page to its appropriate location. */
372     for (int i = 0; i < p->normal_num; i++) {
373         memcpy(p->host + p->normal[i],
374                q->out_buf + p->page_size * i,
375                p->page_size);
376     }
377     return 0;
378 }
379 
380 static MultiFDMethods multifd_qatzip_ops = {
381     .send_setup = qatzip_send_setup,
382     .send_cleanup = qatzip_send_cleanup,
383     .send_prepare = qatzip_send_prepare,
384     .recv_setup = qatzip_recv_setup,
385     .recv_cleanup = qatzip_recv_cleanup,
386     .recv = qatzip_recv
387 };
388 
389 static void multifd_qatzip_register(void)
390 {
391     multifd_register_ops(MULTIFD_COMPRESSION_QATZIP, &multifd_qatzip_ops);
392 }
393 
394 migration_init(multifd_qatzip_register);
395