]> git.proxmox.com Git - mirror_kronosnet.git/commitdiff
[compress] delegate compress_level validation to compress libraries
authorFabio M. Di Nitto <fdinitto@redhat.com>
Sun, 25 Feb 2018 07:23:49 +0000 (08:23 +0100)
committerFabio M. Di Nitto <fdinitto@redhat.com>
Mon, 26 Feb 2018 11:27:23 +0000 (12:27 +0100)
- make val_level internal api call optional
- keep lzo2 val_level around due to the specific nature of compress_level values
- add internal compress_lib_test to do a round-robin (compress/decompress) check with provided values
- drop unnecessary val_level checks around
- update internal compress API include file
- adjust api_knet_handle_compress test to use a known bad value for zlib validation

Signed-off-by: Fabio M. Di Nitto <fdinitto@redhat.com>
libknet/compress.c
libknet/compress_bzip2.c
libknet/compress_lz4.c
libknet/compress_lz4hc.c
libknet/compress_lzma.c
libknet/compress_model.h
libknet/compress_zlib.c
libknet/tests/api_knet_handle_compress.c

index 60799c12261676bd7f4bfbe86e52896480eb89de..19b0d27a7d4cd382e7e3fab368e6ef754c8a4b29 100644 (file)
@@ -87,7 +87,10 @@ static int val_level(
        int compress_model,
        int compress_level)
 {
-       return compress_modules_cmds[compress_model].ops->val_level(knet_h, compress_level);
+       if (compress_modules_cmds[compress_model].ops->val_level != NULL) {
+               return compress_modules_cmds[compress_model].ops->val_level(knet_h, compress_level);
+       }
+       return 0;
 }
 
 /*
@@ -184,6 +187,38 @@ static int compress_load_lib(knet_handle_t knet_h, int cmp_model, int rate_limit
        return 0;
 }
 
+static int compress_lib_test(knet_handle_t knet_h)
+{
+       int savederrno = 0;
+       unsigned char src[KNET_DATABUFSIZE];
+       unsigned char dst[KNET_DATABUFSIZE_COMPRESS];
+       ssize_t dst_comp_len = KNET_DATABUFSIZE_COMPRESS, dst_decomp_len = KNET_DATABUFSIZE;
+
+       memset(src, 0, KNET_DATABUFSIZE);
+       memset(dst, 0, KNET_DATABUFSIZE_COMPRESS);
+
+       /*
+        * NOTE: we cannot use compress and decompress API calls due to locking
+        * so we need to call directly into the modules
+        */
+
+       if (compress_modules_cmds[knet_h->compress_model].ops->compress(knet_h, src, KNET_DATABUFSIZE, dst, &dst_comp_len) < 0) {
+               savederrno = errno;
+               log_err(knet_h, KNET_SUB_COMPRESS, "Unable to compress test buffer. Please check your compression settings: %s", strerror(savederrno));
+               errno = savederrno;
+               return -1;
+       }
+
+       if (compress_modules_cmds[knet_h->compress_model].ops->decompress(knet_h, dst, dst_comp_len, src, &dst_decomp_len) < 0) {
+               savederrno = errno;
+               log_err(knet_h, KNET_SUB_COMPRESS, "Unable to decompress test buffer. Please check your compression settings: %s", strerror(savederrno));
+               errno = savederrno;
+               return -1;
+       }
+
+       return 0;
+}
+
 int compress_init(
        knet_handle_t knet_h)
 {
@@ -277,15 +312,22 @@ int compress_cfg(
                        goto out_unlock;
                }
 
+               knet_h->compress_model = cmp_model;
+               knet_h->compress_level = knet_handle_compress_cfg->compress_level;
+
+               if (compress_lib_test(knet_h) < 0) {
+                       savederrno = errno;
+                       err = -1;
+                       goto out_unlock;
+               }
+
 out_unlock:
                pthread_rwlock_unlock(&shlib_rwlock);
        }
 
