180484f94SBryan Zhang /*
280484f94SBryan Zhang * Multifd QATzip compression implementation
380484f94SBryan Zhang *
480484f94SBryan Zhang * Copyright (c) Bytedance
580484f94SBryan Zhang *
680484f94SBryan Zhang * Authors:
780484f94SBryan Zhang * Bryan Zhang <bryan.zhang@bytedance.com>
880484f94SBryan Zhang * Hao Xiang <hao.xiang@bytedance.com>
980484f94SBryan Zhang * Yichen Wang <yichen.wang@bytedance.com>
1080484f94SBryan Zhang *
1180484f94SBryan Zhang * This work is licensed under the terms of the GNU GPL, version 2 or later.
1280484f94SBryan Zhang * See the COPYING file in the top-level directory.
1380484f94SBryan Zhang */
1480484f94SBryan Zhang
1580484f94SBryan Zhang #include "qemu/osdep.h"
1680484f94SBryan Zhang #include "exec/ramblock.h"
1780484f94SBryan Zhang #include "qapi/error.h"
1880484f94SBryan Zhang #include "qemu/error-report.h"
1980484f94SBryan Zhang #include "qapi/qapi-types-migration.h"
2080484f94SBryan Zhang #include "options.h"
2180484f94SBryan Zhang #include "multifd.h"
2280484f94SBryan Zhang #include <qatzip.h>
2380484f94SBryan Zhang
2480484f94SBryan Zhang typedef struct {
2580484f94SBryan Zhang /*
2680484f94SBryan Zhang * Unique session for use with QATzip API
2780484f94SBryan Zhang */
2880484f94SBryan Zhang QzSession_T sess;
2980484f94SBryan Zhang
3080484f94SBryan Zhang /*
3180484f94SBryan Zhang * For compression: Buffer for pages to compress
3280484f94SBryan Zhang * For decompression: Buffer for data to decompress
3380484f94SBryan Zhang */
3480484f94SBryan Zhang uint8_t *in_buf;
3580484f94SBryan Zhang uint32_t in_len;
3680484f94SBryan Zhang
3780484f94SBryan Zhang /*
3880484f94SBryan Zhang * For compression: Output buffer of compressed data
3980484f94SBryan Zhang * For decompression: Output buffer of decompressed data
4080484f94SBryan Zhang */
4180484f94SBryan Zhang uint8_t *out_buf;
4280484f94SBryan Zhang uint32_t out_len;
4380484f94SBryan Zhang } QatzipData;
4480484f94SBryan Zhang
4580484f94SBryan Zhang /**
4680484f94SBryan Zhang * qatzip_send_setup: Set up QATzip session and private buffers.
4780484f94SBryan Zhang *
4880484f94SBryan Zhang * @param p Multifd channel params
4980484f94SBryan Zhang * @param errp Pointer to error, which will be set in case of error
5080484f94SBryan Zhang * @return 0 on success, -1 on error (and *errp will be set)
5180484f94SBryan Zhang */
qatzip_send_setup(MultiFDSendParams * p,Error ** errp)5280484f94SBryan Zhang static int qatzip_send_setup(MultiFDSendParams *p, Error **errp)
5380484f94SBryan Zhang {
5480484f94SBryan Zhang QatzipData *q;
5580484f94SBryan Zhang QzSessionParamsDeflate_T params;
5680484f94SBryan Zhang const char *err_msg;
5780484f94SBryan Zhang int ret;
5880484f94SBryan Zhang
5980484f94SBryan Zhang q = g_new0(QatzipData, 1);
6080484f94SBryan Zhang p->compress_data = q;
6180484f94SBryan Zhang /* We need one extra place for the packet header */
6280484f94SBryan Zhang p->iov = g_new0(struct iovec, 2);
6380484f94SBryan Zhang
6480484f94SBryan Zhang /*
6580484f94SBryan Zhang * Initialize QAT device with software fallback by default. This allows
6680484f94SBryan Zhang * QATzip to use CPU path when QAT hardware reaches maximum throughput.
6780484f94SBryan Zhang */
6880484f94SBryan Zhang ret = qzInit(&q->sess, true);
6980484f94SBryan Zhang if (ret != QZ_OK && ret != QZ_DUPLICATE) {
7080484f94SBryan Zhang err_msg = "qzInit failed";
7180484f94SBryan Zhang goto err;
7280484f94SBryan Zhang }
7380484f94SBryan Zhang
7480484f94SBryan Zhang ret = qzGetDefaultsDeflate(¶ms);
7580484f94SBryan Zhang if (ret != QZ_OK) {
7680484f94SBryan Zhang err_msg = "qzGetDefaultsDeflate failed";
7780484f94SBryan Zhang goto err;
7880484f94SBryan Zhang }
7980484f94SBryan Zhang
8080484f94SBryan Zhang /* Make sure to use configured QATzip compression level. */
8180484f94SBryan Zhang params.common_params.comp_lvl = migrate_multifd_qatzip_level();
8280484f94SBryan Zhang ret = qzSetupSessionDeflate(&q->sess, ¶ms);
8380484f94SBryan Zhang if (ret != QZ_OK && ret != QZ_DUPLICATE) {
8480484f94SBryan Zhang err_msg = "qzSetupSessionDeflate failed";
8580484f94SBryan Zhang goto err;
8680484f94SBryan Zhang }
8780484f94SBryan Zhang
8880484f94SBryan Zhang if (MULTIFD_PACKET_SIZE > UINT32_MAX) {
8980484f94SBryan Zhang err_msg = "packet size too large for QAT";
9080484f94SBryan Zhang goto err;
9180484f94SBryan Zhang }
9280484f94SBryan Zhang
9380484f94SBryan Zhang q->in_len = MULTIFD_PACKET_SIZE;
9480484f94SBryan Zhang /*
9580484f94SBryan Zhang * PINNED_MEM is an enum from qatzip headers, which means to use
9680484f94SBryan Zhang * kzalloc_node() to allocate memory for QAT DMA purposes. When QAT device
9780484f94SBryan Zhang * is not available or software fallback is used, the malloc flag needs to
9880484f94SBryan Zhang * be set as COMMON_MEM.
9980484f94SBryan Zhang */
10080484f94SBryan Zhang q->in_buf = qzMalloc(q->in_len, 0, PINNED_MEM);
10180484f94SBryan Zhang if (!q->in_buf) {
10280484f94SBryan Zhang q->in_buf = qzMalloc(q->in_len, 0, COMMON_MEM);
10380484f94SBryan Zhang if (!q->in_buf) {
10480484f94SBryan Zhang err_msg = "qzMalloc failed";
10580484f94SBryan Zhang goto err;
10680484f94SBryan Zhang }
10780484f94SBryan Zhang }
10880484f94SBryan Zhang
10980484f94SBryan Zhang q->out_len = qzMaxCompressedLength(MULTIFD_PACKET_SIZE, &q->sess);
11080484f94SBryan Zhang q->out_buf = qzMalloc(q->out_len, 0, PINNED_MEM);
11180484f94SBryan Zhang if (!q->out_buf) {
11280484f94SBryan Zhang q->out_buf = qzMalloc(q->out_len, 0, COMMON_MEM);
11380484f94SBryan Zhang if (!q->out_buf) {
11480484f94SBryan Zhang err_msg = "qzMalloc failed";
11580484f94SBryan Zhang goto err;
11680484f94SBryan Zhang }
11780484f94SBryan Zhang }
11880484f94SBryan Zhang
11980484f94SBryan Zhang return 0;
12080484f94SBryan Zhang
12180484f94SBryan Zhang err:
12280484f94SBryan Zhang error_setg(errp, "multifd %u: [sender] %s", p->id, err_msg);
12380484f94SBryan Zhang return -1;
12480484f94SBryan Zhang }
12580484f94SBryan Zhang
12680484f94SBryan Zhang /**
12780484f94SBryan Zhang * qatzip_send_cleanup: Tear down QATzip session and release private buffers.
12880484f94SBryan Zhang *
12980484f94SBryan Zhang * @param p Multifd channel params
13080484f94SBryan Zhang * @param errp Pointer to error, which will be set in case of error
13180484f94SBryan Zhang * @return None
13280484f94SBryan Zhang */
qatzip_send_cleanup(MultiFDSendParams * p,Error ** errp)13380484f94SBryan Zhang static void qatzip_send_cleanup(MultiFDSendParams *p, Error **errp)
13480484f94SBryan Zhang {
13580484f94SBryan Zhang QatzipData *q = p->compress_data;
13680484f94SBryan Zhang
13780484f94SBryan Zhang if (q) {
13880484f94SBryan Zhang if (q->in_buf) {
13980484f94SBryan Zhang qzFree(q->in_buf);
14080484f94SBryan Zhang }
14180484f94SBryan Zhang if (q->out_buf) {
14280484f94SBryan Zhang qzFree(q->out_buf);
14380484f94SBryan Zhang }
14480484f94SBryan Zhang (void)qzTeardownSession(&q->sess);
14580484f94SBryan Zhang (void)qzClose(&q->sess);
14680484f94SBryan Zhang g_free(q);
14780484f94SBryan Zhang }
14880484f94SBryan Zhang
14980484f94SBryan Zhang g_free(p->iov);
15080484f94SBryan Zhang p->iov = NULL;
15180484f94SBryan Zhang p->compress_data = NULL;
15280484f94SBryan Zhang }
15380484f94SBryan Zhang
15480484f94SBryan Zhang /**
15580484f94SBryan Zhang * qatzip_send_prepare: Compress pages and update IO channel info.
15680484f94SBryan Zhang *
15780484f94SBryan Zhang * @param p Multifd channel params
15880484f94SBryan Zhang * @param errp Pointer to error, which will be set in case of error
15980484f94SBryan Zhang * @return 0 on success, -1 on error (and *errp will be set)
16080484f94SBryan Zhang */
qatzip_send_prepare(MultiFDSendParams * p,Error ** errp)16180484f94SBryan Zhang static int qatzip_send_prepare(MultiFDSendParams *p, Error **errp)
16280484f94SBryan Zhang {
163*561ce014SPeter Xu uint32_t page_size = multifd_ram_page_size();
164*561ce014SPeter Xu MultiFDPages_t *pages = &p->data->u.ram;
16580484f94SBryan Zhang QatzipData *q = p->compress_data;
16680484f94SBryan Zhang int ret;
16780484f94SBryan Zhang unsigned int in_len, out_len;
16880484f94SBryan Zhang
16980484f94SBryan Zhang if (!multifd_send_prepare_common(p)) {
17080484f94SBryan Zhang goto out;
17180484f94SBryan Zhang }
17280484f94SBryan Zhang
17380484f94SBryan Zhang /*
17480484f94SBryan Zhang * Unlike other multifd compression implementations, we use a non-streaming
17580484f94SBryan Zhang * API and place all the data into one buffer, rather than sending each
17680484f94SBryan Zhang * page to the compression API at a time. Based on initial benchmarks, the
17780484f94SBryan Zhang * non-streaming API outperforms the streaming API. Plus, the logic in QEMU
17880484f94SBryan Zhang * is friendly to using the non-streaming API anyway. If either of these
17980484f94SBryan Zhang * statements becomes no longer true, we can revisit adding a streaming
18080484f94SBryan Zhang * implementation.
18180484f94SBryan Zhang */
18280484f94SBryan Zhang for (int i = 0; i < pages->normal_num; i++) {
183*561ce014SPeter Xu memcpy(q->in_buf + (i * page_size),
18480484f94SBryan Zhang pages->block->host + pages->offset[i],
185*561ce014SPeter Xu page_size);
18680484f94SBryan Zhang }
18780484f94SBryan Zhang
188*561ce014SPeter Xu in_len = pages->normal_num * page_size;
18980484f94SBryan Zhang if (in_len > q->in_len) {
19080484f94SBryan Zhang error_setg(errp, "multifd %u: unexpectedly large input", p->id);
19180484f94SBryan Zhang return -1;
19280484f94SBryan Zhang }
19380484f94SBryan Zhang out_len = q->out_len;
19480484f94SBryan Zhang
19580484f94SBryan Zhang ret = qzCompress(&q->sess, q->in_buf, &in_len, q->out_buf, &out_len, 1);
19680484f94SBryan Zhang if (ret != QZ_OK) {
19780484f94SBryan Zhang error_setg(errp, "multifd %u: QATzip returned %d instead of QZ_OK",
19880484f94SBryan Zhang p->id, ret);
19980484f94SBryan Zhang return -1;
20080484f94SBryan Zhang }
201*561ce014SPeter Xu if (in_len != pages->normal_num * page_size) {
20280484f94SBryan Zhang error_setg(errp, "multifd %u: QATzip failed to compress all input",
20380484f94SBryan Zhang p->id);
20480484f94SBryan Zhang return -1;
20580484f94SBryan Zhang }
20680484f94SBryan Zhang
20780484f94SBryan Zhang p->iov[p->iovs_num].iov_base = q->out_buf;
20880484f94SBryan Zhang p->iov[p->iovs_num].iov_len = out_len;
20980484f94SBryan Zhang p->iovs_num++;
21080484f94SBryan Zhang p->next_packet_size = out_len;
21180484f94SBryan Zhang
21280484f94SBryan Zhang out:
21380484f94SBryan Zhang p->flags |= MULTIFD_FLAG_QATZIP;
21480484f94SBryan Zhang multifd_send_fill_packet(p);
21580484f94SBryan Zhang return 0;
21680484f94SBryan Zhang }
21780484f94SBryan Zhang
21880484f94SBryan Zhang /**
21980484f94SBryan Zhang * qatzip_recv_setup: Set up QATzip session and allocate private buffers.
22080484f94SBryan Zhang *
22180484f94SBryan Zhang * @param p Multifd channel params
22280484f94SBryan Zhang * @param errp Pointer to error, which will be set in case of error
22380484f94SBryan Zhang * @return 0 on success, -1 on error (and *errp will be set)
22480484f94SBryan Zhang */
qatzip_recv_setup(MultiFDRecvParams * p,Error ** errp)22580484f94SBryan Zhang static int qatzip_recv_setup(MultiFDRecvParams *p, Error **errp)
22680484f94SBryan Zhang {
22780484f94SBryan Zhang QatzipData *q;
22880484f94SBryan Zhang QzSessionParamsDeflate_T params;
22980484f94SBryan Zhang const char *err_msg;
23080484f94SBryan Zhang int ret;
23180484f94SBryan Zhang
23280484f94SBryan Zhang q = g_new0(QatzipData, 1);
23380484f94SBryan Zhang p->compress_data = q;
23480484f94SBryan Zhang
23580484f94SBryan Zhang /*
23680484f94SBryan Zhang * Initialize QAT device with software fallback by default. This allows
23780484f94SBryan Zhang * QATzip to use CPU path when QAT hardware reaches maximum throughput.
23880484f94SBryan Zhang */
23980484f94SBryan Zhang ret = qzInit(&q->sess, true);
24080484f94SBryan Zhang if (ret != QZ_OK && ret != QZ_DUPLICATE) {
24180484f94SBryan Zhang err_msg = "qzInit failed";
24280484f94SBryan Zhang goto err;
24380484f94SBryan Zhang }
24480484f94SBryan Zhang
24580484f94SBryan Zhang ret = qzGetDefaultsDeflate(¶ms);
24680484f94SBryan Zhang if (ret != QZ_OK) {
24780484f94SBryan Zhang err_msg = "qzGetDefaultsDeflate failed";
24880484f94SBryan Zhang goto err;
24980484f94SBryan Zhang }
25080484f94SBryan Zhang
25180484f94SBryan Zhang ret = qzSetupSessionDeflate(&q->sess, ¶ms);
25280484f94SBryan Zhang if (ret != QZ_OK && ret != QZ_DUPLICATE) {
25380484f94SBryan Zhang err_msg = "qzSetupSessionDeflate failed";
25480484f94SBryan Zhang goto err;
25580484f94SBryan Zhang }
25680484f94SBryan Zhang
25780484f94SBryan Zhang /*
25880484f94SBryan Zhang * Reserve extra spaces for the incoming packets. Current implementation
25980484f94SBryan Zhang * doesn't send uncompressed pages in case the compression gets too big.
26080484f94SBryan Zhang */
26180484f94SBryan Zhang q->in_len = MULTIFD_PACKET_SIZE * 2;
26280484f94SBryan Zhang /*
26380484f94SBryan Zhang * PINNED_MEM is an enum from qatzip headers, which means to use
26480484f94SBryan Zhang * kzalloc_node() to allocate memory for QAT DMA purposes. When QAT device
26580484f94SBryan Zhang * is not available or software fallback is used, the malloc flag needs to
26680484f94SBryan Zhang * be set as COMMON_MEM.
26780484f94SBryan Zhang */
26880484f94SBryan Zhang q->in_buf = qzMalloc(q->in_len, 0, PINNED_MEM);
26980484f94SBryan Zhang if (!q->in_buf) {
27080484f94SBryan Zhang q->in_buf = qzMalloc(q->in_len, 0, COMMON_MEM);
27180484f94SBryan Zhang if (!q->in_buf) {
27280484f94SBryan Zhang err_msg = "qzMalloc failed";
27380484f94SBryan Zhang goto err;
27480484f94SBryan Zhang }
27580484f94SBryan Zhang }
27680484f94SBryan Zhang
27780484f94SBryan Zhang q->out_len = MULTIFD_PACKET_SIZE;
27880484f94SBryan Zhang q->out_buf = qzMalloc(q->out_len, 0, PINNED_MEM);
27980484f94SBryan Zhang if (!q->out_buf) {
28080484f94SBryan Zhang q->out_buf = qzMalloc(q->out_len, 0, COMMON_MEM);
28180484f94SBryan Zhang if (!q->out_buf) {
28280484f94SBryan Zhang err_msg = "qzMalloc failed";
28380484f94SBryan Zhang goto err;
28480484f94SBryan Zhang }
28580484f94SBryan Zhang }
28680484f94SBryan Zhang
28780484f94SBryan Zhang return 0;
28880484f94SBryan Zhang
28980484f94SBryan Zhang err:
29080484f94SBryan Zhang error_setg(errp, "multifd %u: [receiver] %s", p->id, err_msg);
29180484f94SBryan Zhang return -1;
29280484f94SBryan Zhang }
29380484f94SBryan Zhang
29480484f94SBryan Zhang /**
29580484f94SBryan Zhang * qatzip_recv_cleanup: Tear down QATzip session and release private buffers.
29680484f94SBryan Zhang *
29780484f94SBryan Zhang * @param p Multifd channel params
29880484f94SBryan Zhang * @return None
29980484f94SBryan Zhang */
qatzip_recv_cleanup(MultiFDRecvParams * p)30080484f94SBryan Zhang static void qatzip_recv_cleanup(MultiFDRecvParams *p)
30180484f94SBryan Zhang {
30280484f94SBryan Zhang QatzipData *q = p->compress_data;
30380484f94SBryan Zhang
30480484f94SBryan Zhang if (q) {
30580484f94SBryan Zhang if (q->in_buf) {
30680484f94SBryan Zhang qzFree(q->in_buf);
30780484f94SBryan Zhang }
30880484f94SBryan Zhang if (q->out_buf) {
30980484f94SBryan Zhang qzFree(q->out_buf);
31080484f94SBryan Zhang }
31180484f94SBryan Zhang (void)qzTeardownSession(&q->sess);
31280484f94SBryan Zhang (void)qzClose(&q->sess);
31380484f94SBryan Zhang g_free(q);
31480484f94SBryan Zhang }
31580484f94SBryan Zhang p->compress_data = NULL;
31680484f94SBryan Zhang }
31780484f94SBryan Zhang
31880484f94SBryan Zhang
31980484f94SBryan Zhang /**
32080484f94SBryan Zhang * qatzip_recv: Decompress pages and copy them to the appropriate
32180484f94SBryan Zhang * locations.
32280484f94SBryan Zhang *
32380484f94SBryan Zhang * @param p Multifd channel params
32480484f94SBryan Zhang * @param errp Pointer to error, which will be set in case of error
32580484f94SBryan Zhang * @return 0 on success, -1 on error (and *errp will be set)
32680484f94SBryan Zhang */
qatzip_recv(MultiFDRecvParams * p,Error ** errp)32780484f94SBryan Zhang static int qatzip_recv(MultiFDRecvParams *p, Error **errp)
32880484f94SBryan Zhang {
32980484f94SBryan Zhang QatzipData *q = p->compress_data;
33080484f94SBryan Zhang int ret;
33180484f94SBryan Zhang unsigned int in_len, out_len;
33280484f94SBryan Zhang uint32_t in_size = p->next_packet_size;
333*561ce014SPeter Xu uint32_t page_size = multifd_ram_page_size();
334*561ce014SPeter Xu uint32_t expected_size = p->normal_num * page_size;
33580484f94SBryan Zhang uint32_t flags = p->flags & MULTIFD_FLAG_COMPRESSION_MASK;
33680484f94SBryan Zhang
33780484f94SBryan Zhang if (in_size > q->in_len) {
33880484f94SBryan Zhang error_setg(errp, "multifd %u: received unexpectedly large packet",
33980484f94SBryan Zhang p->id);
34080484f94SBryan Zhang return -1;
34180484f94SBryan Zhang }
34280484f94SBryan Zhang
34380484f94SBryan Zhang if (flags != MULTIFD_FLAG_QATZIP) {
34480484f94SBryan Zhang error_setg(errp, "multifd %u: flags received %x flags expected %x",
34580484f94SBryan Zhang p->id, flags, MULTIFD_FLAG_QATZIP);
34680484f94SBryan Zhang return -1;
34780484f94SBryan Zhang }
34880484f94SBryan Zhang
34980484f94SBryan Zhang multifd_recv_zero_page_process(p);
35080484f94SBryan Zhang if (!p->normal_num) {
35180484f94SBryan Zhang assert(in_size == 0);
35280484f94SBryan Zhang return 0;
35380484f94SBryan Zhang }
35480484f94SBryan Zhang
35580484f94SBryan Zhang ret = qio_channel_read_all(p->c, (void *)q->in_buf, in_size, errp);
35680484f94SBryan Zhang if (ret != 0) {
35780484f94SBryan Zhang return ret;
35880484f94SBryan Zhang }
35980484f94SBryan Zhang
36080484f94SBryan Zhang in_len = in_size;
36180484f94SBryan Zhang out_len = q->out_len;
36280484f94SBryan Zhang ret = qzDecompress(&q->sess, q->in_buf, &in_len, q->out_buf, &out_len);
36380484f94SBryan Zhang if (ret != QZ_OK) {
36480484f94SBryan Zhang error_setg(errp, "multifd %u: qzDecompress failed", p->id);
36580484f94SBryan Zhang return -1;
36680484f94SBryan Zhang }
36780484f94SBryan Zhang if (out_len != expected_size) {
36880484f94SBryan Zhang error_setg(errp, "multifd %u: packet size received %u size expected %u",
36980484f94SBryan Zhang p->id, out_len, expected_size);
37080484f94SBryan Zhang return -1;
37180484f94SBryan Zhang }
37280484f94SBryan Zhang
37380484f94SBryan Zhang /* Copy each page to its appropriate location. */
37480484f94SBryan Zhang for (int i = 0; i < p->normal_num; i++) {
375*561ce014SPeter Xu memcpy(p->host + p->normal[i], q->out_buf + page_size * i, page_size);
37680484f94SBryan Zhang }
37780484f94SBryan Zhang return 0;
37880484f94SBryan Zhang }
37980484f94SBryan Zhang
38080484f94SBryan Zhang static MultiFDMethods multifd_qatzip_ops = {
38180484f94SBryan Zhang .send_setup = qatzip_send_setup,
38280484f94SBryan Zhang .send_cleanup = qatzip_send_cleanup,
38380484f94SBryan Zhang .send_prepare = qatzip_send_prepare,
38480484f94SBryan Zhang .recv_setup = qatzip_recv_setup,
38580484f94SBryan Zhang .recv_cleanup = qatzip_recv_cleanup,
38680484f94SBryan Zhang .recv = qatzip_recv
38780484f94SBryan Zhang };
38880484f94SBryan Zhang
multifd_qatzip_register(void)38980484f94SBryan Zhang static void multifd_qatzip_register(void)
39080484f94SBryan Zhang {
39180484f94SBryan Zhang multifd_register_ops(MULTIFD_COMPRESSION_QATZIP, &multifd_qatzip_ops);
39280484f94SBryan Zhang }
39380484f94SBryan Zhang
39480484f94SBryan Zhang migration_init(multifd_qatzip_register);
395