xref: /spdk/test/unit/lib/iscsi/iscsi.c/iscsi_ut.c (revision 367c980b453f48310e52d2574afe7d2774df800c)
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/endian.h"
37 #include "spdk/scsi.h"
38 #include "spdk_cunit.h"
39 
40 #include "CUnit/Basic.h"
41 
42 #include "iscsi/iscsi.c"
43 
44 #include "../common.c"
45 #include "iscsi/portal_grp.h"
46 #include "scsi/scsi_internal.h"
47 #include "common/lib/test_env.c"
48 
49 #include "spdk_internal/mock.h"
50 
51 #define UT_TARGET_NAME1		"iqn.2017-11.spdk.io:t0001"
52 #define UT_TARGET_NAME2		"iqn.2017-11.spdk.io:t0002"
53 #define UT_INITIATOR_NAME1	"iqn.2017-11.spdk.io:i0001"
54 #define UT_INITIATOR_NAME2	"iqn.2017-11.spdk.io:i0002"
55 #define UT_ISCSI_TSIH		256
56 
57 struct spdk_iscsi_tgt_node	g_tgt;
58 
59 struct spdk_iscsi_tgt_node *
60 iscsi_find_tgt_node(const char *target_name)
61 {
62 	if (strcasecmp(target_name, UT_TARGET_NAME1) == 0) {
63 		g_tgt.dev = NULL;
64 		return (struct spdk_iscsi_tgt_node *)&g_tgt;
65 	} else {
66 		return NULL;
67 	}
68 }
69 
70 bool
71 iscsi_tgt_node_access(struct spdk_iscsi_conn *conn,
72 		      struct spdk_iscsi_tgt_node *target,
73 		      const char *iqn, const char *addr)
74 {
75 	if (strcasecmp(conn->initiator_name, UT_INITIATOR_NAME1) == 0) {
76 		return true;
77 	} else {
78 		return false;
79 	}
80 }
81 
82 DEFINE_STUB(iscsi_send_tgts, int,
83 	    (struct spdk_iscsi_conn *conn, const char *iiqn, const char *iaddr,
84 	     const char *tiqn, uint8_t *data, int alloc_len, int data_len),
85 	    0);
86 
87 DEFINE_STUB(iscsi_tgt_node_is_destructed, bool,
88 	    (struct spdk_iscsi_tgt_node *target), false);
89 
90 DEFINE_STUB_V(iscsi_portal_grp_close_all, (void));
91 
92 DEFINE_STUB_V(iscsi_conn_schedule, (struct spdk_iscsi_conn *conn));
93 
94 DEFINE_STUB_V(iscsi_conn_free_pdu,
95 	      (struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu));
96 
97 DEFINE_STUB_V(iscsi_conn_pdu_generic_complete, (void *cb_arg));
98 
99 DEFINE_STUB(iscsi_conn_handle_queued_datain_tasks, int,
100 	    (struct spdk_iscsi_conn *conn), 0);
101 
102 DEFINE_STUB(iscsi_conn_abort_queued_datain_task, int,
103 	    (struct spdk_iscsi_conn *conn, uint32_t ref_task_tag), 0);
104 
105 DEFINE_STUB(iscsi_conn_abort_queued_datain_tasks, int,
106 	    (struct spdk_iscsi_conn *conn, struct spdk_scsi_lun *lun,
107 	     struct spdk_iscsi_pdu *pdu), 0);
108 
109 DEFINE_STUB(iscsi_chap_get_authinfo, int,
110 	    (struct iscsi_chap_auth *auth, const char *authuser, int ag_tag),
111 	    0);
112 
113 DEFINE_STUB(spdk_sock_set_recvbuf, int, (struct spdk_sock *sock, int sz), 0);
114 
115 int
116 spdk_scsi_lun_get_id(const struct spdk_scsi_lun *lun)
117 {
118 	return lun->id;
119 }
120 
121 DEFINE_STUB(spdk_scsi_lun_is_removing, bool, (const struct spdk_scsi_lun *lun),
122 	    true);
123 
124 struct spdk_scsi_lun *
125 spdk_scsi_dev_get_lun(struct spdk_scsi_dev *dev, int lun_id)
126 {
127 	if (lun_id < 0 || lun_id >= SPDK_SCSI_DEV_MAX_LUN) {
128 		return NULL;
129 	}
130 
131 	return dev->lun[lun_id];
132 }
133 
134 DEFINE_STUB(spdk_scsi_lun_id_int_to_fmt, uint64_t, (int lun_id), 0);
135 
136 DEFINE_STUB(spdk_scsi_lun_id_fmt_to_int, int, (uint64_t lun_fmt), 0);
137 
138 DEFINE_STUB(spdk_scsi_lun_get_dif_ctx, bool,
139 	    (struct spdk_scsi_lun *lun, struct spdk_scsi_task *task,
140 	     struct spdk_dif_ctx *dif_ctx), false);
141 
142 static void
143 op_login_check_target_test(void)
144 {
145 	struct spdk_iscsi_conn conn = {};
146 	struct spdk_iscsi_pdu rsp_pdu = {};
147 	struct spdk_iscsi_tgt_node *target;
148 	int rc;
149 
150 	/* expect success */
151 	snprintf(conn.initiator_name, sizeof(conn.initiator_name),
152 		 "%s", UT_INITIATOR_NAME1);
153 
154 	rc = iscsi_op_login_check_target(&conn, &rsp_pdu,
155 					 UT_TARGET_NAME1, &target);
156 	CU_ASSERT(rc == 0);
157 
158 	/* expect failure */
159 	snprintf(conn.initiator_name, sizeof(conn.initiator_name),
160 		 "%s", UT_INITIATOR_NAME1);
161 
162 	rc = iscsi_op_login_check_target(&conn, &rsp_pdu,
163 					 UT_TARGET_NAME2, &target);
164 	CU_ASSERT(rc != 0);
165 
166 	/* expect failure */
167 	snprintf(conn.initiator_name, sizeof(conn.initiator_name),
168 		 "%s", UT_INITIATOR_NAME2);
169 
170 	rc = iscsi_op_login_check_target(&conn, &rsp_pdu,
171 					 UT_TARGET_NAME1, &target);
172 	CU_ASSERT(rc != 0);
173 }
174 
175 static void
176 op_login_session_normal_test(void)
177 {
178 	struct spdk_iscsi_conn conn = {};
179 	struct spdk_iscsi_portal portal = {};
180 	struct spdk_iscsi_portal_grp group = {};
181 	struct spdk_iscsi_pdu rsp_pdu = {};
182 	struct iscsi_bhs_login_rsp *rsph;
183 	struct spdk_iscsi_sess sess = {};
184 	struct iscsi_param param = {};
185 	int rc;
186 
187 	/* setup related data structures */
188 	rsph = (struct iscsi_bhs_login_rsp *)&rsp_pdu.bhs;
189 	rsph->tsih = 0;
190 	memset(rsph->isid, 0, sizeof(rsph->isid));
191 	conn.portal = &portal;
192 	portal.group = &group;
193 	conn.portal->group->tag = 0;
194 	conn.params = NULL;
195 
196 	/* expect failure: NULL params for target name */
197 	rc = iscsi_op_login_session_normal(&conn, &rsp_pdu, UT_INITIATOR_NAME1,
198 					   NULL, 0);
199 	CU_ASSERT(rc != 0);
200 	CU_ASSERT(rsph->status_class == ISCSI_CLASS_INITIATOR_ERROR);
201 	CU_ASSERT(rsph->status_detail == ISCSI_LOGIN_MISSING_PARMS);
202 
203 	/* expect failure: incorrect key for target name */
204 	param.next = NULL;
205 	rc = iscsi_op_login_session_normal(&conn, &rsp_pdu, UT_INITIATOR_NAME1,
206 					   &param, 0);
207 	CU_ASSERT(rc != 0);
208 	CU_ASSERT(rsph->status_class == ISCSI_CLASS_INITIATOR_ERROR);
209 	CU_ASSERT(rsph->status_detail == ISCSI_LOGIN_MISSING_PARMS);
210 
211 	/* expect failure: NULL target name */
212 	param.key = "TargetName";
213 	param.val = NULL;
214 	rc = iscsi_op_login_session_normal(&conn, &rsp_pdu, UT_INITIATOR_NAME1,
215 					   &param, 0);
216 	CU_ASSERT(rc != 0);
217 	CU_ASSERT(rsph->status_class == ISCSI_CLASS_INITIATOR_ERROR);
218 	CU_ASSERT(rsph->status_detail == ISCSI_LOGIN_MISSING_PARMS);
219 
220 	/* expect failure: session not found */
221 	param.key = "TargetName";
222 	param.val = "iqn.2017-11.spdk.io:t0001";
223 	snprintf(conn.initiator_name, sizeof(conn.initiator_name),
224 		 "%s", UT_INITIATOR_NAME1);
225 	rsph->tsih = 1; /* to append the session */
226 	rc = iscsi_op_login_session_normal(&conn, &rsp_pdu, UT_INITIATOR_NAME1,
227 					   &param, 0);
228 	CU_ASSERT(conn.target_port == NULL);
229 	CU_ASSERT(rc != 0);
230 	CU_ASSERT(rsph->status_class == ISCSI_CLASS_INITIATOR_ERROR);
231 	CU_ASSERT(rsph->status_detail == ISCSI_LOGIN_CONN_ADD_FAIL);
232 
233 	/* expect failure: session found while tag is wrong */
234 	g_iscsi.MaxSessions = UT_ISCSI_TSIH * 2;
235 	g_iscsi.session = calloc(1, sizeof(void *) * g_iscsi.MaxSessions);
236 	g_iscsi.session[UT_ISCSI_TSIH - 1] = &sess;
237 	sess.tsih = UT_ISCSI_TSIH;
238 	rsph->tsih = UT_ISCSI_TSIH >> 8; /* to append the session */
239 	sess.tag = 1;
240 	rc = iscsi_op_login_session_normal(&conn, &rsp_pdu, UT_INITIATOR_NAME1,
241 					   &param, 0);
242 	CU_ASSERT(conn.target_port == NULL);
243 	CU_ASSERT(rc != 0);
244 	CU_ASSERT(rsph->status_class == ISCSI_CLASS_INITIATOR_ERROR);
245 	CU_ASSERT(rsph->status_detail == ISCSI_LOGIN_CONN_ADD_FAIL);
246 
247 	/* expect suceess: drop the session */
248 	rsph->tsih = 0; /* to create the session */
249 	g_iscsi.AllowDuplicateIsid = false;
250 	rc = iscsi_op_login_session_normal(&conn, &rsp_pdu, UT_INITIATOR_NAME1,
251 					   &param, 0);
252 	CU_ASSERT(rc == 0);
253 
254 	/* expect suceess: create the session */
255 	rsph->tsih = 0; /* to create the session */
256 	g_iscsi.AllowDuplicateIsid = true;
257 	rc = iscsi_op_login_session_normal(&conn, &rsp_pdu, UT_INITIATOR_NAME1,
258 					   &param, 0);
259 	CU_ASSERT(rc == 0);
260 
261 	free(g_iscsi.session);
262 }
263 
264 static void
265 maxburstlength_test(void)
266 {
267 	struct spdk_iscsi_sess sess = {};
268 	struct spdk_iscsi_conn conn = {};
269 	struct spdk_scsi_dev dev = {};
270 	struct spdk_scsi_lun lun = {};
271 	struct spdk_iscsi_pdu *req_pdu, *data_out_pdu, *r2t_pdu;
272 	struct iscsi_bhs_scsi_req *req;
273 	struct iscsi_bhs_r2t *r2t;
274 	struct iscsi_bhs_data_out *data_out;
275 	struct spdk_iscsi_pdu *response_pdu;
276 	int rc;
277 
278 	req_pdu = iscsi_get_pdu(&conn);
279 	data_out_pdu = iscsi_get_pdu(&conn);
280 
281 	sess.ExpCmdSN = 0;
282 	sess.MaxCmdSN = 64;
283 	sess.session_type = SESSION_TYPE_NORMAL;
284 	sess.MaxBurstLength = 1024;
285 
286 	lun.id = 0;
287 
288 	dev.lun[0] = &lun;
289 
290 	conn.full_feature = 1;
291 	conn.sess = &sess;
292 	conn.dev = &dev;
293 	conn.state = ISCSI_CONN_STATE_RUNNING;
294 	TAILQ_INIT(&conn.write_pdu_list);
295 	TAILQ_INIT(&conn.active_r2t_tasks);
296 
297 	req_pdu->bhs.opcode = ISCSI_OP_SCSI;
298 	req_pdu->data_segment_len = 0;
299 
300 	req = (struct iscsi_bhs_scsi_req *)&req_pdu->bhs;
301 
302 	to_be32(&req->cmd_sn, 0);
303 	to_be32(&req->expected_data_xfer_len, 1028);
304 	to_be32(&req->itt, 0x1234);
305 	req->write_bit = 1;
306 	req->final_bit = 1;
307 
308 	rc = iscsi_pdu_hdr_handle(&conn, req_pdu);
309 	if (rc == 0 && !req_pdu->is_rejected) {
310 		rc = iscsi_pdu_payload_handle(&conn, req_pdu);
311 	}
312 	CU_ASSERT(rc == 0);
313 
314 	response_pdu = TAILQ_FIRST(&g_write_pdu_list);
315 	SPDK_CU_ASSERT_FATAL(response_pdu != NULL);
316 
317 	/*
318 	 * Confirm that a correct R2T reply was sent in response to the
319 	 *  SCSI request.
320 	 */
321 	TAILQ_REMOVE(&g_write_pdu_list, response_pdu, tailq);
322 	CU_ASSERT(response_pdu->bhs.opcode == ISCSI_OP_R2T);
323 	r2t = (struct iscsi_bhs_r2t *)&response_pdu->bhs;
324 	CU_ASSERT(from_be32(&r2t->desired_xfer_len) == 1024);
325 	CU_ASSERT(from_be32(&r2t->buffer_offset) == 0);
326 	CU_ASSERT(from_be32(&r2t->itt) == 0x1234);
327 
328 	data_out_pdu->bhs.opcode = ISCSI_OP_SCSI_DATAOUT;
329 	data_out_pdu->bhs.flags = ISCSI_FLAG_FINAL;
330 	data_out_pdu->data_segment_len = 1028;
331 	data_out = (struct iscsi_bhs_data_out *)&data_out_pdu->bhs;
332 	data_out->itt = r2t->itt;
333 	data_out->ttt = r2t->ttt;
334 	DSET24(data_out->data_segment_len, 1028);
335 
336 	rc = iscsi_pdu_hdr_handle(&conn, data_out_pdu);
337 	if (rc == 0 && !data_out_pdu->is_rejected) {
338 		rc = iscsi_pdu_payload_handle(&conn, data_out_pdu);
339 	}
340 	CU_ASSERT(rc == SPDK_ISCSI_CONNECTION_FATAL);
341 
342 	SPDK_CU_ASSERT_FATAL(response_pdu->task != NULL);
343 	iscsi_task_disassociate_pdu(response_pdu->task);
344 	iscsi_task_put(response_pdu->task);
345 	iscsi_put_pdu(response_pdu);
346 
347 	r2t_pdu = TAILQ_FIRST(&g_write_pdu_list);
348 	CU_ASSERT(r2t_pdu != NULL);
349 	TAILQ_REMOVE(&g_write_pdu_list, r2t_pdu, tailq);
350 	iscsi_put_pdu(r2t_pdu);
351 
352 	iscsi_put_pdu(data_out_pdu);
353 	iscsi_put_pdu(req_pdu);
354 }
355 
356 static void
357 underflow_for_read_transfer_test(void)
358 {
359 	struct spdk_iscsi_sess sess = {};
360 	struct spdk_iscsi_conn conn = {};
361 	struct spdk_iscsi_task task = {};
362 	struct spdk_scsi_dev dev = {};
363 	struct spdk_scsi_lun lun = {};
364 	struct spdk_iscsi_pdu *pdu;
365 	struct iscsi_bhs_scsi_req *scsi_req;
366 	struct iscsi_bhs_data_in *datah;
367 	uint32_t residual_count = 0;
368 
369 	sess.MaxBurstLength = SPDK_ISCSI_MAX_BURST_LENGTH;
370 
371 	conn.sess = &sess;
372 	conn.MaxRecvDataSegmentLength = 8192;
373 
374 	dev.lun[0] = &lun;
375 	conn.dev = &dev;
376 
377 	pdu = iscsi_get_pdu(&conn);
378 	SPDK_CU_ASSERT_FATAL(pdu != NULL);
379 
380 	scsi_req = (struct iscsi_bhs_scsi_req *)&pdu->bhs;
381 	scsi_req->read_bit = 1;
382 
383 	iscsi_task_set_pdu(&task, pdu);
384 	task.parent = NULL;
385 
386 	task.scsi.iovs = &task.scsi.iov;
387 	task.scsi.iovcnt = 1;
388 	task.scsi.length = 512;
389 	task.scsi.transfer_len = 512;
390 	task.bytes_completed = 512;
391 	task.scsi.data_transferred = 256;
392 	task.scsi.status = SPDK_SCSI_STATUS_GOOD;
393 
394 	iscsi_task_response(&conn, &task);
395 	iscsi_put_pdu(pdu);
396 
397 	/*
398 	 * In this case, a SCSI Data-In PDU should contain the Status
399 	 * for the data transfer.
400 	 */
401 	to_be32(&residual_count, 256);
402 
403 	pdu = TAILQ_FIRST(&g_write_pdu_list);
404 	SPDK_CU_ASSERT_FATAL(pdu != NULL);
405 
406 	CU_ASSERT(pdu->bhs.opcode == ISCSI_OP_SCSI_DATAIN);
407 
408 	datah = (struct iscsi_bhs_data_in *)&pdu->bhs;
409 
410 	CU_ASSERT(datah->flags == (ISCSI_DATAIN_UNDERFLOW | ISCSI_FLAG_FINAL | ISCSI_DATAIN_STATUS));
411 	CU_ASSERT(datah->res_cnt == residual_count);
412 
413 	TAILQ_REMOVE(&g_write_pdu_list, pdu, tailq);
414 	iscsi_put_pdu(pdu);
415 
416 	CU_ASSERT(TAILQ_EMPTY(&g_write_pdu_list));
417 }
418 
419 static void
420 underflow_for_zero_read_transfer_test(void)
421 {
422 	struct spdk_iscsi_sess sess = {};
423 	struct spdk_iscsi_conn conn = {};
424 	struct spdk_iscsi_task task = {};
425 	struct spdk_scsi_dev dev = {};
426 	struct spdk_scsi_lun lun = {};
427 	struct spdk_iscsi_pdu *pdu;
428 	struct iscsi_bhs_scsi_req *scsi_req;
429 	struct iscsi_bhs_scsi_resp *resph;
430 	uint32_t residual_count = 0, data_segment_len;
431 
432 	sess.MaxBurstLength = SPDK_ISCSI_MAX_BURST_LENGTH;
433 
434 	conn.sess = &sess;
435 	conn.MaxRecvDataSegmentLength = 8192;
436 
437 	dev.lun[0] = &lun;
438 	conn.dev = &dev;
439 
440 	pdu = iscsi_get_pdu(&conn);
441 	SPDK_CU_ASSERT_FATAL(pdu != NULL);
442 
443 	scsi_req = (struct iscsi_bhs_scsi_req *)&pdu->bhs;
444 	scsi_req->read_bit = 1;
445 
446 	iscsi_task_set_pdu(&task, pdu);
447 	task.parent = NULL;
448 
449 	task.scsi.length = 512;
450 	task.scsi.transfer_len = 512;
451 	task.bytes_completed = 512;
452 	task.scsi.data_transferred = 0;
453 	task.scsi.status = SPDK_SCSI_STATUS_GOOD;
454 
455 	iscsi_task_response(&conn, &task);
456 	iscsi_put_pdu(pdu);
457 
458 	/*
459 	 * In this case, only a SCSI Response PDU is expected and
460 	 * underflow must be set in it.
461 	 * */
462 	to_be32(&residual_count, 512);
463 
464 	pdu = TAILQ_FIRST(&g_write_pdu_list);
465 	SPDK_CU_ASSERT_FATAL(pdu != NULL);
466 
467 	CU_ASSERT(pdu->bhs.opcode == ISCSI_OP_SCSI_RSP);
468 
469 	resph = (struct iscsi_bhs_scsi_resp *)&pdu->bhs;
470 
471 	CU_ASSERT(resph->flags == (ISCSI_SCSI_UNDERFLOW | 0x80));
472 
473 	data_segment_len = DGET24(resph->data_segment_len);
474 	CU_ASSERT(data_segment_len == 0);
475 	CU_ASSERT(resph->res_cnt == residual_count);
476 
477 	TAILQ_REMOVE(&g_write_pdu_list, pdu, tailq);
478 	iscsi_put_pdu(pdu);
479 
480 	CU_ASSERT(TAILQ_EMPTY(&g_write_pdu_list));
481 }
482 
483 static void
484 underflow_for_request_sense_test(void)
485 {
486 	struct spdk_iscsi_sess sess = {};
487 	struct spdk_iscsi_conn conn = {};
488 	struct spdk_iscsi_task task = {};
489 	struct spdk_scsi_dev dev = {};
490 	struct spdk_scsi_lun lun = {};
491 	struct spdk_iscsi_pdu *pdu1, *pdu2;
492 	struct iscsi_bhs_scsi_req *scsi_req;
493 	struct iscsi_bhs_data_in *datah;
494 	struct iscsi_bhs_scsi_resp *resph;
495 	uint32_t residual_count = 0, data_segment_len;
496 
497 	sess.MaxBurstLength = SPDK_ISCSI_MAX_BURST_LENGTH;
498 
499 	conn.sess = &sess;
500 	conn.MaxRecvDataSegmentLength = 8192;
501 
502 	dev.lun[0] = &lun;
503 	conn.dev = &dev;
504 
505 	pdu1 = iscsi_get_pdu(&conn);
506 	SPDK_CU_ASSERT_FATAL(pdu1 != NULL);
507 
508 	scsi_req = (struct iscsi_bhs_scsi_req *)&pdu1->bhs;
509 	scsi_req->read_bit = 1;
510 
511 	iscsi_task_set_pdu(&task, pdu1);
512 	task.parent = NULL;
513 
514 	task.scsi.iovs = &task.scsi.iov;
515 	task.scsi.iovcnt = 1;
516 	task.scsi.length = 512;
517 	task.scsi.transfer_len = 512;
518 	task.bytes_completed = 512;
519 
520 	task.scsi.sense_data_len = 18;
521 	task.scsi.data_transferred = 18;
522 	task.scsi.status = SPDK_SCSI_STATUS_GOOD;
523 
524 	iscsi_task_response(&conn, &task);
525 	iscsi_put_pdu(pdu1);
526 
527 	/*
528 	 * In this case, a SCSI Data-In PDU and a SCSI Response PDU are returned.
529 	 * Sense data are set both in payload and sense area.
530 	 * The SCSI Data-In PDU sets FINAL and the SCSI Response PDU sets UNDERFLOW.
531 	 *
532 	 * Probably there will be different implementation but keeping current SPDK
533 	 * implementation by adding UT will be valuable for any implementation.
534 	 */
535 	to_be32(&residual_count, 494);
536 
537 	pdu1 = TAILQ_FIRST(&g_write_pdu_list);
538 	SPDK_CU_ASSERT_FATAL(pdu1 != NULL);
539 
540 	CU_ASSERT(pdu1->bhs.opcode == ISCSI_OP_SCSI_DATAIN);
541 
542 	datah = (struct iscsi_bhs_data_in *)&pdu1->bhs;
543 
544 	CU_ASSERT(datah->flags == ISCSI_FLAG_FINAL);
545 
546 	data_segment_len = DGET24(datah->data_segment_len);
547 	CU_ASSERT(data_segment_len == 18);
548 	CU_ASSERT(datah->res_cnt == 0);
549 
550 	TAILQ_REMOVE(&g_write_pdu_list, pdu1, tailq);
551 	iscsi_put_pdu(pdu1);
552 
553 	pdu2 = TAILQ_FIRST(&g_write_pdu_list);
554 	/* inform scan-build (clang 6) that these pointers are not the same */
555 	SPDK_CU_ASSERT_FATAL(pdu1 != pdu2);
556 	SPDK_CU_ASSERT_FATAL(pdu2 != NULL);
557 
558 	CU_ASSERT(pdu2->bhs.opcode == ISCSI_OP_SCSI_RSP);
559 
560 	resph = (struct iscsi_bhs_scsi_resp *)&pdu2->bhs;
561 
562 	CU_ASSERT(resph->flags == (ISCSI_SCSI_UNDERFLOW | 0x80));
563 
564 	data_segment_len = DGET24(resph->data_segment_len);
565 	CU_ASSERT(data_segment_len == task.scsi.sense_data_len + 2);
566 	CU_ASSERT(resph->res_cnt == residual_count);
567 
568 	TAILQ_REMOVE(&g_write_pdu_list, pdu2, tailq);
569 	iscsi_put_pdu(pdu2);
570 
571 	CU_ASSERT(TAILQ_EMPTY(&g_write_pdu_list));
572 }
573 
574 static void
575 underflow_for_check_condition_test(void)
576 {
577 	struct spdk_iscsi_sess sess = {};
578 	struct spdk_iscsi_conn conn = {};
579 	struct spdk_iscsi_task task = {};
580 	struct spdk_scsi_dev dev = {};
581 	struct spdk_scsi_lun lun = {};
582 	struct spdk_iscsi_pdu *pdu;
583 	struct iscsi_bhs_scsi_req *scsi_req;
584 	struct iscsi_bhs_scsi_resp *resph;
585 	uint32_t data_segment_len;
586 
587 	sess.MaxBurstLength = SPDK_ISCSI_MAX_BURST_LENGTH;
588 
589 	conn.sess = &sess;
590 	conn.MaxRecvDataSegmentLength = 8192;
591 
592 	dev.lun[0] = &lun;
593 	conn.dev = &dev;
594 
595 	pdu = iscsi_get_pdu(&conn);
596 	SPDK_CU_ASSERT_FATAL(pdu != NULL);
597 
598 	scsi_req = (struct iscsi_bhs_scsi_req *)&pdu->bhs;
599 	scsi_req->read_bit = 1;
600 
601 	iscsi_task_set_pdu(&task, pdu);
602 	task.parent = NULL;
603 
604 	task.scsi.iovs = &task.scsi.iov;
605 	task.scsi.iovcnt = 1;
606 	task.scsi.length = 512;
607 	task.scsi.transfer_len = 512;
608 	task.bytes_completed = 512;
609 
610 	task.scsi.sense_data_len = 18;
611 	task.scsi.data_transferred = 18;
612 	task.scsi.status = SPDK_SCSI_STATUS_CHECK_CONDITION;
613 
614 	iscsi_task_response(&conn, &task);
615 	iscsi_put_pdu(pdu);
616 
617 	/*
618 	 * In this case, a SCSI Response PDU is returned.
619 	 * Sense data is set in sense area.
620 	 * Underflow is not set.
621 	 */
622 	pdu = TAILQ_FIRST(&g_write_pdu_list);
623 	SPDK_CU_ASSERT_FATAL(pdu != NULL);
624 
625 	CU_ASSERT(pdu->bhs.opcode == ISCSI_OP_SCSI_RSP);
626 
627 	resph = (struct iscsi_bhs_scsi_resp *)&pdu->bhs;
628 
629 	CU_ASSERT(resph->flags == 0x80);
630 
631 	data_segment_len = DGET24(resph->data_segment_len);
632 	CU_ASSERT(data_segment_len == task.scsi.sense_data_len + 2);
633 	CU_ASSERT(resph->res_cnt == 0);
634 
635 	TAILQ_REMOVE(&g_write_pdu_list, pdu, tailq);
636 	iscsi_put_pdu(pdu);
637 
638 	CU_ASSERT(TAILQ_EMPTY(&g_write_pdu_list));
639 }
640 
641 static void
642 add_transfer_task_test(void)
643 {
644 	struct spdk_iscsi_sess sess = {};
645 	struct spdk_iscsi_conn conn = {};
646 	struct spdk_iscsi_task task = {};
647 	struct spdk_iscsi_pdu *pdu, *tmp;
648 	struct iscsi_bhs_r2t *r2th;
649 	int rc, count = 0;
650 	uint32_t buffer_offset, desired_xfer_len;
651 
652 	sess.MaxBurstLength = SPDK_ISCSI_MAX_BURST_LENGTH;	/* 1M */
653 	sess.MaxOutstandingR2T = DEFAULT_MAXR2T;	/* 4 */
654 
655 	conn.sess = &sess;
656 	TAILQ_INIT(&conn.queued_r2t_tasks);
657 	TAILQ_INIT(&conn.active_r2t_tasks);
658 
659 	pdu = iscsi_get_pdu(&conn);
660 	SPDK_CU_ASSERT_FATAL(pdu != NULL);
661 
662 	pdu->data_segment_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH;	/* 64K */
663 	task.scsi.transfer_len = 16 * 1024 * 1024;
664 	iscsi_task_set_pdu(&task, pdu);
665 
666 	/* The following tests if the task is queued because R2T tasks are full. */
667 	conn.pending_r2t = DEFAULT_MAXR2T;
668 
669 	rc = add_transfer_task(&conn, &task);
670 
671 	CU_ASSERT(rc == 0);
672 	CU_ASSERT(TAILQ_FIRST(&conn.queued_r2t_tasks) == &task);
673 
674 	TAILQ_REMOVE(&conn.queued_r2t_tasks, &task, link);
675 	CU_ASSERT(TAILQ_EMPTY(&conn.queued_r2t_tasks));
676 
677 	/* The following tests if multiple R2Ts are issued. */
678 	conn.pending_r2t = 0;
679 
680 	rc = add_transfer_task(&conn, &task);
681 
682 	CU_ASSERT(rc == 0);
683 	CU_ASSERT(TAILQ_FIRST(&conn.active_r2t_tasks) == &task);
684 
685 	TAILQ_REMOVE(&conn.active_r2t_tasks, &task, link);
686 	CU_ASSERT(TAILQ_EMPTY(&conn.active_r2t_tasks));
687 
688 	CU_ASSERT(conn.data_out_cnt == 255);
689 	CU_ASSERT(conn.pending_r2t == 1);
690 	CU_ASSERT(conn.ttt == 1);
691 
692 	CU_ASSERT(task.data_out_cnt == 255);
693 	CU_ASSERT(task.ttt == 1);
694 	CU_ASSERT(task.outstanding_r2t == sess.MaxOutstandingR2T);
695 	CU_ASSERT(task.next_r2t_offset ==
696 		  pdu->data_segment_len + sess.MaxBurstLength * sess.MaxOutstandingR2T);
697 
698 
699 	while (!TAILQ_EMPTY(&g_write_pdu_list)) {
700 		tmp = TAILQ_FIRST(&g_write_pdu_list);
701 		TAILQ_REMOVE(&g_write_pdu_list, tmp, tailq);
702 
703 		r2th = (struct iscsi_bhs_r2t *)&tmp->bhs;
704 
705 		buffer_offset = from_be32(&r2th->buffer_offset);
706 		CU_ASSERT(buffer_offset == pdu->data_segment_len + sess.MaxBurstLength * count);
707 
708 		desired_xfer_len = from_be32(&r2th->desired_xfer_len);
709 		CU_ASSERT(desired_xfer_len == sess.MaxBurstLength);
710 
711 		iscsi_put_pdu(tmp);
712 		count++;
713 	}
714 
715 	CU_ASSERT(count == DEFAULT_MAXR2T);
716 
717 	iscsi_put_pdu(pdu);
718 }
719 
720 static void
721 get_transfer_task_test(void)
722 {
723 	struct spdk_iscsi_sess sess = {};
724 	struct spdk_iscsi_conn conn = {};
725 	struct spdk_iscsi_task task1 = {}, task2 = {}, *task;
726 	struct spdk_iscsi_pdu *pdu1, *pdu2, *pdu;
727 	int rc;
728 
729 	sess.MaxBurstLength = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH;
730 	sess.MaxOutstandingR2T = 1;
731 
732 	conn.sess = &sess;
733 	TAILQ_INIT(&conn.active_r2t_tasks);
734 
735 	pdu1 = iscsi_get_pdu(&conn);
736 	SPDK_CU_ASSERT_FATAL(pdu1 != NULL);
737 
738 	pdu1->data_segment_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH;
739 	task1.scsi.transfer_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH;
740 	iscsi_task_set_pdu(&task1, pdu1);
741 
742 	rc = add_transfer_task(&conn, &task1);
743 	CU_ASSERT(rc == 0);
744 
745 	pdu2 = iscsi_get_pdu(&conn);
746 	SPDK_CU_ASSERT_FATAL(pdu2 != NULL);
747 
748 	pdu2->data_segment_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH;
749 	task2.scsi.transfer_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH;
750 	iscsi_task_set_pdu(&task2, pdu2);
751 
752 	rc = add_transfer_task(&conn, &task2);
753 	CU_ASSERT(rc == 0);
754 
755 	task = get_transfer_task(&conn, 1);
756 	CU_ASSERT(task == &task1);
757 
758 	task = get_transfer_task(&conn, 2);
759 	CU_ASSERT(task == &task2);
760 
761 	while (!TAILQ_EMPTY(&conn.active_r2t_tasks)) {
762 		task = TAILQ_FIRST(&conn.active_r2t_tasks);
763 		TAILQ_REMOVE(&conn.active_r2t_tasks, task, link);
764 	}
765 
766 	while (!TAILQ_EMPTY(&g_write_pdu_list)) {
767 		pdu = TAILQ_FIRST(&g_write_pdu_list);
768 		TAILQ_REMOVE(&g_write_pdu_list, pdu, tailq);
769 		iscsi_put_pdu(pdu);
770 	}
771 
772 	iscsi_put_pdu(pdu2);
773 	iscsi_put_pdu(pdu1);
774 }
775 
776 static void
777 del_transfer_task_test(void)
778 {
779 	struct spdk_iscsi_sess sess = {};
780 	struct spdk_iscsi_conn conn = {};
781 	struct spdk_iscsi_task *task1, *task2, *task3, *task4, *task5;
782 	struct spdk_iscsi_pdu *pdu1, *pdu2, *pdu3, *pdu4, *pdu5, *pdu;
783 	int rc;
784 
785 	sess.MaxBurstLength = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH;
786 	sess.MaxOutstandingR2T = 1;
787 
788 	conn.sess = &sess;
789 	TAILQ_INIT(&conn.active_r2t_tasks);
790 	TAILQ_INIT(&conn.queued_r2t_tasks);
791 
792 	pdu1 = iscsi_get_pdu(&conn);
793 	SPDK_CU_ASSERT_FATAL(pdu1 != NULL);
794 
795 	pdu1->data_segment_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH;
796 
797 	task1 = iscsi_task_get(&conn, NULL, NULL);
798 	SPDK_CU_ASSERT_FATAL(task1 != NULL);
799 
800 	task1->scsi.transfer_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH;
801 	iscsi_task_set_pdu(task1, pdu1);
802 	task1->tag = 11;
803 
804 	rc = add_transfer_task(&conn, task1);
805 	CU_ASSERT(rc == 0);
806 
807 	pdu2 = iscsi_get_pdu(&conn);
808 	SPDK_CU_ASSERT_FATAL(pdu2 != NULL);
809 
810 	pdu2->data_segment_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH;
811 
812 	task2 = iscsi_task_get(&conn, NULL, NULL);
813 	SPDK_CU_ASSERT_FATAL(task2 != NULL);
814 
815 	task2->scsi.transfer_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH;
816 	iscsi_task_set_pdu(task2, pdu2);
817 	task2->tag = 12;
818 
819 	rc = add_transfer_task(&conn, task2);
820 	CU_ASSERT(rc == 0);
821 
822 	pdu3 = iscsi_get_pdu(&conn);
823 	SPDK_CU_ASSERT_FATAL(pdu3 != NULL);
824 
825 	pdu3->data_segment_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH;
826 
827 	task3 = iscsi_task_get(&conn, NULL, NULL);
828 	SPDK_CU_ASSERT_FATAL(task3 != NULL);
829 
830 	task3->scsi.transfer_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH;
831 	iscsi_task_set_pdu(task3, pdu3);
832 	task3->tag = 13;
833 
834 	rc = add_transfer_task(&conn, task3);
835 	CU_ASSERT(rc == 0);
836 
837 	pdu4 = iscsi_get_pdu(&conn);
838 	SPDK_CU_ASSERT_FATAL(pdu4 != NULL);
839 
840 	pdu4->data_segment_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH;
841 
842 	task4 = iscsi_task_get(&conn, NULL, NULL);
843 	SPDK_CU_ASSERT_FATAL(task4 != NULL);
844 
845 	task4->scsi.transfer_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH;
846 	iscsi_task_set_pdu(task4, pdu4);
847 	task4->tag = 14;
848 
849 	rc = add_transfer_task(&conn, task4);
850 	CU_ASSERT(rc == 0);
851 
852 	pdu5 = iscsi_get_pdu(&conn);
853 	SPDK_CU_ASSERT_FATAL(pdu5 != NULL);
854 
855 	pdu5->data_segment_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH;
856 
857 	task5 = iscsi_task_get(&conn, NULL, NULL);
858 	SPDK_CU_ASSERT_FATAL(task5 != NULL);
859 
860 	task5->scsi.transfer_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH;
861 	iscsi_task_set_pdu(task5, pdu5);
862 	task5->tag = 15;
863 
864 	rc = add_transfer_task(&conn, task5);
865 	CU_ASSERT(rc == 0);
866 
867 	CU_ASSERT(get_transfer_task(&conn, 1) == task1);
868 	CU_ASSERT(get_transfer_task(&conn, 5) == NULL);
869 	iscsi_del_transfer_task(&conn, 11);
870 	CU_ASSERT(get_transfer_task(&conn, 1) == NULL);
871 	CU_ASSERT(get_transfer_task(&conn, 5) == task5);
872 
873 	CU_ASSERT(get_transfer_task(&conn, 2) == task2);
874 	iscsi_del_transfer_task(&conn, 12);
875 	CU_ASSERT(get_transfer_task(&conn, 2) == NULL);
876 
877 	CU_ASSERT(get_transfer_task(&conn, 3) == task3);
878 	iscsi_del_transfer_task(&conn, 13);
879 	CU_ASSERT(get_transfer_task(&conn, 3) == NULL);
880 
881 	CU_ASSERT(get_transfer_task(&conn, 4) == task4);
882 	iscsi_del_transfer_task(&conn, 14);
883 	CU_ASSERT(get_transfer_task(&conn, 4) == NULL);
884 
885 	CU_ASSERT(get_transfer_task(&conn, 5) == task5);
886 	iscsi_del_transfer_task(&conn, 15);
887 	CU_ASSERT(get_transfer_task(&conn, 5) == NULL);
888 
889 	CU_ASSERT(TAILQ_EMPTY(&conn.active_r2t_tasks));
890 
891 	while (!TAILQ_EMPTY(&g_write_pdu_list)) {
892 		pdu = TAILQ_FIRST(&g_write_pdu_list);
893 		TAILQ_REMOVE(&g_write_pdu_list, pdu, tailq);
894 		iscsi_put_pdu(pdu);
895 	}
896 
897 	iscsi_put_pdu(pdu5);
898 	iscsi_put_pdu(pdu4);
899 	iscsi_put_pdu(pdu3);
900 	iscsi_put_pdu(pdu2);
901 	iscsi_put_pdu(pdu1);
902 }
903 
904 static void
905 clear_all_transfer_tasks_test(void)
906 {
907 	struct spdk_iscsi_sess sess = {};
908 	struct spdk_iscsi_conn conn = {};
909 	struct spdk_iscsi_task *task1, *task2, *task3, *task4, *task5, *task6;
910 	struct spdk_iscsi_pdu *pdu1, *pdu2, *pdu3, *pdu4, *pdu5, *pdu6, *pdu;
911 	struct spdk_iscsi_pdu *mgmt_pdu1, *mgmt_pdu2;
912 	struct spdk_scsi_lun lun1 = {}, lun2 = {};
913 	uint32_t alloc_cmd_sn;
914 	int rc;
915 
916 	sess.MaxBurstLength = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH;
917 	sess.MaxOutstandingR2T = 1;
918 
919 	conn.sess = &sess;
920 	TAILQ_INIT(&conn.active_r2t_tasks);
921 	TAILQ_INIT(&conn.queued_r2t_tasks);
922 
923 	alloc_cmd_sn = 10;
924 
925 	task1 = iscsi_task_get(&conn, NULL, NULL);
926 	SPDK_CU_ASSERT_FATAL(task1 != NULL);
927 	pdu1 = iscsi_get_pdu(&conn);
928 	SPDK_CU_ASSERT_FATAL(pdu1 != NULL);
929 
930 	pdu1->data_segment_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH;
931 	pdu1->cmd_sn = alloc_cmd_sn;
932 	alloc_cmd_sn++;
933 	task1->scsi.transfer_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH;
934 	task1->scsi.lun = &lun1;
935 	iscsi_task_set_pdu(task1, pdu1);
936 
937 	rc = add_transfer_task(&conn, task1);
938 	CU_ASSERT(rc == 0);
939 
940 	mgmt_pdu1 = iscsi_get_pdu(&conn);
941 	SPDK_CU_ASSERT_FATAL(mgmt_pdu1 != NULL);
942 
943 	mgmt_pdu1->cmd_sn = alloc_cmd_sn;
944 	alloc_cmd_sn++;
945 
946 	task2 = iscsi_task_get(&conn, NULL, NULL);
947 	SPDK_CU_ASSERT_FATAL(task2 != NULL);
948 	pdu2 = iscsi_get_pdu(&conn);
949 	SPDK_CU_ASSERT_FATAL(pdu2 != NULL);
950 
951 	pdu2->data_segment_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH;
952 	pdu2->cmd_sn = alloc_cmd_sn;
953 	alloc_cmd_sn++;
954 	task2->scsi.transfer_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH;
955 	task2->scsi.lun = &lun1;
956 	iscsi_task_set_pdu(task2, pdu2);
957 
958 	rc = add_transfer_task(&conn, task2);
959 	CU_ASSERT(rc == 0);
960 
961 	task3 = iscsi_task_get(&conn, NULL, NULL);
962 	SPDK_CU_ASSERT_FATAL(task3 != NULL);
963 	pdu3 = iscsi_get_pdu(&conn);
964 	SPDK_CU_ASSERT_FATAL(pdu3 != NULL);
965 
966 	pdu3->data_segment_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH;
967 	pdu3->cmd_sn = alloc_cmd_sn;
968 	alloc_cmd_sn++;
969 	task3->scsi.transfer_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH;
970 	task3->scsi.lun = &lun1;
971 	iscsi_task_set_pdu(task3, pdu3);
972 
973 	rc = add_transfer_task(&conn, task3);
974 	CU_ASSERT(rc == 0);
975 
976 	task4 = iscsi_task_get(&conn, NULL, NULL);
977 	SPDK_CU_ASSERT_FATAL(task4 != NULL);
978 	pdu4 = iscsi_get_pdu(&conn);
979 	SPDK_CU_ASSERT_FATAL(pdu4 != NULL);
980 
981 	pdu4->data_segment_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH;
982 	pdu4->cmd_sn = alloc_cmd_sn;
983 	alloc_cmd_sn++;
984 	task4->scsi.transfer_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH;
985 	task4->scsi.lun = &lun2;
986 	iscsi_task_set_pdu(task4, pdu4);
987 
988 	rc = add_transfer_task(&conn, task4);
989 	CU_ASSERT(rc == 0);
990 
991 	task5 = iscsi_task_get(&conn, NULL, NULL);
992 	SPDK_CU_ASSERT_FATAL(task5 != NULL);
993 	pdu5 = iscsi_get_pdu(&conn);
994 	SPDK_CU_ASSERT_FATAL(pdu5 != NULL);
995 
996 	pdu5->data_segment_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH;
997 	pdu5->cmd_sn = alloc_cmd_sn;
998 	alloc_cmd_sn++;
999 	task5->scsi.transfer_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH;
1000 	task5->scsi.lun = &lun2;
1001 	iscsi_task_set_pdu(task5, pdu5);
1002 
1003 	rc = add_transfer_task(&conn, task5);
1004 	CU_ASSERT(rc == 0);
1005 
1006 	mgmt_pdu2 = iscsi_get_pdu(&conn);
1007 	SPDK_CU_ASSERT_FATAL(mgmt_pdu2 != NULL);
1008 
1009 	mgmt_pdu2->cmd_sn = alloc_cmd_sn;
1010 	alloc_cmd_sn++;
1011 
1012 	task6 = iscsi_task_get(&conn, NULL, NULL);
1013 	SPDK_CU_ASSERT_FATAL(task6 != NULL);
1014 	pdu6 = iscsi_get_pdu(&conn);
1015 	SPDK_CU_ASSERT_FATAL(pdu6 != NULL);
1016 
1017 	pdu6->data_segment_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH;
1018 	pdu6->cmd_sn = alloc_cmd_sn;
1019 	alloc_cmd_sn++;
1020 	task5->scsi.transfer_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH;
1021 	task6->scsi.lun = &lun2;
1022 	iscsi_task_set_pdu(task6, pdu6);
1023 
1024 	rc = add_transfer_task(&conn, task6);
1025 	CU_ASSERT(rc == 0);
1026 
1027 	CU_ASSERT(conn.ttt == 4);
1028 
1029 	CU_ASSERT(get_transfer_task(&conn, 1) == task1);
1030 	CU_ASSERT(get_transfer_task(&conn, 2) == task2);
1031 	CU_ASSERT(get_transfer_task(&conn, 3) == task3);
1032 	CU_ASSERT(get_transfer_task(&conn, 4) == task4);
1033 	CU_ASSERT(get_transfer_task(&conn, 5) == NULL);
1034 
1035 	iscsi_clear_all_transfer_task(&conn, &lun1, mgmt_pdu1);
1036 
1037 	CU_ASSERT(!TAILQ_EMPTY(&conn.queued_r2t_tasks));
1038 	CU_ASSERT(get_transfer_task(&conn, 1) == NULL);
1039 	CU_ASSERT(get_transfer_task(&conn, 2) == task2);
1040 	CU_ASSERT(get_transfer_task(&conn, 3) == task3);
1041 	CU_ASSERT(get_transfer_task(&conn, 4) == task4);
1042 	CU_ASSERT(get_transfer_task(&conn, 5) == task5);
1043 	CU_ASSERT(get_transfer_task(&conn, 6) == NULL);
1044 
1045 	iscsi_clear_all_transfer_task(&conn, &lun1, NULL);
1046 
1047 	CU_ASSERT(TAILQ_EMPTY(&conn.queued_r2t_tasks));
1048 	CU_ASSERT(get_transfer_task(&conn, 1) == NULL);
1049 	CU_ASSERT(get_transfer_task(&conn, 2) == NULL);
1050 	CU_ASSERT(get_transfer_task(&conn, 3) == NULL);
1051 	CU_ASSERT(get_transfer_task(&conn, 4) == task4);
1052 	CU_ASSERT(get_transfer_task(&conn, 5) == task5);
1053 	CU_ASSERT(get_transfer_task(&conn, 6) == task6);
1054 
1055 	iscsi_clear_all_transfer_task(&conn, &lun2, mgmt_pdu2);
1056 
1057 	CU_ASSERT(get_transfer_task(&conn, 4) == NULL);
1058 	CU_ASSERT(get_transfer_task(&conn, 5) == NULL);
1059 	CU_ASSERT(get_transfer_task(&conn, 6) == task6);
1060 
1061 	iscsi_clear_all_transfer_task(&conn, NULL, NULL);
1062 
1063 	CU_ASSERT(get_transfer_task(&conn, 6) == NULL);
1064 
1065 	CU_ASSERT(TAILQ_EMPTY(&conn.active_r2t_tasks));
1066 	while (!TAILQ_EMPTY(&g_write_pdu_list)) {
1067 		pdu = TAILQ_FIRST(&g_write_pdu_list);
1068 		TAILQ_REMOVE(&g_write_pdu_list, pdu, tailq);
1069 		iscsi_put_pdu(pdu);
1070 	}
1071 
1072 	iscsi_put_pdu(mgmt_pdu2);
1073 	iscsi_put_pdu(mgmt_pdu1);
1074 	iscsi_put_pdu(pdu6);
1075 	iscsi_put_pdu(pdu5);
1076 	iscsi_put_pdu(pdu4);
1077 	iscsi_put_pdu(pdu3);
1078 	iscsi_put_pdu(pdu2);
1079 	iscsi_put_pdu(pdu1);
1080 }
1081 
1082 static void
1083 build_iovs_test(void)
1084 {
1085 	struct spdk_iscsi_conn conn = {};
1086 	struct spdk_iscsi_pdu pdu = {};
1087 	struct iovec iovs[5] = {};
1088 	uint8_t *data;
1089 	uint32_t mapped_length = 0;
1090 	int rc;
1091 
1092 	conn.header_digest = true;
1093 	conn.data_digest = true;
1094 
1095 	DSET24(&pdu.bhs.data_segment_len, 512);
1096 	data = calloc(1, 512);
1097 	SPDK_CU_ASSERT_FATAL(data != NULL);
1098 	pdu.data = data;
1099 
1100 	pdu.bhs.total_ahs_len = 0;
1101 	pdu.bhs.opcode = ISCSI_OP_SCSI;
1102 
1103 	pdu.writev_offset = 0;
1104 	rc = iscsi_build_iovs(&conn, iovs, 5, &pdu, &mapped_length);
1105 	CU_ASSERT(rc == 4);
1106 	CU_ASSERT(iovs[0].iov_base == (void *)&pdu.bhs);
1107 	CU_ASSERT(iovs[0].iov_len == ISCSI_BHS_LEN);
1108 	CU_ASSERT(iovs[1].iov_base == (void *)pdu.header_digest);
1109 	CU_ASSERT(iovs[1].iov_len == ISCSI_DIGEST_LEN);
1110 	CU_ASSERT(iovs[2].iov_base == (void *)pdu.data);
1111 	CU_ASSERT(iovs[2].iov_len == 512);
1112 	CU_ASSERT(iovs[3].iov_base == (void *)pdu.data_digest);
1113 	CU_ASSERT(iovs[3].iov_len == ISCSI_DIGEST_LEN);
1114 	CU_ASSERT(mapped_length == ISCSI_BHS_LEN + ISCSI_DIGEST_LEN + 512 + ISCSI_DIGEST_LEN);
1115 
1116 	pdu.writev_offset = ISCSI_BHS_LEN / 2;
1117 	rc = iscsi_build_iovs(&conn, iovs, 5, &pdu, &mapped_length);
1118 	CU_ASSERT(rc == 4);
1119 	CU_ASSERT(iovs[0].iov_base == (void *)((uint8_t *)&pdu.bhs + ISCSI_BHS_LEN / 2));
1120 	CU_ASSERT(iovs[0].iov_len == ISCSI_BHS_LEN / 2);
1121 	CU_ASSERT(iovs[1].iov_base == (void *)pdu.header_digest);
1122 	CU_ASSERT(iovs[1].iov_len == ISCSI_DIGEST_LEN);
1123 	CU_ASSERT(iovs[2].iov_base == (void *)pdu.data);
1124 	CU_ASSERT(iovs[2].iov_len == 512);
1125 	CU_ASSERT(iovs[3].iov_base == (void *)pdu.data_digest);
1126 	CU_ASSERT(iovs[3].iov_len == ISCSI_DIGEST_LEN);
1127 	CU_ASSERT(mapped_length == ISCSI_BHS_LEN / 2 + ISCSI_DIGEST_LEN + 512 + ISCSI_DIGEST_LEN);
1128 
1129 	pdu.writev_offset = ISCSI_BHS_LEN;
1130 	rc = iscsi_build_iovs(&conn, iovs, 5, &pdu, &mapped_length);
1131 	CU_ASSERT(rc == 3);
1132 	CU_ASSERT(iovs[0].iov_base == (void *)pdu.header_digest);
1133 	CU_ASSERT(iovs[0].iov_len == ISCSI_DIGEST_LEN);
1134 	CU_ASSERT(iovs[1].iov_base == (void *)pdu.data);
1135 	CU_ASSERT(iovs[1].iov_len == 512);
1136 	CU_ASSERT(iovs[2].iov_base == (void *)pdu.data_digest);
1137 	CU_ASSERT(iovs[2].iov_len == ISCSI_DIGEST_LEN);
1138 	CU_ASSERT(mapped_length == ISCSI_DIGEST_LEN + 512 + ISCSI_DIGEST_LEN);
1139 
1140 	pdu.writev_offset = ISCSI_BHS_LEN + ISCSI_DIGEST_LEN / 2;
1141 	rc = iscsi_build_iovs(&conn, iovs, 5, &pdu, &mapped_length);
1142 	CU_ASSERT(rc == 3);
1143 	CU_ASSERT(iovs[0].iov_base == (void *)((uint8_t *)pdu.header_digest + ISCSI_DIGEST_LEN / 2));
1144 	CU_ASSERT(iovs[0].iov_len == ISCSI_DIGEST_LEN / 2);
1145 	CU_ASSERT(iovs[1].iov_base == (void *)pdu.data);
1146 	CU_ASSERT(iovs[1].iov_len == 512);
1147 	CU_ASSERT(iovs[2].iov_base == (void *)pdu.data_digest);
1148 	CU_ASSERT(iovs[2].iov_len == ISCSI_DIGEST_LEN);
1149 	CU_ASSERT(mapped_length == ISCSI_DIGEST_LEN / 2 + 512 + ISCSI_DIGEST_LEN);
1150 
1151 	pdu.writev_offset = ISCSI_BHS_LEN + ISCSI_DIGEST_LEN;
1152 	rc = iscsi_build_iovs(&conn, iovs, 5, &pdu, &mapped_length);
1153 	CU_ASSERT(rc == 2);
1154 	CU_ASSERT(iovs[0].iov_base == (void *)pdu.data);
1155 	CU_ASSERT(iovs[0].iov_len == 512);
1156 	CU_ASSERT(iovs[1].iov_base == (void *)pdu.data_digest);
1157 	CU_ASSERT(iovs[1].iov_len == ISCSI_DIGEST_LEN);
1158 	CU_ASSERT(mapped_length == 512 + ISCSI_DIGEST_LEN);
1159 
1160 	pdu.writev_offset = ISCSI_BHS_LEN + ISCSI_DIGEST_LEN + 512;
1161 	rc = iscsi_build_iovs(&conn, iovs, 5, &pdu, &mapped_length);
1162 	CU_ASSERT(rc == 1);
1163 	CU_ASSERT(iovs[0].iov_base == (void *)pdu.data_digest);
1164 	CU_ASSERT(iovs[0].iov_len == ISCSI_DIGEST_LEN);
1165 	CU_ASSERT(mapped_length == ISCSI_DIGEST_LEN);
1166 
1167 	pdu.writev_offset = ISCSI_BHS_LEN + ISCSI_DIGEST_LEN + 512 + ISCSI_DIGEST_LEN / 2;
1168 	rc = iscsi_build_iovs(&conn, iovs, 5, &pdu, &mapped_length);
1169 	CU_ASSERT(rc == 1);
1170 	CU_ASSERT(iovs[0].iov_base == (void *)((uint8_t *)pdu.data_digest + ISCSI_DIGEST_LEN / 2));
1171 	CU_ASSERT(iovs[0].iov_len == ISCSI_DIGEST_LEN / 2);
1172 	CU_ASSERT(mapped_length == ISCSI_DIGEST_LEN / 2);
1173 
1174 	pdu.writev_offset = ISCSI_BHS_LEN + ISCSI_DIGEST_LEN + 512 + ISCSI_DIGEST_LEN;
1175 	rc = iscsi_build_iovs(&conn, iovs, 5, &pdu, &mapped_length);
1176 	CU_ASSERT(rc == 0);
1177 	CU_ASSERT(mapped_length == 0);
1178 
1179 	pdu.writev_offset = 0;
1180 	rc = iscsi_build_iovs(&conn, iovs, 1, &pdu, &mapped_length);
1181 	CU_ASSERT(rc == 1);
1182 	CU_ASSERT(iovs[0].iov_base == (void *)&pdu.bhs);
1183 	CU_ASSERT(iovs[0].iov_len == ISCSI_BHS_LEN);
1184 	CU_ASSERT(mapped_length == ISCSI_BHS_LEN);
1185 
1186 	rc = iscsi_build_iovs(&conn, iovs, 2, &pdu, &mapped_length);
1187 	CU_ASSERT(rc == 2);
1188 	CU_ASSERT(iovs[0].iov_base == (void *)&pdu.bhs);
1189 	CU_ASSERT(iovs[0].iov_len == ISCSI_BHS_LEN);
1190 	CU_ASSERT(iovs[1].iov_base == (void *)pdu.header_digest);
1191 	CU_ASSERT(iovs[1].iov_len == ISCSI_DIGEST_LEN);
1192 	CU_ASSERT(mapped_length == ISCSI_BHS_LEN + ISCSI_DIGEST_LEN);
1193 
1194 	rc = iscsi_build_iovs(&conn, iovs, 3, &pdu, &mapped_length);
1195 	CU_ASSERT(rc == 3);
1196 	CU_ASSERT(iovs[0].iov_base == (void *)&pdu.bhs);
1197 	CU_ASSERT(iovs[0].iov_len == ISCSI_BHS_LEN);
1198 	CU_ASSERT(iovs[1].iov_base == (void *)pdu.header_digest);
1199 	CU_ASSERT(iovs[1].iov_len == ISCSI_DIGEST_LEN);
1200 	CU_ASSERT(iovs[2].iov_base == (void *)pdu.data);
1201 	CU_ASSERT(iovs[2].iov_len == 512);
1202 	CU_ASSERT(mapped_length == ISCSI_BHS_LEN + ISCSI_DIGEST_LEN + 512);
1203 
1204 	rc = iscsi_build_iovs(&conn, iovs, 4, &pdu, &mapped_length);
1205 	CU_ASSERT(rc == 4);
1206 	CU_ASSERT(iovs[0].iov_base == (void *)&pdu.bhs);
1207 	CU_ASSERT(iovs[0].iov_len == ISCSI_BHS_LEN);
1208 	CU_ASSERT(iovs[1].iov_base == (void *)pdu.header_digest);
1209 	CU_ASSERT(iovs[1].iov_len == ISCSI_DIGEST_LEN);
1210 	CU_ASSERT(iovs[2].iov_base == (void *)pdu.data);
1211 	CU_ASSERT(iovs[2].iov_len == 512);
1212 	CU_ASSERT(iovs[3].iov_base == (void *)pdu.data_digest);
1213 	CU_ASSERT(iovs[3].iov_len == ISCSI_DIGEST_LEN);
1214 	CU_ASSERT(mapped_length == ISCSI_BHS_LEN + ISCSI_DIGEST_LEN + 512 + ISCSI_DIGEST_LEN);
1215 
1216 	free(data);
1217 }
1218 
1219 static void
1220 build_iovs_with_md_test(void)
1221 {
1222 	struct spdk_iscsi_conn conn = {};
1223 	struct spdk_iscsi_pdu pdu = {};
1224 	struct iovec iovs[6] = {};
1225 	uint8_t *data;
1226 	uint32_t mapped_length = 0;
1227 	int rc;
1228 
1229 	conn.header_digest = true;
1230 	conn.data_digest = true;
1231 
1232 	DSET24(&pdu.bhs.data_segment_len, 4096 * 2);
1233 	data = calloc(1, (4096 + 128) * 2);
1234 	SPDK_CU_ASSERT_FATAL(data != NULL);
1235 	pdu.data = data;
1236 	pdu.data_buf_len = (4096 + 128) * 2;
1237 
1238 	pdu.bhs.total_ahs_len = 0;
1239 	pdu.bhs.opcode = ISCSI_OP_SCSI;
1240 
1241 	rc = spdk_dif_ctx_init(&pdu.dif_ctx, 4096 + 128, 128, true, false, SPDK_DIF_TYPE1,
1242 			       0, 0, 0, 0, 0, 0);
1243 	CU_ASSERT(rc == 0);
1244 
1245 	pdu.dif_insert_or_strip = true;
1246 
1247 	pdu.writev_offset = 0;
1248 	rc = iscsi_build_iovs(&conn, iovs, 6, &pdu, &mapped_length);
1249 	CU_ASSERT(rc == 5);
1250 	CU_ASSERT(iovs[0].iov_base == (void *)&pdu.bhs);
1251 	CU_ASSERT(iovs[0].iov_len == ISCSI_BHS_LEN);
1252 	CU_ASSERT(iovs[1].iov_base == (void *)pdu.header_digest);
1253 	CU_ASSERT(iovs[1].iov_len == ISCSI_DIGEST_LEN);
1254 	CU_ASSERT(iovs[2].iov_base == (void *)pdu.data);
1255 	CU_ASSERT(iovs[2].iov_len == 4096);
1256 	CU_ASSERT(iovs[3].iov_base == (void *)(pdu.data + 4096 + 128));
1257 	CU_ASSERT(iovs[3].iov_len == 4096);
1258 	CU_ASSERT(iovs[4].iov_base == (void *)pdu.data_digest);
1259 	CU_ASSERT(iovs[4].iov_len == ISCSI_DIGEST_LEN);
1260 	CU_ASSERT(mapped_length == ISCSI_BHS_LEN + ISCSI_DIGEST_LEN + 4096 * 2 + ISCSI_DIGEST_LEN);
1261 
1262 	pdu.writev_offset = ISCSI_BHS_LEN + ISCSI_DIGEST_LEN + 2048;
1263 	rc = iscsi_build_iovs(&conn, iovs, 6, &pdu, &mapped_length);
1264 	CU_ASSERT(rc == 3);
1265 	CU_ASSERT(iovs[0].iov_base == (void *)(pdu.data + 2048));
1266 	CU_ASSERT(iovs[0].iov_len == 2048);
1267 	CU_ASSERT(iovs[1].iov_base == (void *)(pdu.data + 4096 + 128));
1268 	CU_ASSERT(iovs[1].iov_len == 4096);
1269 	CU_ASSERT(iovs[2].iov_base == (void *)pdu.data_digest);
1270 	CU_ASSERT(iovs[2].iov_len == ISCSI_DIGEST_LEN);
1271 	CU_ASSERT(mapped_length == 2048 + 4096 + ISCSI_DIGEST_LEN);
1272 
1273 	pdu.writev_offset = ISCSI_BHS_LEN + ISCSI_DIGEST_LEN + 4096 * 2;
1274 	rc = iscsi_build_iovs(&conn, iovs, 6, &pdu, &mapped_length);
1275 	CU_ASSERT(rc == 1);
1276 	CU_ASSERT(iovs[0].iov_base == (void *)pdu.data_digest);
1277 	CU_ASSERT(iovs[0].iov_len == ISCSI_DIGEST_LEN);
1278 	CU_ASSERT(mapped_length == ISCSI_DIGEST_LEN);
1279 
1280 	pdu.writev_offset = 0;
1281 	rc = iscsi_build_iovs(&conn, iovs, 3, &pdu, &mapped_length);
1282 	CU_ASSERT(rc == 3);
1283 	CU_ASSERT(iovs[0].iov_base == (void *)&pdu.bhs);
1284 	CU_ASSERT(iovs[0].iov_len == ISCSI_BHS_LEN);
1285 	CU_ASSERT(iovs[1].iov_base == (void *)pdu.header_digest);
1286 	CU_ASSERT(iovs[1].iov_len == ISCSI_DIGEST_LEN);
1287 	CU_ASSERT(iovs[2].iov_base == (void *)pdu.data);
1288 	CU_ASSERT(iovs[2].iov_len == 4096);
1289 	CU_ASSERT(mapped_length == ISCSI_BHS_LEN + ISCSI_DIGEST_LEN + 4096);
1290 
1291 	free(data);
1292 }
1293 
1294 static void
1295 check_iscsi_reject(struct spdk_iscsi_pdu *pdu, uint8_t reason)
1296 {
1297 	struct spdk_iscsi_pdu *rsp_pdu;
1298 	struct iscsi_bhs_reject *reject_bhs;
1299 
1300 	CU_ASSERT(pdu->is_rejected == true);
1301 	rsp_pdu = TAILQ_FIRST(&g_write_pdu_list);
1302 	CU_ASSERT(rsp_pdu != NULL);
1303 	reject_bhs = (struct iscsi_bhs_reject *)&rsp_pdu->bhs;
1304 	CU_ASSERT(reject_bhs->reason == reason);
1305 
1306 	TAILQ_REMOVE(&g_write_pdu_list, rsp_pdu, tailq);
1307 	iscsi_put_pdu(rsp_pdu);
1308 	pdu->is_rejected = false;
1309 }
1310 
1311 static void
1312 check_login_response(uint8_t status_class, uint8_t status_detail)
1313 {
1314 	struct spdk_iscsi_pdu *rsp_pdu;
1315 	struct iscsi_bhs_login_rsp *login_rsph;
1316 
1317 	rsp_pdu = TAILQ_FIRST(&g_write_pdu_list);
1318 	CU_ASSERT(rsp_pdu != NULL);
1319 	login_rsph = (struct iscsi_bhs_login_rsp *)&rsp_pdu->bhs;
1320 	CU_ASSERT(login_rsph->status_class == status_class);
1321 	CU_ASSERT(login_rsph->status_detail == status_detail);
1322 
1323 	TAILQ_REMOVE(&g_write_pdu_list, rsp_pdu, tailq);
1324 	iscsi_put_pdu(rsp_pdu);
1325 }
1326 
1327 static void
1328 pdu_hdr_op_login_test(void)
1329 {
1330 	struct spdk_iscsi_sess sess = {};
1331 	struct spdk_iscsi_conn conn = {};
1332 	struct spdk_iscsi_pdu pdu = {};
1333 	struct iscsi_bhs_login_req *login_reqh;
1334 	int rc;
1335 
1336 	login_reqh = (struct iscsi_bhs_login_req *)&pdu.bhs;
1337 
1338 	/* Case 1 - On discovery session, target only accepts text requests with the
1339 	 * SendTargets key and logout request with reason "close the session".
1340 	 */
1341 	sess.session_type = SESSION_TYPE_DISCOVERY;
1342 	conn.full_feature = true;
1343 	conn.sess = &sess;
1344 
1345 	rc = iscsi_pdu_hdr_op_login(&conn, &pdu);
1346 	CU_ASSERT(rc == SPDK_ISCSI_CONNECTION_FATAL);
1347 
1348 	/* Case 2 - Data segment length is limited to be not more than 8KB, the default
1349 	 * FirstBurstLength, for login request.
1350 	 */
1351 	sess.session_type = SESSION_TYPE_INVALID;
1352 	conn.full_feature = false;
1353 	conn.sess = NULL;
1354 	pdu.data_segment_len = SPDK_ISCSI_FIRST_BURST_LENGTH + 1;
1355 
1356 	rc = iscsi_pdu_hdr_op_login(&conn, &pdu);
1357 	CU_ASSERT(rc == 0);
1358 	check_iscsi_reject(&pdu, ISCSI_REASON_PROTOCOL_ERROR);
1359 
1360 	/* Case 3 - PDU pool is empty */
1361 	pdu.data_segment_len = SPDK_ISCSI_FIRST_BURST_LENGTH;
1362 	g_pdu_pool_is_empty = true;
1363 
1364 	rc = iscsi_pdu_hdr_op_login(&conn, &pdu);
1365 	CU_ASSERT(rc == SPDK_ISCSI_CONNECTION_FATAL);
1366 
1367 	/* Case 4 - A login request with the C bit set to 1 must have the T bit set to 0. */
1368 	g_pdu_pool_is_empty = false;
1369 	login_reqh->flags |= ISCSI_LOGIN_TRANSIT;
1370 	login_reqh->flags |= ISCSI_LOGIN_CONTINUE;
1371 
1372 	rc = iscsi_pdu_hdr_op_login(&conn, &pdu);
1373 	CU_ASSERT(rc == 0);
1374 	check_login_response(ISCSI_CLASS_INITIATOR_ERROR, ISCSI_LOGIN_INITIATOR_ERROR);
1375 
1376 	/* Case 5 - Both version-min and version-max must be set to 0x00. */
1377 	login_reqh->flags = 0;
1378 	login_reqh->version_min = ISCSI_VERSION + 1;
1379 
1380 	rc = iscsi_pdu_hdr_op_login(&conn, &pdu);
1381 	CU_ASSERT(rc == 0);
1382 	check_login_response(ISCSI_CLASS_INITIATOR_ERROR, ISCSI_LOGIN_UNSUPPORTED_VERSION);
1383 
1384 	/* Case 6 - T bit is set to 1 correctly but invalid stage code is set to NSG. */
1385 	login_reqh->version_min = ISCSI_VERSION;
1386 	login_reqh->flags |= ISCSI_LOGIN_TRANSIT;
1387 	login_reqh->flags |= ISCSI_NSG_RESERVED_CODE;
1388 
1389 	rc = iscsi_pdu_hdr_op_login(&conn, &pdu);
1390 	CU_ASSERT(rc == 0);
1391 	check_login_response(ISCSI_CLASS_INITIATOR_ERROR, ISCSI_LOGIN_INITIATOR_ERROR);
1392 
1393 	/* Case 7 - Login request is correct.  Login response is initialized and set to
1394 	 * the current connection.
1395 	 */
1396 	login_reqh->flags = 0;
1397 
1398 	rc = iscsi_pdu_hdr_op_login(&conn, &pdu);
1399 	CU_ASSERT(rc == 0);
1400 	CU_ASSERT(conn.login_rsp_pdu != NULL);
1401 
1402 	iscsi_put_pdu(conn.login_rsp_pdu);
1403 }
1404 
1405 static void
1406 pdu_hdr_op_text_test(void)
1407 {
1408 	struct spdk_iscsi_sess sess = {};
1409 	struct spdk_iscsi_conn conn = {};
1410 	struct spdk_iscsi_pdu pdu = {};
1411 	struct iscsi_bhs_text_req *text_reqh;
1412 	int rc;
1413 
1414 	text_reqh = (struct iscsi_bhs_text_req *)&pdu.bhs;
1415 
1416 	conn.sess = &sess;
1417 
1418 	/* Case 1 - Data segment length for text request must not be more than
1419 	 * FirstBurstLength plus extra space to account for digests.
1420 	 */
1421 	pdu.data_segment_len = iscsi_get_max_immediate_data_size() + 1;
1422 
1423 	rc = iscsi_pdu_hdr_op_text(&conn, &pdu);
1424 	CU_ASSERT(rc == 0);
1425 	check_iscsi_reject(&pdu, ISCSI_REASON_PROTOCOL_ERROR);
1426 
1427 	/* Case 2 - A text request with the C bit set to 1 must have the F bit set to 0. */
1428 	pdu.data_segment_len = iscsi_get_max_immediate_data_size();
1429 	text_reqh->flags |= ISCSI_FLAG_FINAL;
1430 	text_reqh->flags |= ISCSI_TEXT_CONTINUE;
1431 
1432 	rc = iscsi_pdu_hdr_op_text(&conn, &pdu);
1433 	CU_ASSERT(rc == -1);
1434 
1435 	/* Case 3 - ExpStatSN of the text request is expected to match StatSN of the current
1436 	 * connection.  But StarPort iSCSI initiator didn't follow the expectation.  In this
1437 	 * case we overwrite StatSN by ExpStatSN and processes the request as correct.
1438 	 */
1439 	text_reqh->flags = 0;
1440 	to_be32(&text_reqh->exp_stat_sn, 1234);
1441 	to_be32(&conn.StatSN, 4321);
1442 
1443 	rc = iscsi_pdu_hdr_op_text(&conn, &pdu);
1444 	CU_ASSERT(rc == 0);
1445 	CU_ASSERT(conn.StatSN == 1234);
1446 
1447 	/* Case 4 - Text request is the first in the sequence of text requests and responses,
1448 	 * and so its ITT is hold to the current connection.
1449 	 */
1450 	sess.current_text_itt = 0xffffffffU;
1451 	to_be32(&text_reqh->itt, 5678);
1452 
1453 	rc = iscsi_pdu_hdr_op_text(&conn, &pdu);
1454 	CU_ASSERT(rc == 0);
1455 	CU_ASSERT(sess.current_text_itt == 5678);
1456 
1457 	/* Case 5 - If text request is sent as part of a sequence of text requests and responses,
1458 	 * its ITT must be the same for all the text requests.  But it was not.  */
1459 	sess.current_text_itt = 5679;
1460 
1461 	rc = iscsi_pdu_hdr_op_text(&conn, &pdu);
1462 	CU_ASSERT(rc == 0);
1463 	check_iscsi_reject(&pdu, ISCSI_REASON_PROTOCOL_ERROR);
1464 
1465 	/* Case 6 - Different from case 5, its ITT matches the value saved in the connection. */
1466 	text_reqh->flags = 0;
1467 	sess.current_text_itt = 5678;
1468 
1469 	rc = iscsi_pdu_hdr_op_text(&conn, &pdu);
1470 	CU_ASSERT(rc == 0);
1471 }
1472 
1473 static void
1474 check_logout_response(uint8_t response, uint32_t stat_sn, uint32_t exp_cmd_sn,
1475 		      uint32_t max_cmd_sn)
1476 {
1477 	struct spdk_iscsi_pdu *rsp_pdu;
1478 	struct iscsi_bhs_logout_resp *logout_rsph;
1479 
1480 	rsp_pdu = TAILQ_FIRST(&g_write_pdu_list);
1481 	CU_ASSERT(rsp_pdu != NULL);
1482 	logout_rsph = (struct iscsi_bhs_logout_resp *)&rsp_pdu->bhs;
1483 	CU_ASSERT(logout_rsph->response == response);
1484 	CU_ASSERT(from_be32(&logout_rsph->stat_sn) == stat_sn);
1485 	CU_ASSERT(from_be32(&logout_rsph->exp_cmd_sn) == exp_cmd_sn);
1486 	CU_ASSERT(from_be32(&logout_rsph->max_cmd_sn) == max_cmd_sn);
1487 
1488 	TAILQ_REMOVE(&g_write_pdu_list, rsp_pdu, tailq);
1489 	iscsi_put_pdu(rsp_pdu);
1490 }
1491 
1492 static void
1493 pdu_hdr_op_logout_test(void)
1494 {
1495 	struct spdk_iscsi_sess sess = {};
1496 	struct spdk_iscsi_conn conn = {};
1497 	struct spdk_iscsi_pdu pdu = {};
1498 	struct iscsi_bhs_logout_req *logout_reqh;
1499 	int rc;
1500 
1501 	logout_reqh = (struct iscsi_bhs_logout_req *)&pdu.bhs;
1502 
1503 	/* Case 1 - Target can accept logout request only with the reason "close the session"
1504 	 * on discovery session.
1505 	 */
1506 	logout_reqh->reason = 1;
1507 	conn.sess = &sess;
1508 	sess.session_type = SESSION_TYPE_DISCOVERY;
1509 
1510 	rc = iscsi_pdu_hdr_op_logout(&conn, &pdu);
1511 	CU_ASSERT(rc == SPDK_ISCSI_CONNECTION_FATAL);
1512 
1513 	/* Case 2 - Session is not established yet but connection was closed successfully. */
1514 	conn.sess = NULL;
1515 	conn.StatSN = 1234;
1516 	to_be32(&logout_reqh->exp_stat_sn, 1234);
1517 	pdu.cmd_sn = 5678;
1518 
1519 	rc = iscsi_pdu_hdr_op_logout(&conn, &pdu);
1520 	CU_ASSERT(rc == 0);
1521 	check_logout_response(0, 1234, 5678, 5678);
1522 	CU_ASSERT(conn.StatSN == 1235);
1523 
1524 	/* Case 3 - Session type is normal but CID was not found. Hence connection or session
1525 	 * was not closed.
1526 	 */
1527 	sess.session_type = SESSION_TYPE_NORMAL;
1528 	sess.ExpCmdSN = 5679;
1529 	sess.connections = 1;
1530 	conn.sess = &sess;
1531 	conn.id = 1;
1532 
1533 	rc = iscsi_pdu_hdr_op_logout(&conn, &pdu);
1534 	CU_ASSERT(rc == 0);
1535 	check_logout_response(1, 1235, 5679, 1);
1536 	CU_ASSERT(conn.StatSN == 1236);
1537 	CU_ASSERT(sess.MaxCmdSN == 1);
1538 
1539 	/* Case 4 - Session type is normal and CID was found.  Connection or session was closed
1540 	 * successfully.
1541 	 */
1542 	to_be16(&logout_reqh->cid, 1);
1543 
1544 	rc = iscsi_pdu_hdr_op_logout(&conn, &pdu);
1545 	CU_ASSERT(rc == 0);
1546 	check_logout_response(0, 1236, 5679, 2);
1547 	CU_ASSERT(conn.StatSN == 1237);
1548 	CU_ASSERT(sess.MaxCmdSN == 2);
1549 
1550 	/* Case 5 - PDU pool is empty. */
1551 	g_pdu_pool_is_empty = true;
1552 
1553 	rc = iscsi_pdu_hdr_op_logout(&conn, &pdu);
1554 	CU_ASSERT(rc == SPDK_ISCSI_CONNECTION_FATAL);
1555 
1556 	g_pdu_pool_is_empty = false;
1557 }
1558 
1559 static void
1560 check_scsi_task(struct spdk_iscsi_pdu *pdu, enum spdk_scsi_data_dir dir)
1561 {
1562 	struct spdk_iscsi_task *task;
1563 
1564 	task = pdu->task;
1565 	CU_ASSERT(task != NULL);
1566 	CU_ASSERT(task->pdu == pdu);
1567 	CU_ASSERT(task->scsi.dxfer_dir == dir);
1568 
1569 	iscsi_task_put(task);
1570 	pdu->task = NULL;
1571 }
1572 
1573 static void
1574 pdu_hdr_op_scsi_test(void)
1575 {
1576 	struct spdk_iscsi_sess sess = {};
1577 	struct spdk_iscsi_conn conn = {};
1578 	struct spdk_iscsi_pdu pdu = {};
1579 	struct spdk_scsi_dev dev = {};
1580 	struct spdk_scsi_lun lun = {};
1581 	struct iscsi_bhs_scsi_req *scsi_reqh;
1582 	int rc;
1583 
1584 	scsi_reqh = (struct iscsi_bhs_scsi_req *)&pdu.bhs;
1585 
1586 	conn.sess = &sess;
1587 	conn.dev = &dev;
1588 
1589 	/* Case 1 - SCSI command is acceptable only on normal session. */
1590 	sess.session_type = SESSION_TYPE_DISCOVERY;
1591 
1592 	rc = iscsi_pdu_hdr_op_scsi(&conn, &pdu);
1593 	CU_ASSERT(rc == SPDK_ISCSI_CONNECTION_FATAL);
1594 
1595 	/* Case 2 - Task pool is empty. */
1596 	g_task_pool_is_empty = true;
1597 
1598 	rc = iscsi_pdu_hdr_op_scsi(&conn, &pdu);
1599 	CU_ASSERT(rc == SPDK_ISCSI_CONNECTION_FATAL);
1600 
1601 	g_task_pool_is_empty = false;
1602 
1603 	/* Case 3 - bidirectional operations (both R and W flags are set to 1) are not supported. */
1604 	sess.session_type = SESSION_TYPE_NORMAL;
1605 	scsi_reqh->read_bit = 1;
1606 	scsi_reqh->write_bit = 1;
1607 
1608 	rc = iscsi_pdu_hdr_op_scsi(&conn, &pdu);
1609 	CU_ASSERT(rc == SPDK_ISCSI_CONNECTION_FATAL);
1610 
1611 	/* Case 4 - LUN is hot-removed, and return immediately. */
1612 	scsi_reqh->write_bit = 0;
1613 
1614 	rc = iscsi_pdu_hdr_op_scsi(&conn, &pdu);
1615 	CU_ASSERT(rc == 0);
1616 	CU_ASSERT(pdu.task == NULL);
1617 
1618 	/* Case 5 - SCSI read command PDU is correct, and the configured iSCSI task is set to the PDU. */
1619 	dev.lun[0] = &lun;
1620 
1621 	rc = iscsi_pdu_hdr_op_scsi(&conn, &pdu);
1622 	CU_ASSERT(rc == 0);
1623 	check_scsi_task(&pdu, SPDK_SCSI_DIR_FROM_DEV);
1624 
1625 	/* Case 6 - For SCSI write command PDU, its data segment length must not be more than
1626 	 * FirstBurstLength plus extra space to account for digests.
1627 	 */
1628 	scsi_reqh->read_bit = 0;
1629 	scsi_reqh->write_bit = 1;
1630 	pdu.data_segment_len = iscsi_get_max_immediate_data_size() + 1;
1631 
1632 	rc = iscsi_pdu_hdr_op_scsi(&conn, &pdu);
1633 	CU_ASSERT(rc == 0);
1634 	check_iscsi_reject(&pdu, ISCSI_REASON_PROTOCOL_ERROR);
1635 
1636 	/* Case 7 - For SCSI write command PDU, its data segment length must not be more than
1637 	 * Expected Data Transfer Length (EDTL).
1638 	 */
1639 	pdu.data_segment_len = iscsi_get_max_immediate_data_size();
1640 	to_be32(&scsi_reqh->expected_data_xfer_len, pdu.data_segment_len - 1);
1641 
1642 	rc = iscsi_pdu_hdr_op_scsi(&conn, &pdu);
1643 	CU_ASSERT(rc == 0);
1644 	check_iscsi_reject(&pdu, ISCSI_REASON_PROTOCOL_ERROR);
1645 
1646 	/* Case 8 - If ImmediateData is not enabled for the session, SCSI write command PDU
1647 	 * cannot have data segment.
1648 	 */
1649 	to_be32(&scsi_reqh->expected_data_xfer_len, pdu.data_segment_len);
1650 
1651 	rc = iscsi_pdu_hdr_op_scsi(&conn, &pdu);
1652 	CU_ASSERT(rc == 0);
1653 	check_iscsi_reject(&pdu, ISCSI_REASON_PROTOCOL_ERROR);
1654 
1655 	/* Case 9 - For SCSI write command PDU, its data segment length must not be more
1656 	 * than FirstBurstLength.
1657 	 */
1658 	sess.ImmediateData = true;
1659 
1660 	rc = iscsi_pdu_hdr_op_scsi(&conn, &pdu);
1661 	CU_ASSERT(rc == 0);
1662 	check_iscsi_reject(&pdu, ISCSI_REASON_PROTOCOL_ERROR);
1663 
1664 	/* Case 10 - SCSI write command PDU is correct, and the configured iSCSI task is set to the PDU. */
1665 	sess.FirstBurstLength = pdu.data_segment_len;
1666 
1667 	rc = iscsi_pdu_hdr_op_scsi(&conn, &pdu);
1668 	CU_ASSERT(rc == 0);
1669 	check_scsi_task(&pdu, SPDK_SCSI_DIR_TO_DEV);
1670 
1671 	/* Case 11 - R and W must not both be 0 when EDTL is not 0. */
1672 	scsi_reqh->write_bit = 0;
1673 
1674 	rc = iscsi_pdu_hdr_op_scsi(&conn, &pdu);
1675 	CU_ASSERT(rc == 0);
1676 	check_iscsi_reject(&pdu, ISCSI_REASON_INVALID_PDU_FIELD);
1677 
1678 	/* Case 11 - R and W are both 0 and EDTL is also 0, and hence SCSI command PDU is accepted. */
1679 	to_be32(&scsi_reqh->expected_data_xfer_len, 0);
1680 
1681 	rc = iscsi_pdu_hdr_op_scsi(&conn, &pdu);
1682 	CU_ASSERT(rc == 0);
1683 	check_scsi_task(&pdu, SPDK_SCSI_DIR_NONE);
1684 }
1685 
1686 static void
1687 check_iscsi_task_mgmt_response(uint8_t response, uint32_t task_tag, uint32_t stat_sn,
1688 			       uint32_t exp_cmd_sn, uint32_t max_cmd_sn)
1689 {
1690 	struct spdk_iscsi_pdu *rsp_pdu;
1691 	struct iscsi_bhs_task_resp *rsph;
1692 
1693 	rsp_pdu = TAILQ_FIRST(&g_write_pdu_list);
1694 	CU_ASSERT(rsp_pdu != NULL);
1695 	rsph = (struct iscsi_bhs_task_resp *)&rsp_pdu->bhs;
1696 	CU_ASSERT(rsph->response == response);
1697 	CU_ASSERT(from_be32(&rsph->itt) == task_tag);
1698 	CU_ASSERT(from_be32(&rsph->exp_cmd_sn) == exp_cmd_sn);
1699 	CU_ASSERT(from_be32(&rsph->max_cmd_sn) == max_cmd_sn);
1700 
1701 	TAILQ_REMOVE(&g_write_pdu_list, rsp_pdu, tailq);
1702 	iscsi_put_pdu(rsp_pdu);
1703 }
1704 
1705 static void
1706 pdu_hdr_op_task_mgmt_test(void)
1707 {
1708 	struct spdk_iscsi_sess sess = {};
1709 	struct spdk_iscsi_conn conn = {};
1710 	struct spdk_iscsi_pdu pdu = {};
1711 	struct spdk_scsi_dev dev = {};
1712 	struct spdk_scsi_lun lun = {};
1713 	struct iscsi_bhs_task_req *task_reqh;
1714 	int rc;
1715 
1716 	/* TBD: This test covers only error paths before creating iSCSI task for now.
1717 	 * Testing iSCSI task creation in iscsi_pdu_hdr_op_task() by UT is not simple
1718 	 * and do it separately later.
1719 	 */
1720 
1721 	task_reqh = (struct iscsi_bhs_task_req *)&pdu.bhs;
1722 
1723 	conn.sess = &sess;
1724 	conn.dev = &dev;
1725 
1726 	/* Case 1 - Task Management Function request PDU is acceptable only on normal session. */
1727 	sess.session_type = SESSION_TYPE_DISCOVERY;
1728 
1729 	rc = iscsi_pdu_hdr_op_task(&conn, &pdu);
1730 	CU_ASSERT(rc == SPDK_ISCSI_CONNECTION_FATAL);
1731 
1732 	/* Case 2 - LUN is hot removed.  "LUN does not exist" response is sent. */
1733 	sess.session_type = SESSION_TYPE_NORMAL;
1734 	task_reqh->immediate = 0;
1735 	to_be32(&task_reqh->itt, 1234);
1736 
1737 	rc = iscsi_pdu_hdr_op_task(&conn, &pdu);
1738 	CU_ASSERT(rc == 0);
1739 	check_iscsi_task_mgmt_response(ISCSI_TASK_FUNC_RESP_LUN_NOT_EXIST, 1234, 0, 0, 1);
1740 
1741 	/* Case 3 - Unassigned function is specified.  "Function rejected" response is sent. */
1742 	dev.lun[0] = &lun;
1743 	task_reqh->flags = 0;
1744 
1745 	rc = iscsi_pdu_hdr_op_task(&conn, &pdu);
1746 	CU_ASSERT(rc == 0);
1747 	check_iscsi_task_mgmt_response(ISCSI_TASK_FUNC_REJECTED, 1234, 0, 0, 2);
1748 
1749 	/* Case 4 - CLEAR TASK SET is not supported.  "Task management function not supported"
1750 	 * response is sent.
1751 	 */
1752 	task_reqh->flags = ISCSI_TASK_FUNC_CLEAR_TASK_SET;
1753 
1754 	rc = iscsi_pdu_hdr_op_task(&conn, &pdu);
1755 	CU_ASSERT(rc == 0);
1756 	check_iscsi_task_mgmt_response(ISCSI_TASK_FUNC_RESP_FUNC_NOT_SUPPORTED, 1234, 0, 0, 3);
1757 
1758 	/* Case 5 - CLEAR ACA is not supported.  "Task management function not supported" is sent. */
1759 	task_reqh->flags = ISCSI_TASK_FUNC_CLEAR_ACA;
1760 
1761 	rc = iscsi_pdu_hdr_op_task(&conn, &pdu);
1762 	CU_ASSERT(rc == 0);
1763 	check_iscsi_task_mgmt_response(ISCSI_TASK_FUNC_RESP_FUNC_NOT_SUPPORTED, 1234, 0, 0, 4);
1764 
1765 	/* Case 6 - TARGET WARM RESET is not supported.  "Task management function not supported
1766 	 * is sent.
1767 	 */
1768 	task_reqh->flags = ISCSI_TASK_FUNC_TARGET_WARM_RESET;
1769 
1770 	rc = iscsi_pdu_hdr_op_task(&conn, &pdu);
1771 	CU_ASSERT(rc == 0);
1772 	check_iscsi_task_mgmt_response(ISCSI_TASK_FUNC_RESP_FUNC_NOT_SUPPORTED, 1234, 0, 0, 5);
1773 
1774 	/* Case 7 - TARGET COLD RESET is not supported. "Task management function not supported
1775 	 * is sent.
1776 	 */
1777 	task_reqh->flags = ISCSI_TASK_FUNC_TARGET_COLD_RESET;
1778 
1779 	rc = iscsi_pdu_hdr_op_task(&conn, &pdu);
1780 	CU_ASSERT(rc == 0);
1781 	check_iscsi_task_mgmt_response(ISCSI_TASK_FUNC_RESP_FUNC_NOT_SUPPORTED, 1234, 0, 0, 6);
1782 
1783 	/* Case 8 - TASK REASSIGN is not supported. "Task management function not supported" is sent. */
1784 	task_reqh->flags = ISCSI_TASK_FUNC_TASK_REASSIGN;
1785 
1786 	rc = iscsi_pdu_hdr_op_task(&conn, &pdu);
1787 	CU_ASSERT(rc == 0);
1788 	check_iscsi_task_mgmt_response(ISCSI_TASK_FUNC_RESP_FUNC_NOT_SUPPORTED, 1234, 0, 0, 7);
1789 }
1790 
1791 static void
1792 pdu_hdr_op_nopout_test(void)
1793 {
1794 	struct spdk_iscsi_sess sess = {};
1795 	struct spdk_iscsi_conn conn = {};
1796 	struct spdk_iscsi_pdu pdu = {};
1797 	struct iscsi_bhs_nop_out *nopout_reqh;
1798 	int rc;
1799 
1800 	nopout_reqh = (struct iscsi_bhs_nop_out *)&pdu.bhs;
1801 
1802 	conn.sess = &sess;
1803 
1804 	/* Case 1 - NOP-Out PDU is acceptable only on normal session. */
1805 	sess.session_type = SESSION_TYPE_DISCOVERY;
1806 
1807 	rc = iscsi_pdu_hdr_op_nopout(&conn, &pdu);
1808 	CU_ASSERT(rc == SPDK_ISCSI_CONNECTION_FATAL);
1809 
1810 	/* Case 2 - The length of the reflected ping data is limited to MaxRecvDataSegmentLength. */
1811 	sess.session_type = SESSION_TYPE_NORMAL;
1812 	pdu.data_segment_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH + 1;
1813 
1814 	rc = iscsi_pdu_hdr_op_nopout(&conn, &pdu);
1815 	CU_ASSERT(rc == 0);
1816 	check_iscsi_reject(&pdu, ISCSI_REASON_PROTOCOL_ERROR);
1817 
1818 	/* Case 3 - If Initiator Task Tag contains 0xffffffff, the I bit must be set
1819 	 * to 1 and Target Transfer Tag should be copied from NOP-In PDU.  This case
1820 	 * satisfies the former but doesn't satisfy the latter, but ignore the error
1821 	 * for now.
1822 	 */
1823 	pdu.data_segment_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH;
1824 	conn.id = 1234;
1825 	to_be32(&nopout_reqh->ttt, 1235);
1826 	to_be32(&nopout_reqh->itt, 0xffffffffU);
1827 	nopout_reqh->immediate = 1;
1828 
1829 	rc = iscsi_pdu_hdr_op_nopout(&conn, &pdu);
1830 	CU_ASSERT(rc == 0);
1831 
1832 	/* Case 4 - This case doesn't satisfy the above former. This error is not ignored. */
1833 	nopout_reqh->immediate = 0;
1834 
1835 	rc = iscsi_pdu_hdr_op_nopout(&conn, &pdu);
1836 	CU_ASSERT(rc == SPDK_ISCSI_CONNECTION_FATAL);
1837 }
1838 
1839 static void
1840 check_iscsi_r2t(struct spdk_iscsi_task *task, uint32_t len)
1841 {
1842 	struct spdk_iscsi_pdu *rsp_pdu;
1843 	struct iscsi_bhs_r2t *rsph;
1844 
1845 	rsp_pdu = TAILQ_FIRST(&g_write_pdu_list);
1846 	CU_ASSERT(rsp_pdu != NULL);
1847 	rsph = (struct iscsi_bhs_r2t *)&rsp_pdu->bhs;
1848 	CU_ASSERT(rsph->opcode == ISCSI_OP_R2T);
1849 	CU_ASSERT(from_be64(&rsph->lun) == spdk_scsi_lun_id_int_to_fmt(task->lun_id));
1850 	CU_ASSERT(from_be32(&rsph->buffer_offset) == task->next_r2t_offset);
1851 	CU_ASSERT(from_be32(&rsph->desired_xfer_len) == len);
1852 
1853 	TAILQ_REMOVE(&g_write_pdu_list, rsp_pdu, tailq);
1854 	iscsi_put_pdu(rsp_pdu);
1855 }
1856 
1857 static void
1858 pdu_hdr_op_data_test(void)
1859 {
1860 	struct spdk_iscsi_sess sess = {};
1861 	struct spdk_iscsi_conn conn = {};
1862 	struct spdk_iscsi_pdu pdu = {};
1863 	struct spdk_iscsi_task primary = {};
1864 	struct spdk_scsi_dev dev = {};
1865 	struct spdk_scsi_lun lun = {};
1866 	struct iscsi_bhs_data_out *data_reqh;
1867 	int rc;
1868 
1869 	data_reqh = (struct iscsi_bhs_data_out *)&pdu.bhs;
1870 
1871 	conn.sess = &sess;
1872 	conn.dev = &dev;
1873 	TAILQ_INIT(&conn.active_r2t_tasks);
1874 
1875 	/* Case 1 - SCSI Data-Out PDU is acceptable only on normal session. */
1876 	sess.session_type = SESSION_TYPE_DISCOVERY;
1877 
1878 	rc = iscsi_pdu_hdr_op_data(&conn, &pdu);
1879 	CU_ASSERT(rc == SPDK_ISCSI_CONNECTION_FATAL);
1880 
1881 	/* Case 2 - Data segment length must not be more than MaxRecvDataSegmentLength. */
1882 	sess.session_type = SESSION_TYPE_NORMAL;
1883 	pdu.data_segment_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH + 1;
1884 
1885 	rc = iscsi_pdu_hdr_op_data(&conn, &pdu);
1886 	CU_ASSERT(rc == 0);
1887 	check_iscsi_reject(&pdu, ISCSI_REASON_PROTOCOL_ERROR);
1888 
1889 	/* Case 3 - R2T task whose Target Transfer Tag matches is not found. */
1890 	pdu.data_segment_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH;
1891 
1892 	rc = iscsi_pdu_hdr_op_data(&conn, &pdu);
1893 	CU_ASSERT(rc == 0);
1894 	check_iscsi_reject(&pdu, ISCSI_REASON_INVALID_PDU_FIELD);
1895 
1896 	/* Case 4 - R2T task whose Target Transfer Tag matches is found but data segment length
1897 	 * is more than Desired Data Transfer Length of the R2T.
1898 	 */
1899 	primary.desired_data_transfer_length = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH - 1;
1900 	conn.pending_r2t = 1;
1901 	TAILQ_INSERT_TAIL(&conn.active_r2t_tasks, &primary, link);
1902 
1903 	rc = iscsi_pdu_hdr_op_data(&conn, &pdu);
1904 	CU_ASSERT(rc == SPDK_ISCSI_CONNECTION_FATAL);
1905 
1906 	/* Case 5 - Initiator task tag doesn't match tag of R2T task. */
1907 	primary.desired_data_transfer_length = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH;
1908 	to_be32(&data_reqh->itt, 1);
1909 
1910 	rc = iscsi_pdu_hdr_op_data(&conn, &pdu);
1911 	CU_ASSERT(rc == 0);
1912 	check_iscsi_reject(&pdu, ISCSI_REASON_INVALID_PDU_FIELD);
1913 
1914 	/* Case 6 - DataSN doesn't match the Data-Out PDU number within the current
1915 	 * output sequence.
1916 	 */
1917 	to_be32(&data_reqh->itt, 0);
1918 	to_be32(&data_reqh->data_sn, 1);
1919 
1920 	rc = iscsi_pdu_hdr_op_data(&conn, &pdu);
1921 	CU_ASSERT(rc == 0);
1922 	check_iscsi_reject(&pdu, ISCSI_REASON_PROTOCOL_ERROR);
1923 
1924 	/* Case 7 - Output sequence must be in increasing buffer offset and must not
1925 	 * be overlaid but they are not satisfied.
1926 	 */
1927 	to_be32(&data_reqh->data_sn, 0);
1928 	to_be32(&data_reqh->buffer_offset, 4096);
1929 
1930 	rc = iscsi_pdu_hdr_op_data(&conn, &pdu);
1931 	CU_ASSERT(rc == SPDK_ISCSI_CONNECTION_FATAL);
1932 
1933 	/* Case 8 - Data segment length must not exceed MaxBurstLength. */
1934 	to_be32(&data_reqh->buffer_offset, 0);
1935 	sess.MaxBurstLength = pdu.data_segment_len - 1;
1936 
1937 	rc = iscsi_pdu_hdr_op_data(&conn, &pdu);
1938 	CU_ASSERT(rc == SPDK_ISCSI_CONNECTION_FATAL);
1939 
1940 	/* Case 9 - LUN is hot removed. */
1941 	sess.MaxBurstLength = pdu.data_segment_len * 4;
1942 	to_be32(&data_reqh->data_sn, primary.r2t_datasn);
1943 	to_be32(&data_reqh->buffer_offset, primary.next_expected_r2t_offset);
1944 
1945 	rc = iscsi_pdu_hdr_op_data(&conn, &pdu);
1946 	CU_ASSERT(rc == 0);
1947 	CU_ASSERT(pdu.task == NULL);
1948 
1949 	/* Case 10 - SCSI Data-Out PDU is correct and processed. Created task is held
1950 	 * to the PDU, but its F bit is 0 and hence R2T is not sent.
1951 	 */
1952 	dev.lun[0] = &lun;
1953 	to_be32(&data_reqh->data_sn, primary.r2t_datasn);
1954 	to_be32(&data_reqh->buffer_offset, primary.next_expected_r2t_offset);
1955 
1956 	rc = iscsi_pdu_hdr_op_data(&conn, &pdu);
1957 	CU_ASSERT(rc == 0);
1958 	CU_ASSERT(pdu.task != NULL);
1959 	iscsi_task_put(pdu.task);
1960 	pdu.task = NULL;
1961 
1962 	/* Case 11 - SCSI Data-Out PDU is correct and processed. Created task is held
1963 	 * to the PDU, and Its F bit is 1 and hence R2T is sent.
1964 	 */
1965 	data_reqh->flags |= ISCSI_FLAG_FINAL;
1966 	to_be32(&data_reqh->data_sn, primary.r2t_datasn);
1967 	to_be32(&data_reqh->buffer_offset, primary.next_expected_r2t_offset);
1968 	primary.scsi.transfer_len = pdu.data_segment_len * 5;
1969 
1970 	rc = iscsi_pdu_hdr_op_data(&conn, &pdu);
1971 	CU_ASSERT(rc == 0);
1972 	CU_ASSERT(pdu.task != NULL);
1973 	check_iscsi_r2t(pdu.task, pdu.data_segment_len * 4);
1974 	iscsi_task_put(pdu.task);
1975 
1976 	/* Case 12 - Task pool is empty. */
1977 	to_be32(&data_reqh->data_sn, primary.r2t_datasn);
1978 	to_be32(&data_reqh->buffer_offset, primary.next_expected_r2t_offset);
1979 	g_task_pool_is_empty = true;
1980 
1981 	rc = iscsi_pdu_hdr_op_data(&conn, &pdu);
1982 	CU_ASSERT(rc == SPDK_ISCSI_CONNECTION_FATAL);
1983 
1984 	g_task_pool_is_empty = false;
1985 }
1986 
1987 int
1988 main(int argc, char **argv)
1989 {
1990 	CU_pSuite	suite = NULL;
1991 	unsigned int	num_failures;
1992 
1993 	CU_set_error_action(CUEA_ABORT);
1994 	CU_initialize_registry();
1995 
1996 	suite = CU_add_suite("iscsi_suite", NULL, NULL);
1997 
1998 	CU_ADD_TEST(suite, op_login_check_target_test);
1999 	CU_ADD_TEST(suite, op_login_session_normal_test);
2000 	CU_ADD_TEST(suite, maxburstlength_test);
2001 	CU_ADD_TEST(suite, underflow_for_read_transfer_test);
2002 	CU_ADD_TEST(suite, underflow_for_zero_read_transfer_test);
2003 	CU_ADD_TEST(suite, underflow_for_request_sense_test);
2004 	CU_ADD_TEST(suite, underflow_for_check_condition_test);
2005 	CU_ADD_TEST(suite, add_transfer_task_test);
2006 	CU_ADD_TEST(suite, get_transfer_task_test);
2007 	CU_ADD_TEST(suite, del_transfer_task_test);
2008 	CU_ADD_TEST(suite, clear_all_transfer_tasks_test);
2009 	CU_ADD_TEST(suite, build_iovs_test);
2010 	CU_ADD_TEST(suite, build_iovs_with_md_test);
2011 	CU_ADD_TEST(suite, pdu_hdr_op_login_test);
2012 	CU_ADD_TEST(suite, pdu_hdr_op_text_test);
2013 	CU_ADD_TEST(suite, pdu_hdr_op_logout_test);
2014 	CU_ADD_TEST(suite, pdu_hdr_op_scsi_test);
2015 	CU_ADD_TEST(suite, pdu_hdr_op_task_mgmt_test);
2016 	CU_ADD_TEST(suite, pdu_hdr_op_nopout_test);
2017 	CU_ADD_TEST(suite, pdu_hdr_op_data_test);
2018 
2019 	CU_basic_set_mode(CU_BRM_VERBOSE);
2020 	CU_basic_run_tests();
2021 	num_failures = CU_get_number_of_failures();
2022 	CU_cleanup_registry();
2023 	return num_failures;
2024 }
2025