Skip to content

Commit

Permalink
Switch over all plugins to the module system
Browse files Browse the repository at this point in the history
  • Loading branch information
wferi authored and fabbione committed Nov 29, 2017
1 parent e96a687 commit 11ce1a1
Show file tree
Hide file tree
Showing 34 changed files with 523 additions and 1,263 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Expand Up @@ -46,8 +46,6 @@ kronosnetd/kronosnetd
kronosnetd/knet-keygen
kronosnetd/kronosnetd.logrotate
kronosnet.spec
libknet/compress_canary
libknet/crypto_canary
*.swp
*_test
*_bench
Expand Down
18 changes: 0 additions & 18 deletions configure.ac
Expand Up @@ -201,23 +201,10 @@ case "$host_os" in
;;
esac

AC_DEFUN([KNET_PKG_SONAME],[
AC_MSG_CHECKING([for soname of $1])
$CC -shared -nostdlib -o dummy.so -Wl,--no-as-needed $$1_LIBS
# Choosing the first SONAME is a best guess here:
knet_pkg_soname=`$OBJDUMP -p dummy.so | $SED -n '/^Dynamic Section:$/,/^[^ ]/{s/ *NEEDED *//p;}' | head -n1`
rm dummy.so
AC_DEFINE_UNQUOTED([$2],["$knet_pkg_soname"],[SONAME of the $1 library])
AS_IF([test "x$knet_pkg_soname" = x],
[AC_MSG_FAILURE([failed to extract SONAME])],
[AC_MSG_RESULT([$knet_pkg_soname])])
])

# crypto libraries checks
if test "x$enable_crypto_nss" = xyes; then
PKG_CHECK_MODULES([nss],[nss])
AC_DEFINE_UNQUOTED([BUILDCRYPTONSS], [1], [Enable nss crypto])
KNET_PKG_SONAME([nss],[LIBNSS3])
fi

if test "x$enable_crypto_openssl" = xyes; then
Expand All @@ -226,19 +213,16 @@ if test "x$enable_crypto_openssl" = xyes; then
[PKG_CHECK_MODULES([openssl],[libcrypto >= 1.1],
[AC_DEFINE_UNQUOTED([BUILDCRYPTOOPENSSL11], [1], [openssl 1.1 crypto])])])
AC_DEFINE_UNQUOTED([BUILDCRYPTOOPENSSL], [1], [Enable openssl crypto])
KNET_PKG_SONAME([openssl],[LIBOPENSSL])
fi

