xref: /spdk/test/unit/lib/iscsi/common.c (revision 8bb0ded3e55c182cea67af1f6790f8de5f38c05f)
1 #include "iscsi/task.h"
2 #include "iscsi/iscsi.h"
3 #include "iscsi/conn.h"
4 
5 #include "spdk/env.h"
6 #include "spdk/sock.h"
7 #include "spdk_cunit.h"
8 
9 #include "spdk/log.h"
10 #include "spdk_internal/mock.h"
11 
12 #include "scsi/scsi_internal.h"
13 
14 SPDK_LOG_REGISTER_COMPONENT(iscsi)
15 
16 struct spdk_trace_histories *g_trace_histories;
17 DEFINE_STUB_V(spdk_trace_add_register_fn, (struct spdk_trace_register_fn *reg_fn));
18 DEFINE_STUB_V(spdk_trace_register_owner, (uint8_t type, char id_prefix));
19 DEFINE_STUB_V(spdk_trace_register_object, (uint8_t type, char id_prefix));
20 DEFINE_STUB_V(spdk_trace_register_description, (const char *name,
21 		uint16_t tpoint_id, uint8_t owner_type, uint8_t object_type, uint8_t new_object,
22 		uint8_t arg1_type, const char *arg1_name));
23 DEFINE_STUB_V(_spdk_trace_record, (uint64_t tsc, uint16_t tpoint_id, uint16_t poller_id,
24 				   uint32_t size, uint64_t object_id, uint64_t arg1));
25 
26 TAILQ_HEAD(, spdk_iscsi_pdu) g_write_pdu_list = TAILQ_HEAD_INITIALIZER(g_write_pdu_list);
27 
28 static bool g_task_pool_is_empty = false;
29 static bool g_pdu_pool_is_empty = false;
30 
31 struct spdk_iscsi_task *
32 iscsi_task_get(struct spdk_iscsi_conn *conn,
33 	       struct spdk_iscsi_task *parent,
34 	       spdk_scsi_task_cpl cpl_fn)
35 {
36 	struct spdk_iscsi_task *task;
37 
38 	if (g_task_pool_is_empty) {
39 		return NULL;
40 	}
41 
42 	task = calloc(1, sizeof(*task));
43 	if (!task) {
44 		return NULL;
45 	}
46 
47 	task->conn = conn;
48 	task->scsi.cpl_fn = cpl_fn;
49 	if (parent) {
50 		parent->scsi.ref++;
51 		task->parent = parent;
52 		task->tag = parent->tag;
53 		task->lun_id = parent->lun_id;
54 		task->scsi.dxfer_dir = parent->scsi.dxfer_dir;
55 		task->scsi.transfer_len = parent->scsi.transfer_len;
56 		task->scsi.lun = parent->scsi.lun;
57 		task->scsi.cdb = parent->scsi.cdb;
58 		task->scsi.target_port = parent->scsi.target_port;
59 		task->scsi.initiator_port = parent->scsi.initiator_port;
60 		if (conn && (task->scsi.dxfer_dir == SPDK_SCSI_DIR_FROM_DEV)) {
61 			conn->data_in_cnt++;
62 		}
63 	}
64 
65 	task->scsi.iovs = &task->scsi.iov;
66 	return task;
67 }
68 
69 void
70 spdk_scsi_task_put(struct spdk_scsi_task *task)
71 {
72 	free(task);
73 }
74 
75 void
76 iscsi_put_pdu(struct spdk_iscsi_pdu *pdu)
77 {
78 	if (!pdu) {
79 		return;
80 	}
81 
82 	pdu->ref--;
83 	if (pdu->ref < 0) {
84 		CU_FAIL("negative ref count");
85 		pdu->ref = 0;
86 	}
87 
88 	if (pdu->ref == 0) {
89 		if (pdu->data && !pdu->data_from_mempool) {
90 			free(pdu->data);
91 		}
92 		free(pdu);
93 	}
94 }
95 
96 struct spdk_iscsi_pdu *
97 iscsi_get_pdu(struct spdk_iscsi_conn *conn)
98 {
99 	struct spdk_iscsi_pdu *pdu;
100 
101 	assert(conn != NULL);
102 	if (g_pdu_pool_is_empty) {
103 		return NULL;
104 	}
105 
106 	pdu = malloc(sizeof(*pdu));
107 	if (!pdu) {
108 		return NULL;
109 	}
110 
111 	memset(pdu, 0, offsetof(struct spdk_iscsi_pdu, ahs));
112 	pdu->ref = 1;
113 	pdu->conn = conn;
114 
115 	return pdu;
116 }
117 
118 DEFINE_STUB_V(spdk_scsi_task_process_null_lun, (struct spdk_scsi_task *task));
119 
120 DEFINE_STUB_V(spdk_scsi_task_process_abort, (struct spdk_scsi_task *task));
121 
122 DEFINE_STUB_V(spdk_scsi_dev_queue_task,
123 	      (struct spdk_scsi_dev *dev, struct spdk_scsi_task *task));
124 
125 DEFINE_STUB(spdk_scsi_dev_find_port_by_id, struct spdk_scsi_port *,
126 	    (struct spdk_scsi_dev *dev, uint64_t id), NULL);
127 
128 DEFINE_STUB_V(spdk_scsi_dev_queue_mgmt_task,
129 	      (struct spdk_scsi_dev *dev, struct spdk_scsi_task *task));
130 
131 const char *
132 spdk_scsi_dev_get_name(const struct spdk_scsi_dev *dev)
133 {
134 	if (dev != NULL) {
135 		return dev->name;
136 	}
137 
138 	return NULL;
139 }
140 
141 DEFINE_STUB(spdk_scsi_dev_construct, struct spdk_scsi_dev *,
142 	    (const char *name, const char **bdev_name_list,
143 	     int *lun_id_list, int num_luns, uint8_t protocol_id,
144 	     void (*hotremove_cb)(const struct spdk_scsi_lun *, void *),
145 	     void *hotremove_ctx),
146 	    NULL);
147 
148 DEFINE_STUB_V(spdk_scsi_dev_destruct,
149 	      (struct spdk_scsi_dev *dev, spdk_scsi_dev_destruct_cb_t cb_fn, void *cb_arg));
150 
151 DEFINE_STUB(spdk_scsi_dev_add_port, int,
152 	    (struct spdk_scsi_dev *dev, uint64_t id, const char *name), 0);
153 
154 DEFINE_STUB(iscsi_drop_conns, int,
155 	    (struct spdk_iscsi_conn *conn, const char *conn_match, int drop_all),
156 	    0);
157 
158 DEFINE_STUB(spdk_scsi_dev_delete_port, int,
159 	    (struct spdk_scsi_dev *dev, uint64_t id), 0);
160 
161 DEFINE_STUB_V(shutdown_iscsi_conns, (void));
162 
163 DEFINE_STUB_V(iscsi_conns_request_logout, (struct spdk_iscsi_tgt_node *target, int pg_tag));
164 
165 DEFINE_STUB(iscsi_get_active_conns, int, (struct spdk_iscsi_tgt_node *target), 0);
166 
167 void
168 iscsi_task_cpl(struct spdk_scsi_task *scsi_task)
169 {
170 	struct spdk_iscsi_task *iscsi_task;
171 
172 	if (scsi_task != NULL) {
173 		iscsi_task = iscsi_task_from_scsi_task(scsi_task);
174 		if (iscsi_task->parent && (iscsi_task->scsi.dxfer_dir == SPDK_SCSI_DIR_FROM_DEV)) {
175 			assert(iscsi_task->conn->data_in_cnt > 0);
176 			iscsi_task->conn->data_in_cnt--;
177 		}
178 
179 		free(iscsi_task);
180 	}
181 }
182 
183 DEFINE_STUB_V(iscsi_task_mgmt_cpl, (struct spdk_scsi_task *scsi_task));
184 
185 DEFINE_STUB(iscsi_conn_read_data, int,
186 	    (struct spdk_iscsi_conn *conn, int bytes, void *buf), 0);
187 
188 DEFINE_STUB(iscsi_conn_readv_data, int,
189 	    (struct spdk_iscsi_conn *conn, struct iovec *iov, int iovcnt), 0);
190 
191 void
192 iscsi_conn_write_pdu(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu,
193 		     iscsi_conn_xfer_complete_cb cb_fn, void *cb_arg)
194 {
195 	TAILQ_INSERT_TAIL(&g_write_pdu_list, pdu, tailq);
196 }
197 
198 DEFINE_STUB_V(iscsi_conn_logout, (struct spdk_iscsi_conn *conn));
199 
200 DEFINE_STUB_V(spdk_scsi_task_set_status,
201 	      (struct spdk_scsi_task *task, int sc, int sk, int asc, int ascq));
202 
203 void
204 spdk_scsi_task_set_data(struct spdk_scsi_task *task, void *data, uint32_t len)
205 {
206 	SPDK_CU_ASSERT_FATAL(task->iovs != NULL);
207 	task->iovs[0].iov_base = data;
208 	task->iovs[0].iov_len = len;
209 }
210