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