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 #include "subsystem.h" 38 39 #include "subsystem.c" 40 41 SPDK_LOG_REGISTER_TRACE_FLAG("nvmf", SPDK_TRACE_NVMF) 42 43 struct spdk_nvmf_tgt g_nvmf_tgt; 44 45 int 46 spdk_nvmf_subsystem_bdev_attach(struct spdk_nvmf_subsystem *subsystem) 47 { 48 return -1; 49 } 50 51 void 52 spdk_nvmf_subsystem_bdev_detach(struct spdk_nvmf_subsystem *subsystem) 53 { 54 } 55 56 struct spdk_nvmf_listen_addr * 57 spdk_nvmf_listen_addr_create(struct spdk_nvme_transport_id *trid) 58 { 59 struct spdk_nvmf_listen_addr *listen_addr; 60 61 listen_addr = calloc(1, sizeof(*listen_addr)); 62 if (!listen_addr) { 63 return NULL; 64 } 65 66 listen_addr->trid = *trid; 67 68 return listen_addr; 69 } 70 71 void 72 spdk_nvmf_listen_addr_destroy(struct spdk_nvmf_listen_addr *addr) 73 { 74 free(addr); 75 } 76 77 int 78 spdk_nvmf_transport_listen(struct spdk_nvmf_transport *transport, 79 const struct spdk_nvme_transport_id *trid) 80 { 81 return 0; 82 } 83 84 void 85 spdk_nvmf_transport_listen_addr_discover(struct spdk_nvmf_transport *transport, 86 struct spdk_nvmf_listen_addr *listen_addr, 87 struct spdk_nvmf_discovery_log_page_entry *entry) 88 { 89 entry->trtype = 42; 90 } 91 92 bool 93 spdk_nvmf_transport_qpair_is_idle(struct spdk_nvmf_qpair *qpair) 94 { 95 return false; 96 } 97 98 static struct spdk_nvmf_transport g_transport = {}; 99 100 struct spdk_nvmf_transport * 101 spdk_nvmf_transport_create(struct spdk_nvmf_tgt *tgt, 102 enum spdk_nvme_transport_type type) 103 { 104 if (type == SPDK_NVME_TRANSPORT_RDMA) { 105 g_transport.tgt = tgt; 106 return &g_transport; 107 } 108 109 return NULL; 110 } 111 112 struct spdk_nvmf_transport * 113 spdk_nvmf_tgt_get_transport(struct spdk_nvmf_tgt *tgt, enum spdk_nvme_transport_type trtype) 114 { 115 if (trtype == SPDK_NVME_TRANSPORT_RDMA) { 116 return &g_transport; 117 } 118 119 return NULL; 120 } 121 122 int 123 spdk_nvme_transport_id_parse_trtype(enum spdk_nvme_transport_type *trtype, const char *str) 124 { 125 if (trtype == NULL || str == NULL) { 126 return -EINVAL; 127 } 128 129 if (strcasecmp(str, "PCIe") == 0) { 130 *trtype = SPDK_NVME_TRANSPORT_PCIE; 131 } else if (strcasecmp(str, "RDMA") == 0) { 132 *trtype = SPDK_NVME_TRANSPORT_RDMA; 133 } else { 134 return -ENOENT; 135 } 136 return 0; 137 } 138 139 int 140 spdk_nvme_transport_id_compare(const struct spdk_nvme_transport_id *trid1, 141 const struct spdk_nvme_transport_id *trid2) 142 { 143 return 0; 144 } 145 146 int32_t 147 spdk_nvme_ctrlr_process_admin_completions(struct spdk_nvme_ctrlr *ctrlr) 148 { 149 return -1; 150 } 151 152 int32_t 153 spdk_nvme_qpair_process_completions(struct spdk_nvme_qpair *qpair, uint32_t max_completions) 154 { 155 return -1; 156 } 157 158 int 159 spdk_nvme_detach(struct spdk_nvme_ctrlr *ctrlr) 160 { 161 return -1; 162 } 163 164 void 165 spdk_nvmf_ctrlr_destruct(struct spdk_nvmf_ctrlr *ctrlr) 166 { 167 } 168 169 int 170 spdk_nvmf_ctrlr_poll(struct spdk_nvmf_ctrlr *ctrlr) 171 { 172 return -1; 173 } 174 175 int 176 spdk_bdev_open(struct spdk_bdev *bdev, bool write, spdk_bdev_remove_cb_t remove_cb, 177 void *remove_ctx, struct spdk_bdev_desc **desc) 178 { 179 return 0; 180 } 181 182 const char * 183 spdk_bdev_get_name(const struct spdk_bdev *bdev) 184 { 185 return "test"; 186 } 187 188 static void 189 test_spdk_nvmf_tgt_listen(void) 190 { 191 struct spdk_nvmf_listen_addr *listen_addr; 192 struct spdk_nvme_transport_id trid = {}; 193 194 /* Invalid trtype */ 195 trid.trtype = 55; 196 trid.adrfam = SPDK_NVMF_ADRFAM_IPV4; 197 snprintf(trid.traddr, sizeof(trid.traddr), "192.168.100.1"); 198 snprintf(trid.trsvcid, sizeof(trid.trsvcid), "4420"); 199 CU_ASSERT(spdk_nvmf_tgt_listen(&trid) == NULL); 200 201 trid.trtype = SPDK_NVME_TRANSPORT_RDMA; 202 trid.adrfam = SPDK_NVMF_ADRFAM_IPV4; 203 snprintf(trid.traddr, sizeof(trid.traddr), "192.168.100.1"); 204 snprintf(trid.trsvcid, sizeof(trid.trsvcid), "4420"); 205 listen_addr = spdk_nvmf_tgt_listen(&trid); 206 SPDK_CU_ASSERT_FATAL(listen_addr != NULL); 207 spdk_nvmf_listen_addr_destroy(listen_addr); 208 209 } 210 211 static void 212 test_spdk_nvmf_subsystem_add_ns(void) 213 { 214 struct spdk_nvmf_subsystem subsystem = { 215 .max_nsid = 0, 216 .ns = {}, 217 }; 218 struct spdk_bdev bdev1 = {}, bdev2 = {}; 219 uint32_t nsid; 220 221 /* Allow NSID to be assigned automatically */ 222 nsid = spdk_nvmf_subsystem_add_ns(&subsystem, &bdev1, 0); 223 /* NSID 1 is the first unused ID */ 224 CU_ASSERT(nsid == 1); 225 CU_ASSERT(subsystem.max_nsid == 1); 226 CU_ASSERT(subsystem.ns[nsid - 1].bdev == &bdev1); 227 228 /* Request a specific NSID */ 229 nsid = spdk_nvmf_subsystem_add_ns(&subsystem, &bdev2, 5); 230 CU_ASSERT(nsid == 5); 231 CU_ASSERT(subsystem.max_nsid == 5); 232 CU_ASSERT(subsystem.ns[nsid - 1].bdev == &bdev2); 233 234 /* Request an NSID that is already in use */ 235 nsid = spdk_nvmf_subsystem_add_ns(&subsystem, &bdev2, 5); 236 CU_ASSERT(nsid == 0); 237 CU_ASSERT(subsystem.max_nsid == 5); 238 } 239 240 static void 241 nvmf_test_create_subsystem(void) 242 { 243 char nqn[256]; 244 struct spdk_nvmf_subsystem *subsystem; 245 TAILQ_INIT(&g_nvmf_tgt.subsystems); 246 247 strncpy(nqn, "nqn.2016-06.io.spdk:subsystem1", sizeof(nqn)); 248 subsystem = spdk_nvmf_create_subsystem(nqn, SPDK_NVMF_SUBTYPE_NVME, 249 NULL, NULL, NULL); 250 SPDK_CU_ASSERT_FATAL(subsystem != NULL); 251 CU_ASSERT_STRING_EQUAL(subsystem->subnqn, nqn); 252 spdk_nvmf_delete_subsystem(subsystem); 253 254 /* Longest valid name */ 255 strncpy(nqn, "nqn.2016-06.io.spdk:", sizeof(nqn)); 256 memset(nqn + strlen(nqn), 'a', 223 - strlen(nqn)); 257 nqn[223] = '\0'; 258 CU_ASSERT(strlen(nqn) == 223); 259 subsystem = spdk_nvmf_create_subsystem(nqn, SPDK_NVMF_SUBTYPE_NVME, 260 NULL, NULL, NULL); 261 SPDK_CU_ASSERT_FATAL(subsystem != NULL); 262 CU_ASSERT_STRING_EQUAL(subsystem->subnqn, nqn); 263 spdk_nvmf_delete_subsystem(subsystem); 264 265 /* Name that is one byte longer than allowed */ 266 strncpy(nqn, "nqn.2016-06.io.spdk:", sizeof(nqn)); 267 memset(nqn + strlen(nqn), 'a', 224 - strlen(nqn)); 268 nqn[224] = '\0'; 269 CU_ASSERT(strlen(nqn) == 224); 270 subsystem = spdk_nvmf_create_subsystem(nqn, SPDK_NVMF_SUBTYPE_NVME, 271 NULL, NULL, NULL); 272 CU_ASSERT(subsystem == NULL); 273 } 274 275 static void 276 nvmf_test_find_subsystem(void) 277 { 278 CU_ASSERT_PTR_NULL(spdk_nvmf_find_subsystem(NULL)); 279 CU_ASSERT_PTR_NULL(spdk_nvmf_find_subsystem("fake")); 280 } 281 282 int main(int argc, char **argv) 283 { 284 CU_pSuite suite = NULL; 285 unsigned int num_failures; 286 287 if (CU_initialize_registry() != CUE_SUCCESS) { 288 return CU_get_error(); 289 } 290 291 suite = CU_add_suite("nvmf", NULL, NULL); 292 if (suite == NULL) { 293 CU_cleanup_registry(); 294 return CU_get_error(); 295 } 296 297 if ( 298 CU_add_test(suite, "create_subsystem", nvmf_test_create_subsystem) == NULL || 299 CU_add_test(suite, "nvmf_tgt_listen", test_spdk_nvmf_tgt_listen) == NULL || 300 CU_add_test(suite, "nvmf_subsystem_add_ns", test_spdk_nvmf_subsystem_add_ns) == NULL || 301 CU_add_test(suite, "find_subsystem", nvmf_test_find_subsystem) == NULL) { 302 CU_cleanup_registry(); 303 return CU_get_error(); 304 } 305 306 CU_basic_set_mode(CU_BRM_VERBOSE); 307 CU_basic_run_tests(); 308 num_failures = CU_get_number_of_failures(); 309 CU_cleanup_registry(); 310 return num_failures; 311 } 312