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