2019-12-13 15:47:14 +03:00
|
|
|
/*
|
|
|
|
* Multifd zlib compression implementation
|
|
|
|
*
|
|
|
|
* Copyright (c) 2020 Red Hat Inc
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Juan Quintela <quintela@redhat.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
|
|
* See the COPYING file in the top-level directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu/osdep.h"
|
|
|
|
#include <zstd.h>
|
|
|
|
#include "qemu/rcu.h"
|
2021-11-19 15:16:25 +03:00
|
|
|
#include "exec/ramblock.h"
|
2019-12-13 15:47:14 +03:00
|
|
|
#include "exec/target_page.h"
|
|
|
|
#include "qapi/error.h"
|
|
|
|
#include "migration.h"
|
|
|
|
#include "trace.h"
|
2023-03-02 02:39:03 +03:00
|
|
|
#include "options.h"
|
2019-12-13 15:47:14 +03:00
|
|
|
#include "multifd.h"
|
|
|
|
|
|
|
|
struct zstd_data {
|
|
|
|
/* stream for compression */
|
|
|
|
ZSTD_CStream *zcs;
|
|
|
|
/* stream for decompression */
|
|
|
|
ZSTD_DStream *zds;
|
|
|
|
/* buffers */
|
|
|
|
ZSTD_inBuffer in;
|
|
|
|
ZSTD_outBuffer out;
|
|
|
|
/* compressed buffer */
|
|
|
|
uint8_t *zbuff;
|
|
|
|
/* size of compressed buffer */
|
|
|
|
uint32_t zbuff_len;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Multifd zstd compression */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* zstd_send_setup: setup send side
|
|
|
|
*
|
|
|
|
* Setup each channel with zstd compression.
|
|
|
|
*
|
|
|
|
* Returns 0 for success or -1 for error
|
|
|
|
*
|
|
|
|
* @p: Params for the channel that we are using
|
|
|
|
* @errp: pointer to an error
|
|
|
|
*/
|
|
|
|
static int zstd_send_setup(MultiFDSendParams *p, Error **errp)
|
|
|
|
{
|
|
|
|
struct zstd_data *z = g_new0(struct zstd_data, 1);
|
|
|
|
int res;
|
|
|
|
|
2024-02-29 18:30:06 +03:00
|
|
|
p->compress_data = z;
|
2019-12-13 15:47:14 +03:00
|
|
|
z->zcs = ZSTD_createCStream();
|
|
|
|
if (!z->zcs) {
|
|
|
|
g_free(z);
|
2021-12-15 16:20:48 +03:00
|
|
|
error_setg(errp, "multifd %u: zstd createCStream failed", p->id);
|
2019-12-13 15:47:14 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = ZSTD_initCStream(z->zcs, migrate_multifd_zstd_level());
|
|
|
|
if (ZSTD_isError(res)) {
|
|
|
|
ZSTD_freeCStream(z->zcs);
|
|
|
|
g_free(z);
|
2021-12-15 16:20:48 +03:00
|
|
|
error_setg(errp, "multifd %u: initCStream failed with error %s",
|
2019-12-13 15:47:14 +03:00
|
|
|
p->id, ZSTD_getErrorName(res));
|
|
|
|
return -1;
|
|
|
|
}
|
2023-07-14 14:32:41 +03:00
|
|
|
/* This is the maximum size of the compressed buffer */
|
2021-11-26 12:30:32 +03:00
|
|
|
z->zbuff_len = ZSTD_compressBound(MULTIFD_PACKET_SIZE);
|
2019-12-13 15:47:14 +03:00
|
|
|
z->zbuff = g_try_malloc(z->zbuff_len);
|
|
|
|
if (!z->zbuff) {
|
|
|
|
ZSTD_freeCStream(z->zcs);
|
|
|
|
g_free(z);
|
2021-12-15 16:20:48 +03:00
|
|
|
error_setg(errp, "multifd %u: out of memory for zbuff", p->id);
|
2019-12-13 15:47:14 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* zstd_send_cleanup: cleanup send side
|
|
|
|
*
|
|
|
|
* Close the channel and return memory.
|
|
|
|
*
|
|
|
|
* @p: Params for the channel that we are using
|
2021-11-22 13:58:41 +03:00
|
|
|
* @errp: pointer to an error
|
2019-12-13 15:47:14 +03:00
|
|
|
*/
|
|
|
|
static void zstd_send_cleanup(MultiFDSendParams *p, Error **errp)
|
|
|
|
{
|
2024-02-29 18:30:06 +03:00
|
|
|
struct zstd_data *z = p->compress_data;
|
2019-12-13 15:47:14 +03:00
|
|
|
|
|
|
|
ZSTD_freeCStream(z->zcs);
|
|
|
|
z->zcs = NULL;
|
|
|
|
g_free(z->zbuff);
|
|
|
|
z->zbuff = NULL;
|
2024-02-29 18:30:06 +03:00
|
|
|
g_free(p->compress_data);
|
|
|
|
p->compress_data = NULL;
|
2019-12-13 15:47:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* zstd_send_prepare: prepare date to be able to send
|
|
|
|
*
|
|
|
|
* Create a compressed buffer with all the pages that we are going to
|
|
|
|
* send.
|
|
|
|
*
|
|
|
|
* Returns 0 for success or -1 for error
|
|
|
|
*
|
|
|
|
* @p: Params for the channel that we are using
|
2021-11-22 13:58:41 +03:00
|
|
|
* @errp: pointer to an error
|
2019-12-13 15:47:14 +03:00
|
|
|
*/
|
2021-11-22 14:08:08 +03:00
|
|
|
static int zstd_send_prepare(MultiFDSendParams *p, Error **errp)
|
2019-12-13 15:47:14 +03:00
|
|
|
{
|
2024-02-02 13:28:39 +03:00
|
|
|
MultiFDPages_t *pages = p->pages;
|
2024-02-29 18:30:06 +03:00
|
|
|
struct zstd_data *z = p->compress_data;
|
2019-12-13 15:47:14 +03:00
|
|
|
int ret;
|
|
|
|
uint32_t i;
|
|
|
|
|
2024-03-11 21:00:12 +03:00
|
|
|
if (!multifd_send_prepare_common(p)) {
|
|
|
|
goto out;
|
|
|
|
}
|
2024-02-02 13:28:47 +03:00
|
|
|
|
2019-12-13 15:47:14 +03:00
|
|
|
z->out.dst = z->zbuff;
|
|
|
|
z->out.size = z->zbuff_len;
|
|
|
|
z->out.pos = 0;
|
|
|
|
|
2024-03-11 21:00:12 +03:00
|
|
|
for (i = 0; i < pages->normal_num; i++) {
|
2019-12-13 15:47:14 +03:00
|
|
|
ZSTD_EndDirective flush = ZSTD_e_continue;
|
|
|
|
|
2024-03-11 21:00:12 +03:00
|
|
|
if (i == pages->normal_num - 1) {
|
2019-12-13 15:47:14 +03:00
|
|
|
flush = ZSTD_e_flush;
|
|
|
|
}
|
2024-02-02 13:28:39 +03:00
|
|
|
z->in.src = p->pages->block->host + pages->offset[i];
|
2022-05-02 17:45:35 +03:00
|
|
|
z->in.size = p->page_size;
|
2019-12-13 15:47:14 +03:00
|
|
|
z->in.pos = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Welcome to compressStream2 semantics
|
|
|
|
*
|
|
|
|
* We need to loop while:
|
|
|
|
* - return is > 0
|
|
|
|
* - there is input available
|
|
|
|
* - there is output space free
|
|
|
|
*/
|
|
|
|
do {
|
|
|
|
ret = ZSTD_compressStream2(z->zcs, &z->out, &z->in, flush);
|
|
|
|
} while (ret > 0 && (z->in.size - z->in.pos > 0)
|
|
|
|
&& (z->out.size - z->out.pos > 0));
|
|
|
|
if (ret > 0 && (z->in.size - z->in.pos > 0)) {
|
2021-12-15 16:20:48 +03:00
|
|
|
error_setg(errp, "multifd %u: compressStream buffer too small",
|
2019-12-13 15:47:14 +03:00
|
|
|
p->id);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (ZSTD_isError(ret)) {
|
2021-12-15 16:20:48 +03:00
|
|
|
error_setg(errp, "multifd %u: compressStream error %s",
|
2019-12-13 15:47:14 +03:00
|
|
|
p->id, ZSTD_getErrorName(ret));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2021-11-19 17:05:23 +03:00
|
|
|
p->iov[p->iovs_num].iov_base = z->zbuff;
|
|
|
|
p->iov[p->iovs_num].iov_len = z->out.pos;
|
|
|
|
p->iovs_num++;
|
2019-12-13 15:47:14 +03:00
|
|
|
p->next_packet_size = z->out.pos;
|
|
|
|
|
2024-03-11 21:00:12 +03:00
|
|
|
out:
|
|
|
|
p->flags |= MULTIFD_FLAG_ZSTD;
|
2024-02-02 13:28:47 +03:00
|
|
|
multifd_send_fill_packet(p);
|
2019-12-13 15:47:14 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* zstd_recv_setup: setup receive side
|
|
|
|
*
|
|
|
|
* Create the compressed channel and buffer.
|
|
|
|
*
|
|
|
|
* Returns 0 for success or -1 for error
|
|
|
|
*
|
|
|
|
* @p: Params for the channel that we are using
|
|
|
|
* @errp: pointer to an error
|
|
|
|
*/
|
|
|
|
static int zstd_recv_setup(MultiFDRecvParams *p, Error **errp)
|
|
|
|
{
|
|
|
|
struct zstd_data *z = g_new0(struct zstd_data, 1);
|
|
|
|
int ret;
|
|
|
|
|
2024-02-29 18:30:06 +03:00
|
|
|
p->compress_data = z;
|
2019-12-13 15:47:14 +03:00
|
|
|
z->zds = ZSTD_createDStream();
|
|
|
|
if (!z->zds) {
|
|
|
|
g_free(z);
|
2021-12-15 16:20:48 +03:00
|
|
|
error_setg(errp, "multifd %u: zstd createDStream failed", p->id);
|
2019-12-13 15:47:14 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = ZSTD_initDStream(z->zds);
|
|
|
|
if (ZSTD_isError(ret)) {
|
|
|
|
ZSTD_freeDStream(z->zds);
|
|
|
|
g_free(z);
|
2021-12-15 16:20:48 +03:00
|
|
|
error_setg(errp, "multifd %u: initDStream failed with error %s",
|
2019-12-13 15:47:14 +03:00
|
|
|
p->id, ZSTD_getErrorName(ret));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-11-22 22:18:28 +03:00
|
|
|
/* To be safe, we reserve twice the size of the packet */
|
|
|
|
z->zbuff_len = MULTIFD_PACKET_SIZE * 2;
|
2019-12-13 15:47:14 +03:00
|
|
|
z->zbuff = g_try_malloc(z->zbuff_len);
|
|
|
|
if (!z->zbuff) {
|
|
|
|
ZSTD_freeDStream(z->zds);
|
|
|
|
g_free(z);
|
2021-12-15 16:20:48 +03:00
|
|
|
error_setg(errp, "multifd %u: out of memory for zbuff", p->id);
|
2019-12-13 15:47:14 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* zstd_recv_cleanup: setup receive side
|
|
|
|
*
|
|
|
|
* For no compression this function does nothing.
|
|
|
|
*
|
|
|
|
* @p: Params for the channel that we are using
|
|
|
|
*/
|
|
|
|
static void zstd_recv_cleanup(MultiFDRecvParams *p)
|
|
|
|
{
|
2024-02-29 18:30:06 +03:00
|
|
|
struct zstd_data *z = p->compress_data;
|
2019-12-13 15:47:14 +03:00
|
|
|
|
|
|
|
ZSTD_freeDStream(z->zds);
|
|
|
|
z->zds = NULL;
|
|
|
|
g_free(z->zbuff);
|
|
|
|
z->zbuff = NULL;
|
2024-02-29 18:30:06 +03:00
|
|
|
g_free(p->compress_data);
|
|
|
|
p->compress_data = NULL;
|
2019-12-13 15:47:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-02-29 18:30:07 +03:00
|
|
|
* zstd_recv: read the data from the channel into actual pages
|
2019-12-13 15:47:14 +03:00
|
|
|
*
|
|
|
|
* Read the compressed buffer, and uncompress it into the actual
|
|
|
|
* pages.
|
|
|
|
*
|
|
|
|
* Returns 0 for success or -1 for error
|
|
|
|
*
|
|
|
|
* @p: Params for the channel that we are using
|
|
|
|
* @errp: pointer to an error
|
|
|
|
*/
|
2024-02-29 18:30:07 +03:00
|
|
|
static int zstd_recv(MultiFDRecvParams *p, Error **errp)
|
2019-12-13 15:47:14 +03:00
|
|
|
{
|
|
|
|
uint32_t in_size = p->next_packet_size;
|
|
|
|
uint32_t out_size = 0;
|
2022-05-02 17:45:35 +03:00
|
|
|
uint32_t expected_size = p->normal_num * p->page_size;
|
2019-12-13 15:47:14 +03:00
|
|
|
uint32_t flags = p->flags & MULTIFD_FLAG_COMPRESSION_MASK;
|
2024-02-29 18:30:06 +03:00
|
|
|
struct zstd_data *z = p->compress_data;
|
2019-12-13 15:47:14 +03:00
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (flags != MULTIFD_FLAG_ZSTD) {
|
2021-12-15 16:20:48 +03:00
|
|
|
error_setg(errp, "multifd %u: flags received %x flags expected %x",
|
2019-12-13 15:47:14 +03:00
|
|
|
p->id, flags, MULTIFD_FLAG_ZSTD);
|
|
|
|
return -1;
|
|
|
|
}
|
2024-03-11 21:00:12 +03:00
|
|
|
|
|
|
|
multifd_recv_zero_page_process(p);
|
|
|
|
|
|
|
|
if (!p->normal_num) {
|
|
|
|
assert(in_size == 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-12-13 15:47:14 +03:00
|
|
|
ret = qio_channel_read_all(p->c, (void *)z->zbuff, in_size, errp);
|
|
|
|
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
z->in.src = z->zbuff;
|
|
|
|
z->in.size = in_size;
|
|
|
|
z->in.pos = 0;
|
|
|
|
|
2021-11-22 15:41:06 +03:00
|
|
|
for (i = 0; i < p->normal_num; i++) {
|
2021-11-22 16:10:57 +03:00
|
|
|
z->out.dst = p->host + p->normal[i];
|
2022-05-02 17:45:35 +03:00
|
|
|
z->out.size = p->page_size;
|
2019-12-13 15:47:14 +03:00
|
|
|
z->out.pos = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Welcome to decompressStream semantics
|
|
|
|
*
|
|
|
|
* We need to loop while:
|
|
|
|
* - return is > 0
|
|
|
|
* - there is input available
|
|
|
|
* - we haven't put out a full page
|
|
|
|
*/
|
|
|
|
do {
|
|
|
|
ret = ZSTD_decompressStream(z->zds, &z->out, &z->in);
|
|
|
|
} while (ret > 0 && (z->in.size - z->in.pos > 0)
|
2022-05-02 17:45:35 +03:00
|
|
|
&& (z->out.pos < p->page_size));
|
|
|
|
if (ret > 0 && (z->out.pos < p->page_size)) {
|
2021-12-15 16:20:48 +03:00
|
|
|
error_setg(errp, "multifd %u: decompressStream buffer too small",
|
2019-12-13 15:47:14 +03:00
|
|
|
p->id);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (ZSTD_isError(ret)) {
|
2021-12-15 16:20:48 +03:00
|
|
|
error_setg(errp, "multifd %u: decompressStream returned %s",
|
2019-12-13 15:47:14 +03:00
|
|
|
p->id, ZSTD_getErrorName(ret));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
out_size += z->out.pos;
|
|
|
|
}
|
|
|
|
if (out_size != expected_size) {
|
2021-12-15 16:20:48 +03:00
|
|
|
error_setg(errp, "multifd %u: packet size received %u size expected %u",
|
2019-12-13 15:47:14 +03:00
|
|
|
p->id, out_size, expected_size);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static MultiFDMethods multifd_zstd_ops = {
|
|
|
|
.send_setup = zstd_send_setup,
|
|
|
|
.send_cleanup = zstd_send_cleanup,
|
|
|
|
.send_prepare = zstd_send_prepare,
|
|
|
|
.recv_setup = zstd_recv_setup,
|
|
|
|
.recv_cleanup = zstd_recv_cleanup,
|
2024-02-29 18:30:07 +03:00
|
|
|
.recv = zstd_recv
|
2019-12-13 15:47:14 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
static void multifd_zstd_register(void)
|
|
|
|
{
|
|
|
|
multifd_register_ops(MULTIFD_COMPRESSION_ZSTD, &multifd_zstd_ops);
|
|
|
|
}
|
|
|
|
|
|
|
|
migration_init(multifd_zstd_register);
|