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_write_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, void *buf, 112 uint64_t offset_blocks, uint64_t num_blocks, 113 spdk_bdev_io_completion_cb cb, void *cb_arg) 114 { 115 return 0; 116 } 117 118 int 119 spdk_bdev_writev_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 120 struct iovec *iov, int iovcnt, 121 uint64_t offset_blocks, uint64_t num_blocks, 122 spdk_bdev_io_completion_cb cb, void *cb_arg) 123 { 124 return 0; 125 } 126 127 int 128 spdk_bdev_read_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, void *buf, 129 uint64_t offset_blocks, uint64_t num_blocks, 130 spdk_bdev_io_completion_cb cb, void *cb_arg) 131 { 132 return 0; 133 } 134 135 int spdk_bdev_readv_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 136 struct iovec *iov, int iovcnt, 137 uint64_t offset_blocks, uint64_t num_blocks, 138 spdk_bdev_io_completion_cb cb, void *cb_arg) 139 { 140 return 0; 141 } 142 143 int 144 spdk_bdev_write_zeroes_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 145 uint64_t offset_blocks, uint64_t num_blocks, 146 spdk_bdev_io_completion_cb cb, void *cb_arg) 147 { 148 return 0; 149 } 150 151 int 152 spdk_bdev_nvme_io_passthru(struct spdk_bdev_desc *desc, 153 struct spdk_io_channel *ch, 154 const struct spdk_nvme_cmd *cmd, 155 void *buf, size_t nbytes, 156 spdk_bdev_io_completion_cb cb, void *cb_arg) 157 { 158 return 0; 159 } 160 161 void spdk_bdev_free_io(struct spdk_bdev_io *bdev_io) 162 { 163 } 164 165 const char *spdk_nvmf_subsystem_get_nqn(struct spdk_nvmf_subsystem *subsystem) 166 { 167 return NULL; 168 } 169 170 struct spdk_nvmf_ns * 171 spdk_nvmf_subsystem_get_ns(struct spdk_nvmf_subsystem *subsystem, uint32_t nsid) 172 { 173 abort(); 174 return NULL; 175 } 176 177 struct spdk_nvmf_ns * 178 spdk_nvmf_subsystem_get_first_ns(struct spdk_nvmf_subsystem *subsystem) 179 { 180 abort(); 181 return NULL; 182 } 183 184 struct spdk_nvmf_ns * 185 spdk_nvmf_subsystem_get_next_ns(struct spdk_nvmf_subsystem *subsystem, struct spdk_nvmf_ns *prev_ns) 186 { 187 abort(); 188 return NULL; 189 } 190 191 void spdk_bdev_io_get_nvme_status(const struct spdk_bdev_io *bdev_io, int *sct, int *sc) 192 { 193 } 194 195 static void 196 test_get_rw_params(void) 197 { 198 struct spdk_nvme_cmd cmd = {0}; 199 uint64_t lba; 200 uint64_t count; 201 202 lba = 0; 203 count = 0; 204 to_le64(&cmd.cdw10, 0x1234567890ABCDEF); 205 to_le32(&cmd.cdw12, 0x9875 | SPDK_NVME_IO_FLAGS_FORCE_UNIT_ACCESS); 206 nvmf_bdev_ctrlr_get_rw_params(&cmd, &lba, &count); 207 CU_ASSERT(lba == 0x1234567890ABCDEF); 208 CU_ASSERT(count == 0x9875 + 1); /* NOTE: this field is 0's based, hence the +1 */ 209 } 210 211 static void 212 test_lba_in_range(void) 213 { 214 /* Trivial cases (no overflow) */ 215 CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(1000, 0, 1) == true); 216 CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(1000, 0, 1000) == true); 217 CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(1000, 0, 1001) == false); 218 CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(1000, 1, 999) == true); 219 CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(1000, 1, 1000) == false); 220 CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(1000, 999, 1) == true); 221 CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(1000, 1000, 1) == false); 222 CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(1000, 1001, 1) == false); 223 224 /* Overflow edge cases */ 225 CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(UINT64_MAX, 0, UINT64_MAX) == true); 226 CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(UINT64_MAX, 1, UINT64_MAX) == false) 227 CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(UINT64_MAX, UINT64_MAX - 1, 1) == true); 228 CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(UINT64_MAX, UINT64_MAX, 1) == false); 229 } 230 231 int main(int argc, char **argv) 232 { 233 CU_pSuite suite = NULL; 234 unsigned int num_failures; 235 236 if (CU_initialize_registry() != CUE_SUCCESS) { 237 return CU_get_error(); 238 } 239 240 suite = CU_add_suite("nvmf", NULL, NULL); 241 if (suite == NULL) { 242 CU_cleanup_registry(); 243 return CU_get_error(); 244 } 245 246 if ( 247 CU_add_test(suite, "get_rw_params", test_get_rw_params) == NULL || 248 CU_add_test(suite, "lba_in_range", test_lba_in_range) == NULL 249 ) { 250 CU_cleanup_registry(); 251 return CU_get_error(); 252 } 253 254 CU_basic_set_mode(CU_BRM_VERBOSE); 255 CU_basic_run_tests(); 256 num_failures = CU_get_number_of_failures(); 257 CU_cleanup_registry(); 258 return num_failures; 259 } 260