xref: /spdk/lib/iscsi/iscsi.h (revision eb8b1e20a9c8a6bc79f32fde8693d2791a74c34d)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (C) 2008-2012 Daisuke Aoyama <aoyama@peach.ne.jp>.
5  *   Copyright (c) Intel Corporation.
6  *   All rights reserved.
7  *
8  *   Redistribution and use in source and binary forms, with or without
9  *   modification, are permitted provided that the following conditions
10  *   are met:
11  *
12  *     * Redistributions of source code must retain the above copyright
13  *       notice, this list of conditions and the following disclaimer.
14  *     * Redistributions in binary form must reproduce the above copyright
15  *       notice, this list of conditions and the following disclaimer in
16  *       the documentation and/or other materials provided with the
17  *       distribution.
18  *     * Neither the name of Intel Corporation nor the names of its
19  *       contributors may be used to endorse or promote products derived
20  *       from this software without specific prior written permission.
21  *
22  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #ifndef SPDK_ISCSI_H
36 #define SPDK_ISCSI_H
37 
38 #include "spdk/stdinc.h"
39 
40 #include "spdk/bdev.h"
41 #include "spdk/iscsi_spec.h"
42 
43 #include "iscsi/param.h"
44 #include "iscsi/tgt_node.h"
45 
46 #include "spdk/assert.h"
47 
48 #define SPDK_ISCSI_BUILD_ETC "/usr/local/etc/spdk"
49 #define SPDK_ISCSI_DEFAULT_CONFIG SPDK_ISCSI_BUILD_ETC "/iscsi.conf"
50 #define SPDK_ISCSI_DEFAULT_AUTHFILE SPDK_ISCSI_BUILD_ETC "/auth.conf"
51 #define SPDK_ISCSI_DEFAULT_NODEBASE "iqn.2016-06.io.spdk"
52 
53 #define DEFAULT_MAXR2T 4
54 #define MAX_INITIATOR_NAME 256
55 #define MAX_TARGET_NAME 256
56 
57 #define MAX_ISCSI_NAME 256
58 
59 #define MAX_PORTAL 1024
60 #define MAX_INITIATOR 256
61 #define MAX_NETMASK 256
62 #define MAX_PORTAL_GROUP 4096
63 #define MAX_INITIATOR_GROUP 4096
64 #define MAX_ISCSI_TARGET_NODE 4096
65 #define MAX_SESSIONS 1024
66 #define MAX_ISCSI_CONNECTIONS MAX_SESSIONS
67 #define MAX_FIRSTBURSTLENGTH	16777215
68 
69 #define DEFAULT_PORT 3260
70 #define DEFAULT_MAX_SESSIONS 128
71 #define DEFAULT_MAX_CONNECTIONS_PER_SESSION 2
72 #define DEFAULT_MAXOUTSTANDINGR2T 1
73 #define DEFAULT_DEFAULTTIME2WAIT 2
74 #define DEFAULT_DEFAULTTIME2RETAIN 20
75 #define DEFAULT_FIRSTBURSTLENGTH 8192
76 #define DEFAULT_INITIALR2T 1
77 #define DEFAULT_IMMEDIATEDATA 1
78 #define DEFAULT_DATAPDUINORDER 1
79 #define DEFAULT_DATASEQUENCEINORDER 1
80 #define DEFAULT_ERRORRECOVERYLEVEL 0
81 #define DEFAULT_TIMEOUT 60
82 #define MAX_NOPININTERVAL 60
83 #define DEFAULT_NOPININTERVAL 30
84 #define DEFAULT_FLUSH_TIMEOUT 8
85 
86 /*
87  * SPDK iSCSI target currently only supports 64KB as the maximum data segment length
88  *  it can receive from initiators.  Other values may work, but no guarantees.
89  */
90 #define SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH  65536
91 
92 /*
93  * SPDK iSCSI target will only send a maximum of SPDK_BDEV_LARGE_BUF_MAX_SIZE data segments, even if the
94  * connection can support more.
95  */
96 #define SPDK_ISCSI_MAX_SEND_DATA_SEGMENT_LENGTH SPDK_BDEV_LARGE_BUF_MAX_SIZE
97 
98 /*
99  * Defines maximum number of data out buffers each connection can have in
100  *  use at any given time.
101  */
102 #define MAX_DATA_OUT_PER_CONNECTION 16
103 
104 /*
105  * Defines maximum number of data in buffers each connection can have in
106  *  use at any given time. So this limit does not affect I/O smaller than
107  *  SPDK_BDEV_SMALL_BUF_MAX_SIZE.
108  */
109 #define MAX_LARGE_DATAIN_PER_CONNECTION 64
110 
111 #define NUM_PDU_PER_CONNECTION	(2 * (SPDK_ISCSI_MAX_QUEUE_DEPTH + MAX_LARGE_DATAIN_PER_CONNECTION + 8))
112 
113 #define SPDK_ISCSI_MAX_BURST_LENGTH	\
114 		(SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH * MAX_DATA_OUT_PER_CONNECTION)
115 
116 #define SPDK_ISCSI_FIRST_BURST_LENGTH	8192
117 
118 /** Defines how long we should wait for a TCP close after responding to a
119  *   logout request, before terminating the connection ourselves.
120  */
121 #define ISCSI_LOGOUT_TIMEOUT 5 /* in seconds */
122 
123 /* according to RFC1982 */
124 #define SN32_CMPMAX (((uint32_t)1U) << (32 - 1))
125 #define SN32_LT(S1,S2) \
126 	(((uint32_t)(S1) != (uint32_t)(S2))				\
127 	    && (((uint32_t)(S1) < (uint32_t)(S2)			\
128 		    && ((uint32_t)(S2) - (uint32_t)(S1) < SN32_CMPMAX))	\
129 		|| ((uint32_t)(S1) > (uint32_t)(S2)			\
130 		    && ((uint32_t)(S1) - (uint32_t)(S2) > SN32_CMPMAX))))
131 #define SN32_GT(S1,S2) \
132 	(((uint32_t)(S1) != (uint32_t)(S2))				\
133 	    && (((uint32_t)(S1) < (uint32_t)(S2)			\
134 		    && ((uint32_t)(S2) - (uint32_t)(S1) > SN32_CMPMAX))	\
135 		|| ((uint32_t)(S1) > (uint32_t)(S2)			\
136 		    && ((uint32_t)(S1) - (uint32_t)(S2) < SN32_CMPMAX))))
137 
138 /* For spdk_iscsi_login_in related function use, we need to avoid the conflict
139  * with other errors
140  * */
141 #define SPDK_ISCSI_LOGIN_ERROR_RESPONSE -1000
142 #define SPDK_ISCSI_LOGIN_ERROR_PARAMETER -1001
143 #define SPDK_ISCSI_PARAMETER_EXCHANGE_NOT_ONCE -1002
144 
145 #define ISCSI_AHS_LEN 60
146 
147 struct spdk_mobj {
148 	struct rte_mempool *mp;
149 	void *buf;
150 	size_t len;
151 	uint64_t reserved; /* do not use */
152 };
153 
154 struct spdk_iscsi_pdu {
155 	struct iscsi_bhs bhs;
156 	struct spdk_mobj *mobj;
157 	uint8_t *data_buf;
158 	uint8_t *data;
159 	uint8_t header_digest[ISCSI_DIGEST_LEN];
160 	uint8_t data_digest[ISCSI_DIGEST_LEN];
161 	size_t data_segment_len;
162 	int bhs_valid_bytes;
163 	int ahs_valid_bytes;
164 	int data_valid_bytes;
165 	int hdigest_valid_bytes;
166 	int ddigest_valid_bytes;
167 	int ref;
168 	bool data_from_mempool;  /* indicate whether the data buffer is allocated from mempool */
169 	struct spdk_iscsi_task *task; /* data tied to a task buffer */
170 	uint32_t cmd_sn;
171 	uint32_t writev_offset;
172 	TAILQ_ENTRY(spdk_iscsi_pdu)	tailq;
173 
174 
175 	/*
176 	 * 60 bytes of AHS should suffice for now.
177 	 * This should always be at the end of PDU data structure.
178 	 * we need to not zero this out when doing memory clear.
179 	 */
180 	uint8_t ahs[ISCSI_AHS_LEN];
181 
182 	struct {
183 		uint16_t length; /* iSCSI SenseLength (big-endian) */
184 		uint8_t data[32];
185 	} sense;
186 };
187 
188 enum iscsi_connection_state {
189 	ISCSI_CONN_STATE_INVALID = 0,
190 	ISCSI_CONN_STATE_RUNNING = 1,
191 	ISCSI_CONN_STATE_LOGGED_OUT = 2,
192 	ISCSI_CONN_STATE_EXITING = 3,
193 	ISCSI_CONN_STATE_EXITED = 4,
194 };
195 
196 enum iscsi_chap_phase {
197 	ISCSI_CHAP_PHASE_NONE = 0,
198 	ISCSI_CHAP_PHASE_WAIT_A = 1,
199 	ISCSI_CHAP_PHASE_WAIT_NR = 2,
200 	ISCSI_CHAP_PHASE_END = 3,
201 };
202 
203 enum session_type {
204 	SESSION_TYPE_INVALID = 0,
205 	SESSION_TYPE_NORMAL = 1,
206 	SESSION_TYPE_DISCOVERY = 2,
207 };
208 
209 #define ISCSI_CHAP_CHALLENGE_LEN 1024
210 struct iscsi_chap_auth {
211 	enum iscsi_chap_phase chap_phase;
212 
213 	char *user;
214 	char *secret;
215 	char *muser;
216 	char *msecret;
217 
218 	uint8_t chap_id[1];
219 	uint8_t chap_mid[1];
220 	int chap_challenge_len;
221 	uint8_t chap_challenge[ISCSI_CHAP_CHALLENGE_LEN];
222 	int chap_mchallenge_len;
223 	uint8_t chap_mchallenge[ISCSI_CHAP_CHALLENGE_LEN];
224 };
225 
226 struct spdk_iscsi_sess {
227 	uint32_t connections;
228 	struct spdk_iscsi_conn **conns;
229 
230 	struct spdk_scsi_port *initiator_port;
231 	int tag;
232 
233 	uint64_t isid;
234 	uint16_t tsih;
235 	struct spdk_iscsi_tgt_node *target;
236 	int queue_depth;
237 
238 	struct iscsi_param *params;
239 
240 	enum session_type session_type;
241 	uint32_t MaxConnections;
242 	uint32_t MaxOutstandingR2T;
243 	uint32_t DefaultTime2Wait;
244 	uint32_t DefaultTime2Retain;
245 	uint32_t FirstBurstLength;
246 	uint32_t MaxBurstLength;
247 	uint32_t InitialR2T;
248 	uint32_t ImmediateData;
249 	uint32_t DataPDUInOrder;
250 	uint32_t DataSequenceInOrder;
251 	uint32_t ErrorRecoveryLevel;
252 
253 	uint32_t ExpCmdSN;
254 	uint32_t MaxCmdSN;
255 
256 	uint32_t current_text_itt;
257 };
258 
259 struct spdk_iscsi_globals {
260 	char *authfile;
261 	char *nodebase;
262 	pthread_mutex_t mutex;
263 	TAILQ_HEAD(, spdk_iscsi_portal)		portal_head;
264 	TAILQ_HEAD(, spdk_iscsi_portal_grp)	pg_head;
265 	TAILQ_HEAD(, spdk_iscsi_init_grp)	ig_head;
266 	int ntargets;
267 	struct spdk_iscsi_tgt_node *target[MAX_ISCSI_TARGET_NODE];
268 
269 	int timeout;
270 	int nopininterval;
271 	int no_discovery_auth;
272 	int req_discovery_auth;
273 	int req_discovery_auth_mutual;
274 	int discovery_auth_group;
275 	uint64_t flush_timeout;
276 
277 	uint32_t MaxSessions;
278 	uint32_t MaxConnectionsPerSession;
279 	uint32_t MaxConnections;
280 	uint32_t MaxOutstandingR2T;
281 	uint32_t DefaultTime2Wait;
282 	uint32_t DefaultTime2Retain;
283 	uint32_t FirstBurstLength;
284 	uint32_t MaxBurstLength;
285 	uint32_t MaxRecvDataSegmentLength;
286 	uint32_t InitialR2T;
287 	uint32_t ImmediateData;
288 	uint32_t DataPDUInOrder;
289 	uint32_t DataSequenceInOrder;
290 	uint32_t ErrorRecoveryLevel;
291 	uint32_t AllowDuplicateIsid;
292 
293 	struct rte_mempool *pdu_pool;
294 	struct rte_mempool *pdu_immediate_data_pool;
295 	struct rte_mempool *pdu_data_out_pool;
296 	struct rte_mempool *session_pool;
297 	struct rte_mempool *task_pool;
298 
299 	struct spdk_iscsi_sess	**session;
300 };
301 
302 #define ISCSI_SECURITY_NEGOTIATION_PHASE	0
303 #define ISCSI_OPERATIONAL_NEGOTIATION_PHASE	1
304 #define ISCSI_NSG_RESERVED_CODE			2
305 #define ISCSI_FULL_FEATURE_PHASE		3
306 
307 enum spdk_error_codes {
308 	SPDK_SUCCESS		= 0,
309 	SPDK_ISCSI_CONNECTION_FATAL	= -1,
310 	SPDK_PDU_FATAL		= -2,
311 };
312 
313 #define DGET24(B)											\
314 	(((  (uint32_t) *((uint8_t *)(B)+0)) << 16)				\
315 	 | (((uint32_t) *((uint8_t *)(B)+1)) << 8)				\
316 	 | (((uint32_t) *((uint8_t *)(B)+2)) << 0))
317 
318 #define DSET24(B,D)													\
319 	(((*((uint8_t *)(B)+0)) = (uint8_t)((uint32_t)(D) >> 16)),		\
320 	 ((*((uint8_t *)(B)+1)) = (uint8_t)((uint32_t)(D) >> 8)),		\
321 	 ((*((uint8_t *)(B)+2)) = (uint8_t)((uint32_t)(D) >> 0)))
322 
323 #define xstrdup(s) (s ? strdup(s) : (char *)NULL)
324 
325 extern struct spdk_iscsi_globals g_spdk_iscsi;
326 
327 struct spdk_iscsi_task;
328 
329 int spdk_iscsi_init(void);
330 void spdk_iscsi_fini(void);
331 void spdk_iscsi_config_text(FILE *fp);
332 
333 int spdk_iscsi_send_nopin(struct spdk_iscsi_conn *conn);
334 void spdk_iscsi_task_response(struct spdk_iscsi_conn *conn,
335 			      struct spdk_iscsi_task *task);
336 int spdk_iscsi_execute(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu);
337 int spdk_iscsi_build_iovecs(struct spdk_iscsi_conn *conn,
338 			    struct iovec *iovec, struct spdk_iscsi_pdu *pdu);
339 int
340 spdk_iscsi_read_pdu(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu **_pdu);
341 void spdk_iscsi_task_mgmt_response(struct spdk_iscsi_conn *conn,
342 				   struct spdk_iscsi_task *task);
343 
344 int spdk_iscsi_conn_params_init(struct iscsi_param **params);
345 int spdk_iscsi_sess_params_init(struct iscsi_param **params);
346 
347 void spdk_free_sess(struct spdk_iscsi_sess *sess);
348 void spdk_clear_all_transfer_task(struct spdk_iscsi_conn *conn,
349 				  struct spdk_scsi_lun *lun);
350 void spdk_del_connection_queued_task(void *tailq, struct spdk_scsi_lun *lun);
351 void spdk_del_transfer_task(struct spdk_iscsi_conn *conn, uint32_t CmdSN);
352 bool  spdk_iscsi_is_deferred_free_pdu(struct spdk_iscsi_pdu *pdu);
353 
354 void spdk_iscsi_shutdown(void);
355 int spdk_iscsi_negotiate_params(struct spdk_iscsi_conn *conn,
356 				struct iscsi_param **params_p, uint8_t *data,
357 				int alloc_len, int data_len);
358 int spdk_iscsi_copy_param2var(struct spdk_iscsi_conn *conn);
359 
360 void spdk_iscsi_task_cpl(struct spdk_scsi_task *scsi_task);
361 void spdk_iscsi_task_mgmt_cpl(struct spdk_scsi_task *scsi_task);
362 
363 /* Memory management */
364 void spdk_put_pdu(struct spdk_iscsi_pdu *pdu);
365 struct spdk_iscsi_pdu *spdk_get_pdu(void);
366 int spdk_iscsi_conn_handle_queued_tasks(struct spdk_iscsi_conn *conn);
367 
368 static inline int
369 spdk_get_immediate_data_buffer_size(void)
370 {
371 	/*
372 	 * Specify enough extra space in addition to FirstBurstLength to
373 	 *  account for a header digest, data digest and additional header
374 	 *  segments (AHS).  These are not normally used but they do not
375 	 *  take up much space and we need to make sure the worst-case scenario
376 	 *  can be satisified by the size returned here.
377 	 */
378 	return g_spdk_iscsi.FirstBurstLength +
379 	       ISCSI_DIGEST_LEN + /* data digest */
380 	       ISCSI_DIGEST_LEN + /* header digest */
381 	       8 +		   /* bidirectional AHS */
382 	       52;		   /* extended CDB AHS (for a 64-byte CDB) */
383 }
384 
385 static inline int
386 spdk_get_data_out_buffer_size(void)
387 {
388 	return g_spdk_iscsi.MaxRecvDataSegmentLength;
389 }
390 
391 #endif /* SPDK_ISCSI_H */
392