From e6918187568dbd01842d8d1d2c808ce16a894239 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 21 Apr 2024 13:54:28 +0200 Subject: Adding upstream version 18.2.2. Signed-off-by: Daniel Baumann --- src/spdk/test/env/Makefile | 50 ++ src/spdk/test/env/env.sh | 27 ++ src/spdk/test/env/env_dpdk_post_init/.gitignore | 1 + src/spdk/test/env/env_dpdk_post_init/Makefile | 39 ++ .../env/env_dpdk_post_init/env_dpdk_post_init.c | 126 +++++ src/spdk/test/env/mem_callbacks/.gitignore | 1 + src/spdk/test/env/mem_callbacks/Makefile | 41 ++ src/spdk/test/env/mem_callbacks/mem_callbacks.c | 217 +++++++++ src/spdk/test/env/memory/.gitignore | 1 + src/spdk/test/env/memory/Makefile | 40 ++ src/spdk/test/env/memory/memory_ut.c | 524 +++++++++++++++++++++ src/spdk/test/env/pci/.gitignore | 1 + src/spdk/test/env/pci/Makefile | 40 ++ src/spdk/test/env/pci/pci_ut.c | 238 ++++++++++ src/spdk/test/env/vtophys/.gitignore | 1 + src/spdk/test/env/vtophys/Makefile | 39 ++ src/spdk/test/env/vtophys/vtophys.c | 196 ++++++++ 17 files changed, 1582 insertions(+) create mode 100644 src/spdk/test/env/Makefile create mode 100755 src/spdk/test/env/env.sh create mode 100644 src/spdk/test/env/env_dpdk_post_init/.gitignore create mode 100644 src/spdk/test/env/env_dpdk_post_init/Makefile create mode 100644 src/spdk/test/env/env_dpdk_post_init/env_dpdk_post_init.c create mode 100644 src/spdk/test/env/mem_callbacks/.gitignore create mode 100644 src/spdk/test/env/mem_callbacks/Makefile create mode 100644 src/spdk/test/env/mem_callbacks/mem_callbacks.c create mode 100644 src/spdk/test/env/memory/.gitignore create mode 100644 src/spdk/test/env/memory/Makefile create mode 100644 src/spdk/test/env/memory/memory_ut.c create mode 100644 src/spdk/test/env/pci/.gitignore create mode 100644 src/spdk/test/env/pci/Makefile create mode 100644 src/spdk/test/env/pci/pci_ut.c create mode 100644 src/spdk/test/env/vtophys/.gitignore create mode 100644 src/spdk/test/env/vtophys/Makefile create mode 100644 src/spdk/test/env/vtophys/vtophys.c (limited to 'src/spdk/test/env') diff --git a/src/spdk/test/env/Makefile b/src/spdk/test/env/Makefile new file mode 100644 index 000000000..33b7c903b --- /dev/null +++ b/src/spdk/test/env/Makefile @@ -0,0 +1,50 @@ +# +# 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 + +ENV_NAME := $(notdir $(CONFIG_ENV)) + +DIRS-y = mem_callbacks vtophys + +ifeq ($(ENV_NAME),env_dpdk) +DIRS-y += env_dpdk_post_init memory pci +endif + +.PHONY: all clean $(DIRS-y) + +all: $(DIRS-y) +clean: $(DIRS-y) + +include $(SPDK_ROOT_DIR)/mk/spdk.subdirs.mk diff --git a/src/spdk/test/env/env.sh b/src/spdk/test/env/env.sh new file mode 100755 index 000000000..696c14b08 --- /dev/null +++ b/src/spdk/test/env/env.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash + +testdir=$(readlink -f $(dirname $0)) +rootdir=$(readlink -f $testdir/../..) +source $rootdir/test/common/autotest_common.sh + +run_test "env_memory" $testdir/memory/memory_ut +run_test "env_vtophys" $testdir/vtophys/vtophys +run_test "env_pci" $testdir/pci/pci_ut + +argv="-c 0x1 " +if [ $(uname) = Linux ]; then + # The default base virtaddr falls into a region reserved by ASAN. + # DPDK will try to find the nearest available address space by + # trying to do mmap over and over, which will take ages to finish. + # We speed up the process by specifying an address that's not + # supposed to be reserved by ASAN. Regular SPDK applications do + # this implicitly. + argv+="--base-virtaddr=0x200000000000" +fi +run_test "env_dpdk_post_init" $testdir/env_dpdk_post_init/env_dpdk_post_init $argv + +if [ $(uname) = Linux ]; then + # This tests the --match-allocations DPDK parameter which is only + # supported on Linux + run_test "env_mem_callbacks" $testdir/mem_callbacks/mem_callbacks +fi diff --git a/src/spdk/test/env/env_dpdk_post_init/.gitignore b/src/spdk/test/env/env_dpdk_post_init/.gitignore new file mode 100644 index 000000000..39bd89884 --- /dev/null +++ b/src/spdk/test/env/env_dpdk_post_init/.gitignore @@ -0,0 +1 @@ +env_dpdk_post_init diff --git a/src/spdk/test/env/env_dpdk_post_init/Makefile b/src/spdk/test/env/env_dpdk_post_init/Makefile new file mode 100644 index 000000000..ea0ff7bb7 --- /dev/null +++ b/src/spdk/test/env/env_dpdk_post_init/Makefile @@ -0,0 +1,39 @@ +# +# 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)/../../..) + +CFLAGS += $(ENV_CFLAGS) +APP = env_dpdk_post_init + +include $(SPDK_ROOT_DIR)/mk/nvme.libtest.mk diff --git a/src/spdk/test/env/env_dpdk_post_init/env_dpdk_post_init.c b/src/spdk/test/env/env_dpdk_post_init/env_dpdk_post_init.c new file mode 100644 index 000000000..1b3897ea8 --- /dev/null +++ b/src/spdk/test/env/env_dpdk_post_init/env_dpdk_post_init.c @@ -0,0 +1,126 @@ +/*- + * 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/nvme.h" +#include "spdk/env.h" +#include "spdk/env_dpdk.h" +#include +#include + +#define MAX_DEVS 64 + +struct dev { + struct spdk_nvme_ctrlr *ctrlr; + struct spdk_nvme_ns *ns; + struct spdk_nvme_qpair *qpair; + char name[SPDK_NVMF_TRADDR_MAX_LEN + 1]; +}; + +static struct dev g_nvme_devs[MAX_DEVS]; +static int g_num_devs = 0; +static int g_failed = 0; + +static bool +probe_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid, + struct spdk_nvme_ctrlr_opts *opts) +{ + printf("Attaching to %s\n", trid->traddr); + + return true; +} + +static void +attach_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid, + struct spdk_nvme_ctrlr *ctrlr, const struct spdk_nvme_ctrlr_opts *opts) +{ + struct dev *dev; + uint32_t nsid; + + /* add to dev list */ + dev = &g_nvme_devs[g_num_devs++]; + if (g_num_devs >= MAX_DEVS) { + return; + } + + dev->ctrlr = ctrlr; + nsid = spdk_nvme_ctrlr_get_first_active_ns(ctrlr); + dev->ns = spdk_nvme_ctrlr_get_ns(ctrlr, nsid); + + dev->qpair = spdk_nvme_ctrlr_alloc_io_qpair(ctrlr, NULL, 0); + if (dev->qpair == NULL) { + g_failed = 1; + return; + } + + snprintf(dev->name, sizeof(dev->name), "%s", + trid->traddr); + + printf("Attached to %s\n", dev->name); +} + +int +main(int argc, char **argv) +{ + int ret; + int i; + + printf("Starting DPDK initialization...\n"); + ret = rte_eal_init(argc, argv); + if (ret < 0) { + fprintf(stderr, "Failed to initialize DPDK\n"); + return -1; + } + + printf("Starting SPDK post initialization...\n"); + ret = spdk_env_dpdk_post_init(false); + if (ret < 0) { + fprintf(stderr, "Failed to initialize SPDK\n"); + return -1; + } + + printf("SPDK NVMe probe\n"); + if (spdk_nvme_probe(NULL, NULL, probe_cb, attach_cb, NULL) != 0) { + fprintf(stderr, "spdk_nvme_probe() failed\n"); + return 1; + } + + printf("Cleaning up...\n"); + for (i = 0; i < g_num_devs; i++) { + struct dev *dev = &g_nvme_devs[i]; + spdk_nvme_detach(dev->ctrlr); + } + + return g_failed; +} diff --git a/src/spdk/test/env/mem_callbacks/.gitignore b/src/spdk/test/env/mem_callbacks/.gitignore new file mode 100644 index 000000000..aff8f922b --- /dev/null +++ b/src/spdk/test/env/mem_callbacks/.gitignore @@ -0,0 +1 @@ +mem_callbacks diff --git a/src/spdk/test/env/mem_callbacks/Makefile b/src/spdk/test/env/mem_callbacks/Makefile new file mode 100644 index 000000000..f31a765ab --- /dev/null +++ b/src/spdk/test/env/mem_callbacks/Makefile @@ -0,0 +1,41 @@ +# +# 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 + +UNIT_TEST_LINK_ENV = 1 +TEST_FILE = mem_callbacks.c +CFLAGS += $(ENV_CFLAGS) + +include $(SPDK_ROOT_DIR)/mk/spdk.unittest.mk diff --git a/src/spdk/test/env/mem_callbacks/mem_callbacks.c b/src/spdk/test/env/mem_callbacks/mem_callbacks.c new file mode 100644 index 000000000..165ddb3d8 --- /dev/null +++ b/src/spdk/test/env/mem_callbacks/mem_callbacks.c @@ -0,0 +1,217 @@ +/*- + * 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/util.h" +#include "spdk/queue.h" +#include "spdk_cunit.h" + +#include +#include +#include +#include +#include + +struct mem_allocation { + uintptr_t vaddr; + size_t len; + TAILQ_ENTRY(mem_allocation) link; +}; + +static TAILQ_HEAD(, mem_allocation) g_mem_allocations = TAILQ_HEAD_INITIALIZER(g_mem_allocations); + +static void +memory_hotplug_cb(enum rte_mem_event event_type, const void *addr, size_t len, void *arg) +{ + struct mem_allocation *allocation; + + if (event_type == RTE_MEM_EVENT_ALLOC) { + allocation = calloc(1, sizeof(*allocation)); + SPDK_CU_ASSERT_FATAL(allocation != NULL); + + printf("register %p %ju\n", addr, len); + allocation->vaddr = (uintptr_t)addr; + allocation->len = len; + TAILQ_INSERT_TAIL(&g_mem_allocations, allocation, link); + } else if (event_type == RTE_MEM_EVENT_FREE) { + TAILQ_FOREACH(allocation, &g_mem_allocations, link) { + if (allocation->vaddr == (uintptr_t)addr && allocation->len == len) { + break; + } + } + printf("unregister %p %ju %s\n", addr, len, allocation == NULL ? "FAILED" : "PASSED"); + SPDK_CU_ASSERT_FATAL(allocation != NULL); + TAILQ_REMOVE(&g_mem_allocations, allocation, link); + free(allocation); + } +} + +static int +memory_iter_cb(const struct rte_memseg_list *msl, + const struct rte_memseg *ms, size_t len, void *arg) +{ + struct mem_allocation *allocation; + + allocation = calloc(1, sizeof(*allocation)); + SPDK_CU_ASSERT_FATAL(allocation != NULL); + + printf("register %p %ju\n", ms->addr, len); + allocation->vaddr = (uintptr_t)ms->addr; + allocation->len = len; + TAILQ_INSERT_TAIL(&g_mem_allocations, allocation, link); + + return 0; +} + +static void +verify_buffer(void *_buf, size_t len) +{ + uintptr_t buf = (uintptr_t)_buf; + struct mem_allocation *allocation; + + SPDK_CU_ASSERT_FATAL(_buf != NULL); + printf("buf %p len %ju ", _buf, len); + TAILQ_FOREACH(allocation, &g_mem_allocations, link) { + if (buf >= allocation->vaddr && + buf + len <= allocation->vaddr + allocation->len) { + break; + } + } + printf("%s\n", allocation == NULL ? "FAILED" : "PASSED"); + CU_ASSERT(allocation != NULL); +} + +static void +test(void) +{ + void *buf1, *buf2, *buf3, *buf4; + size_t len1, len2, len3, len4; + + printf("\n"); + + rte_mem_event_callback_register("test", memory_hotplug_cb, NULL); + rte_memseg_contig_walk(memory_iter_cb, NULL); + + /* First allocate a 3MB buffer. This will allocate a 4MB hugepage + * region, with the 3MB buffer allocated from the end of it. + */ + len1 = 3 * 1024 * 1024; + printf("malloc %ju\n", len1); + buf1 = rte_malloc(NULL, len1, 0); + verify_buffer(buf1, len1); + + /* Now allocate a very small buffer. This will get allocated from + * the previous 4MB hugepage allocation, just before the 3MB buffer + * allocated just above. + */ + len2 = 64; + printf("malloc %ju\n", len2); + buf2 = rte_malloc(NULL, len2, 0); + verify_buffer(buf2, len2); + + /* Allocate a 4MB buffer. This should trigger a new hugepage allocation + * just for thie 4MB buffer. + */ + len3 = 4 * 1024 * 1024; + printf("malloc %ju\n", len3); + buf3 = rte_malloc(NULL, len3, 0); + verify_buffer(buf3, len3); + + /* Free the three buffers. Specifically free buf1 first. buf2 was + * allocated from the same huge page allocation as buf1 - so we want + * to make sure that DPDK doesn't try to free part of the first huge + * page allocation - it needs to wait until buf2 is also freed so it + * can free all of it. + */ + printf("free %p %ju\n", buf1, len1); + rte_free(buf1); + printf("free %p %ju\n", buf2, len2); + rte_free(buf2); + printf("free %p %ju\n", buf3, len3); + rte_free(buf3); + + /* Do a single 8MB hugepage allocation and then free it. This covers + * the more simple case. + */ + len4 = 8 * 1024 * 1024; + printf("malloc %ju\n", len4); + buf4 = rte_malloc(NULL, len4, 0); + verify_buffer(buf4, len4); + + printf("free %p %ju\n", buf4, len4); + rte_free(buf4); +} + +int +main(int argc, char **argv) +{ + CU_pSuite suite = NULL; + unsigned int num_failures; + char *dpdk_arg[] = { + "mem_callbacks", "-c 0x1", + "--base-virtaddr=0x200000000000", + "--match-allocations", + }; + int rc; + + rc = rte_eal_init(SPDK_COUNTOF(dpdk_arg), dpdk_arg); + if (rc < 0) { + printf("Err: Unable to initialize DPDK\n"); + return 1; + } + + if (CU_initialize_registry() != CUE_SUCCESS) { + return CU_get_error(); + } + + suite = CU_add_suite("memory", NULL, NULL); + if (suite == NULL) { + CU_cleanup_registry(); + return CU_get_error(); + } + + if ( + CU_add_test(suite, "test", test) == 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; +} diff --git a/src/spdk/test/env/memory/.gitignore b/src/spdk/test/env/memory/.gitignore new file mode 100644 index 000000000..7bef3dc03 --- /dev/null +++ b/src/spdk/test/env/memory/.gitignore @@ -0,0 +1 @@ +memory_ut diff --git a/src/spdk/test/env/memory/Makefile b/src/spdk/test/env/memory/Makefile new file mode 100644 index 000000000..623a81cb9 --- /dev/null +++ b/src/spdk/test/env/memory/Makefile @@ -0,0 +1,40 @@ +# +# 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)/../../..) + +CFLAGS += $(ENV_CFLAGS) +CFLAGS += -I$(SPDK_ROOT_DIR)/test/lib +TEST_FILE = memory_ut.c + +include $(SPDK_ROOT_DIR)/mk/spdk.unittest.mk diff --git a/src/spdk/test/env/memory/memory_ut.c b/src/spdk/test/env/memory/memory_ut.c new file mode 100644 index 000000000..bdf6a76bf --- /dev/null +++ b/src/spdk/test/env/memory/memory_ut.c @@ -0,0 +1,524 @@ +/*- + * 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 "env_dpdk/memory.c" + +#define UNIT_TEST_NO_VTOPHYS +#define UNIT_TEST_NO_PCI_ADDR +#include "common/lib/test_env.c" +#include "spdk_cunit.h" + +#include "spdk/bit_array.h" + +#define PAGE_ARRAY_SIZE (100) +static struct spdk_bit_array *g_page_array; +static void *g_vaddr_to_fail = (void *)UINT64_MAX; + +DEFINE_STUB(rte_memseg_contig_walk, int, (rte_memseg_contig_walk_t func, void *arg), 0); +DEFINE_STUB(rte_mem_virt2memseg, struct rte_memseg *, + (const void *virt, const struct rte_memseg_list *msl), NULL); +DEFINE_STUB(spdk_env_dpdk_external_init, bool, (void), true); +DEFINE_STUB(rte_mem_event_callback_register, int, + (const char *name, rte_mem_event_callback_t clb, void *arg), 0); +DEFINE_STUB(rte_mem_virt2iova, rte_iova_t, (const void *virtaddr), 0); + +static int +test_mem_map_notify(void *cb_ctx, struct spdk_mem_map *map, + enum spdk_mem_map_notify_action action, + void *vaddr, size_t len) +{ + uint32_t i, end; + + SPDK_CU_ASSERT_FATAL(((uintptr_t)vaddr & MASK_2MB) == 0); + SPDK_CU_ASSERT_FATAL((len & MASK_2MB) == 0); + + /* + * This is a test requirement - the bit array we use to verify + * pages are valid is only so large. + */ + SPDK_CU_ASSERT_FATAL((uintptr_t)vaddr < (VALUE_2MB * PAGE_ARRAY_SIZE)); + + i = (uintptr_t)vaddr >> SHIFT_2MB; + end = i + (len >> SHIFT_2MB); + for (; i < end; i++) { + switch (action) { + case SPDK_MEM_MAP_NOTIFY_REGISTER: + /* This page should not already be registered */ + SPDK_CU_ASSERT_FATAL(spdk_bit_array_get(g_page_array, i) == false); + SPDK_CU_ASSERT_FATAL(spdk_bit_array_set(g_page_array, i) == 0); + break; + case SPDK_MEM_MAP_NOTIFY_UNREGISTER: + SPDK_CU_ASSERT_FATAL(spdk_bit_array_get(g_page_array, i) == true); + spdk_bit_array_clear(g_page_array, i); + break; + default: + SPDK_UNREACHABLE(); + } + } + + return 0; +} + +static int +test_mem_map_notify_fail(void *cb_ctx, struct spdk_mem_map *map, + enum spdk_mem_map_notify_action action, void *vaddr, size_t size) +{ + struct spdk_mem_map *reg_map = cb_ctx; + + switch (action) { + case SPDK_MEM_MAP_NOTIFY_REGISTER: + if (vaddr == g_vaddr_to_fail) { + /* Test the error handling. */ + return -1; + } + break; + case SPDK_MEM_MAP_NOTIFY_UNREGISTER: + /* Clear the same region in the other mem_map to be able to + * verify that there was no memory left still registered after + * the mem_map creation failure. + */ + spdk_mem_map_clear_translation(reg_map, (uint64_t)vaddr, size); + break; + } + + return 0; +} + +static int +test_mem_map_notify_checklen(void *cb_ctx, struct spdk_mem_map *map, + enum spdk_mem_map_notify_action action, void *vaddr, size_t size) +{ + size_t *len_arr = cb_ctx; + + /* + * This is a test requirement - the len array we use to verify + * pages are valid is only so large. + */ + SPDK_CU_ASSERT_FATAL((uintptr_t)vaddr < (VALUE_2MB * PAGE_ARRAY_SIZE)); + + switch (action) { + case SPDK_MEM_MAP_NOTIFY_REGISTER: + assert(size == len_arr[(uintptr_t)vaddr / VALUE_2MB]); + break; + case SPDK_MEM_MAP_NOTIFY_UNREGISTER: + CU_ASSERT(size == len_arr[(uintptr_t)vaddr / VALUE_2MB]); + break; + } + + return 0; +} + +static int +test_check_regions_contiguous(uint64_t addr1, uint64_t addr2) +{ + return addr1 == addr2; +} + +const struct spdk_mem_map_ops test_mem_map_ops = { + .notify_cb = test_mem_map_notify, + .are_contiguous = test_check_regions_contiguous +}; + +const struct spdk_mem_map_ops test_mem_map_ops_no_contig = { + .notify_cb = test_mem_map_notify, + .are_contiguous = NULL +}; + +struct spdk_mem_map_ops test_map_ops_notify_fail = { + .notify_cb = test_mem_map_notify_fail, + .are_contiguous = NULL +}; + +struct spdk_mem_map_ops test_map_ops_notify_checklen = { + .notify_cb = test_mem_map_notify_checklen, + .are_contiguous = NULL +}; + +static void +test_mem_map_alloc_free(void) +{ + struct spdk_mem_map *map, *failed_map; + uint64_t default_translation = 0xDEADBEEF0BADF00D; + int i; + + map = spdk_mem_map_alloc(default_translation, &test_mem_map_ops, NULL); + SPDK_CU_ASSERT_FATAL(map != NULL); + spdk_mem_map_free(&map); + CU_ASSERT(map == NULL); + + map = spdk_mem_map_alloc(default_translation, NULL, NULL); + SPDK_CU_ASSERT_FATAL(map != NULL); + + /* Register some memory for the initial memory walk in + * spdk_mem_map_alloc(). We'll fail registering the last region + * and will check if the mem_map cleaned up all its previously + * initialized translations. + */ + for (i = 0; i < 5; i++) { + spdk_mem_register((void *)(uintptr_t)(2 * i * VALUE_2MB), VALUE_2MB); + } + + /* The last region */ + g_vaddr_to_fail = (void *)(8 * VALUE_2MB); + failed_map = spdk_mem_map_alloc(default_translation, &test_map_ops_notify_fail, map); + CU_ASSERT(failed_map == NULL); + + for (i = 0; i < 4; i++) { + uint64_t reg, size = VALUE_2MB; + + reg = spdk_mem_map_translate(map, 2 * i * VALUE_2MB, &size); + /* check if `failed_map` didn't leave any translations behind */ + CU_ASSERT(reg == default_translation); + } + + for (i = 0; i < 5; i++) { + spdk_mem_unregister((void *)(uintptr_t)(2 * i * VALUE_2MB), VALUE_2MB); + } + + spdk_mem_map_free(&map); + CU_ASSERT(map == NULL); +} + +static void +test_mem_map_translation(void) +{ + struct spdk_mem_map *map; + uint64_t default_translation = 0xDEADBEEF0BADF00D; + uint64_t addr; + uint64_t mapping_length; + int rc; + + map = spdk_mem_map_alloc(default_translation, &test_mem_map_ops, NULL); + SPDK_CU_ASSERT_FATAL(map != NULL); + + /* Try to get translation for address with no translation */ + addr = spdk_mem_map_translate(map, 10, NULL); + CU_ASSERT(addr == default_translation); + + /* Set translation for region of non-2MB multiple size */ + rc = spdk_mem_map_set_translation(map, VALUE_2MB, 1234, VALUE_2MB); + CU_ASSERT(rc == -EINVAL); + + /* Set translation for vaddr that isn't 2MB aligned */ + rc = spdk_mem_map_set_translation(map, 1234, VALUE_2MB, VALUE_2MB); + CU_ASSERT(rc == -EINVAL); + + /* Set translation for one 2MB page */ + rc = spdk_mem_map_set_translation(map, VALUE_2MB, VALUE_2MB, VALUE_2MB); + CU_ASSERT(rc == 0); + + /* Set translation for region that overlaps the previous translation */ + rc = spdk_mem_map_set_translation(map, 0, 3 * VALUE_2MB, 0); + CU_ASSERT(rc == 0); + + /* Make sure we indicate that the three regions are contiguous */ + mapping_length = VALUE_2MB * 3; + addr = spdk_mem_map_translate(map, 0, &mapping_length); + CU_ASSERT(addr == 0); + CU_ASSERT(mapping_length == VALUE_2MB * 3); + + /* Translate an unaligned address */ + mapping_length = VALUE_2MB * 3; + addr = spdk_mem_map_translate(map, VALUE_4KB, &mapping_length); + CU_ASSERT(addr == 0); + CU_ASSERT(mapping_length == VALUE_2MB * 3 - VALUE_4KB); + + /* Clear translation for the middle page of the larger region. */ + rc = spdk_mem_map_clear_translation(map, VALUE_2MB, VALUE_2MB); + CU_ASSERT(rc == 0); + + /* Get translation for first page */ + addr = spdk_mem_map_translate(map, 0, NULL); + CU_ASSERT(addr == 0); + + /* Make sure we indicate that the three regions are no longer contiguous */ + mapping_length = VALUE_2MB * 3; + addr = spdk_mem_map_translate(map, 0, &mapping_length); + CU_ASSERT(addr == 0); + CU_ASSERT(mapping_length == VALUE_2MB); + + /* Get translation for an unallocated block. Make sure size is 0 */ + mapping_length = VALUE_2MB * 3; + addr = spdk_mem_map_translate(map, VALUE_2MB, &mapping_length); + CU_ASSERT(addr == default_translation); + CU_ASSERT(mapping_length == VALUE_2MB); + + /* Verify translation for 2nd page is the default */ + addr = spdk_mem_map_translate(map, VALUE_2MB, NULL); + CU_ASSERT(addr == default_translation); + + /* Get translation for third page */ + addr = spdk_mem_map_translate(map, 2 * VALUE_2MB, NULL); + /* + * Note that addr should be 0, not 4MB. When we set the + * translation above, we said the whole 6MB region + * should translate to 0. + */ + CU_ASSERT(addr == 0); + + /* Translate only a subset of a 2MB page */ + mapping_length = 543; + addr = spdk_mem_map_translate(map, 0, &mapping_length); + CU_ASSERT(addr == 0); + CU_ASSERT(mapping_length == 543); + + /* Translate another subset of a 2MB page */ + mapping_length = 543; + addr = spdk_mem_map_translate(map, VALUE_4KB, &mapping_length); + CU_ASSERT(addr == 0); + CU_ASSERT(mapping_length == 543); + + /* Try to translate an unaligned region that is only partially registered */ + mapping_length = 543; + addr = spdk_mem_map_translate(map, 3 * VALUE_2MB - 196, &mapping_length); + CU_ASSERT(addr == 0); + CU_ASSERT(mapping_length == 196); + + /* Clear translation for the first page */ + rc = spdk_mem_map_clear_translation(map, 0, VALUE_2MB); + CU_ASSERT(rc == 0); + + /* Get translation for the first page */ + addr = spdk_mem_map_translate(map, 0, NULL); + CU_ASSERT(addr == default_translation); + + /* Clear translation for the third page */ + rc = spdk_mem_map_clear_translation(map, 2 * VALUE_2MB, VALUE_2MB); + CU_ASSERT(rc == 0); + + /* Get translation for the third page */ + addr = spdk_mem_map_translate(map, 2 * VALUE_2MB, NULL); + CU_ASSERT(addr == default_translation); + + /* Set translation for the last valid 2MB region */ + rc = spdk_mem_map_set_translation(map, 0xffffffe00000ULL, VALUE_2MB, 0x1234); + CU_ASSERT(rc == 0); + + /* Verify translation for last valid 2MB region */ + addr = spdk_mem_map_translate(map, 0xffffffe00000ULL, NULL); + CU_ASSERT(addr == 0x1234); + + /* Attempt to set translation for the first invalid address */ + rc = spdk_mem_map_set_translation(map, 0x1000000000000ULL, VALUE_2MB, 0x5678); + CU_ASSERT(rc == -EINVAL); + + /* Attempt to set translation starting at a valid address but exceeding the valid range */ + rc = spdk_mem_map_set_translation(map, 0xffffffe00000ULL, VALUE_2MB * 2, 0x123123); + CU_ASSERT(rc != 0); + + spdk_mem_map_free(&map); + CU_ASSERT(map == NULL); + + /* Allocate a map without a contiguous region checker */ + map = spdk_mem_map_alloc(default_translation, &test_mem_map_ops_no_contig, NULL); + SPDK_CU_ASSERT_FATAL(map != NULL); + + /* map three contiguous regions */ + rc = spdk_mem_map_set_translation(map, 0, 3 * VALUE_2MB, 0); + CU_ASSERT(rc == 0); + + /* Since we can't check their contiguity, make sure we only return the size of one page */ + mapping_length = VALUE_2MB * 3; + addr = spdk_mem_map_translate(map, 0, &mapping_length); + CU_ASSERT(addr == 0); + CU_ASSERT(mapping_length == VALUE_2MB); + + /* Translate only a subset of a 2MB page */ + mapping_length = 543; + addr = spdk_mem_map_translate(map, 0, &mapping_length); + CU_ASSERT(addr == 0); + CU_ASSERT(mapping_length == 543); + + /* Clear the translation */ + rc = spdk_mem_map_clear_translation(map, 0, VALUE_2MB * 3); + CU_ASSERT(rc == 0); + + spdk_mem_map_free(&map); + CU_ASSERT(map == NULL); +} + +static void +test_mem_map_registration(void) +{ + int rc; + struct spdk_mem_map *map; + uint64_t default_translation = 0xDEADBEEF0BADF00D; + + map = spdk_mem_map_alloc(default_translation, &test_mem_map_ops, NULL); + SPDK_CU_ASSERT_FATAL(map != NULL); + + /* Unregister memory region that wasn't previously registered */ + rc = spdk_mem_unregister((void *)VALUE_2MB, VALUE_2MB); + CU_ASSERT(rc == -EINVAL); + + /* Register non-2MB multiple size */ + rc = spdk_mem_register((void *)VALUE_2MB, 1234); + CU_ASSERT(rc == -EINVAL); + + /* Register region that isn't 2MB aligned */ + rc = spdk_mem_register((void *)1234, VALUE_2MB); + CU_ASSERT(rc == -EINVAL); + + /* Register one 2MB page */ + rc = spdk_mem_register((void *)VALUE_2MB, VALUE_2MB); + CU_ASSERT(rc == 0); + + /* Register an overlapping address range */ + rc = spdk_mem_register((void *)0, 3 * VALUE_2MB); + CU_ASSERT(rc == -EBUSY); + + /* Unregister a 2MB page */ + rc = spdk_mem_unregister((void *)VALUE_2MB, VALUE_2MB); + CU_ASSERT(rc == 0); + + /* Register non overlapping address range */ + rc = spdk_mem_register((void *)0, 3 * VALUE_2MB); + CU_ASSERT(rc == 0); + + /* Unregister the middle page of the larger region. */ + rc = spdk_mem_unregister((void *)VALUE_2MB, VALUE_2MB); + CU_ASSERT(rc == -ERANGE); + + /* Unregister the first page */ + rc = spdk_mem_unregister((void *)0, VALUE_2MB); + CU_ASSERT(rc == -ERANGE); + + /* Unregister the third page */ + rc = spdk_mem_unregister((void *)(2 * VALUE_2MB), VALUE_2MB); + CU_ASSERT(rc == -ERANGE); + + /* Unregister the entire address range */ + rc = spdk_mem_unregister((void *)0, 3 * VALUE_2MB); + CU_ASSERT(rc == 0); + + spdk_mem_map_free(&map); + CU_ASSERT(map == NULL); +} + +static void +test_mem_map_registration_adjacent(void) +{ + struct spdk_mem_map *map, *newmap; + uint64_t default_translation = 0xDEADBEEF0BADF00D; + uintptr_t vaddr; + unsigned i; + size_t notify_len[PAGE_ARRAY_SIZE] = {0}; + size_t chunk_len[] = { 2, 1, 3, 2, 1, 1 }; + + map = spdk_mem_map_alloc(default_translation, + &test_map_ops_notify_checklen, notify_len); + SPDK_CU_ASSERT_FATAL(map != NULL); + + vaddr = 0; + for (i = 0; i < SPDK_COUNTOF(chunk_len); i++) { + notify_len[vaddr / VALUE_2MB] = chunk_len[i] * VALUE_2MB; + spdk_mem_register((void *)vaddr, notify_len[vaddr / VALUE_2MB]); + vaddr += notify_len[vaddr / VALUE_2MB]; + } + + /* Verify the memory is translated in the same chunks it was registered */ + newmap = spdk_mem_map_alloc(default_translation, + &test_map_ops_notify_checklen, notify_len); + SPDK_CU_ASSERT_FATAL(newmap != NULL); + spdk_mem_map_free(&newmap); + CU_ASSERT(newmap == NULL); + + vaddr = 0; + for (i = 0; i < SPDK_COUNTOF(chunk_len); i++) { + notify_len[vaddr / VALUE_2MB] = chunk_len[i] * VALUE_2MB; + spdk_mem_unregister((void *)vaddr, notify_len[vaddr / VALUE_2MB]); + vaddr += notify_len[vaddr / VALUE_2MB]; + } + + /* Register all chunks again just to unregister them again, but this + * time with only a single unregister() call. + */ + vaddr = 0; + for (i = 0; i < SPDK_COUNTOF(chunk_len); i++) { + notify_len[vaddr / VALUE_2MB] = chunk_len[i] * VALUE_2MB; + spdk_mem_register((void *)vaddr, notify_len[vaddr / VALUE_2MB]); + vaddr += notify_len[vaddr / VALUE_2MB]; + } + spdk_mem_unregister(0, vaddr); + + spdk_mem_map_free(&map); + CU_ASSERT(map == NULL); +} + +int +main(int argc, char **argv) +{ + CU_pSuite suite = NULL; + unsigned int num_failures; + + /* + * These tests can use PAGE_ARRAY_SIZE 2MB pages of memory. + * Note that the tests just verify addresses - this memory + * is not actually allocated. + */ + g_page_array = spdk_bit_array_create(PAGE_ARRAY_SIZE); + + /* Initialize the memory map */ + if (mem_map_init(false) < 0) { + return CUE_NOMEMORY; + } + + if (CU_initialize_registry() != CUE_SUCCESS) { + return CU_get_error(); + } + + suite = CU_add_suite("memory", NULL, NULL); + if (suite == NULL) { + CU_cleanup_registry(); + return CU_get_error(); + } + + if ( + CU_add_test(suite, "alloc and free memory map", test_mem_map_alloc_free) == NULL || + CU_add_test(suite, "mem map translation", test_mem_map_translation) == NULL || + CU_add_test(suite, "mem map registration", test_mem_map_registration) == NULL || + CU_add_test(suite, "mem map adjacent registrations", test_mem_map_registration_adjacent) == 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(); + + spdk_bit_array_free(&g_page_array); + + return num_failures; +} diff --git a/src/spdk/test/env/pci/.gitignore b/src/spdk/test/env/pci/.gitignore new file mode 100644 index 000000000..11d1c65ba --- /dev/null +++ b/src/spdk/test/env/pci/.gitignore @@ -0,0 +1 @@ +pci_ut diff --git a/src/spdk/test/env/pci/Makefile b/src/spdk/test/env/pci/Makefile new file mode 100644 index 000000000..85d03ec34 --- /dev/null +++ b/src/spdk/test/env/pci/Makefile @@ -0,0 +1,40 @@ +# +# 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)/../../..) + +UNIT_TEST_LINK_ENV = 1 +CFLAGS += $(ENV_CFLAGS) +TEST_FILE = pci_ut.c + +include $(SPDK_ROOT_DIR)/mk/spdk.unittest.mk diff --git a/src/spdk/test/env/pci/pci_ut.c b/src/spdk/test/env/pci/pci_ut.c new file mode 100644 index 000000000..66d36b980 --- /dev/null +++ b/src/spdk/test/env/pci/pci_ut.c @@ -0,0 +1,238 @@ +/*- + * 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 "CUnit/Basic.h" +#include "spdk_internal/mock.h" + +#include "env_dpdk/pci.c" + +static void +pci_claim_test(struct spdk_pci_device *dev) +{ + int rc = 0; + pid_t childPid; + int status, ret; + + rc = spdk_pci_device_claim(dev); + CU_ASSERT(rc >= 0); + + childPid = fork(); + CU_ASSERT(childPid >= 0); + if (childPid == 0) { + ret = spdk_pci_device_claim(dev); + CU_ASSERT(ret == -1); + exit(0); + } else { + waitpid(childPid, &status, 0); + } +} + +static struct spdk_pci_driver ut_pci_driver; + +struct ut_pci_dev { + struct spdk_pci_device pci; + char config[16]; + char bar[16]; + bool attached; +}; + +static int +ut_map_bar(struct spdk_pci_device *dev, uint32_t bar, + void **mapped_addr, uint64_t *phys_addr, uint64_t *size) +{ + struct ut_pci_dev *ut_dev = (struct ut_pci_dev *)dev; + + /* just one bar */ + if (bar > 0) { + return -1; + } + + *mapped_addr = ut_dev->bar; + *phys_addr = 0; + *size = sizeof(ut_dev->bar); + return 0; +} + +static int +ut_unmap_bar(struct spdk_pci_device *device, uint32_t bar, void *addr) +{ + return 0; +} + +static int +ut_cfg_read(struct spdk_pci_device *dev, void *value, uint32_t len, uint32_t offset) +{ + struct ut_pci_dev *ut_dev = (struct ut_pci_dev *)dev; + + if (len + offset >= sizeof(ut_dev->config)) { + return -1; + } + + memcpy(value, (void *)((uintptr_t)ut_dev->config + offset), len); + return 0; +} + +static int ut_cfg_write(struct spdk_pci_device *dev, void *value, uint32_t len, uint32_t offset) +{ + struct ut_pci_dev *ut_dev = (struct ut_pci_dev *)dev; + + if (len + offset >= sizeof(ut_dev->config)) { + return -1; + } + + memcpy((void *)((uintptr_t)ut_dev->config + offset), value, len); + return 0; +} + + +static int +ut_enum_cb(void *ctx, struct spdk_pci_device *dev) +{ + struct ut_pci_dev *ut_dev = (struct ut_pci_dev *)dev; + + ut_dev->attached = true; + return 0; +} + +static void +pci_hook_test(void) +{ + struct ut_pci_dev ut_dev = {}; + uint32_t value_32; + void *bar0_vaddr; + uint64_t bar0_paddr, bar0_size; + int rc; + + ut_dev.pci.type = "custom"; + ut_dev.pci.id.vendor_id = 0x4; + ut_dev.pci.id.device_id = 0x8; + + /* Use add parse for initilization */ + spdk_pci_addr_parse(&ut_dev.pci.addr, "10000:00:01.0"); + CU_ASSERT(ut_dev.pci.addr.domain == 0x10000); + CU_ASSERT(ut_dev.pci.addr.bus == 0x0); + CU_ASSERT(ut_dev.pci.addr.dev == 0x1); + CU_ASSERT(ut_dev.pci.addr.func == 0x0); + + ut_dev.pci.map_bar = ut_map_bar; + ut_dev.pci.unmap_bar = ut_unmap_bar; + ut_dev.pci.cfg_read = ut_cfg_read; + ut_dev.pci.cfg_write = ut_cfg_write; + + /* hook the device into the PCI layer */ + spdk_pci_hook_device(&ut_pci_driver, &ut_dev.pci); + + /* try to attach a device with the matching driver and bdf */ + rc = spdk_pci_device_attach(&ut_pci_driver, ut_enum_cb, NULL, &ut_dev.pci.addr); + CU_ASSERT(rc == 0); + CU_ASSERT(ut_dev.pci.internal.attached); + CU_ASSERT(ut_dev.attached); + + /* check PCI config writes and reads */ + value_32 = 0xDEADBEEF; + rc = spdk_pci_device_cfg_write32(&ut_dev.pci, value_32, 0); + CU_ASSERT(rc == 0); + + value_32 = 0x0BADF00D; + rc = spdk_pci_device_cfg_write32(&ut_dev.pci, value_32, 4); + CU_ASSERT(rc == 0); + + rc = spdk_pci_device_cfg_read32(&ut_dev.pci, &value_32, 0); + CU_ASSERT(rc == 0); + CU_ASSERT(value_32 == 0xDEADBEEF); + CU_ASSERT(memcmp(&value_32, &ut_dev.config[0], 4) == 0); + + rc = spdk_pci_device_cfg_read32(&ut_dev.pci, &value_32, 4); + CU_ASSERT(rc == 0); + CU_ASSERT(value_32 == 0x0BADF00D); + CU_ASSERT(memcmp(&value_32, &ut_dev.config[4], 4) == 0); + + /* out-of-bounds write */ + rc = spdk_pci_device_cfg_read32(&ut_dev.pci, &value_32, sizeof(ut_dev.config)); + CU_ASSERT(rc != 0); + + /* map a bar */ + rc = spdk_pci_device_map_bar(&ut_dev.pci, 0, &bar0_vaddr, &bar0_paddr, &bar0_size); + CU_ASSERT(rc == 0); + CU_ASSERT(bar0_vaddr == ut_dev.bar); + CU_ASSERT(bar0_size == sizeof(ut_dev.bar)); + spdk_pci_device_unmap_bar(&ut_dev.pci, 0, bar0_vaddr); + + /* map an inaccessible bar */ + rc = spdk_pci_device_map_bar(&ut_dev.pci, 1, &bar0_vaddr, &bar0_paddr, &bar0_size); + CU_ASSERT(rc != 0); + + /* test spdk_pci_device_claim() */ + pci_claim_test(&ut_dev.pci); + + spdk_pci_device_detach(&ut_dev.pci); + CU_ASSERT(!ut_dev.pci.internal.attached); + + /* unhook the device */ + spdk_pci_unhook_device(&ut_dev.pci); + + /* try to attach the same device again */ + rc = spdk_pci_device_attach(&ut_pci_driver, ut_enum_cb, NULL, &ut_dev.pci.addr); + CU_ASSERT(rc != 0); +} + +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("pci", NULL, NULL); + if (suite == NULL) { + CU_cleanup_registry(); + return CU_get_error(); + } + + if ( + CU_add_test(suite, "pci_hook", pci_hook_test) == 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; +} diff --git a/src/spdk/test/env/vtophys/.gitignore b/src/spdk/test/env/vtophys/.gitignore new file mode 100644 index 000000000..a03b46cc9 --- /dev/null +++ b/src/spdk/test/env/vtophys/.gitignore @@ -0,0 +1 @@ +vtophys diff --git a/src/spdk/test/env/vtophys/Makefile b/src/spdk/test/env/vtophys/Makefile new file mode 100644 index 000000000..68c4632a3 --- /dev/null +++ b/src/spdk/test/env/vtophys/Makefile @@ -0,0 +1,39 @@ +# +# 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)/../../..) + +UNIT_TEST_LINK_ENV = 1 +TEST_FILE = vtophys.c + +include $(SPDK_ROOT_DIR)/mk/spdk.unittest.mk diff --git a/src/spdk/test/env/vtophys/vtophys.c b/src/spdk/test/env/vtophys/vtophys.c new file mode 100644 index 000000000..e0d4d7992 --- /dev/null +++ b/src/spdk/test/env/vtophys/vtophys.c @@ -0,0 +1,196 @@ +/*- + * 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/config.h" +#include "spdk/env.h" +#include "spdk/util.h" + +#include "CUnit/Basic.h" + +#define __SPDK_ENV_NAME(path) (strrchr(#path, '/') + 1) +#define _SPDK_ENV_NAME(path) __SPDK_ENV_NAME(path) +#define SPDK_ENV_NAME _SPDK_ENV_NAME(SPDK_CONFIG_ENV) + +static void +vtophys_malloc_test(void) +{ + void *p = NULL; + int i; + unsigned int size = 1; + uint64_t paddr; + + /* Verify vtophys doesn't work on regular malloc memory */ + for (i = 0; i < 31; i++) { + p = malloc(size); + if (p == NULL) { + continue; + } + + paddr = spdk_vtophys(p, NULL); + CU_ASSERT(paddr == SPDK_VTOPHYS_ERROR); + + free(p); + size = size << 1; + } + + /* Test addresses that are not in the valid x86-64 usermode range */ + paddr = spdk_vtophys((void *)0x0000800000000000ULL, NULL); + CU_ASSERT(paddr == SPDK_VTOPHYS_ERROR); +} + +static void +vtophys_spdk_malloc_test(void) +{ + void *buf = NULL, *p = NULL; + size_t buf_align = 512; + int i; + unsigned int size = 1; + uint64_t paddr, tmpsize; + + /* Test vtophys on memory allocated through SPDK */ + for (i = 0; i < 31; i++) { + buf = spdk_zmalloc(size, buf_align, NULL, SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA); + if (buf == NULL) { + continue; + } + + /* test vtophys translation with no length parameter */ + paddr = spdk_vtophys(buf, NULL); + CU_ASSERT(paddr != SPDK_VTOPHYS_ERROR); + + /* translate the entire buffer; it's not necessarily contiguous */ + p = buf; + tmpsize = size; + while (p < buf + size) { + paddr = spdk_vtophys(p, &tmpsize); + CU_ASSERT(paddr != SPDK_VTOPHYS_ERROR); + CU_ASSERT(tmpsize >= spdk_min(size, buf_align)); + p += tmpsize; + tmpsize = buf + size - p; + } + CU_ASSERT(tmpsize == 0); + + /* translate a valid vaddr, but with length 0 */ + p = buf; + tmpsize = 0; + paddr = spdk_vtophys(p, &tmpsize); + CU_ASSERT(paddr != SPDK_VTOPHYS_ERROR); + CU_ASSERT(tmpsize == 0); + + /* translate the first half of the buffer */ + p = buf; + tmpsize = size / 2; + while (p < buf + size / 2) { + paddr = spdk_vtophys(p, &tmpsize); + CU_ASSERT(paddr != SPDK_VTOPHYS_ERROR); + CU_ASSERT(tmpsize >= spdk_min(size / 2, buf_align)); + p += tmpsize; + tmpsize = buf + size / 2 - p; + } + CU_ASSERT(tmpsize == 0); + + /* translate the second half of the buffer */ + p = buf + size / 2; + tmpsize = size / 2; + while (p < buf + size) { + paddr = spdk_vtophys(p, &tmpsize); + CU_ASSERT(paddr != SPDK_VTOPHYS_ERROR); + CU_ASSERT(tmpsize >= spdk_min(size / 2, buf_align)); + p += tmpsize; + tmpsize = buf + size - p; + } + CU_ASSERT(tmpsize == 0); + + /* translate a region that's not entirely registered */ + p = buf; + tmpsize = UINT64_MAX; + while (p < buf + size) { + paddr = spdk_vtophys(p, &tmpsize); + CU_ASSERT(paddr != SPDK_VTOPHYS_ERROR); + CU_ASSERT(tmpsize >= buf_align); + p += tmpsize; + /* verify our region is really contiguous */ + CU_ASSERT(paddr + tmpsize - 1 == spdk_vtophys(p - 1, &tmpsize)); + tmpsize = UINT64_MAX; + } + + spdk_free(buf); + size = size << 1; + } +} + +int +main(int argc, char **argv) +{ + struct spdk_env_opts opts; + CU_pSuite suite = NULL; + unsigned num_failures; + + spdk_env_opts_init(&opts); + opts.name = "vtophys"; + opts.core_mask = "0x1"; + if (strcmp(SPDK_ENV_NAME, "env_dpdk") == 0) { + opts.env_context = "--log-level=lib.eal:8"; + } + + if (spdk_env_init(&opts) < 0) { + printf("Err: Unable to initialize SPDK env\n"); + return 1; + } + + if (CU_initialize_registry() != CUE_SUCCESS) { + return CU_get_error(); + } + + suite = CU_add_suite("components_suite", NULL, NULL); + if (suite == NULL) { + CU_cleanup_registry(); + return CU_get_error(); + } + + if ( + CU_add_test(suite, "vtophys_malloc_test", vtophys_malloc_test) == NULL || + CU_add_test(suite, "vtophys_spdk_malloc_test", vtophys_spdk_malloc_test) == 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; +} -- cgit v1.2.3