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