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 <daniel.verkamp@intel.com>
Reviewed-on: https://review.gerrithub.io/370765
Tested-by: SPDK Automated Test System <sys_sgsw@intel.com>
Reviewed-by: Ben Walker <benjamin.walker@intel.com>
Reviewed-by: Jim Harris <james.r.harris@intel.com>
This commit is contained in:
Daniel Verkamp 2017-07-21 14:44:43 -07:00
parent 7fedfb484f
commit ae60710ab8
17 changed files with 465 additions and 100 deletions

101
include/spdk/crc32.h Normal file
View File

@ -0,0 +1,101 @@
/*-
* BSD LICENSE
*
* Copyright (C) 2008-2012 Daisuke Aoyama <aoyama@peach.ne.jp>.
* 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 */

View File

@ -33,7 +33,9 @@
#include "gpt.h" #include "gpt.h"
#include "spdk/crc32.h"
#include "spdk/endian.h" #include "spdk/endian.h"
#include "spdk/event.h"
#include "spdk_internal/log.h" #include "spdk_internal/log.h"
@ -41,40 +43,6 @@
#define PRIMARY_PARTITION_NUMBER 4 #define PRIMARY_PARTITION_NUMBER 4
#define GPT_PROTECTIVE_MBR 1 #define GPT_PROTECTIVE_MBR 1
#define SPDK_MAX_NUM_PARTITION_ENTRIES 128 #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 static int
spdk_gpt_read_partitions(struct spdk_gpt *gpt) 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 + gpt->partitions = (struct spdk_gpt_partition_entry *)(gpt->buf +
partition_start_lba * gpt->sector_size); 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)) { if (crc32 != from_le32(&head->partition_entry_array_crc32)) {
SPDK_ERRLOG("GPT partition entry array crc32 did not match\n"); 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); original_crc = from_le32(&head->header_crc32);
head->header_crc32 = 0; 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 */ /* restore header crc32 */
to_le32(&head->header_crc32, original_crc); to_le32(&head->header_crc32, original_crc);

View File

@ -35,7 +35,7 @@ SPDK_ROOT_DIR := $(abspath $(CURDIR)/../..)
include $(SPDK_ROOT_DIR)/mk/spdk.common.mk include $(SPDK_ROOT_DIR)/mk/spdk.common.mk
CFLAGS += $(ENV_CFLAGS) -I$(SPDK_ROOT_DIR)/lib 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 \ init_grp.c iscsi.c md5.c param.c portal_grp.c \
tgt_node.c iscsi_subsystem.c \ tgt_node.c iscsi_subsystem.c \
iscsi_rpc.c task.c iscsi_rpc.c task.c

View File

@ -37,6 +37,7 @@
#include <rte_config.h> #include <rte_config.h>
#include <rte_mempool.h> #include <rte_mempool.h>
#include "spdk/crc32.h"
#include "spdk/endian.h" #include "spdk/endian.h"
#include "spdk/env.h" #include "spdk/env.h"
#include "spdk/trace.h" #include "spdk/trace.h"
@ -44,7 +45,7 @@
#include "spdk/queue.h" #include "spdk/queue.h"
#include "spdk/conf.h" #include "spdk/conf.h"
#include "spdk/net.h" #include "spdk/net.h"
#include "iscsi/crc32c.h"
#include "iscsi/md5.h" #include "iscsi/md5.h"
#include "iscsi/iscsi.h" #include "iscsi/iscsi.h"
#include "iscsi/param.h" #include "iscsi/param.h"
@ -60,6 +61,9 @@
#define MAX_TMPBUF 1024 #define MAX_TMPBUF 1024
#define SPDK_CRC32C_INITIAL 0xffffffffUL
#define SPDK_CRC32C_XOR 0xffffffffUL
#ifdef __FreeBSD__ #ifdef __FreeBSD__
#define HAVE_SRANDOMDEV 1 #define HAVE_SRANDOMDEV 1
#define HAVE_ARC4RANDOM 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; uint32_t ahs_len_bytes = pdu->bhs.total_ahs_len * 4;
crc32c = SPDK_CRC32C_INITIAL; 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) { 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. */ /* 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; uint32_t mod;
crc32c = SPDK_CRC32C_INITIAL; 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; mod = data_len % ISCSI_ALIGNMENT;
if (mod != 0) { 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 > 0);
assert(pad_length <= sizeof(pad)); 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; crc32c = crc32c ^ SPDK_CRC32C_XOR;

View File

@ -35,7 +35,7 @@ SPDK_ROOT_DIR := $(abspath $(CURDIR)/../..)
include $(SPDK_ROOT_DIR)/mk/spdk.common.mk include $(SPDK_ROOT_DIR)/mk/spdk.common.mk
CFLAGS += $(ENV_CFLAGS) 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 LIBNAME = util
include $(SPDK_ROOT_DIR)/mk/spdk.lib.mk include $(SPDK_ROOT_DIR)/mk/spdk.lib.mk

View File

@ -32,20 +32,10 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#include "spdk/stdinc.h" #include "spdk/crc32.h"
#include "iscsi/iscsi.h" void
#include "iscsi/crc32c.h" spdk_crc32_table_init(struct spdk_crc32_table *table, uint32_t polynomial_reflect)
#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)
{ {
int i, j; int i, j;
uint32_t val; uint32_t val;
@ -54,42 +44,24 @@ spdk_init_crc32c(void)
val = i; val = i;
for (j = 0; j < 8; j++) { for (j = 0; j < 8; j++) {
if (val & 1) { if (val & 1) {
val = (val >> 1) ^ SPDK_CRC32C_POLYNOMIAL_REFLECT; val = (val >> 1) ^ polynomial_reflect;
} else { } else {
val = (val >> 1); val = (val >> 1);
} }
} }
spdk_crc32c_table[i] = val; table->table[i] = val;
} }
} }
#endif /* SPDK_USE_CRC32C_TABLE */
#ifndef USE_ISAL
uint32_t 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; const uint8_t *buf_u8 = buf;
#ifndef SPDK_USE_CRC32C_TABLE size_t i;
int i;
uint32_t val;
#endif /* SPDK_USE_CRC32C_TABLE */
for (s = 0; s < len; s++) { for (i = 0; i < len; i++) {
#ifdef SPDK_USE_CRC32C_TABLE crc = (crc >> 8) ^ table->table[(crc ^ buf_u8[i]) & 0xff];
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 */
} }
return crc; return crc;
} }
#endif /* USE_ISAL */