# compress libraries checks
if test "x$enable_compress_zlib" = xyes; then
PKG_CHECK_MODULES([zlib], [zlib])
AC_DEFINE_UNQUOTED([BUILDCOMPZLIB], [1], [Enable zlib compression])
KNET_PKG_SONAME([zlib],[LIBZ_1])
fi
if test "x$enable_compress_lz4" = xyes; then
PKG_CHECK_MODULES([liblz4], [liblz4])
AC_DEFINE_UNQUOTED([BUILDCOMPLZ4], [1], [Enable lz4 compress])
KNET_PKG_SONAME([liblz4],[LIBLZ4_1])
fi
if test "x$enable_compress_lzo2" = xyes; then
PKG_CHECK_MODULES([lzo2], [lzo2],,
Expand All @@ -247,12 +231,10 @@ if test "x$enable_compress_lzo2" = xyes; then
[AC_SUBST([lzo2_LIBS], [-llzo2])])],
[AC_MSG_ERROR(["missing required lzo/lzo1x.h header"])])])
AC_DEFINE_UNQUOTED([BUILDCOMPLZO2], [1], [Enable lzo2 compress])
KNET_PKG_SONAME([lzo2],[LIBLZO2_2])
fi
if test "x$enable_compress_lzma" = xyes; then
PKG_CHECK_MODULES([liblzma], [liblzma])
AC_DEFINE_UNQUOTED([BUILDCOMPLZMA], [1], [Enable lzma compress])
KNET_PKG_SONAME([liblzma],[LIBLZMA_5])
fi
if test "x$enable_compress_bzip2" = xyes; then
PKG_CHECK_MODULES([bzip2], [bzip2],,
Expand Down
75 changes: 46 additions & 29 deletions libknet/Makefile.am
Expand Up @@ -26,13 +26,7 @@ sources = \
common.c \
compat.c \
compress.c \
compress_zlib.c \
compress_lz4.c \
compress_lzo2.c \
compress_lzma.c \
crypto.c \
crypto_nss.c \
crypto_openssl.c \
handle.c \
host.c \
links.c \
Expand Down Expand Up @@ -61,26 +55,14 @@ noinst_HEADERS = \
compat.h \
compress.h \
compress_model.h \
compress_lz4.h \
compress_lz4_remap.h \
compress_lzma.h \
compress_lzma_remap.h \
compress_lzo2.h \
compress_lzo2_remap.h \
compress_zlib.h \
compress_zlib_remap.h \
crypto.h \
crypto_nss.h \
crypto_nss_remap.h \
crypto_openssl.h \
crypto_openssl_remap.h \
crypto_model.h \
host.h \
internals.h \
links.h \
logging.h \
netutils.h \
onwire.h \
remap.h \
threads_common.h \
threads_dsthandler.h \
threads_heartbeat.h \
Expand All @@ -97,9 +79,7 @@ lib_LTLIBRARIES = libknet.la

libknet_la_SOURCES = $(sources)

libknet_la_CFLAGS = $(PTHREAD_CFLAGS) \
$(nss_CFLAGS) $(openssl_CFLAGS) \
$(zlib_CFLAGS) $(liblz4_CFLAGS) $(lzo2_CFLAGS) $(liblzma_CFLAGS)
libknet_la_CFLAGS = $(PTHREAD_CFLAGS)

EXTRA_libknet_la_DEPENDENCIES = $(SYMFILE)

Expand All @@ -113,20 +93,57 @@ libknet_la_LIBADD = $(PTHREAD_LIBS) $(dl_LIBS) $(rt_LIBS) $(m_LIBS)
# Prepare empty value for appending
pkglib_LTLIBRARIES =

if BUILDCOMPZLIB
pkglib_LTLIBRARIES += compress_zlib.la
compress_zlib_la_LDFLAGS = -module -avoid-version
compress_zlib_la_CFLAGS = $(zlib_CFLAGS)
compress_zlib_la_LIBADD = $(zlib_LIBS)
endif

if BUILDCOMPLZ4
pkglib_LTLIBRARIES += compress_lz4.la compress_lz4hc.la
compress_lz4_la_LDFLAGS = -module -avoid-version
compress_lz4_la_CFLAGS = $(liblz4_CFLAGS)
compress_lz4_la_LIBADD = $(liblz4_LIBS)
compress_lz4hc_la_LDFLAGS = -module -avoid-version
compress_lz4hc_la_CFLAGS = $(liblz4_CFLAGS)
compress_lz4hc_la_LIBADD = $(liblz4_LIBS)
endif

if BUILDCOMPLZO2
pkglib_LTLIBRARIES += compress_lzo2.la
compress_lzo2_la_LDFLAGS = -module -avoid-version
compress_lzo2_la_CFLAGS = $(lzo2_CFLAGS)
compress_lzo2_la_LIBADD = $(lzo2_LIBS)
endif

if BUILDCOMPLZMA
pkglib_LTLIBRARIES += compress_lzma.la
compress_lzma_la_LDFLAGS = -module -avoid-version
compress_lzma_la_CFLAGS = $(liblzma_CFLAGS)
compress_lzma_la_LIBADD = $(liblzma_LIBS)
endif

if BUILDCOMPBZIP2
pkglib_LTLIBRARIES += compress_bzip2.la
compress_bzip2_la_LDFLAGS = -module -avoid-version
compress_bzip2_la_CFLAGS = $(bzip2_CFLAGS)
compress_bzip2_la_LIBADD = $(bzip2_LIBS)
endif

noinst_PROGRAMS = crypto_canary compress_canary
crypto_canary_SOURCES = crypto_canary.c
crypto_canary_CFLAGS = $(nss_CFLAGS) $(openssl_CFLAGS)
crypto_canary_LDADD = $(nss_LIBS) $(openssl_LIBS)
compress_canary_SOURCES = compress_canary.c
compress_canary_CFLAGS = $(zlib_CFLAGS) $(liblz4_CFLAGS) $(lzo2_CFLAGS) $(liblzma_CFLAGS)
compress_canary_LDADD = $(zlib_LIBS) $(liblz4_LIBS) $(lzo2_LIBS) $(liblzma_LIBS)
if BUILDCRYPTONSS
pkglib_LTLIBRARIES += crypto_nss.la
crypto_nss_la_LDFLAGS = -module -avoid-version
crypto_nss_la_CFLAGS = $(nss_CFLAGS)
crypto_nss_la_LIBADD = $(nss_LIBS)
endif

if BUILDCRYPTOOPENSSL
pkglib_LTLIBRARIES += crypto_openssl.la
crypto_openssl_la_LDFLAGS = -module -avoid-version
crypto_openssl_la_CFLAGS = $(openssl_CFLAGS)
crypto_openssl_la_LIBADD = $(openssl_LIBS)
endif

dist_man_MANS = man

Expand Down
44 changes: 34 additions & 10 deletions libknet/common.c
Expand Up @@ -119,16 +119,7 @@ void *open_lib(knet_handle_t knet_h, const char *libname, int extra_flags)
return ret;
}

void *remap_symbol(knet_handle_t knet_h, uint8_t subsystem,
void *lib_handle, const char *symbol_name)
{
void *symbol = dlsym (lib_handle, symbol_name);
if (!symbol) {
log_err (knet_h, subsystem, "unable to map %s: %s", symbol_name, dlerror ());
}
return symbol;
}

/* Separate these into compress.c and crypto.c or keep them together? */
int load_compress_lib(knet_handle_t knet_h, compress_model_t *model)
{
void *module;
Expand Down Expand Up @@ -159,3 +150,36 @@ int load_compress_lib(knet_handle_t knet_h, compress_model_t *model)
model->decompress = module_cmds->decompress;
return 0;
}

