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