View File

@ -1,7 +1,6 @@
/*- /*-
* BSD LICENSE * BSD LICENSE
* *
* Copyright (C) 2008-2012 Daisuke Aoyama <aoyama@peach.ne.jp>.
* Copyright (c) Intel Corporation. * Copyright (c) Intel Corporation.
* All rights reserved. * All rights reserved.
* *
@ -32,21 +31,18 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#ifndef SPDK_CRC32C_H #include "spdk/crc32.h"
#define SPDK_CRC32C_H
#include "spdk/stdinc.h" static struct spdk_crc32_table g_crc32_ieee_table;
#define SPDK_CRC32C_INITIAL 0xffffffffUL __attribute__((constructor)) static void
#define SPDK_CRC32C_XOR 0xffffffffUL spdk_crc32_ieee_init(void)
#define SPDK_CRC32C_POLYNOMIAL 0x1edc6f41UL {
#define SPDK_CRC32C_POLYNOMIAL_REFLECT 0x82f63b78UL spdk_crc32_table_init(&g_crc32_ieee_table, SPDK_CRC32_POLYNOMIAL_REFLECT);
}
#ifdef USE_ISAL uint32_t
uint32_t crc32_iscsi(const uint8_t *buf, size_t len, uint32_t crc); spdk_crc32_ieee_update(const void *buf, size_t len, uint32_t crc)
#define spdk_update_crc32c(a,b,c) crc32_iscsi(a,b,c) {
#else return spdk_crc32_update(&g_crc32_ieee_table, buf, len, crc);
uint32_t spdk_update_crc32c(const uint8_t *buf, size_t len, uint32_t crc); }
#endif
#endif /* SPDK_CRC32C_H */

48
lib/util/crc32c.c Normal file
View File

@ -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);
}

View File

@ -43,7 +43,7 @@ CFLAGS += -I$(SPDK_ROOT_DIR)/lib
LIBS += $(SPDK_LIB_LINKER_ARGS) $(ENV_LINKER_ARGS) LIBS += $(SPDK_LIB_LINKER_ARGS) $(ENV_LINKER_ARGS)
LIBS += -lcunit -lcrypto LIBS += -lcunit -lcrypto
SCSI_OBJS = port SCSI_OBJS = port
ISCSI_OBJS = crc32c md5 param ISCSI_OBJS = md5 param
OBJS += $(SCSI_OBJS:%=$(SPDK_ROOT_DIR)/lib/scsi/%.o) OBJS += $(SCSI_OBJS:%=$(SPDK_ROOT_DIR)/lib/scsi/%.o)
OBJS += $(ISCSI_OBJS:%=$(SPDK_ROOT_DIR)/lib/iscsi/%.o) OBJS += $(ISCSI_OBJS:%=$(SPDK_ROOT_DIR)/lib/iscsi/%.o)

View File

@ -34,7 +34,7 @@
SPDK_ROOT_DIR := $(abspath $(CURDIR)/../../../..) SPDK_ROOT_DIR := $(abspath $(CURDIR)/../../../..)
include $(SPDK_ROOT_DIR)/mk/spdk.common.mk 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) .PHONY: all clean $(DIRS-y)

View File

@ -0,0 +1 @@
crc32_ieee_ut

View File

@ -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

View File

@ -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;
}

View File

@ -0,0 +1 @@
crc32c_ut

View File

@ -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

View File

@ -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;
}

View File

@ -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/iscsi/iscsi.c/iscsi_ut
$valgrind test/unit/lib/util/bit_array.c/bit_array_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/io_channel.c/io_channel_ut
$valgrind test/unit/lib/util/string.c/string_ut $valgrind test/unit/lib/util/string.c/string_ut