From d88ccda689dcc643feea67cf1dccda1f49103a76 Mon Sep 17 00:00:00 2001 From: Mao Jiang Date: Fri, 26 Feb 2021 14:43:46 +0800 Subject: [PATCH] test/nvme_fabric: build nvme fabric unittest file Include cases for setting/getting nvme fabric properties. Change-Id: I4757cc38b8e1fe7d6af287b21f02539540a28de9 Signed-off-by: Mao Jiang Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/6552 Community-CI: Broadcom CI Community-CI: Mellanox Build Bot Tested-by: SPDK CI Jenkins Reviewed-by: Aleksey Marchuk Reviewed-by: Changpeng Liu --- test/unit/lib/nvme/Makefile | 2 +- test/unit/lib/nvme/nvme_fabric.c/.gitignore | 1 + test/unit/lib/nvme/nvme_fabric.c/Makefile | 38 ++++ .../lib/nvme/nvme_fabric.c/nvme_fabric_ut.c | 197 ++++++++++++++++++ test/unit/unittest.sh | 1 + 5 files changed, 238 insertions(+), 1 deletion(-) create mode 100644 test/unit/lib/nvme/nvme_fabric.c/.gitignore create mode 100644 test/unit/lib/nvme/nvme_fabric.c/Makefile create mode 100644 test/unit/lib/nvme/nvme_fabric.c/nvme_fabric_ut.c diff --git a/test/unit/lib/nvme/Makefile b/test/unit/lib/nvme/Makefile index f594bc14c..11690f326 100644 --- a/test/unit/lib/nvme/Makefile +++ b/test/unit/lib/nvme/Makefile @@ -35,7 +35,7 @@ SPDK_ROOT_DIR := $(abspath $(CURDIR)/../../../..) include $(SPDK_ROOT_DIR)/mk/spdk.common.mk DIRS-y = nvme.c nvme_ctrlr.c nvme_ctrlr_cmd.c nvme_ctrlr_ocssd_cmd.c nvme_ns.c nvme_ns_cmd.c nvme_ns_ocssd_cmd.c nvme_pcie.c nvme_poll_group.c nvme_qpair.c \ - nvme_quirks.c nvme_tcp.c nvme_transport.c nvme_io_msg.c nvme_pcie_common.c \ + nvme_quirks.c nvme_tcp.c nvme_transport.c nvme_io_msg.c nvme_pcie_common.c nvme_fabric.c \ DIRS-$(CONFIG_RDMA) += nvme_rdma.c DIRS-$(CONFIG_NVME_CUSE) += nvme_cuse.c diff --git a/test/unit/lib/nvme/nvme_fabric.c/.gitignore b/test/unit/lib/nvme/nvme_fabric.c/.gitignore new file mode 100644 index 000000000..a6f90ff50 --- /dev/null +++ b/test/unit/lib/nvme/nvme_fabric.c/.gitignore @@ -0,0 +1 @@ +nvme_fabric_ut diff --git a/test/unit/lib/nvme/nvme_fabric.c/Makefile b/test/unit/lib/nvme/nvme_fabric.c/Makefile new file mode 100644 index 000000000..1a5c76f16 --- /dev/null +++ b/test/unit/lib/nvme/nvme_fabric.c/Makefile @@ -0,0 +1,38 @@ +# +# 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)/../../../../..) + +TEST_FILE = nvme_fabric_ut.c + +include $(SPDK_ROOT_DIR)/mk/spdk.unittest.mk diff --git a/test/unit/lib/nvme/nvme_fabric.c/nvme_fabric_ut.c b/test/unit/lib/nvme/nvme_fabric.c/nvme_fabric_ut.c new file mode 100644 index 000000000..1df04c010 --- /dev/null +++ b/test/unit/lib/nvme/nvme_fabric.c/nvme_fabric_ut.c @@ -0,0 +1,197 @@ +/*- + * 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 "nvme/nvme_fabric.c" +#include "common/lib/test_env.c" + +SPDK_LOG_REGISTER_COMPONENT(nvme) + +struct spdk_nvmf_fabric_prop_set_cmd g_ut_cmd = {}; +struct spdk_nvmf_fabric_prop_get_rsp g_ut_response = {}; + +DEFINE_STUB_V(nvme_completion_poll_cb, (void *arg, const struct spdk_nvme_cpl *cpl)); + +DEFINE_STUB(nvme_wait_for_completion, int, + (struct spdk_nvme_qpair *qpair, + struct nvme_completion_poll_status *status), 0); + +DEFINE_STUB_V(spdk_nvme_ctrlr_get_default_ctrlr_opts, + (struct spdk_nvme_ctrlr_opts *opts, size_t opts_size)); + +DEFINE_STUB(nvme_transport_ctrlr_set_reg_4, int, + (struct spdk_nvme_ctrlr *ctrlr, + uint32_t offset, uint32_t value), 0); + +DEFINE_STUB_V(nvme_ctrlr_destruct, (struct spdk_nvme_ctrlr *ctrlr)); + +DEFINE_STUB(nvme_ctrlr_cmd_identify, int, + (struct spdk_nvme_ctrlr *ctrlr, uint8_t cns, uint16_t cntid, + uint32_t nsid, uint8_t csi, void *payload, size_t payload_size, + spdk_nvme_cmd_cb cb_fn, void *cb_arg), 0); + +DEFINE_STUB_V(nvme_ctrlr_connected, (struct spdk_nvme_probe_ctx *probe_ctx, + struct spdk_nvme_ctrlr *ctrlr)); +DEFINE_STUB(nvme_ctrlr_add_process, int, + (struct spdk_nvme_ctrlr *ctrlr, void *devhandle), 0); + +DEFINE_STUB(spdk_nvme_ctrlr_cmd_get_log_page, int, + (struct spdk_nvme_ctrlr *ctrlr, uint8_t log_page, + uint32_t nsid, void *payload, uint32_t payload_size, + uint64_t offset, spdk_nvme_cmd_cb cb_fn, void *cb_arg), 0); + +DEFINE_STUB(spdk_nvme_transport_id_populate_trstring, int, + (struct spdk_nvme_transport_id *trid, const char *trstring), 0); + +DEFINE_STUB(spdk_nvme_transport_available_by_name, bool, + (const char *transport_name), true); + +DEFINE_STUB(nvme_transport_ctrlr_construct, struct spdk_nvme_ctrlr *, + (const struct spdk_nvme_transport_id *trid, + const struct spdk_nvme_ctrlr_opts *opts, + void *devhandle), NULL); + +DEFINE_STUB(nvme_ctrlr_probe, int, + (const struct spdk_nvme_transport_id *trid, + struct spdk_nvme_probe_ctx *probe_ctx, void *devhandle), 0); + +DEFINE_STUB(spdk_nvme_ctrlr_cmd_io_raw, int, (struct spdk_nvme_ctrlr *ctrlr, + struct spdk_nvme_qpair *qpair, struct spdk_nvme_cmd *cmd, void *buf, + uint32_t len, spdk_nvme_cmd_cb cb_fn, void *cb_arg), 0); + +DEFINE_STUB(nvme_wait_for_completion_timeout, int, + (struct spdk_nvme_qpair *qpair, + struct nvme_completion_poll_status *status, + uint64_t timeout_in_usecs), 0); + +DEFINE_STUB(spdk_nvme_transport_id_trtype_str, const char *, + (enum spdk_nvme_transport_type trtype), NULL); + +DEFINE_STUB(spdk_nvme_transport_id_adrfam_str, const char *, + (enum spdk_nvmf_adrfam adrfam), NULL); + +DEFINE_RETURN_MOCK(spdk_nvme_ctrlr_cmd_admin_raw, int); +int +spdk_nvme_ctrlr_cmd_admin_raw(struct spdk_nvme_ctrlr *ctrlr, + struct spdk_nvme_cmd *cmd, + void *buf, uint32_t len, + spdk_nvme_cmd_cb cb_fn, void *cb_arg) +{ + struct spdk_nvmf_fabric_prop_set_cmd *cmd_tmp = (void *)cmd; + struct nvme_completion_poll_status *status = cb_arg; + struct spdk_nvmf_fabric_prop_get_rsp *response = (void *)&status->cpl; + + g_ut_cmd.opcode = cmd_tmp->opcode; + g_ut_cmd.fctype = cmd_tmp->fctype; + g_ut_cmd.ofst = cmd_tmp->ofst; + g_ut_cmd.attrib.size = cmd_tmp->attrib.size; + + if (cmd_tmp->fctype == SPDK_NVMF_FABRIC_COMMAND_PROPERTY_SET) { + g_ut_cmd.value.u64 = cmd_tmp->value.u64; + } else if (cmd_tmp->fctype == SPDK_NVMF_FABRIC_COMMAND_PROPERTY_GET) { + memcpy(&g_ut_response, response, sizeof(g_ut_response)); + } + + HANDLE_RETURN_MOCK(spdk_nvme_ctrlr_cmd_admin_raw); + return 0; +} + +static void +test_nvme_fabric_prop_set_cmd(void) +{ + int rc; + struct spdk_nvme_ctrlr ctrlr = {}; + + memset(&g_ut_cmd, 0, sizeof(g_ut_cmd)); + + rc = nvme_fabric_prop_set_cmd(&ctrlr, 1024, SPDK_NVMF_PROP_SIZE_8, 4096); + CU_ASSERT(rc == 0); + CU_ASSERT(g_ut_cmd.opcode == SPDK_NVME_OPC_FABRIC); + CU_ASSERT(g_ut_cmd.fctype == SPDK_NVMF_FABRIC_COMMAND_PROPERTY_SET); + CU_ASSERT(g_ut_cmd.ofst == 1024); + CU_ASSERT(g_ut_cmd.attrib.size == SPDK_NVMF_PROP_SIZE_8); + CU_ASSERT(g_ut_cmd.value.u64 == 4096); +} + +static void +test_nvme_fabric_prop_get_cmd(void) +{ + int rc; + uint64_t value; + struct spdk_nvme_ctrlr ctrlr = {}; + + memset(&g_ut_cmd, 0, sizeof(g_ut_cmd)); + memset(&g_ut_response, 0, sizeof(g_ut_response)); + value = 0xFFDEADBEEF; + + /* Case 1: size is SPDK_NVMF_PROP_SIZE_4 */ + rc = nvme_fabric_prop_get_cmd(&ctrlr, 1024, SPDK_NVMF_PROP_SIZE_4, &value); + CU_ASSERT(rc == 0); + CU_ASSERT(g_ut_cmd.opcode == SPDK_NVME_OPC_FABRIC); + CU_ASSERT(g_ut_cmd.fctype == SPDK_NVMF_FABRIC_COMMAND_PROPERTY_GET); + CU_ASSERT(g_ut_cmd.ofst == 1024); + CU_ASSERT(g_ut_cmd.attrib.size == SPDK_NVMF_PROP_SIZE_4); + CU_ASSERT(g_ut_response.value.u32.low == (value & 0xFFFFFFFF)); + + /* Case 2: size is SPDK_NVMF_PROP_SIZE_8 */ + memset(&g_ut_cmd, 0, sizeof(g_ut_cmd)); + memset(&g_ut_response, 0, sizeof(g_ut_response)); + + rc = nvme_fabric_prop_get_cmd(&ctrlr, 1024, SPDK_NVMF_PROP_SIZE_8, &value); + CU_ASSERT(rc == 0); + CU_ASSERT(g_ut_cmd.opcode == SPDK_NVME_OPC_FABRIC); + CU_ASSERT(g_ut_cmd.fctype == SPDK_NVMF_FABRIC_COMMAND_PROPERTY_GET); + CU_ASSERT(g_ut_cmd.ofst == 1024); + CU_ASSERT(g_ut_cmd.attrib.size == SPDK_NVMF_PROP_SIZE_8); + CU_ASSERT(g_ut_response.value.u64 == value); +} + +int main(int argc, char **argv) +{ + CU_pSuite suite = NULL; + unsigned int num_failures; + + CU_set_error_action(CUEA_ABORT); + CU_initialize_registry(); + + suite = CU_add_suite("nvme_fabric", NULL, NULL); + CU_ADD_TEST(suite, test_nvme_fabric_prop_set_cmd); + CU_ADD_TEST(suite, test_nvme_fabric_prop_get_cmd); + + 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/unittest.sh b/test/unit/unittest.sh index ac96e3623..24d78b491 100755 --- a/test/unit/unittest.sh +++ b/test/unit/unittest.sh @@ -86,6 +86,7 @@ function unittest_nvme() { $valgrind $testdir/lib/nvme/nvme_transport.c/nvme_transport_ut $valgrind $testdir/lib/nvme/nvme_io_msg.c/nvme_io_msg_ut $valgrind $testdir/lib/nvme/nvme_pcie_common.c/nvme_pcie_common_ut + $valgrind $testdir/lib/nvme/nvme_fabric.c/nvme_fabric_ut } function unittest_nvmf() {