From ae60710ab891d6562f3ced471c85139a48779fa5 Mon Sep 17 00:00:00 2001 From: Daniel Verkamp Date: Fri, 21 Jul 2017 14:44:43 -0700 Subject: [PATCH] util: add CRC32 utility functions Factor out the iSCSI and GPT CRC32 functions into generic library functions. Change-Id: I1f1a5f3968a983b663a51bd984500492eeb12605 Signed-off-by: Daniel Verkamp Reviewed-on: https://review.gerrithub.io/370765 Tested-by: SPDK Automated Test System Reviewed-by: Ben Walker Reviewed-by: Jim Harris --- include/spdk/crc32.h | 101 ++++++++++++++++++ lib/bdev/gpt/gpt.c | 42 ++------ lib/iscsi/Makefile | 2 +- lib/iscsi/iscsi.c | 14 ++- lib/util/Makefile | 2 +- lib/{iscsi/crc32c.c => util/crc32.c} | 50 ++------- lib/{iscsi/crc32c.h => util/crc32_ieee.c} | 28 +++-- lib/util/crc32c.c | 48 +++++++++ test/unit/lib/iscsi/iscsi.c/Makefile | 2 +- test/unit/lib/util/Makefile | 2 +- test/unit/lib/util/crc32_ieee.c/.gitignore | 1 + test/unit/lib/util/crc32_ieee.c/Makefile | 52 +++++++++ .../lib/util/crc32_ieee.c/crc32_ieee_ut.c | 83 ++++++++++++++ test/unit/lib/util/crc32c.c/.gitignore | 1 + test/unit/lib/util/crc32c.c/Makefile | 52 +++++++++ test/unit/lib/util/crc32c.c/crc32c_ut.c | 83 ++++++++++++++ unittest.sh | 2 + 17 files changed, 465 insertions(+), 100 deletions(-) create mode 100644 include/spdk/crc32.h rename lib/{iscsi/crc32c.c => util/crc32.c} (67%) rename lib/{iscsi/crc32c.h => util/crc32_ieee.c} (74%) create mode 100644 lib/util/crc32c.c create mode 100644 test/unit/lib/util/crc32_ieee.c/.gitignore create mode 100644 test/unit/lib/util/crc32_ieee.c/Makefile create mode 100644 test/unit/lib/util/crc32_ieee.c/crc32_ieee_ut.c create mode 100644 test/unit/lib/util/crc32c.c/.gitignore create mode 100644 test/unit/lib/util/crc32c.c/Makefile create mode 100644 test/unit/lib/util/crc32c.c/crc32c_ut.c diff --git a/include/spdk/crc32.h b/include/spdk/crc32.h new file mode 100644 index 000000000..cb0254e7c --- /dev/null +++ b/include/spdk/crc32.h @@ -0,0 +1,101 @@ +/*- + * BSD LICENSE + * + * Copyright (C) 2008-2012 Daisuke Aoyama . + * Copyright (c) Intel Corporation. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Intel Corporation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * \file + * CRC-32 utility functions + */ + +#ifndef SPDK_CRC32_H +#define SPDK_CRC32_H + +#include "spdk/stdinc.h" + +/** + * IEEE CRC-32 polynomial (bit reflected) + */ +#define SPDK_CRC32_POLYNOMIAL_REFLECT 0xedb88320UL + +/** + * CRC-32C (Castagnoli) polynomial (bit reflected) + */ +#define SPDK_CRC32C_POLYNOMIAL_REFLECT 0x82f63b78UL + +struct spdk_crc32_table { + uint32_t table[256]; +}; + +/** + * Initialize a CRC32 lookup table for a given polynomial. + * + * \param table Table to fill with precalculated CRC-32 data. + * \param polynomial_reflect Bit-reflected CRC-32 polynomial. + */ +void spdk_crc32_table_init(struct spdk_crc32_table *table, + uint32_t polynomial_reflect); + +/** + * Calculate a partial CRC-32 checksum. + * + * \param table CRC-32 table initialized with spdk_crc32_table_init(). + * \param buf Data buffer to checksum. + * \param len Length of buf in bytes. + * \param crc Previous CRC-32 value. + * \return Updated CRC-32 value. + */ +uint32_t spdk_crc32_update(const struct spdk_crc32_table *table, + const void *buf, size_t len, + uint32_t crc); + +/** + * Calculate a partial CRC-32 IEEE checksum. + * + * \param buf Data buffer to checksum. + * \param len Length of buf in bytes. + * \param crc Previous CRC-32 value. + * \return Updated CRC-32 value. + */ +uint32_t spdk_crc32_ieee_update(const void *buf, size_t len, uint32_t crc); + +/** + * Calculate a partial CRC-32C checksum. + * + * \param buf Data buffer to checksum. + * \param len Length of buf in bytes. + * \param crc Previous CRC-32C value. + * \return Updated CRC-32C value. + */ +uint32_t spdk_crc32c_update(const void *buf, size_t len, uint32_t crc); + +#endif /* SPDK_CRC32_H */ diff --git a/lib/bdev/gpt/gpt.c b/lib/bdev/gpt/gpt.c index 72a1b42e3..794196879 100644 --- a/lib/bdev/gpt/gpt.c +++ b/lib/bdev/gpt/gpt.c @@ -33,7 +33,9 @@ #include "gpt.h" +#include "spdk/crc32.h" #include "spdk/endian.h" +#include "spdk/event.h" #include "spdk_internal/log.h" @@ -41,40 +43,6 @@ #define PRIMARY_PARTITION_NUMBER 4 #define GPT_PROTECTIVE_MBR 1 #define SPDK_MAX_NUM_PARTITION_ENTRIES 128 -#define SPDK_GPT_CRC32_POLYNOMIAL_REFLECT 0xedb88320UL - -static uint32_t spdk_gpt_crc32_table[256]; - -__attribute__((constructor)) static void -spdk_gpt_init_crc32(void) -{ - int i, j; - uint32_t val; - - for (i = 0; i < 256; i++) { - val = i; - for (j = 0; j < 8; j++) { - if (val & 1) { - val = (val >> 1) ^ SPDK_GPT_CRC32_POLYNOMIAL_REFLECT; - } else { - val = (val >> 1); - } - } - spdk_gpt_crc32_table[i] = val; - } -} - -static uint32_t -spdk_gpt_crc32(const uint8_t *buf, uint32_t size, uint32_t seed) -{ - uint32_t i, crc32 = seed; - - for (i = 0; i < size; i++) { - crc32 = spdk_gpt_crc32_table[(crc32 ^ buf[i]) & 0xff] ^ (crc32 >> 8); - } - - return crc32 ^ seed; -} static int spdk_gpt_read_partitions(struct spdk_gpt *gpt) @@ -108,7 +76,8 @@ spdk_gpt_read_partitions(struct spdk_gpt *gpt) gpt->partitions = (struct spdk_gpt_partition_entry *)(gpt->buf + partition_start_lba * gpt->sector_size); - crc32 = spdk_gpt_crc32((uint8_t *)gpt->partitions, total_partition_size, ~0); + crc32 = spdk_crc32_ieee_update(gpt->partitions, total_partition_size, ~0); + crc32 ^= ~0; if (crc32 != from_le32(&head->partition_entry_array_crc32)) { SPDK_ERRLOG("GPT partition entry array crc32 did not match\n"); @@ -163,7 +132,8 @@ spdk_gpt_read_header(struct spdk_gpt *gpt) original_crc = from_le32(&head->header_crc32); head->header_crc32 = 0; - new_crc = spdk_gpt_crc32((uint8_t *)head, from_le32(&head->header_size), ~0); + new_crc = spdk_crc32_ieee_update(head, from_le32(&head->header_size), ~0); + new_crc ^= ~0; /* restore header crc32 */ to_le32(&head->header_crc32, original_crc); diff --git a/lib/iscsi/Makefile b/lib/iscsi/Makefile index 2b5726064..7dfd51dd2 100644 --- a/lib/iscsi/Makefile +++ b/lib/iscsi/Makefile @@ -35,7 +35,7 @@ SPDK_ROOT_DIR := $(abspath $(CURDIR)/../..) include $(SPDK_ROOT_DIR)/mk/spdk.common.mk CFLAGS += $(ENV_CFLAGS) -I$(SPDK_ROOT_DIR)/lib -C_SRCS = acceptor.c conn.c crc32c.c \ +C_SRCS = acceptor.c conn.c \ init_grp.c iscsi.c md5.c param.c portal_grp.c \ tgt_node.c iscsi_subsystem.c \ iscsi_rpc.c task.c diff --git a/lib/iscsi/iscsi.c b/lib/iscsi/iscsi.c index 620b1efdc..7f562c15d 100644 --- a/lib/iscsi/iscsi.c +++ b/lib/iscsi/iscsi.c @@ -37,6 +37,7 @@ #include #include +#include "spdk/crc32.h" #include "spdk/endian.h" #include "spdk/env.h" #include "spdk/trace.h" @@ -44,7 +45,7 @@ #include "spdk/queue.h" #include "spdk/conf.h" #include "spdk/net.h" -#include "iscsi/crc32c.h" + #include "iscsi/md5.h" #include "iscsi/iscsi.h" #include "iscsi/param.h" @@ -60,6 +61,9 @@ #define MAX_TMPBUF 1024 +#define SPDK_CRC32C_INITIAL 0xffffffffUL +#define SPDK_CRC32C_XOR 0xffffffffUL + #ifdef __FreeBSD__ #define HAVE_SRANDOMDEV 1 #define HAVE_ARC4RANDOM 1 @@ -310,10 +314,10 @@ spdk_iscsi_pdu_calc_header_digest(struct spdk_iscsi_pdu *pdu) uint32_t ahs_len_bytes = pdu->bhs.total_ahs_len * 4; crc32c = SPDK_CRC32C_INITIAL; - crc32c = spdk_update_crc32c((uint8_t *)&pdu->bhs, ISCSI_BHS_LEN, crc32c); + crc32c = spdk_crc32c_update(&pdu->bhs, ISCSI_BHS_LEN, crc32c); if (ahs_len_bytes) { - crc32c = spdk_update_crc32c((uint8_t *)pdu->ahs, ahs_len_bytes, crc32c); + crc32c = spdk_crc32c_update(pdu->ahs, ahs_len_bytes, crc32c); } /* BHS and AHS are always 4-byte multiples in length, so no padding is necessary. */ @@ -329,7 +333,7 @@ spdk_iscsi_pdu_calc_data_digest(struct spdk_iscsi_pdu *pdu) uint32_t mod; crc32c = SPDK_CRC32C_INITIAL; - crc32c = spdk_update_crc32c(pdu->data, data_len, crc32c); + crc32c = spdk_crc32c_update(pdu->data, data_len, crc32c); mod = data_len % ISCSI_ALIGNMENT; if (mod != 0) { @@ -338,7 +342,7 @@ spdk_iscsi_pdu_calc_data_digest(struct spdk_iscsi_pdu *pdu) assert(pad_length > 0); assert(pad_length <= sizeof(pad)); - crc32c = spdk_update_crc32c(pad, pad_length, crc32c); + crc32c = spdk_crc32c_update(pad, pad_length, crc32c); } crc32c = crc32c ^ SPDK_CRC32C_XOR; diff --git a/lib/util/Makefile b/lib/util/Makefile index eb4a4d74c..ad4c6be1b 100644 --- a/lib/util/Makefile +++ b/lib/util/Makefile @@ -35,7 +35,7 @@ SPDK_ROOT_DIR := $(abspath $(CURDIR)/../..) include $(SPDK_ROOT_DIR)/mk/spdk.common.mk CFLAGS += $(ENV_CFLAGS) -C_SRCS = bit_array.c fd.c io_channel.c string.c +C_SRCS = bit_array.c crc32.c crc32c.c crc32_ieee.c fd.c io_channel.c string.c LIBNAME = util include $(SPDK_ROOT_DIR)/mk/spdk.lib.mk diff --git a/lib/iscsi/crc32c.c b/lib/util/crc32.c similarity index 67% rename from lib/iscsi/crc32c.c rename to lib/util/crc32.c index e9f344f6e..8294d056c 100644 --- a/lib/iscsi/crc32c.c +++ b/lib/util/crc32.c @@ -32,20 +32,10 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include "spdk/stdinc.h" +#include "spdk/crc32.h" -#include "iscsi/iscsi.h" -#include "iscsi/crc32c.h" - -#ifndef USE_ISAL -#define SPDK_USE_CRC32C_TABLE -#endif - -#ifdef SPDK_USE_CRC32C_TABLE -static uint32_t spdk_crc32c_table[256]; - -__attribute__((constructor)) static void -spdk_init_crc32c(void) +void +spdk_crc32_table_init(struct spdk_crc32_table *table, uint32_t polynomial_reflect) { int i, j; uint32_t val; @@ -54,42 +44,24 @@ spdk_init_crc32c(void) val = i; for (j = 0; j < 8; j++) { if (val & 1) { - val = (val >> 1) ^ SPDK_CRC32C_POLYNOMIAL_REFLECT; + val = (val >> 1) ^ polynomial_reflect; } else { val = (val >> 1); } } - spdk_crc32c_table[i] = val; + table->table[i] = val; } } -#endif /* SPDK_USE_CRC32C_TABLE */ - -#ifndef USE_ISAL uint32_t -spdk_update_crc32c(const uint8_t *buf, size_t len, uint32_t crc) +spdk_crc32_update(const struct spdk_crc32_table *table, const void *buf, size_t len, uint32_t crc) { - size_t s; -#ifndef SPDK_USE_CRC32C_TABLE - int i; - uint32_t val; -#endif /* SPDK_USE_CRC32C_TABLE */ + const uint8_t *buf_u8 = buf; + size_t i; - for (s = 0; s < len; s++) { -#ifdef SPDK_USE_CRC32C_TABLE - crc = (crc >> 8) ^ spdk_crc32c_table[(crc ^ buf[s]) & 0xff]; -#else - val = buf[s]; - for (i = 0; i < 8; i++) { - if ((crc ^ val) & 1) { - crc = (crc >> 1) ^ SPDK_CRC32C_POLYNOMIAL_REFLECT; - } else { - crc = (crc >> 1); - } - val = val >> 1; - } -#endif /* SPDK_USE_CRC32C_TABLE */ + for (i = 0; i < len; i++) { + crc = (crc >> 8) ^ table->table[(crc ^ buf_u8[i]) & 0xff]; } + return crc; } -#endif /* USE_ISAL */ diff --git a/lib/iscsi/crc32c.h b/lib/util/crc32_ieee.c similarity index 74% rename from lib/iscsi/crc32c.h rename to lib/util/crc32_ieee.c index 183ca4383..2956e3fc6 100644 --- a/lib/iscsi/crc32c.h +++ b/lib/util/crc32_ieee.c @@ -1,7 +1,6 @@ /*- * BSD LICENSE * - * Copyright (C) 2008-2012 Daisuke Aoyama . * Copyright (c) Intel Corporation. * All rights reserved. * @@ -32,21 +31,18 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef SPDK_CRC32C_H -#define SPDK_CRC32C_H +#include "spdk/crc32.h" -#include "spdk/stdinc.h" +static struct spdk_crc32_table g_crc32_ieee_table; -#define SPDK_CRC32C_INITIAL 0xffffffffUL -#define SPDK_CRC32C_XOR 0xffffffffUL -#define SPDK_CRC32C_POLYNOMIAL 0x1edc6f41UL -#define SPDK_CRC32C_POLYNOMIAL_REFLECT 0x82f63b78UL +__attribute__((constructor)) static void +spdk_crc32_ieee_init(void) +{ + spdk_crc32_table_init(&g_crc32_ieee_table, SPDK_CRC32_POLYNOMIAL_REFLECT); +} -#ifdef USE_ISAL -uint32_t crc32_iscsi(const uint8_t *buf, size_t len, uint32_t crc); -#define spdk_update_crc32c(a,b,c) crc32_iscsi(a,b,c) -#else -uint32_t spdk_update_crc32c(const uint8_t *buf, size_t len, uint32_t crc); -#endif - -#endif /* SPDK_CRC32C_H */ +uint32_t +spdk_crc32_ieee_update(const void *buf, size_t len, uint32_t crc) +{ + return spdk_crc32_update(&g_crc32_ieee_table, buf, len, crc); +} diff --git a/lib/util/crc32c.c b/lib/util/crc32c.c new file mode 100644 index 000000000..f5bf36f0c --- /dev/null +++ b/lib/util/crc32c.c @@ -0,0 +1,48 @@ +/*- + * BSD LICENSE + * + * Copyright (c) Intel Corporation. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Intel Corporation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "spdk/crc32.h" + +static struct spdk_crc32_table g_crc32c_table; + +__attribute__((constructor)) static void +spdk_crc32c_init(void) +{ + spdk_crc32_table_init(&g_crc32c_table, SPDK_CRC32C_POLYNOMIAL_REFLECT); +} + +uint32_t +spdk_crc32c_update(const void *buf, size_t len, uint32_t crc) +{ + return spdk_crc32_update(&g_crc32c_table, buf, len, crc); +} diff --git a/test/unit/lib/iscsi/iscsi.c/Makefile b/test/unit/lib/iscsi/iscsi.c/Makefile index a5a625d0c..04a189dfe 100644 --- a/test/unit/lib/iscsi/iscsi.c/Makefile +++ b/test/unit/lib/iscsi/iscsi.c/Makefile @@ -43,7 +43,7 @@ CFLAGS += -I$(SPDK_ROOT_DIR)/lib LIBS += $(SPDK_LIB_LINKER_ARGS) $(ENV_LINKER_ARGS) LIBS += -lcunit -lcrypto SCSI_OBJS = port -ISCSI_OBJS = crc32c md5 param +ISCSI_OBJS = md5 param OBJS += $(SCSI_OBJS:%=$(SPDK_ROOT_DIR)/lib/scsi/%.o) OBJS += $(ISCSI_OBJS:%=$(SPDK_ROOT_DIR)/lib/iscsi/%.o) diff --git a/test/unit/lib/util/Makefile b/test/unit/lib/util/Makefile index 668e7aea8..bc684cd96 100644 --- a/test/unit/lib/util/Makefile +++ b/test/unit/lib/util/Makefile @@ -34,7 +34,7 @@ SPDK_ROOT_DIR := $(abspath $(CURDIR)/../../../..) include $(SPDK_ROOT_DIR)/mk/spdk.common.mk -DIRS-y = bit_array.c io_channel.c string.c +DIRS-y = bit_array.c crc32_ieee.c crc32c.c io_channel.c string.c .PHONY: all clean $(DIRS-y) diff --git a/test/unit/lib/util/crc32_ieee.c/.gitignore b/test/unit/lib/util/crc32_ieee.c/.gitignore new file mode 100644 index 000000000..40a85a93f --- /dev/null +++ b/test/unit/lib/util/crc32_ieee.c/.gitignore @@ -0,0 +1 @@ +crc32_ieee_ut diff --git a/test/unit/lib/util/crc32_ieee.c/Makefile b/test/unit/lib/util/crc32_ieee.c/Makefile new file mode 100644 index 000000000..288878026 --- /dev/null +++ b/test/unit/lib/util/crc32_ieee.c/Makefile @@ -0,0 +1,52 @@ +# +# BSD LICENSE +# +# Copyright (c) Intel Corporation. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# * Neither the name of Intel Corporation nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +SPDK_ROOT_DIR := $(abspath $(CURDIR)/../../../../..) +include $(SPDK_ROOT_DIR)/mk/spdk.common.mk + +CFLAGS += -I$(SPDK_ROOT_DIR)/test +CFLAGS += -I$(SPDK_ROOT_DIR)/lib/util +APP = crc32_ieee_ut +C_SRCS := crc32_ieee_ut.c + +LIBS += -lcunit + +all : $(APP) + +$(APP) : $(OBJS) $(SPDK_LIBS) + $(LINK_C) + +clean : + $(CLEAN_C) $(APP) + +include $(SPDK_ROOT_DIR)/mk/spdk.deps.mk diff --git a/test/unit/lib/util/crc32_ieee.c/crc32_ieee_ut.c b/test/unit/lib/util/crc32_ieee.c/crc32_ieee_ut.c new file mode 100644 index 000000000..263b2866d --- /dev/null +++ b/test/unit/lib/util/crc32_ieee.c/crc32_ieee_ut.c @@ -0,0 +1,83 @@ +/*- + * BSD LICENSE + * + * Copyright (c) Intel Corporation. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Intel Corporation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "spdk/stdinc.h" + +#include "spdk_cunit.h" + +#include "crc32.c" +#include "crc32_ieee.c" + +static void +test_crc32_ieee(void) +{ + uint32_t crc; + char buf[] = "Hello world!"; + + crc = 0xFFFFFFFFu; + crc = spdk_crc32_ieee_update(buf, strlen(buf), crc); + crc ^= 0xFFFFFFFFu; + CU_ASSERT(crc == 0x1b851995); +} + +int +main(int argc, char **argv) +{ + CU_pSuite suite = NULL; + unsigned int num_failures; + + if (CU_initialize_registry() != CUE_SUCCESS) { + return CU_get_error(); + } + + suite = CU_add_suite("crc32_ieee", NULL, NULL); + if (suite == NULL) { + CU_cleanup_registry(); + return CU_get_error(); + } + + if ( + CU_add_test(suite, "test_crc32_ieee", test_crc32_ieee) == NULL) { + CU_cleanup_registry(); + return CU_get_error(); + } + + CU_basic_set_mode(CU_BRM_VERBOSE); + + CU_basic_run_tests(); + + num_failures = CU_get_number_of_failures(); + CU_cleanup_registry(); + + return num_failures; +} diff --git a/test/unit/lib/util/crc32c.c/.gitignore b/test/unit/lib/util/crc32c.c/.gitignore new file mode 100644 index 000000000..55bedec7f --- /dev/null +++ b/test/unit/lib/util/crc32c.c/.gitignore @@ -0,0 +1 @@ +crc32c_ut diff --git a/test/unit/lib/util/crc32c.c/Makefile b/test/unit/lib/util/crc32c.c/Makefile new file mode 100644 index 000000000..b7342a7c7 --- /dev/null +++ b/test/unit/lib/util/crc32c.c/Makefile @@ -0,0 +1,52 @@ +# +# BSD LICENSE +# +# Copyright (c) Intel Corporation. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# * Neither the name of Intel Corporation nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +SPDK_ROOT_DIR := $(abspath $(CURDIR)/../../../../..) +include $(SPDK_ROOT_DIR)/mk/spdk.common.mk + +CFLAGS += -I$(SPDK_ROOT_DIR)/test +CFLAGS += -I$(SPDK_ROOT_DIR)/lib/util +APP = crc32c_ut +C_SRCS := crc32c_ut.c + +LIBS += -lcunit + +all : $(APP) + +$(APP) : $(OBJS) $(SPDK_LIBS) + $(LINK_C) + +clean : + $(CLEAN_C) $(APP) + +include $(SPDK_ROOT_DIR)/mk/spdk.deps.mk diff --git a/test/unit/lib/util/crc32c.c/crc32c_ut.c b/test/unit/lib/util/crc32c.c/crc32c_ut.c new file mode 100644 index 000000000..837f1de2e --- /dev/null +++ b/test/unit/lib/util/crc32c.c/crc32c_ut.c @@ -0,0 +1,83 @@ +/*- + * BSD LICENSE + * + * Copyright (c) Intel Corporation. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Intel Corporation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "spdk/stdinc.h" + +#include "spdk_cunit.h" + +#include "crc32.c" +#include "crc32c.c" + +static void +test_crc32c(void) +{ + uint32_t crc; + char buf[] = "Hello world!"; + + crc = 0xFFFFFFFFu; + crc = spdk_crc32c_update(buf, strlen(buf), crc); + crc ^= 0xFFFFFFFFu; + CU_ASSERT(crc == 0x7b98e751); +} + +int +main(int argc, char **argv) +{ + CU_pSuite suite = NULL; + unsigned int num_failures; + + if (CU_initialize_registry() != CUE_SUCCESS) { + return CU_get_error(); + } + + suite = CU_add_suite("crc32c", NULL, NULL); + if (suite == NULL) { + CU_cleanup_registry(); + return CU_get_error(); + } + + if ( + CU_add_test(suite, "test_crc32c", test_crc32c) == NULL) { + CU_cleanup_registry(); + return CU_get_error(); + } + + CU_basic_set_mode(CU_BRM_VERBOSE); + + CU_basic_run_tests(); + + num_failures = CU_get_number_of_failures(); + CU_cleanup_registry(); + + return num_failures; +} diff --git a/unittest.sh b/unittest.sh index ed819eac2..35d60459d 100755 --- a/unittest.sh +++ b/unittest.sh @@ -85,6 +85,8 @@ $valgrind test/unit/lib/iscsi/tgt_node.c/tgt_node_ut test/unit/lib/iscsi/tgt_nod $valgrind test/unit/lib/iscsi/iscsi.c/iscsi_ut $valgrind test/unit/lib/util/bit_array.c/bit_array_ut +$valgrind test/unit/lib/util/crc32_ieee.c/crc32_ieee_ut +$valgrind test/unit/lib/util/crc32c.c/crc32c_ut $valgrind test/unit/lib/util/io_channel.c/io_channel_ut $valgrind test/unit/lib/util/string.c/string_ut