-       if (!err) {
-               knet_h->compress_model = cmp_model;
-               knet_h->compress_level = knet_handle_compress_cfg->compress_level;
-       } else {
+       if (err) {
                knet_h->compress_model = 0;
+               knet_h->compress_level = 0;
        }
 
        errno = savederrno;
index 597e03aac3e062510683720c65c270f4a71d71f5..5b47e1c9af2664ad404c6b569b6a8b7d90c54788 100644 (file)
 #include "logging.h"
 #include "compress_model.h"
 
-static int bzip2_val_level(
-       knet_handle_t knet_h,
-       int compress_level)
-{
-       if ((compress_level < 1) || (compress_level > 9)) {
-                log_err(knet_h, KNET_SUB_BZIP2COMP, "bzip2 unsupported compression level %d (accepted values from 1 to 9)", compress_level);
-               errno = EINVAL;
-               return -1;
-       }
-       return 0;
-}
-
 static int bzip2_compress(
        knet_handle_t knet_h,
        const unsigned char *buf_in,
@@ -120,7 +108,7 @@ compress_ops_t compress_model = {
        NULL,
        NULL,
        NULL,
-       bzip2_val_level,
+       NULL,
        bzip2_compress,
        bzip2_decompress
 };
index 8f90a5a9e2fd909a984e93eb07ec85245a22c279..263428a34b3322d7d2c5f10ac183c71c4608f0d5 100644 (file)
 #include "logging.h"
 #include "compress_model.h"
 
-static int lz4_val_level(
-       knet_handle_t knet_h,
-       int compress_level)
-{
-       if (compress_level <= 0) {
-               log_info(knet_h, KNET_SUB_LZ4COMP, "lz4 acceleration level 0 (or negatives) are automatically remapped to 1");
-       }
-
-       return 0;
-}
-
 static int lz4_compress(
        knet_handle_t knet_h,
        const unsigned char *buf_in,
@@ -96,7 +85,7 @@ compress_ops_t compress_model = {
        NULL,
        NULL,
        NULL,
-       lz4_val_level,
+       NULL,
        lz4_compress,
        lz4_decompress
 };
index aa832af6912dc8213b1f4f1b01fd0881068ccc51..da40fbc9090035cad96011d2522885c1d7b244a9 100644 (file)
 #define KNET_LZ4HC_MAX 16
 #endif
 
-static int lz4hc_val_level(
-       knet_handle_t knet_h,
-       int compress_level)
-{
-       if (compress_level < 1) {
-               log_err(knet_h, KNET_SUB_LZ4HCCOMP, "lz4hc supports only 1+ values for compression level");
-               errno = EINVAL;
-               return -1;
-       }
-
-       if (compress_level < 4) {
-               log_info(knet_h, KNET_SUB_LZ4HCCOMP, "lz4hc recommends 4+ compression level for better results");
-       }
-
-       if (compress_level > KNET_LZ4HC_MAX) {
-               log_warn(knet_h, KNET_SUB_LZ4HCCOMP, "lz4hc installed on this system supports up to compression level %d. Higher values behaves as %d", KNET_LZ4HC_MAX, KNET_LZ4HC_MAX);
-       }
-
-       return 0;
-}
-
 static int lz4hc_compress(
        knet_handle_t knet_h,
        const unsigned char *buf_in,
@@ -117,7 +96,7 @@ compress_ops_t compress_model = {
        NULL,
        NULL,
        NULL,
-       lz4hc_val_level,
+       NULL,
        lz4hc_compress,
        lz4_decompress
 };
index 2a6c356cfeb60a63335d631e53bbde8b66f63690..cd0374033de5504bb4e07c369d150a14c9eb0edd 100644 (file)
 #include "logging.h"
 #include "compress_model.h"
 
-static int lzma_val_level(
-       knet_handle_t knet_h,
-       int compress_level)
-{
-       if ((compress_level < 0) || (compress_level > 9)) {
-                log_err(knet_h, KNET_SUB_LZMACOMP, "lzma unsupported compression preset %d (accepted values from 0 to 9)", compress_level);
-               errno = EINVAL;
-               return -1;
-       }
-
-       return 0;
-}
-
 static int lzma_compress(
        knet_handle_t knet_h,
        const unsigned char *buf_in,
@@ -132,7 +119,7 @@ compress_ops_t compress_model = {
        NULL,
        NULL,
        NULL,
-       lzma_val_level,
+       NULL,
        lzma_compress,
        lzma_decompress
 };
index 082cc5af83efb80a5caea41f4b4f519dd98a25d3..5d6a640e497824b7dd4131ad3e9a0181ecc0152e 100644 (file)
@@ -48,8 +48,6 @@ typedef struct {
         */
 
        /*
-        * required functions
-        *
         * val_level is called upon compress configuration changes
         * to make sure that the requested compress_level is valid
         * within the context of a given module.
@@ -58,6 +56,8 @@ typedef struct {
                         int compress_level);
 
        /*
+        * required functions
+        *
         * hopefully those 2 don't require any explanation....
         */
        int (*compress) (knet_handle_t knet_h,
index 56bbfc53ad5d6b85bf2a38791ca49a4fd6963de9..3f86ac1446e804570a8ba75ddb5cf4a1d7414b23 100644 (file)
 #include "logging.h"
 #include "compress_model.h"
 
-static int zlib_val_level(
-       knet_handle_t knet_h,
-       int compress_level)
-{
-       if (compress_level < 0) {
-               log_err(knet_h, KNET_SUB_ZLIBCOMP, "zlib does not support negative compression level %d",
-                        compress_level);
-               return -1;
-       }
-       if (compress_level > 9) {
-               log_err(knet_h, KNET_SUB_ZLIBCOMP, "zlib does not support compression level higher than 9");
-               return -1;
-       }
-       if (compress_level == 0) {
-               log_warn(knet_h, KNET_SUB_ZLIBCOMP, "zlib compress level 0 does NOT perform any compression");
-       }
-       return 0;
-}
-
 static int zlib_compress(
        knet_handle_t knet_h,
        const unsigned char *buf_in,
@@ -130,7 +111,7 @@ compress_ops_t compress_model = {
        NULL,
        NULL,
        NULL,
-       zlib_val_level,
+       NULL,
        zlib_compress,
        zlib_decompress
 };
index d8f9714e91893fed55fedd2dac3b86f5683d91f9..76a0ce673d1ca8ff18a913e9682840b9c5959de2 100644 (file)
@@ -81,14 +81,14 @@ static void test(void)
 
        flush_logs(logfds[0], stdout);
 
-       printf("Test knet_handle_compress with zlib compress and negative level\n");
+       printf("Test knet_handle_compress with zlib compress and negative level (-2)\n");
 
        memset(&knet_handle_compress_cfg, 0, sizeof(struct knet_handle_compress_cfg));
        strncpy(knet_handle_compress_cfg.compress_model, "zlib", sizeof(knet_handle_compress_cfg.compress_model) - 1);
-       knet_handle_compress_cfg.compress_level = -1;
+       knet_handle_compress_cfg.compress_level = -2;
 
        if ((!knet_handle_compress(knet_h, &knet_handle_compress_cfg)) || (errno != EINVAL)) {
-               printf("knet_handle_compress accepted invalid (-1) compress level for zlib\n");
+               printf("knet_handle_compress accepted invalid (-2) compress level for zlib\n");
                knet_handle_free(knet_h);
                flush_logs(logfds[0], stdout);
                close_logpipes(logfds);