diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 18:24:20 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 18:24:20 +0000 |
commit | 483eb2f56657e8e7f419ab1a4fab8dce9ade8609 (patch) | |
tree | e5d88d25d870d5dedacb6bbdbe2a966086a0a5cf /src/spdk/test/env | |
parent | Initial commit. (diff) | |
download | ceph-483eb2f56657e8e7f419ab1a4fab8dce9ade8609.tar.xz ceph-483eb2f56657e8e7f419ab1a4fab8dce9ade8609.zip |
Adding upstream version 14.2.21.upstream/14.2.21upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/spdk/test/env')
-rw-r--r-- | src/spdk/test/env/Makefile | 50 | ||||
-rwxr-xr-x | src/spdk/test/env/env.sh | 24 | ||||
-rw-r--r-- | src/spdk/test/env/memory/.gitignore | 1 | ||||
-rw-r--r-- | src/spdk/test/env/memory/Makefile | 43 | ||||
-rw-r--r-- | src/spdk/test/env/memory/memory_ut.c | 504 | ||||
-rw-r--r-- | src/spdk/test/env/pci/.gitignore | 1 | ||||
-rw-r--r-- | src/spdk/test/env/pci/Makefile | 43 | ||||
-rw-r--r-- | src/spdk/test/env/pci/pci_ut.c | 94 | ||||
-rw-r--r-- | src/spdk/test/env/vtophys/.gitignore | 1 | ||||
-rw-r--r-- | src/spdk/test/env/vtophys/Makefile | 42 | ||||
-rw-r--r-- | src/spdk/test/env/vtophys/vtophys.c | 135 |
11 files changed, 938 insertions, 0 deletions
diff --git a/src/spdk/test/env/Makefile b/src/spdk/test/env/Makefile new file mode 100644 index 00000000..d90696a1 --- /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 = vtophys + +ifeq ($(ENV_NAME),env_dpdk) +DIRS-y += 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 00000000..7aa560f2 --- /dev/null +++ b/src/spdk/test/env/env.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash + +set -e + +testdir=$(readlink -f $(dirname $0)) +rootdir=$(readlink -f $testdir/../..) +source $rootdir/test/common/autotest_common.sh + +timing_enter env + +timing_enter memory +$testdir/memory/memory_ut +timing_exit memory + +timing_enter vtophys +$testdir/vtophys/vtophys +timing_exit vtophys + +timing_enter pci +$testdir/pci/pci_ut +timing_exit pci + +report_test_completion "env" +timing_exit env diff --git a/src/spdk/test/env/memory/.gitignore b/src/spdk/test/env/memory/.gitignore new file mode 100644 index 00000000..7bef3dc0 --- /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 00000000..b6fb88e0 --- /dev/null +++ b/src/spdk/test/env/memory/Makefile @@ -0,0 +1,43 @@ +# +# 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 +include $(SPDK_ROOT_DIR)/mk/spdk.app.mk + +CFLAGS += $(ENV_CFLAGS) +CFLAGS += -I$(SPDK_ROOT_DIR)/test/lib +TEST_FILE = memory_ut.c + +ADDITIONAL_LIBS += $(ENV_LIBS) +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 00000000..39a89a10 --- /dev/null +++ b/src/spdk/test/env/memory/memory_ut.c @@ -0,0 +1,504 @@ +/*- + * 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" + +#include "common/lib/test_env.c" +#include "spdk_cunit.h" + +#include "spdk/bit_array.h" + +static struct rte_mem_config g_mcfg = {}; + +static struct rte_config g_cfg = { + .mem_config = &g_mcfg, +}; + +struct rte_config * +rte_eal_get_configuration(void) +{ + return &g_cfg; +} + +#if RTE_VERSION >= RTE_VERSION_NUM(18, 05, 0, 0) +DEFINE_STUB(rte_mem_event_callback_register, int, (const char *name, rte_mem_event_callback_t clb, + void *arg), 0); +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 *addr, + const struct rte_memseg_list *msl), NULL); +#endif + +#define PAGE_ARRAY_SIZE (100) +static struct spdk_bit_array *g_page_array; +static void *g_vaddr_to_fail = (void *)UINT64_MAX; + +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) + + /* 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); + + /* 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) + + /* 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 (spdk_mem_map_init() < 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 00000000..11d1c65b --- /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 00000000..34165584 --- /dev/null +++ b/src/spdk/test/env/pci/Makefile @@ -0,0 +1,43 @@ +# +# 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 +include $(SPDK_ROOT_DIR)/mk/spdk.app.mk + +CFLAGS += $(ENV_CFLAGS) +LIBS += $(ENV_LINKER_ARGS) +TEST_FILE = pci_ut.c + +ADDITIONAL_LIBS += $(ENV_LIBS) +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 00000000..bdb3a7cc --- /dev/null +++ b/src/spdk/test/env/pci/pci_ut.c @@ -0,0 +1,94 @@ +/*- + * 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 "env_dpdk/pci.c" + +static void +pci_test(void) +{ + int rc = 0; + pid_t childPid; + int status, ret; + struct spdk_pci_addr pci_addr; + + pci_addr.domain = 0x0; + pci_addr.bus = 0x5; + pci_addr.dev = 0x4; + pci_addr.func = 1; + + rc = spdk_pci_device_claim(&pci_addr); + CU_ASSERT(rc >= 0); + + childPid = fork(); + CU_ASSERT(childPid >= 0); + if (childPid == 0) { + ret = spdk_pci_device_claim(&pci_addr); + CU_ASSERT(ret == -1); + exit(0); + } else { + waitpid(childPid, &status, 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_ut1", pci_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 00000000..a03b46cc --- /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 00000000..7dc4d005 --- /dev/null +++ b/src/spdk/test/env/vtophys/Makefile @@ -0,0 +1,42 @@ +# +# 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 +include $(SPDK_ROOT_DIR)/mk/spdk.app.mk + +LIBS += $(ENV_LINKER_ARGS) +TEST_FILE = vtophys.c + +ADDITIONAL_LIBS += $(ENV_LIBS) +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 00000000..d672b1ae --- /dev/null +++ b/src/spdk/test/env/vtophys/vtophys.c @@ -0,0 +1,135 @@ +/*- + * 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/env.h" + +static int +vtophys_negative_test(void) +{ + void *p = NULL; + int i; + unsigned int size = 1; + int rc = 0; + + for (i = 0; i < 31; i++) { + p = malloc(size); + if (p == NULL) { + continue; + } + + if (spdk_vtophys(p) != SPDK_VTOPHYS_ERROR) { + rc = -1; + printf("Err: VA=%p is mapped to a huge_page,\n", p); + free(p); + break; + } + + free(p); + size = size << 1; + } + + /* Test addresses that are not in the valid x86-64 usermode range */ + + if (spdk_vtophys((void *)0x0000800000000000ULL) != SPDK_VTOPHYS_ERROR) { + rc = -1; + printf("Err: kernel-mode address incorrectly allowed\n"); + } + + if (!rc) { + printf("vtophys_negative_test passed\n"); + } else { + printf("vtophys_negative_test failed\n"); + } + + return rc; +} + +static int +vtophys_positive_test(void) +{ + void *p = NULL; + int i; + unsigned int size = 1; + int rc = 0; + + for (i = 0; i < 31; i++) { + p = spdk_dma_zmalloc(size, 512, NULL); + if (p == NULL) { + continue; + } + + if (spdk_vtophys(p) == SPDK_VTOPHYS_ERROR) { + rc = -1; + printf("Err: VA=%p is not mapped to a huge_page,\n", p); + spdk_dma_free(p); + break; + } + + spdk_dma_free(p); + size = size << 1; + } + + if (!rc) { + printf("vtophys_positive_test passed\n"); + } else { + printf("vtophys_positive_test failed\n"); + } + + return rc; +} + +int +main(int argc, char **argv) +{ + int rc; + struct spdk_env_opts opts; + + spdk_env_opts_init(&opts); + opts.name = "vtophys"; + opts.core_mask = "0x1"; + opts.mem_size = 256; + if (spdk_env_init(&opts) < 0) { + printf("Err: Unable to initialize SPDK env\n"); + return 1; + } + + rc = vtophys_negative_test(); + if (rc < 0) { + return rc; + } + + rc = vtophys_positive_test(); + return rc; +} |