int load_crypto_lib(knet_handle_t knet_h, crypto_model_t *model)
{
void *module;
crypto_model_t *module_cmds;
char soname[MAXPATHLEN];
const char model_sym[] = "crypto_model";

if (model->loaded) {
return 0;
}
snprintf (soname, sizeof soname, "crypto_%s.so", model->model_name);
module = open_lib(knet_h, soname, 0);
if (!module) {
return -1;
}
module_cmds = dlsym (module, model_sym);
if (!module_cmds) {
log_err (knet_h, KNET_SUB_CRYPTO, "unable to map symbol %s in module %s: %s",
model_sym, soname, dlerror ());
errno = EINVAL;
return -1;
}
if (module_cmds->load_lib && (*module_cmds->load_lib)(knet_h)) {
return -1;
}
model->init = module_cmds->init;
model->fini = module_cmds->fini;
model->crypt = module_cmds->crypt;
model->cryptv = module_cmds->cryptv;
model->decrypt = module_cmds->decrypt;
return 0;
}
4 changes: 2 additions & 2 deletions libknet/common.h
Expand Up @@ -9,15 +9,15 @@

#include "internals.h"
#include "compress_model.h"
#include "crypto_model.h"

#ifndef __KNET_COMMON_H__
#define __KNET_COMMON_H__

int _fdset_cloexec(int fd);
int _fdset_nonblock(int fd);
void *open_lib(knet_handle_t knet_h, const char *libname, int extra_flags);
void *remap_symbol(knet_handle_t knet_h, uint8_t subsystem,
void *lib_handle, const char *symbol_name);
int load_compress_lib(knet_handle_t knet_h, compress_model_t *model);
int load_crypto_lib(knet_handle_t knet_h, crypto_model_t *model);

#endif
27 changes: 7 additions & 20 deletions libknet/compress.c
Expand Up @@ -21,19 +21,6 @@
#include "threads_common.h"
#include "common.h"

#ifdef BUILDCOMPZLIB
#include "compress_zlib.h"
#endif
#ifdef BUILDCOMPLZ4
#include "compress_lz4.h"
#endif
#ifdef BUILDCOMPLZO2
#include "compress_lzo2.h"
#endif
#ifdef BUILDCOMPLZMA
#include "compress_lzma.h"
#endif

/*
* internal module switch data
*/
Expand All @@ -51,37 +38,37 @@ compress_model_t compress_modules_cmds[] = {
{ "none", 0, empty_module
{ "zlib", 1,
#ifdef BUILDCOMPZLIB
1, zlib_load_lib, 0, NULL, NULL, NULL, zlib_val_level, zlib_compress, zlib_decompress },
1, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL },
#else
empty_module
#endif
{ "lz4", 2,
#ifdef BUILDCOMPLZ4
1, lz4_load_lib, 0, NULL, NULL, NULL, lz4_val_level, lz4_compress, lz4_decompress },
1, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL },
#else
empty_module
#endif
{ "lz4hc", 3,
#ifdef BUILDCOMPLZ4
1, lz4_load_lib, 0, NULL, NULL, NULL, lz4hc_val_level, lz4hc_compress, lz4_decompress },
1, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL },
#else
empty_module
#endif
{ "lzo2", 4,
#ifdef BUILDCOMPLZO2
1, lzo2_load_lib, 0, lzo2_is_init, lzo2_init, lzo2_fini, lzo2_val_level, lzo2_compress, lzo2_decompress },
1, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL },
#else
empty_module
#endif
{ "lzma", 5,
#ifdef BUILDCOMPLZMA
1, lzma_load_lib, 0, NULL, NULL, NULL, lzma_val_level, lzma_compress, lzma_decompress },
1, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL },
#else
empty_module
#endif
{ "bzip2", 6,
#ifdef BUILDCOMPBZIP2
1, load_compress_lib, 0, NULL, NULL, NULL, NULL, NULL, NULL },
1, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL },
#else
empty_module
#endif
Expand Down Expand Up @@ -202,7 +189,7 @@ static int compress_load_lib(knet_handle_t knet_h, int cmp_model, int rate_limit
}

if (compress_modules_cmds[cmp_model].loaded == 0) {
if (compress_modules_cmds[cmp_model].load_lib(knet_h, compress_modules_cmds+cmp_model) < 0) {
if (load_compress_lib(knet_h, compress_modules_cmds+cmp_model) < 0) {
clock_gettime(CLOCK_MONOTONIC, &last_load_failure);
return -1;
}
Expand Down
2 changes: 1 addition & 1 deletion libknet/compress_bzip2.c
Expand Up @@ -114,4 +114,4 @@ static int bzip2_decompress(
return err;
}

compress_model_t compress_model = { "bzip2", 0, 1, NULL, 1, NULL, NULL, NULL, bzip2_val_level, bzip2_compress, bzip2_decompress };
compress_model_t compress_model = { "", 0, 0, NULL, 0, NULL, NULL, NULL, bzip2_val_level, bzip2_compress, bzip2_decompress };
32 changes: 0 additions & 32 deletions libknet/compress_canary.c

This file was deleted.

0 comments on commit 11ce1a1

Please sign in to comment.