From 2b9a331d0b1c373bffc61487806811a3f9c6eb6d Mon Sep 17 00:00:00 2001 From: Tomasz Kulasek Date: Tue, 11 Feb 2020 19:48:05 +0100 Subject: [PATCH] test/nvme: dynamic nvme cuse devices update test This test changes the range of active namespaces and checks if apropriate CUSE devices for namespaces was stopped/started properly on CUSE update. Change-Id: I9683a0ab6ff28328d4788c49a834cef31c0582cc Signed-off-by: Tomasz Kulasek Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/780 Community-CI: Broadcom CI Community-CI: Mellanox Build Bot Tested-by: SPDK CI Jenkins Reviewed-by: Changpeng Liu Reviewed-by: Tomasz Zawadzki --- test/common/skipped_build_files.txt | 1 + test/nvme/Makefile | 1 + test/nvme/cuse/.gitignore | 1 + test/nvme/cuse/Makefile | 38 ++++++ test/nvme/cuse/cuse.c | 189 ++++++++++++++++++++++++++++ test/nvme/nvme.sh | 3 + 6 files changed, 233 insertions(+) create mode 100644 test/nvme/cuse/.gitignore create mode 100644 test/nvme/cuse/Makefile create mode 100644 test/nvme/cuse/cuse.c diff --git a/test/common/skipped_build_files.txt b/test/common/skipped_build_files.txt index c9ef15ad3..e2f4017db 100644 --- a/test/common/skipped_build_files.txt +++ b/test/common/skipped_build_files.txt @@ -30,6 +30,7 @@ lib/virtio/virtio_user/vhost_user # Cuse related files, enable when ready. lib/nvme/nvme_cuse module/bdev/nvme/bdev_nvme_cuse_rpc +test/nvme/cuse/cuse # Currently we don't have this plumbed for testing, enable when ready. module/bdev/uring/bdev_uring diff --git a/test/nvme/Makefile b/test/nvme/Makefile index 37cc9944c..b2ed73a09 100644 --- a/test/nvme/Makefile +++ b/test/nvme/Makefile @@ -36,6 +36,7 @@ include $(SPDK_ROOT_DIR)/mk/spdk.common.mk DIRS-y = aer reset sgl e2edp overhead deallocated_value err_injection \ startup reserve +DIRS-$(CONFIG_NVME_CUSE) += cuse .PHONY: all clean $(DIRS-y) diff --git a/test/nvme/cuse/.gitignore b/test/nvme/cuse/.gitignore new file mode 100644 index 000000000..b13d42337 --- /dev/null +++ b/test/nvme/cuse/.gitignore @@ -0,0 +1 @@ +cuse diff --git a/test/nvme/cuse/Makefile b/test/nvme/cuse/Makefile new file mode 100644 index 000000000..c847fe13f --- /dev/null +++ b/test/nvme/cuse/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 = cuse.c + +include $(SPDK_ROOT_DIR)/mk/spdk.unittest.mk diff --git a/test/nvme/cuse/cuse.c b/test/nvme/cuse/cuse.c new file mode 100644 index 000000000..fe5c26f0c --- /dev/null +++ b/test/nvme/cuse/cuse.c @@ -0,0 +1,189 @@ + +/*- + * 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_cunit.h" + +#include "common/lib/test_env.c" +#include "nvme/nvme_cuse.c" + +DEFINE_STUB(nvme_io_msg_send, int, (struct spdk_nvme_ctrlr *ctrlr, uint32_t nsid, + spdk_nvme_io_msg_fn fn, void *arg), 0); + +DEFINE_STUB(spdk_nvme_ctrlr_alloc_cmb_io_buffer, void *, (struct spdk_nvme_ctrlr *ctrlr, + size_t size), NULL); + +DEFINE_STUB(spdk_nvme_ctrlr_cmd_admin_raw, int, (struct spdk_nvme_ctrlr *ctrlr, + struct spdk_nvme_cmd *cmd, void *buf, uint32_t len, + spdk_nvme_cmd_cb cb_fn, void *cb_arg), 0); + +DEFINE_STUB(spdk_nvme_ctrlr_get_num_ns, uint32_t, (struct spdk_nvme_ctrlr *ctrlr), 128); + +static uint32_t g_active_num_ns = 4; +static uint32_t g_active_nsid_min = 1; + +bool +spdk_nvme_ctrlr_is_active_ns(struct spdk_nvme_ctrlr *ctrlr, uint32_t nsid) +{ + return nsid >= g_active_nsid_min && nsid < g_active_num_ns + g_active_nsid_min; +} + +DEFINE_STUB(spdk_nvme_ctrlr_reset, int, (struct spdk_nvme_ctrlr *ctrlr), 0); + +DEFINE_STUB(spdk_nvme_ns_cmd_read, int, (struct spdk_nvme_ns *ns, struct spdk_nvme_qpair *qpair, + void *payload, + uint64_t lba, uint32_t lba_count, spdk_nvme_cmd_cb cb_fn, void *cb_arg, + uint32_t io_flags), 0); + +DEFINE_STUB(spdk_nvme_ns_cmd_write, int, (struct spdk_nvme_ns *ns, struct spdk_nvme_qpair *qpair, + void *payload, + uint64_t lba, uint32_t lba_count, spdk_nvme_cmd_cb cb_fn, void *cb_arg, + uint32_t io_flags), 0); + +DEFINE_STUB(spdk_nvme_ns_get_num_sectors, uint64_t, (struct spdk_nvme_ns *ns), 0); + +DEFINE_STUB(spdk_nvme_ns_get_sector_size, uint32_t, (struct spdk_nvme_ns *ns), 0); + +DEFINE_STUB_V(spdk_unaffinitize_thread, (void)); + +DEFINE_STUB(spdk_nvme_ctrlr_get_ns, struct spdk_nvme_ns *, (struct spdk_nvme_ctrlr *ctrlr, + uint32_t nsid), NULL); + +static bool +wait_for_file(char *filename, bool exists) +{ + int i; + + for (i = 0; i < 1000; i++) { + if ((access(filename, F_OK) != -1) ^ (!exists)) { + return true; + } + usleep(100); + } + return false; +} + +static void +verify_devices(struct spdk_nvme_ctrlr *ctrlr) +{ + char ctrlr_name[256]; + size_t ctrlr_name_size; + char ctrlr_dev[256], ns_dev[256 + 10]; + uint32_t nsid, num_ns; + int rv; + + ctrlr_name_size = sizeof(ctrlr_name); + rv = spdk_nvme_cuse_get_ctrlr_name(ctrlr, ctrlr_name, &ctrlr_name_size); + SPDK_CU_ASSERT_FATAL(rv == 0); + + rv = snprintf(ctrlr_dev, sizeof(ctrlr_dev), "/dev/%s", ctrlr_name); + CU_ASSERT(rv > 0); + CU_ASSERT(wait_for_file(ctrlr_dev, true)); + + num_ns = spdk_nvme_ctrlr_get_num_ns(ctrlr); + + for (nsid = 1; nsid <= num_ns; nsid++) { + snprintf(ns_dev, sizeof(ns_dev), "%sn%" PRIu32, ctrlr_dev, nsid); + if (spdk_nvme_ctrlr_is_active_ns(ctrlr, nsid)) { + CU_ASSERT(wait_for_file(ns_dev, true)); + } else { + CU_ASSERT(wait_for_file(ns_dev, false)); + } + } + + /* Next one should never exist */ + snprintf(ns_dev, sizeof(ns_dev), "%sn%" PRIu32, ctrlr_dev, nsid); + CU_ASSERT(wait_for_file(ns_dev, false)); +} + +static void +test_cuse_update(void) +{ + int rc; + struct spdk_nvme_ctrlr ctrlr = {}; + + rc = nvme_cuse_start(&ctrlr); + CU_ASSERT(rc == 0); + + g_active_num_ns = 4; + g_active_nsid_min = 1; + nvme_cuse_update(&ctrlr); + verify_devices(&ctrlr); + + g_active_num_ns = 0; + nvme_cuse_update(&ctrlr); + verify_devices(&ctrlr); + + g_active_num_ns = 4; + g_active_nsid_min = spdk_nvme_ctrlr_get_num_ns(&ctrlr) - g_active_num_ns; + nvme_cuse_update(&ctrlr); + verify_devices(&ctrlr); + + g_active_num_ns = 2; + g_active_nsid_min = 2; + nvme_cuse_update(&ctrlr); + verify_devices(&ctrlr); + + g_active_num_ns = 10; + g_active_nsid_min = 5; + nvme_cuse_update(&ctrlr); + verify_devices(&ctrlr); + + g_active_num_ns = 5; + g_active_nsid_min = 3; + nvme_cuse_update(&ctrlr); + verify_devices(&ctrlr); + + g_active_num_ns = 6; + g_active_nsid_min = 1; + nvme_cuse_update(&ctrlr); + verify_devices(&ctrlr); + + nvme_cuse_stop(&ctrlr); +} + +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_cuse", NULL, NULL); + CU_ADD_TEST(suite, test_cuse_update); + 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/nvme/nvme.sh b/test/nvme/nvme.sh index f115492e8..8e8fa94be 100755 --- a/test/nvme/nvme.sh +++ b/test/nvme/nvme.sh @@ -117,6 +117,9 @@ run_test "nvme_reserve" $testdir/reserve/reserve run_test "nvme_err_injection" $testdir/err_injection/err_injection run_test "nvme_overhead" $testdir/overhead/overhead -s 4096 -t 1 -H run_test "nvme_arbitration" $rootdir/examples/nvme/arbitration/arbitration -t 3 -i 0 +if [ $SPDK_TEST_NVME_CUSE -eq 1 ]; then + run_test "nvme_cuse" $testdir/cuse/cuse +fi if [[ $CONFIG_FIO_PLUGIN == y ]]; then run_test "nvme_fio" nvme_fio_test