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