xref: /spdk/test/unit/lib/nvmf/tcp.c/tcp_ut.c (revision c4d9daeb7bf491bc0eb6e8d417b75d44773cb009)
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 "spdk_internal/mock.h"
39 #include "spdk_internal/thread.h"
40 
41 #include "common/lib/test_env.c"
42 #include "common/lib/test_sock.c"
43 
44 #include "nvmf/ctrlr.c"
45 #include "nvmf/tcp.c"
46 
47 #define UT_IPV4_ADDR "192.168.0.1"
48 #define UT_PORT "4420"
49 #define UT_NVMF_ADRFAM_INVALID 0xf
50 #define UT_MAX_QUEUE_DEPTH 128
51 #define UT_MAX_QPAIRS_PER_CTRLR 128
52 #define UT_IN_CAPSULE_DATA_SIZE 1024
53 #define UT_MAX_IO_SIZE 4096
54 #define UT_IO_UNIT_SIZE 1024
55 #define UT_MAX_AQ_DEPTH 64
56 #define UT_SQ_HEAD_MAX 128
57 #define UT_NUM_SHARED_BUFFERS 128
58 
59 SPDK_LOG_REGISTER_COMPONENT("nvmf", SPDK_LOG_NVMF)
60 SPDK_LOG_REGISTER_COMPONENT("nvme", SPDK_LOG_NVME)
61 
62 DEFINE_STUB(spdk_nvmf_qpair_get_listen_trid,
63 	    int,
64 	    (struct spdk_nvmf_qpair *qpair, struct spdk_nvme_transport_id *trid),
65 	    0);
66 
67 DEFINE_STUB(spdk_nvmf_subsystem_add_ctrlr,
68 	    int,
69 	    (struct spdk_nvmf_subsystem *subsystem, struct spdk_nvmf_ctrlr *ctrlr),
70 	    0);
71 
72 DEFINE_STUB(spdk_nvmf_subsystem_get_ctrlr,
73 	    struct spdk_nvmf_ctrlr *,
74 	    (struct spdk_nvmf_subsystem *subsystem, uint16_t cntlid),
75 	    NULL);
76 
77 DEFINE_STUB(spdk_nvmf_tgt_find_subsystem,
78 	    struct spdk_nvmf_subsystem *,
79 	    (struct spdk_nvmf_tgt *tgt, const char *subnqn),
80 	    NULL);
81 
82 DEFINE_STUB(spdk_nvmf_subsystem_listener_allowed,
83 	    bool,
84 	    (struct spdk_nvmf_subsystem *subsystem, struct spdk_nvme_transport_id *trid),
85 	    true);
86 
87 DEFINE_STUB(spdk_nvmf_transport_qpair_set_sqsize,
88 	    int,
89 	    (struct spdk_nvmf_qpair *qpair),
90 	    0);
91 
92 DEFINE_STUB_V(spdk_nvmf_get_discovery_log_page,
93 	      (struct spdk_nvmf_tgt *tgt, struct iovec *iov, uint32_t iovcnt, uint64_t offset, uint32_t length));
94 
95 DEFINE_STUB_V(spdk_nvmf_subsystem_remove_ctrlr,
96 	      (struct spdk_nvmf_subsystem *subsystem, struct spdk_nvmf_ctrlr *ctrlr));
97 
98 DEFINE_STUB(spdk_nvmf_subsystem_get_first_ns,
99 	    struct spdk_nvmf_ns *,
100 	    (struct spdk_nvmf_subsystem *subsystem),
101 	    NULL);
102 
103 DEFINE_STUB(spdk_nvmf_subsystem_get_next_ns,
104 	    struct spdk_nvmf_ns *,
105 	    (struct spdk_nvmf_subsystem *subsystem, struct spdk_nvmf_ns *prev_ns),
106 	    NULL);
107 
108 DEFINE_STUB(spdk_nvmf_subsystem_host_allowed,
109 	    bool,
110 	    (struct spdk_nvmf_subsystem *subsystem, const char *hostnqn),
111 	    true);
112 
113 DEFINE_STUB(spdk_nvmf_ctrlr_dsm_supported,
114 	    bool,
115 	    (struct spdk_nvmf_ctrlr *ctrlr),
116 	    false);
117 
118 DEFINE_STUB(spdk_nvmf_ctrlr_write_zeroes_supported,
119 	    bool,
120 	    (struct spdk_nvmf_ctrlr *ctrlr),
121 	    false);
122 
123 DEFINE_STUB(spdk_nvmf_bdev_ctrlr_read_cmd,
124 	    int,
125 	    (struct spdk_bdev *bdev, struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
126 	     struct spdk_nvmf_request *req),
127 	    0);
128 
129 DEFINE_STUB(spdk_nvmf_bdev_ctrlr_write_cmd,
130 	    int,
131 	    (struct spdk_bdev *bdev, struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
132 	     struct spdk_nvmf_request *req),
133 	    0);
134 
135 DEFINE_STUB(spdk_nvmf_bdev_ctrlr_write_zeroes_cmd,
136 	    int,
137 	    (struct spdk_bdev *bdev, struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
138 	     struct spdk_nvmf_request *req),
139 	    0);
140 
141 DEFINE_STUB(spdk_nvmf_bdev_ctrlr_flush_cmd,
142 	    int,
143 	    (struct spdk_bdev *bdev, struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
144 	     struct spdk_nvmf_request *req),
145 	    0);
146 
147 DEFINE_STUB(spdk_nvmf_bdev_ctrlr_dsm_cmd,
148 	    int,
149 	    (struct spdk_bdev *bdev, struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
150 	     struct spdk_nvmf_request *req),
151 	    0);
152 
153 DEFINE_STUB(spdk_nvmf_bdev_ctrlr_nvme_passthru_io,
154 	    int,
155 	    (struct spdk_bdev *bdev, struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
156 	     struct spdk_nvmf_request *req),
157 	    0);
158 
159 DEFINE_STUB(spdk_nvmf_transport_req_complete,
160 	    int,
161 	    (struct spdk_nvmf_request *req),
162 	    0);
163 
164 DEFINE_STUB_V(spdk_nvmf_ns_reservation_request, (void *ctx));
165 
166 struct spdk_trace_histories *g_trace_histories;
167 
168 struct spdk_bdev {
169 	int ut_mock;
170 	uint64_t blockcnt;
171 };
172 
173 int
174 spdk_nvme_transport_id_compare(const struct spdk_nvme_transport_id *trid1,
175 			       const struct spdk_nvme_transport_id *trid2)
176 {
177 	return 0;
178 }
179 
180 void
181 spdk_trace_register_object(uint8_t type, char id_prefix)
182 {
183 }
184 
185 void
186 spdk_trace_register_description(const char *name,
187 				uint16_t tpoint_id, uint8_t owner_type,
188 				uint8_t object_type, uint8_t new_object,
189 				uint8_t arg1_type, const char *arg1_name)
190 {
191 }
192 
193 void
194 _spdk_trace_record(uint64_t tsc, uint16_t tpoint_id, uint16_t poller_id,
195 		   uint32_t size, uint64_t object_id, uint64_t arg1)
196 {
197 }
198 
199 int
200 spdk_nvmf_qpair_disconnect(struct spdk_nvmf_qpair *qpair, nvmf_qpair_disconnect_cb cb_fn, void *ctx)
201 {
202 	return 0;
203 }
204 
205 void
206 spdk_nvmf_bdev_ctrlr_identify_ns(struct spdk_nvmf_ns *ns, struct spdk_nvme_ns_data *nsdata)
207 {
208 	uint64_t num_blocks;
209 
210 	SPDK_CU_ASSERT_FATAL(ns->bdev != NULL);
211 	num_blocks = ns->bdev->blockcnt;
212 	nsdata->nsze = num_blocks;
213 	nsdata->ncap = num_blocks;
214 	nsdata->nuse = num_blocks;
215 	nsdata->nlbaf = 0;
216 	nsdata->flbas.format = 0;
217 	nsdata->lbaf[0].lbads = spdk_u32log2(512);
218 }
219 
220 const char *
221 spdk_nvmf_subsystem_get_sn(const struct spdk_nvmf_subsystem *subsystem)
222 {
223 	return subsystem->sn;
224 }
225 
226 const char *
227 spdk_nvmf_subsystem_get_mn(const struct spdk_nvmf_subsystem *subsystem)
228 {
229 	return subsystem->mn;
230 }
231 
232 void
233 spdk_trace_add_register_fn(struct spdk_trace_register_fn *reg_fn)
234 {
235 }
236 
237 static void
238 test_nvmf_tcp_create(void)
239 {
240 	struct spdk_thread *thread;
241 	struct spdk_nvmf_transport *transport;
242 	struct spdk_nvmf_tcp_transport *ttransport;
243 	struct spdk_nvmf_transport_opts opts;
244 
245 	thread = spdk_thread_create(NULL, NULL);
246 	SPDK_CU_ASSERT_FATAL(thread != NULL);
247 	spdk_set_thread(thread);
248 
249 	/* case 1 */
250 	memset(&opts, 0, sizeof(opts));
251 	opts.max_queue_depth = UT_MAX_QUEUE_DEPTH;
252 	opts.max_qpairs_per_ctrlr = UT_MAX_QPAIRS_PER_CTRLR;
253 	opts.in_capsule_data_size = UT_IN_CAPSULE_DATA_SIZE;
254 	opts.max_io_size = UT_MAX_IO_SIZE;
255 	opts.io_unit_size = UT_IO_UNIT_SIZE;
256 	opts.max_aq_depth = UT_MAX_AQ_DEPTH;
257 	opts.num_shared_buffers = UT_NUM_SHARED_BUFFERS;
258 	/* expect success */
259 	transport = spdk_nvmf_tcp_create(&opts);
260 	CU_ASSERT_PTR_NOT_NULL(transport);
261 	ttransport = SPDK_CONTAINEROF(transport, struct spdk_nvmf_tcp_transport, transport);
262 	SPDK_CU_ASSERT_FATAL(ttransport != NULL);
263 	transport->opts = opts;
264 	CU_ASSERT(transport->opts.max_queue_depth == UT_MAX_QUEUE_DEPTH);
265 	CU_ASSERT(transport->opts.max_io_size == UT_MAX_IO_SIZE);
266 	CU_ASSERT(transport->opts.in_capsule_data_size == UT_IN_CAPSULE_DATA_SIZE);
267 	CU_ASSERT(transport->opts.io_unit_size == UT_IO_UNIT_SIZE);
268 	/* destroy transport */
269 	spdk_mempool_free(ttransport->transport.data_buf_pool);
270 	free(ttransport);
271 
272 	/* case 2 */
273 	memset(&opts, 0, sizeof(opts));
274 	opts.max_queue_depth = UT_MAX_QUEUE_DEPTH;
275 	opts.max_qpairs_per_ctrlr = UT_MAX_QPAIRS_PER_CTRLR;
276 	opts.in_capsule_data_size = UT_IN_CAPSULE_DATA_SIZE;
277 	opts.max_io_size = UT_MAX_IO_SIZE;
278 	opts.io_unit_size = UT_MAX_IO_SIZE + 1;
279 	opts.max_aq_depth = UT_MAX_AQ_DEPTH;
280 	opts.num_shared_buffers = UT_NUM_SHARED_BUFFERS;
281 	/* expect success */
282 	transport = spdk_nvmf_tcp_create(&opts);
283 	CU_ASSERT_PTR_NOT_NULL(transport);
284 	ttransport = SPDK_CONTAINEROF(transport, struct spdk_nvmf_tcp_transport, transport);
285 	SPDK_CU_ASSERT_FATAL(ttransport != NULL);
286 	transport->opts = opts;
287 	CU_ASSERT(transport->opts.max_queue_depth == UT_MAX_QUEUE_DEPTH);
288 	CU_ASSERT(transport->opts.max_io_size == UT_MAX_IO_SIZE);
289 	CU_ASSERT(transport->opts.in_capsule_data_size == UT_IN_CAPSULE_DATA_SIZE);
290 	CU_ASSERT(transport->opts.io_unit_size == UT_MAX_IO_SIZE);
291 	/* destroy transport */
292 	spdk_mempool_free(ttransport->transport.data_buf_pool);
293 	free(ttransport);
294 
295 	/* case 3 */
296 	memset(&opts, 0, sizeof(opts));
297 	opts.max_queue_depth = UT_MAX_QUEUE_DEPTH;
298 	opts.max_qpairs_per_ctrlr = UT_MAX_QPAIRS_PER_CTRLR;
299 	opts.in_capsule_data_size = UT_IN_CAPSULE_DATA_SIZE;
300 	opts.max_io_size = UT_MAX_IO_SIZE;
301 	opts.io_unit_size = 16;
302 	opts.max_aq_depth = UT_MAX_AQ_DEPTH;
303 	/* expect failse */
304 	transport = spdk_nvmf_tcp_create(&opts);
305 	CU_ASSERT_PTR_NULL(transport);
306 
307 	spdk_thread_exit(thread);
308 }
309 
310 static void
311 test_nvmf_tcp_destroy(void)
312 {
313 	struct spdk_thread *thread;
314 	struct spdk_nvmf_transport *transport;
315 	struct spdk_nvmf_transport_opts opts;
316 
317 	thread = spdk_thread_create(NULL, NULL);
318 	SPDK_CU_ASSERT_FATAL(thread != NULL);
319 	spdk_set_thread(thread);
320 
321 	/* case 1 */
322 	memset(&opts, 0, sizeof(opts));
323 	opts.max_queue_depth = UT_MAX_QUEUE_DEPTH;
324 	opts.max_qpairs_per_ctrlr = UT_MAX_QPAIRS_PER_CTRLR;
325 	opts.in_capsule_data_size = UT_IN_CAPSULE_DATA_SIZE;
326 	opts.max_io_size = UT_MAX_IO_SIZE;
327 	opts.io_unit_size = UT_IO_UNIT_SIZE;
328 	opts.max_aq_depth = UT_MAX_AQ_DEPTH;
329 	opts.num_shared_buffers = UT_NUM_SHARED_BUFFERS;
330 	transport = spdk_nvmf_tcp_create(&opts);
331 	CU_ASSERT_PTR_NOT_NULL(transport);
332 	transport->opts = opts;
333 	/* destroy transport */
334 	CU_ASSERT(spdk_nvmf_tcp_destroy(transport) == 0);
335 
336 	spdk_thread_exit(thread);
337 }
338 
339 static void
340 test_nvmf_tcp_poll_group_create(void)
341 {
342 	struct spdk_nvmf_transport *transport;
343 	struct spdk_nvmf_transport_poll_group *group;
344 	struct spdk_thread *thread;
345 	struct spdk_nvmf_transport_opts opts;
346 	struct spdk_sock_group grp = {};
347 
348 	thread = spdk_thread_create(NULL, NULL);
349 	SPDK_CU_ASSERT_FATAL(thread != NULL);
350 	spdk_set_thread(thread);
351 
352 	memset(&opts, 0, sizeof(opts));
353 	opts.max_queue_depth = UT_MAX_QUEUE_DEPTH;
354 	opts.max_qpairs_per_ctrlr = UT_MAX_QPAIRS_PER_CTRLR;
355 	opts.in_capsule_data_size = UT_IN_CAPSULE_DATA_SIZE;
356 	opts.max_io_size = UT_MAX_IO_SIZE;
357 	opts.io_unit_size = UT_IO_UNIT_SIZE;
358 	opts.max_aq_depth = UT_MAX_AQ_DEPTH;
359 	opts.num_shared_buffers = UT_NUM_SHARED_BUFFERS;
360 	transport = spdk_nvmf_tcp_create(&opts);
361 	CU_ASSERT_PTR_NOT_NULL(transport);
362 	transport->opts = opts;
363 	MOCK_SET(spdk_sock_group_create, &grp);
364 	group = spdk_nvmf_tcp_poll_group_create(transport);
365 	MOCK_CLEAR_P(spdk_sock_group_create);
366 	SPDK_CU_ASSERT_FATAL(group);
367 	group->transport = transport;
368 	spdk_nvmf_tcp_poll_group_destroy(group);
369 	spdk_nvmf_tcp_destroy(transport);
370 
371 	spdk_thread_exit(thread);
372 }
373 
374 int main(int argc, char **argv)
375 {
376 	CU_pSuite	suite = NULL;
377 	unsigned int	num_failures;
378 
379 	if (CU_initialize_registry() != CUE_SUCCESS) {
380 		return CU_get_error();
381 	}
382 
383 	suite = CU_add_suite("nvmf", NULL, NULL);
384 	if (suite == NULL) {
385 		CU_cleanup_registry();
386 		return CU_get_error();
387 	}
388 
389 	if (
390 		CU_add_test(suite, "nvmf_tcp_create", test_nvmf_tcp_create) == NULL ||
391 		CU_add_test(suite, "nvmf_tcp_destroy", test_nvmf_tcp_destroy) == NULL ||
392 		CU_add_test(suite, "nvmf_tcp_poll_group_create", test_nvmf_tcp_poll_group_create) == NULL
393 	) {
394 		CU_cleanup_registry();
395 		return CU_get_error();
396 	}
397 
398 	CU_basic_set_mode(CU_BRM_VERBOSE);
399 	CU_basic_run_tests();
400 	num_failures = CU_get_number_of_failures();
401 	CU_cleanup_registry();
402 	return num_failures;
403 }
404