1 /*- 2 * BSD LICENSE 3 * 4 * Copyright (c) Intel Corporation. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * * Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * * Neither the name of Intel Corporation nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include "spdk/stdinc.h" 35 36 #include "spdk_cunit.h" 37 38 #include "nvmf/ctrlr_bdev.c" 39 40 41 SPDK_LOG_REGISTER_COMPONENT("nvmf", SPDK_LOG_NVMF) 42 43 int 44 spdk_nvmf_request_complete(struct spdk_nvmf_request *req) 45 { 46 return -1; 47 } 48 49 const char * 50 spdk_bdev_get_name(const struct spdk_bdev *bdev) 51 { 52 return "test"; 53 } 54 55 uint32_t 56 spdk_bdev_get_block_size(const struct spdk_bdev *bdev) 57 { 58 abort(); 59 return 0; 60 } 61 62 uint64_t 63 spdk_bdev_get_num_blocks(const struct spdk_bdev *bdev) 64 { 65 abort(); 66 return 0; 67 } 68 69 uint32_t 70 spdk_bdev_get_optimal_io_boundary(const struct spdk_bdev *bdev) 71 { 72 abort(); 73 return 0; 74 } 75 76 struct spdk_io_channel * 77 spdk_bdev_get_io_channel(struct spdk_bdev_desc *desc) 78 { 79 return NULL; 80 } 81 82 int 83 spdk_bdev_flush_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 84 uint64_t offset_blocks, uint64_t num_blocks, 85 spdk_bdev_io_completion_cb cb, void *cb_arg) 86 { 87 return 0; 88 } 89 90 int 91 spdk_bdev_unmap_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 92 uint64_t offset_blocks, uint64_t num_blocks, 93 spdk_bdev_io_completion_cb cb, void *cb_arg) 94 { 95 return 0; 96 } 97 98 void 99 spdk_trace_record(uint16_t tpoint_id, uint16_t poller_id, uint32_t size, uint64_t object_id, 100 uint64_t arg1) 101 { 102 } 103 104 bool 105 spdk_bdev_io_type_supported(struct spdk_bdev *bdev, enum spdk_bdev_io_type io_type) 106 { 107 return false; 108 } 109 110 int 111 spdk_bdev_queue_io_wait(struct spdk_bdev *bdev, struct spdk_io_channel *ch, 112 struct spdk_bdev_io_wait_entry *entry) 113 { 114 return 0; 115 } 116 117 int 118 spdk_bdev_write_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, void *buf, 119 uint64_t offset_blocks, uint64_t num_blocks, 120 spdk_bdev_io_completion_cb cb, void *cb_arg) 121 { 122 return 0; 123 } 124 125 int 126 spdk_bdev_writev_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 127 struct iovec *iov, int iovcnt, 128 uint64_t offset_blocks, uint64_t num_blocks, 129 spdk_bdev_io_completion_cb cb, void *cb_arg) 130 { 131 return 0; 132 } 133 134 int 135 spdk_bdev_read_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, void *buf, 136 uint64_t offset_blocks, uint64_t num_blocks, 137 spdk_bdev_io_completion_cb cb, void *cb_arg) 138 { 139 return 0; 140 } 141 142 int spdk_bdev_readv_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 143 struct iovec *iov, int iovcnt, 144 uint64_t offset_blocks, uint64_t num_blocks, 145 spdk_bdev_io_completion_cb cb, void *cb_arg) 146 { 147 return 0; 148 } 149 150 int 151 spdk_bdev_write_zeroes_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 152 uint64_t offset_blocks, uint64_t num_blocks, 153 spdk_bdev_io_completion_cb cb, void *cb_arg) 154 { 155 return 0; 156 } 157 158 int 159 spdk_bdev_nvme_io_passthru(struct spdk_bdev_desc *desc, 160 struct spdk_io_channel *ch, 161 const struct spdk_nvme_cmd *cmd, 162 void *buf, size_t nbytes, 163 spdk_bdev_io_completion_cb cb, void *cb_arg) 164 { 165 return 0; 166 } 167 168 void spdk_bdev_free_io(struct spdk_bdev_io *bdev_io) 169 { 170 } 171 172 const char *spdk_nvmf_subsystem_get_nqn(struct spdk_nvmf_subsystem *subsystem) 173 { 174 return NULL; 175 } 176 177 struct spdk_nvmf_ns * 178 spdk_nvmf_subsystem_get_ns(struct spdk_nvmf_subsystem *subsystem, uint32_t nsid) 179 { 180 abort(); 181 return NULL; 182 } 183 184 struct spdk_nvmf_ns * 185 spdk_nvmf_subsystem_get_first_ns(struct spdk_nvmf_subsystem *subsystem) 186 { 187 abort(); 188 return NULL; 189 } 190 191 struct spdk_nvmf_ns * 192 spdk_nvmf_subsystem_get_next_ns(struct spdk_nvmf_subsystem *subsystem, struct spdk_nvmf_ns *prev_ns) 193 { 194 abort(); 195 return NULL; 196 } 197 198 void spdk_bdev_io_get_nvme_status(const struct spdk_bdev_io *bdev_io, int *sct, int *sc) 199 { 200 } 201 202 static void 203 test_get_rw_params(void) 204 { 205 struct spdk_nvme_cmd cmd = {0}; 206 uint64_t lba; 207 uint64_t count; 208 209 lba = 0; 210 count = 0; 211 to_le64(&cmd.cdw10, 0x1234567890ABCDEF); 212 to_le32(&cmd.cdw12, 0x9875 | SPDK_NVME_IO_FLAGS_FORCE_UNIT_ACCESS); 213 nvmf_bdev_ctrlr_get_rw_params(&cmd, &lba, &count); 214 CU_ASSERT(lba == 0x1234567890ABCDEF); 215 CU_ASSERT(count == 0x9875 + 1); /* NOTE: this field is 0's based, hence the +1 */ 216 } 217 218 static void 219 test_lba_in_range(void) 220 { 221 /* Trivial cases (no overflow) */ 222 CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(1000, 0, 1) == true); 223 CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(1000, 0, 1000) == true); 224 CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(1000, 0, 1001) == false); 225 CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(1000, 1, 999) == true); 226 CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(1000, 1, 1000) == false); 227 CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(1000, 999, 1) == true); 228 CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(1000, 1000, 1) == false); 229 CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(1000, 1001, 1) == false); 230 231 /* Overflow edge cases */ 232 CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(UINT64_MAX, 0, UINT64_MAX) == true); 233 CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(UINT64_MAX, 1, UINT64_MAX) == false) 234 CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(UINT64_MAX, UINT64_MAX - 1, 1) == true); 235 CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(UINT64_MAX, UINT64_MAX, 1) == false); 236 } 237 238 int main(int argc, char **argv) 239 { 240 CU_pSuite suite = NULL; 241 unsigned int num_failures; 242 243 if (CU_initialize_registry() != CUE_SUCCESS) { 244 return CU_get_error(); 245 } 246 247 suite = CU_add_suite("nvmf", NULL, NULL); 248 if (suite == NULL) { 249 CU_cleanup_registry(); 250 return CU_get_error(); 251 } 252 253 if ( 254 CU_add_test(suite, "get_rw_params", test_get_rw_params) == NULL || 255 CU_add_test(suite, "lba_in_range", test_lba_in_range) == NULL 256 ) { 257 CU_cleanup_registry(); 258 return CU_get_error(); 259 } 260 261 CU_basic_set_mode(CU_BRM_VERBOSE); 262 CU_basic_run_tests(); 263 num_failures = CU_get_number_of_failures(); 264 CU_cleanup_registry(); 265 return num_failures; 266 } 267