Commit f7d49034 authored by Shuhei Matsumoto's avatar Shuhei Matsumoto Committed by Tomasz Zawadzki
Browse files

dif: Fix metadata config check of dif_ctx_init() and add unit test cases



For 32b Guard PI format and 64b Guard PI format, data block size should
be equal or greater than 4KiB. However, the check was done only for
separate metadata. This was wrong. Move the check to the common part to
be available both for interleaved metadata and separate metadata.

Unit test was not enough for such checks. Add more cases.

Signed-off-by: default avatarShuhei Matsumoto <smatsumoto@nvidia.com>
Change-Id: Ibb3b0c4bf3c56ffc70a6e16eacbe8ca390e36ebb
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/24204


Reviewed-by: default avatarJim Harris <jim.harris@samsung.com>
Reviewed-by: default avatarAleksey Marchuk <alexeymar@nvidia.com>
Community-CI: Mellanox Build Bot
Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
parent baa0c332
Loading
Loading
Loading
Loading
+17 −12
Original line number Diff line number Diff line
@@ -548,21 +548,26 @@ spdk_dif_ctx_init(struct spdk_dif_ctx *ctx, uint32_t block_size, uint32_t md_siz
		}
		data_block_size = block_size - md_size;
	} else {
		data_block_size = block_size;
	}

	if (data_block_size == 0) {
		SPDK_ERRLOG("Zero data block size is not allowed\n");
		return -EINVAL;
	}

	if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
			if (block_size == 0 || (block_size % 512) != 0) {
				SPDK_ERRLOG("Zero block size is not allowed and should be a multiple of 512B\n");
		if ((data_block_size % 512) != 0) {
			SPDK_ERRLOG("Data block size should be a multiple of 512B\n");
			return -EINVAL;
		}
	} else {
			if (block_size == 0 || (block_size % 4096) != 0) {
				SPDK_ERRLOG("Zero block size is not allowed and should be a multiple of 4kB\n");
		if ((data_block_size % 4096) != 0) {
			SPDK_ERRLOG("Data block size should be a multiple of 4kB\n");
			return -EINVAL;
		}
	}

		data_block_size = block_size;
	}

	ctx->block_size = block_size;
	ctx->md_size = md_size;
	ctx->md_interleave = md_interleave;
+118 −5
Original line number Diff line number Diff line
@@ -534,6 +534,21 @@ dif_apptag_mask_test(void)
	_dif_apptag_mask_test(SPDK_DIF_PI_FORMAT_32);
}

static void
dif_sec_8_md_8_error_test(void)
{
	struct spdk_dif_ctx ctx = {};
	int rc;
	struct spdk_dif_ctx_init_ext_opts dif_opts;

	dif_opts.size = SPDK_SIZEOF(&dif_opts, dif_pi_format);
	dif_opts.dif_pi_format = SPDK_DIF_PI_FORMAT_16;
	/* Metadata size is 8 and block size is 8. */
	rc = spdk_dif_ctx_init(&ctx, 8, 8, true, false, SPDK_DIF_TYPE1, 0,
			       0, 0, 0, 0, 0, &dif_opts);
	CU_ASSERT(rc != 0);
}

static void
dif_sec_512_md_0_error_test(void)
{
@@ -550,7 +565,29 @@ dif_sec_512_md_0_error_test(void)
}

static void
_dif_sec_4096_md_0_error_test(enum spdk_dif_pi_format dif_pi_format)
_dif_sec_512_md_16_error_test(enum spdk_dif_pi_format dif_pi_format)
{
	struct spdk_dif_ctx ctx = {};
	int rc;
	struct spdk_dif_ctx_init_ext_opts dif_opts;

	dif_opts.size = SPDK_SIZEOF(&dif_opts, dif_pi_format);
	dif_opts.dif_pi_format = dif_pi_format;
	/* Metadata size is 16 but block size is 512. */
	rc = spdk_dif_ctx_init(&ctx, 512, 16, true, false, SPDK_DIF_TYPE1, 0,
			       0, 0, 0, 0, 0, &dif_opts);
	CU_ASSERT(rc != 0);
}

static void
dif_sec_512_md_16_error_test(void)
{
	_dif_sec_512_md_16_error_test(SPDK_DIF_PI_FORMAT_32);
	_dif_sec_512_md_16_error_test(SPDK_DIF_PI_FORMAT_64);
}

static void
_dif_sec_4096_md_0_8_error_test(enum spdk_dif_pi_format dif_pi_format)
{
	struct spdk_dif_ctx ctx = {};
	int rc;
@@ -562,13 +599,20 @@ _dif_sec_4096_md_0_error_test(enum spdk_dif_pi_format dif_pi_format)
	rc = spdk_dif_ctx_init(&ctx, 4096, 0, true, false, SPDK_DIF_TYPE1, 0,
			       0, 0, 0, 0, 0, &dif_opts);
	CU_ASSERT(rc != 0);

	dif_opts.size = SPDK_SIZEOF(&dif_opts, dif_pi_format);
	dif_opts.dif_pi_format = dif_pi_format;
	/* Metadata size is 8. */
	rc = spdk_dif_ctx_init(&ctx, 4096, 8, true, false, SPDK_DIF_TYPE1, 0,
			       0, 0, 0, 0, 0, &dif_opts);
	CU_ASSERT(rc != 0);
}

static void
dif_sec_4096_md_0_error_test(void)
dif_sec_4096_md_0_8_error_test(void)
{
	_dif_sec_4096_md_0_error_test(SPDK_DIF_PI_FORMAT_32);
	_dif_sec_4096_md_0_error_test(SPDK_DIF_PI_FORMAT_64);
	_dif_sec_4096_md_0_8_error_test(SPDK_DIF_PI_FORMAT_32);
	_dif_sec_4096_md_0_8_error_test(SPDK_DIF_PI_FORMAT_64);
}

