Skip to content

Commit

Permalink
[compress] add support for libzstd
Browse files Browse the repository at this point in the history
Signed-off-by: Fabio M. Di Nitto <fdinitto@redhat.com>
  • Loading branch information
fabbione committed Apr 10, 2019
1 parent 03f0c3b commit 2754268
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Makefile.am
Expand Up @@ -138,6 +138,11 @@ if BUILD_COMPRESS_BZIP2
else
sed -i -e "s#@bzip2@#bcond_with#g" $@-t
endif
if BUILD_COMPRESS_ZSTD
sed -i -e "s#@zstd@#bcond_without#g" $@-t
else
sed -i -e "s#@zstd@#bcond_with#g" $@-t
endif
if BUILD_KRONOSNETD
sed -i -e "s#@kronosnetd@#bcond_without#g" $@-t
else
Expand Down
2 changes: 2 additions & 0 deletions configure.ac
Expand Up @@ -124,6 +124,8 @@ AC_ARG_ENABLE([compress-all],
[AS_HELP_STRING([--disable-compress-all],[disable libknet all compress modules support])],,
[ enable_compress_all="yes" ])

KNET_OPTION_DEFINES([zstd],[compress],[PKG_CHECK_MODULES([libzstd], [libzstd])])

KNET_OPTION_DEFINES([zlib],[compress],[PKG_CHECK_MODULES([zlib], [zlib])])
KNET_OPTION_DEFINES([lz4],[compress],[PKG_CHECK_MODULES([liblz4], [liblz4])])
KNET_OPTION_DEFINES([lzo2],[compress],[
Expand Down
29 changes: 29 additions & 0 deletions kronosnet.spec.in
Expand Up @@ -24,6 +24,7 @@
%@lzo2@ lzo2
%@lzma@ lzma
%@bzip2@ bzip2
%@zstd@ zstd
%@kronosnetd@ kronosnetd
%@libnozzle@ libnozzle
%@runautogen@ runautogen
Expand Down Expand Up @@ -60,6 +61,9 @@
%if %{with bzip2}
%global buildcompressbzip2 1
%endif
%if %{with zstd}
%global buildcompresszstd 1
%endif
%if %{with libnozzle}
%global buildlibnozzle 1
%endif
Expand Down Expand Up @@ -123,6 +127,9 @@ BuildRequires: xz-devel
%if %{defined buildcompressbzip2}
BuildRequires: /usr/include/bzlib.h
%endif
%if %{defined buildcompresszstd}
BuildRequires: libzstd-devel
%endif
%if %{defined buildkronosnetd}
BuildRequires: pam-devel
%endif
Expand Down Expand Up @@ -194,6 +201,11 @@ BuildRequires: libtool
%else
--disable-compress-bzip2 \
%endif
%if %{defined buildcompresszstd}
--enable-compress-zstd \
%else
--disable-compress-zstd \
%endif
%if %{defined buildkronosnetd}
--enable-kronosnetd \
%endif
Expand Down Expand Up @@ -490,6 +502,20 @@ Requires: libknet1 = %{version}-%{release}
%{_libdir}/kronosnet/compress_bzip2.so
%endif

%if %{defined buildcompresszstd}
%package -n libknet1-compress-zstd-plugin
Group: System Environment/Libraries
Summary: libknet1 zstd support
Requires: libknet1 = %{version}-%{release}

%description -n libknet1-compress-zstd-plugin
zstd compression support for libknet1.

%files -n libknet1-compress-zstd-plugin
%defattr(-,root,root,-)
%{_libdir}/kronosnet/compress_zstd.so
%endif

%package -n libknet1-crypto-plugins-all
Group: System Environment/Libraries
Summary: libknet1 crypto plugins meta package
Expand Down Expand Up @@ -523,6 +549,9 @@ Requires: libknet1-compress-lzma-plugin
%if %{defined buildcompressbzip2}
Requires: libknet1-compress-bzip2-plugin
%endif
%if %{defined buildcompresszstd}
Requires: libknet1-compress-zstd-plugin
%endif

%description -n libknet1-compress-plugins-all
meta package to install all of libknet1 compress plugins
Expand Down
7 changes: 7 additions & 0 deletions libknet/Makefile.am
Expand Up @@ -103,6 +103,13 @@ pkglib_LTLIBRARIES =
# MODULE_LDFLAGS would mean a target-specific variable for Automake
MODULELDFLAGS = $(AM_LDFLAGS) -module -avoid-version -export-dynamic

if BUILD_COMPRESS_ZSTD
pkglib_LTLIBRARIES += compress_zstd.la
compress_zstd_la_LDFLAGS = $(MODULELDFLAGS)
compress_zstd_la_CFLAGS = $(AM_CFLAGS) $(libzstd_CFLAGS)
compress_zstd_la_LIBADD = $(libzstd_LIBS)
endif

if BUILD_COMPRESS_ZLIB
pkglib_LTLIBRARIES += compress_zlib.la
compress_zlib_la_LDFLAGS = $(MODULELDFLAGS)
Expand Down
1 change: 1 addition & 0 deletions libknet/compress.c
Expand Up @@ -40,6 +40,7 @@ static compress_model_t compress_modules_cmds[] = {
{ "lzo2" , 4, WITH_COMPRESS_LZO2 , 0, NULL },
{ "lzma" , 5, WITH_COMPRESS_LZMA , 0, NULL },
{ "bzip2", 6, WITH_COMPRESS_BZIP2, 0, NULL },
{ "zstd" , 7, WITH_COMPRESS_ZSTD, 0, NULL },
{ NULL, 255, 0, 0, NULL }
};

Expand Down
160 changes: 160 additions & 0 deletions libknet/compress_zstd.c
@@ -0,0 +1,160 @@
/*
* Copyright (C) 2017-2019 Red Hat, Inc. All rights reserved.
*
* Author: Fabio M. Di Nitto <fabbione@kronosnet.org>
*
* This software licensed under GPL-2.0+, LGPL-2.0+
*/
#define KNET_MODULE

#include "config.h"

#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <zstd.h>

#include "logging.h"
#include "compress_model.h"

struct zstd_ctx {
ZSTD_CCtx* cctx;
ZSTD_DCtx* dctx;
};

static int zstd_is_init(
knet_handle_t knet_h,
int method_idx)
{
if (knet_h->compress_int_data[method_idx]) {
return 1;
}
return 0;
}

static void zstd_fini(
knet_handle_t knet_h,
int method_idx)
{
struct zstd_ctx *zstd_ctx = knet_h->compress_int_data[knet_h->compress_model];

if (zstd_ctx) {
if (zstd_ctx->cctx) {
ZSTD_freeCCtx(zstd_ctx->cctx);
}
if (zstd_ctx->dctx) {
ZSTD_freeDCtx(zstd_ctx->dctx);
}
free(knet_h->compress_int_data[method_idx]);
knet_h->compress_int_data[method_idx] = NULL;
}
return;
}

static int zstd_init(
knet_handle_t knet_h,
int method_idx)
{
struct zstd_ctx *zstd_ctx;
int err = 0;

if (!knet_h->compress_int_data[method_idx]) {
zstd_ctx = malloc(sizeof(struct zstd_ctx));
if (!zstd_ctx) {
errno = ENOMEM;
return -1;
}
memset(zstd_ctx, 0, sizeof(struct zstd_ctx));

zstd_ctx->cctx = ZSTD_createCCtx();
if (!zstd_ctx->cctx) {
log_err(knet_h, KNET_SUB_ZSTDCOMP, "Unable to create compression context");
err = -1;
goto out_err;
}

zstd_ctx->dctx = ZSTD_createDCtx();
if (!zstd_ctx->dctx) {
log_err(knet_h, KNET_SUB_ZSTDCOMP, "Unable to create decompression context");
err = -1;
goto out_err;
}

knet_h->compress_int_data[method_idx] = zstd_ctx;
}

out_err:
if (err) {
zstd_fini(knet_h, method_idx);
}
return err;
}

static int zstd_compress(
knet_handle_t knet_h,
const unsigned char *buf_in,
const ssize_t buf_in_len,
unsigned char *buf_out,
ssize_t *buf_out_len)
{
struct zstd_ctx *zstd_ctx = knet_h->compress_int_data[knet_h->compress_model];
size_t compress_size;

compress_size = ZSTD_compressCCtx(zstd_ctx->cctx,
buf_out, *buf_out_len,
buf_in, buf_in_len,
knet_h->compress_level);

if (ZSTD_isError(compress_size)) {
log_err(knet_h, KNET_SUB_ZSTDCOMP, "error compressing packet: %s", ZSTD_getErrorName(compress_size));
/*
* ZSTD has lots of internal errors that are not easy to map
* to standard errnos. Use a generic one for now
*/
errno = EINVAL;
return -1;
}

*buf_out_len = compress_size;

return 0;
}

static int zstd_decompress(
knet_handle_t knet_h,
const unsigned char *buf_in,
const ssize_t buf_in_len,
unsigned char *buf_out,
ssize_t *buf_out_len)
{
struct zstd_ctx *zstd_ctx = knet_h->compress_int_data[knet_h->compress_model];
size_t decompress_size;

decompress_size = ZSTD_decompressDCtx(zstd_ctx->dctx,
buf_out, *buf_out_len,
buf_in, buf_in_len);

if (ZSTD_isError(decompress_size)) {
log_err(knet_h, KNET_SUB_ZSTDCOMP, "error decompressing packet: %s", ZSTD_getErrorName(decompress_size));
/*
* ZSTD has lots of internal errors that are not easy to map
* to standard errnos. Use a generic one for now
*/
errno = EINVAL;
return -1;
}

*buf_out_len = decompress_size;

return 0;
}

compress_ops_t compress_model = {
KNET_COMPRESS_MODEL_ABI,
zstd_is_init,
zstd_init,
zstd_fini,
NULL,
zstd_compress,
zstd_decompress
};
1 change: 1 addition & 0 deletions libknet/libknet.h
Expand Up @@ -2141,6 +2141,7 @@ int knet_link_enable_status_change_notify(knet_handle_t knet_h,
#define KNET_SUB_LZO2COMP 73 /* compress_lzo.c */
#define KNET_SUB_LZMACOMP 74 /* compress_lzma.c */
#define KNET_SUB_BZIP2COMP 75 /* compress_bzip2.c */
#define KNET_SUB_ZSTDCOMP 76 /* compress_zstd.c */

#define KNET_SUB_UNKNOWN UINT8_MAX - 1
#define KNET_MAX_SUBSYSTEMS UINT8_MAX
Expand Down
1 change: 1 addition & 0 deletions libknet/logging.c
Expand Up @@ -47,6 +47,7 @@ static struct pretty_names subsystem_names[] =
{ "lzo2comp", KNET_SUB_LZO2COMP },
{ "lzmacomp", KNET_SUB_LZMACOMP },
{ "bzip2comp", KNET_SUB_BZIP2COMP },
{ "zstdcomp", KNET_SUB_ZSTDCOMP },
{ "unknown", KNET_SUB_UNKNOWN } /* unknown MUST always be last in this array */
};

Expand Down

0 comments on commit 2754268

Please sign in to comment.