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_nvmf_ctrlr_poll(struct spdk_nvmf_ctrlr *ctrlr) 147 { 148 return -1; 149 } 150 151 int 152 spdk_bdev_open(struct spdk_bdev *bdev, bool write, spdk_bdev_remove_cb_t remove_cb, 153 void *remove_ctx, struct spdk_bdev_desc **desc) 154 { 155 return 0; 156 } 157 158 const char * 159 spdk_bdev_get_name(const struct spdk_bdev *bdev) 160 { 161 return "test"; 162 } 163 164 static void 165 test_spdk_nvmf_subsystem_add_ns(void) 166 { 167 struct spdk_nvmf_subsystem subsystem = { 168 .max_nsid = 0, 169 .ns = NULL, 170 }; 171 struct spdk_bdev bdev1 = {}, bdev2 = {}; 172 uint32_t nsid; 173 174 /* Allow NSID to be assigned automatically */ 175 nsid = spdk_nvmf_subsystem_add_ns(&subsystem, &bdev1, 0); 176 /* NSID 1 is the first unused ID */ 177 CU_ASSERT(nsid == 1); 178 CU_ASSERT(subsystem.max_nsid == 1); 179 CU_ASSERT(subsystem.ns[nsid - 1].bdev == &bdev1); 180 181 /* Request a specific NSID */ 182 nsid = spdk_nvmf_subsystem_add_ns(&subsystem, &bdev2, 5); 183 CU_ASSERT(nsid == 5); 184 CU_ASSERT(subsystem.max_nsid == 5); 185 CU_ASSERT(subsystem.ns[nsid - 1].bdev == &bdev2); 186 187 /* Request an NSID that is already in use */ 188 nsid = spdk_nvmf_subsystem_add_ns(&subsystem, &bdev2, 5); 189 CU_ASSERT(nsid == 0); 190 CU_ASSERT(subsystem.max_nsid == 5); 191 192 /* Request 0xFFFFFFFF (invalid NSID, reserved for broadcast) */ 193 nsid = spdk_nvmf_subsystem_add_ns(&subsystem, &bdev2, 0xFFFFFFFF); 194 CU_ASSERT(nsid == 0); 195 CU_ASSERT(subsystem.max_nsid == 5); 196 197 free(subsystem.ns); 198 } 199 200 static void 201 nvmf_test_create_subsystem(void) 202 { 203 struct spdk_nvmf_tgt tgt = {}; 204 char nqn[256]; 205 struct spdk_nvmf_subsystem *subsystem; 206 207 strncpy(nqn, "nqn.2016-06.io.spdk:subsystem1", sizeof(nqn)); 208 subsystem = spdk_nvmf_create_subsystem(&tgt, nqn, SPDK_NVMF_SUBTYPE_NVME, 0); 209 SPDK_CU_ASSERT_FATAL(subsystem != NULL); 210 CU_ASSERT_STRING_EQUAL(subsystem->subnqn, nqn); 211 spdk_nvmf_delete_subsystem(subsystem); 212 213 /* Longest valid name */ 214 strncpy(nqn, "nqn.2016-06.io.spdk:", sizeof(nqn)); 215 memset(nqn + strlen(nqn), 'a', 223 - strlen(nqn)); 216 nqn[223] = '\0'; 217 CU_ASSERT(strlen(nqn) == 223); 218 subsystem = spdk_nvmf_create_subsystem(&tgt, nqn, SPDK_NVMF_SUBTYPE_NVME, 0); 219 SPDK_CU_ASSERT_FATAL(subsystem != NULL); 220 CU_ASSERT_STRING_EQUAL(subsystem->subnqn, nqn); 221 spdk_nvmf_delete_subsystem(subsystem); 222 223 /* Name that is one byte longer than allowed */ 224 strncpy(nqn, "nqn.2016-06.io.spdk:", sizeof(nqn)); 225 memset(nqn + strlen(nqn), 'a', 224 - strlen(nqn)); 226 nqn[224] = '\0'; 227 CU_ASSERT(strlen(nqn) == 224); 228 subsystem = spdk_nvmf_create_subsystem(&tgt, nqn, SPDK_NVMF_SUBTYPE_NVME, 0); 229 CU_ASSERT(subsystem == NULL); 230 231 free(tgt.subsystems); 232 } 233 234 int main(int argc, char **argv) 235 { 236 CU_pSuite suite = NULL; 237 unsigned int num_failures; 238 239 if (CU_initialize_registry() != CUE_SUCCESS) { 240 return CU_get_error(); 241 } 242 243 suite = CU_add_suite("nvmf", NULL, NULL); 244 if (suite == NULL) { 245 CU_cleanup_registry(); 246 return CU_get_error(); 247 } 248 249 if ( 250 CU_add_test(suite, "create_subsystem", nvmf_test_create_subsystem) == NULL || 251 CU_add_test(suite, "nvmf_subsystem_add_ns", test_spdk_nvmf_subsystem_add_ns) == NULL) { 252 CU_cleanup_registry(); 253 return CU_get_error(); 254 } 255 256 CU_basic_set_mode(CU_BRM_VERBOSE); 257 CU_basic_run_tests(); 258 num_failures = CU_get_number_of_failures(); 259 CU_cleanup_registry(); 260 return num_failures; 261 } 262