Commit ae60710a authored by Daniel Verkamp's avatar Daniel Verkamp
Browse files

util: add CRC32 utility functions



Factor out the iSCSI and GPT CRC32 functions into generic library
functions.

Change-Id: I1f1a5f3968a983b663a51bd984500492eeb12605
Signed-off-by: default avatarDaniel Verkamp <daniel.verkamp@intel.com>
Reviewed-on: https://review.gerrithub.io/370765


Tested-by: default avatarSPDK Automated Test System <sys_sgsw@intel.com>
Reviewed-by: default avatarBen Walker <benjamin.walker@intel.com>
Reviewed-by: default avatarJim Harris <james.r.harris@intel.com>
parent 7fedfb48
Loading
Loading
Loading
Loading

include/spdk/crc32.h

0 → 100644
+101 −0
Original line number Diff line number Diff line
/*-
 *   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 */
+6 −36
Original line number Diff line number Diff line
@@ -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);

+1 −1
Original line number Diff line number Diff line
@@ -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
+9 −5
Original line number Diff line number Diff line
@@ -37,6 +37,7 @@
#include <rte_config.h>
#include <rte_mempool.h>

#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;
+1 −1
Original line number Diff line number Diff line
@@ -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
Loading