xref: /spdk/test/unit/lib/iscsi/conn.c/conn_ut.c (revision d1165a653907c85812beba21bf9cb6c657cf3bf1)
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 "CUnit/Basic.h"
38 
39 #include "iscsi/conn.c"
40 
41 SPDK_LOG_REGISTER_COMPONENT("iscsi", SPDK_LOG_ISCSI)
42 
43 #define DMIN32(A,B) ((uint32_t) ((uint32_t)(A) > (uint32_t)(B) ? (uint32_t)(B) : (uint32_t)(A)))
44 
45 struct spdk_iscsi_globals g_spdk_iscsi;
46 static TAILQ_HEAD(, spdk_iscsi_task) g_ut_read_tasks = TAILQ_HEAD_INITIALIZER(g_ut_read_tasks);
47 
48 int
49 spdk_app_get_shm_id(void)
50 {
51 	return 0;
52 }
53 
54 uint32_t
55 spdk_env_get_current_core(void)
56 {
57 	return 0;
58 }
59 
60 uint32_t
61 spdk_env_get_first_core(void)
62 {
63 	return 0;
64 }
65 
66 uint32_t
67 spdk_env_get_last_core(void)
68 {
69 	return 0;
70 }
71 
72 uint32_t
73 spdk_env_get_next_core(uint32_t prev_core)
74 {
75 	return 0;
76 }
77 
78 uint64_t spdk_get_ticks(void)
79 {
80 	return 0;
81 }
82 
83 uint64_t spdk_get_ticks_hz(void)
84 {
85 	return 0;
86 }
87 
88 struct spdk_event *
89 spdk_event_allocate(uint32_t lcore, spdk_event_fn fn, void *arg1, void *arg2)
90 {
91 	return NULL;
92 }
93 
94 void
95 spdk_event_call(struct spdk_event *event)
96 {
97 }
98 
99 int
100 spdk_sock_getaddr(struct spdk_sock *sock, char *saddr, int slen, char *caddr, int clen)
101 {
102 	return 0;
103 }
104 
105 int
106 spdk_sock_close(struct spdk_sock **sock)
107 {
108 	*sock = NULL;
109 	return 0;
110 }
111 
112 ssize_t
113 spdk_sock_recv(struct spdk_sock *sock, void *buf, size_t len)
114 {
115 	return 0;
116 }
117 
118 ssize_t
119 spdk_sock_writev(struct spdk_sock *sock, struct iovec *iov, int iovcnt)
120 {
121 	return 0;
122 }
123 
124 int
125 spdk_sock_set_recvlowat(struct spdk_sock *s, int nbytes)
126 {
127 	return 0;
128 }
129 
130 int
131 spdk_sock_set_recvbuf(struct spdk_sock *sock, int sz)
132 {
133 	return 0;
134 }
135 
136 int
137 spdk_sock_set_sendbuf(struct spdk_sock *sock, int sz)
138 {
139 	return 0;
140 }
141 
142 int
143 spdk_sock_group_add_sock(struct spdk_sock_group *group, struct spdk_sock *sock,
144 			 spdk_sock_cb cb_fn, void *cb_arg)
145 {
146 	return 0;
147 }
148 
149 int
150 spdk_sock_group_remove_sock(struct spdk_sock_group *group, struct spdk_sock *sock)
151 {
152 	return 0;
153 }
154 
155 void
156 spdk_scsi_task_put(struct spdk_scsi_task *task)
157 {
158 }
159 
160 void
161 spdk_scsi_dev_free_io_channels(struct spdk_scsi_dev *dev)
162 {
163 }
164 
165 int
166 spdk_scsi_dev_allocate_io_channels(struct spdk_scsi_dev *dev)
167 {
168 	return 0;
169 }
170 
171 const char *
172 spdk_scsi_port_get_name(const struct spdk_scsi_port *port)
173 {
174 	return NULL;
175 }
176 
177 void
178 spdk_put_pdu(struct spdk_iscsi_pdu *pdu)
179 {
180 }
181 
182 void
183 spdk_iscsi_param_free(struct iscsi_param *params)
184 {
185 }
186 
187 int
188 spdk_iscsi_conn_params_init(struct iscsi_param **params)
189 {
190 	return 0;
191 }
192 
193 void spdk_clear_all_transfer_task(struct spdk_iscsi_conn *conn,
194 				  struct spdk_scsi_lun *lun)
195 {
196 }
197 
198 int
199 spdk_iscsi_build_iovecs(struct spdk_iscsi_conn *conn, struct iovec *iovec,
200 			struct spdk_iscsi_pdu *pdu)
201 {
202 	return 0;
203 }
204 
205 bool spdk_iscsi_is_deferred_free_pdu(struct spdk_iscsi_pdu *pdu)
206 {
207 	return false;
208 }
209 
210 void spdk_iscsi_task_response(struct spdk_iscsi_conn *conn,
211 			      struct spdk_iscsi_task *task)
212 {
213 }
214 
215 void
216 spdk_iscsi_task_mgmt_response(struct spdk_iscsi_conn *conn,
217 			      struct spdk_iscsi_task *task)
218 {
219 }
220 
221 int spdk_iscsi_send_nopin(struct spdk_iscsi_conn *conn)
222 {
223 	return 0;
224 }
225 
226 int
227 spdk_iscsi_execute(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu)
228 {
229 	return 0;
230 }
231 
232 void spdk_del_transfer_task(struct spdk_iscsi_conn *conn, uint32_t task_tag)
233 {
234 }
235 
236 int spdk_iscsi_conn_handle_queued_datain_tasks(struct spdk_iscsi_conn *conn)
237 {
238 	return 0;
239 }
240 
241 int
242 spdk_iscsi_read_pdu(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu **_pdu)
243 {
244 	return 0;
245 }
246 
247 void spdk_free_sess(struct spdk_iscsi_sess *sess)
248 {
249 }
250 
251 int
252 spdk_iscsi_tgt_node_cleanup_luns(struct spdk_iscsi_conn *conn,
253 				 struct spdk_iscsi_tgt_node *target)
254 {
255 	return 0;
256 }
257 
258 void
259 spdk_shutdown_iscsi_conns_done(void)
260 {
261 }
262 
263 static struct spdk_iscsi_task *
264 ut_conn_task_get(struct spdk_iscsi_task *parent)
265 {
266 	struct spdk_iscsi_task *task;
267 
268 	task = calloc(1, sizeof(*task));
269 	SPDK_CU_ASSERT_FATAL(task != NULL);
270 
271 	if (parent) {
272 		task->parent = parent;
273 	}
274 	return task;
275 }
276 
277 static void
278 ut_conn_create_read_tasks(int transfer_len)
279 {
280 	struct spdk_iscsi_task *task, *subtask;
281 	int32_t remaining_size = 0;
282 
283 	task = ut_conn_task_get(NULL);
284 
285 	task->scsi.transfer_len = transfer_len;
286 	task->scsi.offset = 0;
287 	task->scsi.length = DMIN32(SPDK_BDEV_LARGE_BUF_MAX_SIZE, task->scsi.transfer_len);
288 	task->scsi.status = SPDK_SCSI_STATUS_GOOD;
289 
290 	remaining_size = task->scsi.transfer_len - task->scsi.length;
291 	task->current_datain_offset = 0;
292 
293 	if (remaining_size == 0) {
294 		TAILQ_INSERT_TAIL(&g_ut_read_tasks, task, link);
295 		return;
296 	}
297 
298 	while (1) {
299 		if (task->current_datain_offset == 0) {
300 			task->current_datain_offset = task->scsi.length;
301 			TAILQ_INSERT_TAIL(&g_ut_read_tasks, task, link);
302 			continue;
303 		}
304 
305 		if (task->current_datain_offset < task->scsi.transfer_len) {
306 			remaining_size = task->scsi.transfer_len - task->current_datain_offset;
307 
308 			subtask = ut_conn_task_get(task);
309 
310 			subtask->scsi.offset = task->current_datain_offset;
311 			subtask->scsi.length = DMIN32(SPDK_BDEV_LARGE_BUF_MAX_SIZE, remaining_size);
312 			subtask->scsi.status = SPDK_SCSI_STATUS_GOOD;
313 
314 			task->current_datain_offset += subtask->scsi.length;
315 
316 			TAILQ_INSERT_TAIL(&g_ut_read_tasks, subtask, link);
317 		}
318 
319 		if (task->current_datain_offset == task->scsi.transfer_len) {
320 			break;
321 		}
322 	}
323 }
324 
325 static void
326 read_task_split_in_order_case(void)
327 {
328 	struct spdk_iscsi_task *primary, *task, *tmp;
329 
330 	ut_conn_create_read_tasks(SPDK_BDEV_LARGE_BUF_MAX_SIZE * 8);
331 
332 	TAILQ_FOREACH(task, &g_ut_read_tasks, link) {
333 		primary = spdk_iscsi_task_get_primary(task);
334 		process_read_task_completion(NULL, task, primary);
335 	}
336 
337 	primary = TAILQ_FIRST(&g_ut_read_tasks);
338 	SPDK_CU_ASSERT_FATAL(primary != NULL);
339 
340 	if (primary != NULL) {
341 		CU_ASSERT(primary->bytes_completed == primary->scsi.transfer_len);
342 	}
343 
344 	TAILQ_FOREACH_SAFE(task, &g_ut_read_tasks, link, tmp) {
345 		TAILQ_REMOVE(&g_ut_read_tasks, task, link);
346 		free(task);
347 	}
348 
349 	CU_ASSERT(TAILQ_EMPTY(&g_ut_read_tasks));
350 }
351 
352 int
353 main(int argc, char **argv)
354 {
355 	CU_pSuite	suite = NULL;
356 	unsigned int	num_failures;
357 
358 	if (CU_initialize_registry() != CUE_SUCCESS) {
359 		return CU_get_error();
360 	}
361 
362 	suite = CU_add_suite("conn_suite", NULL, NULL);
363 	if (suite == NULL) {
364 		CU_cleanup_registry();
365 		return CU_get_error();
366 	}
367 
368 	if (
369 		CU_add_test(suite, "read task split in order", read_task_split_in_order_case) == NULL
370 	) {
371 		CU_cleanup_registry();
372 		return CU_get_error();
373 	}
374 
375 	CU_basic_set_mode(CU_BRM_VERBOSE);
376 	CU_basic_run_tests();
377 	num_failures = CU_get_number_of_failures();
378 	CU_cleanup_registry();
379 	return num_failures;
380 }
381