static void
@@ -1992,6 +2036,20 @@ dif_copy_sec_4096_md_128_inject_1_2_4_8_multi_iovs_split_test(void)
	_iov_free_buf(&bounce_iov);
}

static void
dix_sec_0_md_8_error(void)
{
	struct spdk_dif_ctx ctx;
	int rc;
	struct spdk_dif_ctx_init_ext_opts dif_opts;

	dif_opts.size = SPDK_SIZEOF(&dif_opts, dif_pi_format);
	dif_opts.dif_pi_format = SPDK_DIF_PI_FORMAT_16;
	rc = spdk_dif_ctx_init(&ctx, 0, 8, false, false, SPDK_DIF_TYPE1, 0,
			       0, 0, 0, 0, 0, &dif_opts);
	CU_ASSERT(rc != 0);
}

static void
dix_sec_512_md_0_error(void)
{
@@ -2006,6 +2064,56 @@ dix_sec_512_md_0_error(void)
	CU_ASSERT(rc != 0);
}

static void
_dix_sec_512_md_16_error(enum spdk_dif_pi_format dif_pi_format)
{
	struct spdk_dif_ctx ctx;
	int rc;
	struct spdk_dif_ctx_init_ext_opts dif_opts;

	dif_opts.size = SPDK_SIZEOF(&dif_opts, dif_pi_format);
	dif_opts.dif_pi_format = dif_pi_format;
	rc = spdk_dif_ctx_init(&ctx, 512, 16, false, false, SPDK_DIF_TYPE1, 0,
			       0, 0, 0, 0, 0, &dif_opts);
	CU_ASSERT(rc != 0);
}

static void
dix_sec_512_md_16_error(void)
{
	_dix_sec_512_md_16_error(SPDK_DIF_PI_FORMAT_32);
	_dix_sec_512_md_16_error(SPDK_DIF_PI_FORMAT_64);
}

static void
_dix_sec_4096_md_0_8_error(enum spdk_dif_pi_format dif_pi_format)
{
	struct spdk_dif_ctx ctx = {};
	int rc;
	struct spdk_dif_ctx_init_ext_opts dif_opts;

	dif_opts.size = SPDK_SIZEOF(&dif_opts, dif_pi_format);
	dif_opts.dif_pi_format = dif_pi_format;
	/* Metadata size is 0. */
	rc = spdk_dif_ctx_init(&ctx, 4096, 0, true, false, SPDK_DIF_TYPE1, 0,
			       0, 0, 0, 0, 0, &dif_opts);
	CU_ASSERT(rc != 0);

	dif_opts.size = SPDK_SIZEOF(&dif_opts, dif_pi_format);
	dif_opts.dif_pi_format = dif_pi_format;
	/* Metadata size is 0. */
	rc = spdk_dif_ctx_init(&ctx, 4096, 8, true, false, SPDK_DIF_TYPE1, 0,
			       0, 0, 0, 0, 0, &dif_opts);
	CU_ASSERT(rc != 0);
}

static void
dix_sec_4096_md_0_8_error(void)
{
	_dix_sec_4096_md_0_8_error(SPDK_DIF_PI_FORMAT_32);
	_dix_sec_4096_md_0_8_error(SPDK_DIF_PI_FORMAT_64);
}

static void
dix_generate_and_verify(struct iovec *iovs, int iovcnt, struct iovec *md_iov,
			uint32_t block_size, uint32_t md_size, uint32_t num_blocks,
@@ -4117,8 +4225,10 @@ main(int argc, char **argv)
	CU_ADD_TEST(suite, dif_disable_check_test);
	CU_ADD_TEST(suite, dif_generate_and_verify_different_pi_formats_test);
	CU_ADD_TEST(suite, dif_apptag_mask_test);
	CU_ADD_TEST(suite, dif_sec_8_md_8_error_test);
	CU_ADD_TEST(suite, dif_sec_512_md_0_error_test);
	CU_ADD_TEST(suite, dif_sec_4096_md_0_error_test);
	CU_ADD_TEST(suite, dif_sec_512_md_16_error_test);
	CU_ADD_TEST(suite, dif_sec_4096_md_0_8_error_test);
	CU_ADD_TEST(suite, dif_sec_4100_md_128_error_test);
	CU_ADD_TEST(suite, dif_guard_seed_test);
	CU_ADD_TEST(suite, dif_guard_value_test);
@@ -4159,7 +4269,10 @@ main(int argc, char **argv)
	CU_ADD_TEST(suite, dif_copy_sec_4096_md_128_prchk_7_multi_iovs_complex_splits_test);
	CU_ADD_TEST(suite, dif_copy_sec_4096_md_128_inject_1_2_4_8_multi_iovs_test);
	CU_ADD_TEST(suite, dif_copy_sec_4096_md_128_inject_1_2_4_8_multi_iovs_split_test);
	CU_ADD_TEST(suite, dix_sec_0_md_8_error);
	CU_ADD_TEST(suite, dix_sec_512_md_0_error);
	CU_ADD_TEST(suite, dix_sec_512_md_16_error);
	CU_ADD_TEST(suite, dix_sec_4096_md_0_8_error);
	CU_ADD_TEST(suite, dix_sec_512_md_8_prchk_0_single_iov);
	CU_ADD_TEST(suite, dix_sec_4096_md_128_prchk_0_single_iov_test);
	CU_ADD_TEST(suite, dix_sec_512_md_8_prchk_0_1_2_4_multi_iovs);