xref: /spdk/lib/iscsi/iscsi.h (revision cc6920a4763d4b9a43aa40583c8397d8f14fa100)
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 #include "spdk/env.h"
40 #include "spdk/bdev.h"
41 #include "spdk/iscsi_spec.h"
42 #include "spdk/thread.h"
43 #include "spdk/sock.h"
44 
45 #include "spdk/scsi.h"
46 #include "iscsi/param.h"
47 
48 #include "spdk/assert.h"
49 #include "spdk/dif.h"
50 #include "spdk/util.h"
51 
52 #define SPDK_ISCSI_DEFAULT_NODEBASE "iqn.2016-06.io.spdk"
53 
54 #define DEFAULT_MAXR2T 4
55 #define MAX_INITIATOR_PORT_NAME 256
56 #define MAX_INITIATOR_NAME 223
57 #define MAX_TARGET_NAME 223
58 
59 #define MAX_PORTAL 1024
60 #define MAX_INITIATOR 256
61 #define MAX_NETMASK 256
62 #define MAX_ISCSI_CONNECTIONS 1024
63 #define MAX_PORTAL_ADDR 256
64 #define MAX_PORTAL_PORT 32
65 
66 #define DEFAULT_PORT 3260
67 #define DEFAULT_MAX_SESSIONS 128
68 #define DEFAULT_MAX_CONNECTIONS_PER_SESSION 2
69 #define DEFAULT_MAXOUTSTANDINGR2T 1
70 #define DEFAULT_DEFAULTTIME2WAIT 2
71 #define DEFAULT_DEFAULTTIME2RETAIN 20
72 #define DEFAULT_INITIALR2T true
73 #define DEFAULT_IMMEDIATEDATA true
74 #define DEFAULT_DATAPDUINORDER true
75 #define DEFAULT_DATASEQUENCEINORDER true
76 #define DEFAULT_ERRORRECOVERYLEVEL 0
77 #define DEFAULT_TIMEOUT 60
78 #define MAX_NOPININTERVAL 60
79 #define DEFAULT_NOPININTERVAL 30
80 
81 /*
82  * SPDK iSCSI target currently only supports 64KB as the maximum data segment length
83  *  it can receive from initiators.  Other values may work, but no guarantees.
84  */
85 #define SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH  65536
86 
87 /*
88  * Defines maximum number of data out buffers each connection can have in
89  *  use at any given time.
90  */
91 #define MAX_DATA_OUT_PER_CONNECTION 16
92 
93 /*
94  * Defines default maximum number of data in buffers each connection can have in
95  *  use at any given time. So this limit does not affect I/O smaller than
96  *  SPDK_BDEV_SMALL_BUF_MAX_SIZE.
97  */
98 #define DEFAULT_MAX_LARGE_DATAIN_PER_CONNECTION 64
99 
100 #define SPDK_ISCSI_MAX_BURST_LENGTH	\
101 		(SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH * MAX_DATA_OUT_PER_CONNECTION)
102 
103 /*
104  * Defines default maximum amount in bytes of unsolicited data the iSCSI
105  *  initiator may send to the SPDK iSCSI target during the execution of
106  *  a single SCSI command. And it is smaller than the MaxBurstLength.
107  */
108 #define SPDK_ISCSI_FIRST_BURST_LENGTH	8192
109 
110 /*
111  * Defines minimum amount in bytes of unsolicited data the iSCSI initiator
112  *  may send to the SPDK iSCSI target during the execution of a single
113  *  SCSI command.
114  */
115 #define SPDK_ISCSI_MIN_FIRST_BURST_LENGTH	512
116 
117 #define SPDK_ISCSI_MAX_FIRST_BURST_LENGTH	16777215
118 
119 /*
120  * Defines default maximum queue depth per connection and this can be
121  * changed by configuration file.
122  */
123 #define DEFAULT_MAX_QUEUE_DEPTH	64
124 
125 /** Defines how long we should wait for a logout request when the target
126  *   requests logout to the initiator asynchronously.
127  */
128 #define ISCSI_LOGOUT_REQUEST_TIMEOUT 30 /* in seconds */
129 
130 /** Defines how long we should wait for a TCP close after responding to a
131  *   logout request, before terminating the connection ourselves.
132  */
133 #define ISCSI_LOGOUT_TIMEOUT 5 /* in seconds */
134 
135 /** Defines how long we should wait until login process completes. */
136 #define ISCSI_LOGIN_TIMEOUT 30 /* in seconds */
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 spdk_mempool *mp;
149 	void *buf;
150 	uint32_t data_len;
151 };
152 
153 /*
154  * Maximum number of SGL elements, i.e.,
155  * BHS, AHS, Header Digest, Data Segment and Data Digest.
156  */
157 #define SPDK_ISCSI_MAX_SGL_DESCRIPTORS	(5)
158 
159 #define SPDK_CRC32C_INITIAL	0xffffffffUL
160 #define SPDK_CRC32C_XOR		0xffffffffUL
161 
162 typedef void (*iscsi_conn_xfer_complete_cb)(void *cb_arg);
163 
164 struct spdk_iscsi_pdu {
165 	struct iscsi_bhs bhs;
166 
167 	/* Merge multiple Data-OUT PDUs in a sequence into a subtask up to
168 	 * SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH or the final PDU comes.
169 	 *
170 	 * Both the size of a data buffer and MaxRecvDataSegmentLength are
171 	 * SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH at most. Hence the data segment of
172 	 * a Data-OUT PDU can be split into two data buffers at most.
173 	 */
174 	struct spdk_mobj *mobj[2];
175 
176 	bool is_rejected;
177 	uint8_t *data;
178 	uint8_t header_digest[ISCSI_DIGEST_LEN];
179 	uint8_t data_digest[ISCSI_DIGEST_LEN];
180 	size_t data_segment_len;
181 	int bhs_valid_bytes;
182 	int ahs_valid_bytes;
183 	uint32_t data_valid_bytes;
184 	int hdigest_valid_bytes;
185 	int ddigest_valid_bytes;
186 	int ref;
187 	bool data_from_mempool;  /* indicate whether the data buffer is allocated from mempool */
188 	struct spdk_iscsi_task *task; /* data tied to a task buffer */
189 	uint32_t cmd_sn;
190 	uint32_t writev_offset;
191 	uint32_t data_buf_len;
192 	uint32_t data_offset;
193 	uint32_t crc32c;
194 	bool dif_insert_or_strip;
195 	struct spdk_dif_ctx dif_ctx;
196 	struct spdk_iscsi_conn *conn;
197 
198 	iscsi_conn_xfer_complete_cb		cb_fn;
199 	void					*cb_arg;
200 
201 	/* The sock request ends with a 0 length iovec. Place the actual iovec immediately
202 	 * after it. There is a static assert below to check if the compiler inserted
203 	 * any unwanted padding */
204 	int32_t						mapped_length;
205 	struct spdk_sock_request			sock_req;
206 	struct iovec					iov[SPDK_ISCSI_MAX_SGL_DESCRIPTORS];
207 	TAILQ_ENTRY(spdk_iscsi_pdu)	tailq;
208 
209 
210 	/*
211 	 * 60 bytes of AHS should suffice for now.
212 	 * This should always be at the end of PDU data structure.
213 	 * we need to not zero this out when doing memory clear.
214 	 */
215 	uint8_t ahs[ISCSI_AHS_LEN];
216 
217 	struct {
218 		uint16_t length; /* iSCSI SenseLength (big-endian) */
219 		uint8_t data[32];
220 	} sense;
221 };
222 SPDK_STATIC_ASSERT(offsetof(struct spdk_iscsi_pdu,
223 			    sock_req) + sizeof(struct spdk_sock_request) == offsetof(struct spdk_iscsi_pdu, iov),
224 		   "Compiler inserted padding between iov and sock_req");
225 
226 enum iscsi_connection_state {
227 	ISCSI_CONN_STATE_INVALID = 0,
228 	ISCSI_CONN_STATE_RUNNING = 1,
229 	ISCSI_CONN_STATE_EXITING = 2,
230 	ISCSI_CONN_STATE_EXITED = 3,
231 };
232 
233 enum iscsi_chap_phase {
234 	ISCSI_CHAP_PHASE_NONE = 0,
235 	ISCSI_CHAP_PHASE_WAIT_A = 1,
236 	ISCSI_CHAP_PHASE_WAIT_NR = 2,
237 	ISCSI_CHAP_PHASE_END = 3,
238 };
239 
240 enum session_type {
241 	SESSION_TYPE_INVALID = 0,
242 	SESSION_TYPE_NORMAL = 1,
243 	SESSION_TYPE_DISCOVERY = 2,
244 };
245 
246 #define ISCSI_CHAP_CHALLENGE_LEN	1024
247 #define ISCSI_CHAP_MAX_USER_LEN		255
248 #define ISCSI_CHAP_MAX_SECRET_LEN	255
249 
250 struct iscsi_chap_auth {
251 	enum iscsi_chap_phase chap_phase;
252 
253 	char user[ISCSI_CHAP_MAX_USER_LEN + 1];
254 	char secret[ISCSI_CHAP_MAX_SECRET_LEN + 1];
255 	char muser[ISCSI_CHAP_MAX_USER_LEN + 1];
256 	char msecret[ISCSI_CHAP_MAX_SECRET_LEN + 1];
257 
258 	uint8_t chap_id[1];
259 	uint8_t chap_mid[1];
260 	int chap_challenge_len;
261 	uint8_t chap_challenge[ISCSI_CHAP_CHALLENGE_LEN];
262 	int chap_mchallenge_len;
263 	uint8_t chap_mchallenge[ISCSI_CHAP_CHALLENGE_LEN];
264 };
265 
266 struct spdk_iscsi_auth_secret {
267 	char user[ISCSI_CHAP_MAX_USER_LEN + 1];
268 	char secret[ISCSI_CHAP_MAX_SECRET_LEN + 1];
269 	char muser[ISCSI_CHAP_MAX_USER_LEN + 1];
270 	char msecret[ISCSI_CHAP_MAX_SECRET_LEN + 1];
271 	TAILQ_ENTRY(spdk_iscsi_auth_secret) tailq;
272 };
273 
274 struct spdk_iscsi_auth_group {
275 	int32_t tag;
276 	TAILQ_HEAD(, spdk_iscsi_auth_secret) secret_head;
277 	TAILQ_ENTRY(spdk_iscsi_auth_group) tailq;
278 };
279 
280 struct spdk_iscsi_sess {
281 	uint32_t connections;
282 	struct spdk_iscsi_conn **conns;
283 
284 	struct spdk_scsi_port *initiator_port;
285 	int tag;
286 
287 	uint64_t isid;
288 	uint16_t tsih;
289 	struct spdk_iscsi_tgt_node *target;
290 	int queue_depth;
291 
292 	struct iscsi_param *params;
293 
294 	enum session_type session_type;
295 	uint32_t MaxConnections;
296 	uint32_t MaxOutstandingR2T;
297 	uint32_t DefaultTime2Wait;
298 	uint32_t DefaultTime2Retain;
299 	uint32_t FirstBurstLength;
300 	uint32_t MaxBurstLength;
301 	bool InitialR2T;
302 	bool ImmediateData;
303 	bool DataPDUInOrder;
304 	bool DataSequenceInOrder;
305 	uint32_t ErrorRecoveryLevel;
306 
307 	uint32_t ExpCmdSN;
308 	uint32_t MaxCmdSN;
309 
310 	uint32_t current_text_itt;
311 };
312 
313 struct spdk_iscsi_poll_group {
314 	struct spdk_poller				*poller;
315 	struct spdk_poller				*nop_poller;
316 	STAILQ_HEAD(connections, spdk_iscsi_conn)	connections;
317 	struct spdk_sock_group				*sock_group;
318 	TAILQ_ENTRY(spdk_iscsi_poll_group)		link;
319 };
320 
321 struct spdk_iscsi_opts {
322 	char *authfile;
323 	char *nodebase;
324 	int32_t timeout;
325 	int32_t nopininterval;
326 	bool disable_chap;
327 	bool require_chap;
328 	bool mutual_chap;
329 	int32_t chap_group;
330 	uint32_t MaxSessions;
331 	uint32_t MaxConnectionsPerSession;
332 	uint32_t MaxConnections;
333 	uint32_t MaxQueueDepth;
334 	uint32_t DefaultTime2Wait;
335 	uint32_t DefaultTime2Retain;
336 	uint32_t FirstBurstLength;
337 	bool ImmediateData;
338 	uint32_t ErrorRecoveryLevel;
339 	bool AllowDuplicateIsid;
340 	uint32_t MaxLargeDataInPerConnection;
341 	uint32_t MaxR2TPerConnection;
342 	uint32_t pdu_pool_size;
343 	uint32_t immediate_data_pool_size;
344 	uint32_t data_out_pool_size;
345 };
346 
347 struct spdk_iscsi_globals {
348 	char *authfile;
349 	char *nodebase;
350 	pthread_mutex_t mutex;
351 	uint32_t refcnt;
352 	TAILQ_HEAD(, spdk_iscsi_portal)		portal_head;
353 	TAILQ_HEAD(, spdk_iscsi_portal_grp)	pg_head;
354 	TAILQ_HEAD(, spdk_iscsi_init_grp)	ig_head;
355 	TAILQ_HEAD(, spdk_iscsi_tgt_node)	target_head;
356 	TAILQ_HEAD(, spdk_iscsi_auth_group)	auth_group_head;
357 	TAILQ_HEAD(, spdk_iscsi_poll_group)	poll_group_head;
358 
359 	int32_t timeout;
360 	int32_t nopininterval;
361 	bool disable_chap;
362 	bool require_chap;
363 	bool mutual_chap;
364 	int32_t chap_group;
365 
366 	uint32_t MaxSessions;
367 	uint32_t MaxConnectionsPerSession;
368 	uint32_t MaxConnections;
369 	uint32_t MaxQueueDepth;
370 	uint32_t DefaultTime2Wait;
371 	uint32_t DefaultTime2Retain;
372 	uint32_t FirstBurstLength;
373 	bool ImmediateData;
374 	uint32_t ErrorRecoveryLevel;
375 	bool AllowDuplicateIsid;
376 	uint32_t MaxLargeDataInPerConnection;
377 	uint32_t MaxR2TPerConnection;
378 	uint32_t pdu_pool_size;
379 	uint32_t immediate_data_pool_size;
380 	uint32_t data_out_pool_size;
381 
382 	struct spdk_mempool *pdu_pool;
383 	struct spdk_mempool *pdu_immediate_data_pool;
384 	struct spdk_mempool *pdu_data_out_pool;
385 	struct spdk_mempool *session_pool;
386 	struct spdk_mempool *task_pool;
387 
388 	struct spdk_iscsi_sess	**session;
389 };
390 
391 #define ISCSI_SECURITY_NEGOTIATION_PHASE	0
392 #define ISCSI_OPERATIONAL_NEGOTIATION_PHASE	1
393 #define ISCSI_NSG_RESERVED_CODE			2
394 #define ISCSI_FULL_FEATURE_PHASE		3
395 
396 /* logout reason */
397 #define ISCSI_LOGOUT_REASON_CLOSE_SESSION		0
398 #define ISCSI_LOGOUT_REASON_CLOSE_CONNECTION		1
399 #define ISCSI_LOGOUT_REASON_REMOVE_CONN_FOR_RECOVERY	2
400 
401 enum spdk_error_codes {
402 	SPDK_ISCSI_CONNECTION_FATAL	= -1,
403 	SPDK_PDU_FATAL		= -2,
404 };
405 
406 #define DGET24(B)											\
407 	(((  (uint32_t) *((uint8_t *)(B)+0)) << 16)				\
408 	 | (((uint32_t) *((uint8_t *)(B)+1)) << 8)				\
409 	 | (((uint32_t) *((uint8_t *)(B)+2)) << 0))
410 
411 #define DSET24(B,D)													\
412 	(((*((uint8_t *)(B)+0)) = (uint8_t)((uint32_t)(D) >> 16)),		\
413 	 ((*((uint8_t *)(B)+1)) = (uint8_t)((uint32_t)(D) >> 8)),		\
414 	 ((*((uint8_t *)(B)+2)) = (uint8_t)((uint32_t)(D) >> 0)))
415 
416 #define xstrdup(s) (s ? strdup(s) : (char *)NULL)
417 
418 extern struct spdk_iscsi_globals g_iscsi;
419 extern struct spdk_iscsi_opts *g_spdk_iscsi_opts;
420 
421 struct spdk_iscsi_task;
422 struct spdk_json_write_ctx;
423 
424 typedef void (*spdk_iscsi_init_cb)(void *cb_arg, int rc);
425 
426 void spdk_iscsi_init(spdk_iscsi_init_cb cb_fn, void *cb_arg);
427 typedef void (*spdk_iscsi_fini_cb)(void *arg);
428 void spdk_iscsi_fini(spdk_iscsi_fini_cb cb_fn, void *cb_arg);
429 void shutdown_iscsi_conns_done(void);
430 void spdk_iscsi_config_json(struct spdk_json_write_ctx *w);
431 
432 struct spdk_iscsi_opts *iscsi_opts_alloc(void);
433 void iscsi_opts_free(struct spdk_iscsi_opts *opts);
434 struct spdk_iscsi_opts *iscsi_opts_copy(struct spdk_iscsi_opts *src);
435 void iscsi_opts_info_json(struct spdk_json_write_ctx *w);
436 int iscsi_set_discovery_auth(bool disable_chap, bool require_chap,
437 			     bool mutual_chap, int32_t chap_group);
438 int iscsi_chap_get_authinfo(struct iscsi_chap_auth *auth, const char *authuser,
439 			    int ag_tag);
440 int iscsi_add_auth_group(int32_t tag, struct spdk_iscsi_auth_group **_group);
441 struct spdk_iscsi_auth_group *iscsi_find_auth_group_by_tag(int32_t tag);
442 void iscsi_delete_auth_group(struct spdk_iscsi_auth_group *group);
443 int iscsi_auth_group_add_secret(struct spdk_iscsi_auth_group *group,
444 				const char *user, const char *secret,
445 				const char *muser, const char *msecret);
446 int iscsi_auth_group_delete_secret(struct spdk_iscsi_auth_group *group,
447 				   const char *user);
448 void iscsi_auth_groups_info_json(struct spdk_json_write_ctx *w);
449 
450 void iscsi_task_response(struct spdk_iscsi_conn *conn,
451 			 struct spdk_iscsi_task *task);
452 int iscsi_build_iovs(struct spdk_iscsi_conn *conn, struct iovec *iovs, int iovcnt,
453 		     struct spdk_iscsi_pdu *pdu, uint32_t *mapped_length);
454 int iscsi_handle_incoming_pdus(struct spdk_iscsi_conn *conn);
455 void iscsi_task_mgmt_response(struct spdk_iscsi_conn *conn,
456 			      struct spdk_iscsi_task *task);
457 
458 void iscsi_free_sess(struct spdk_iscsi_sess *sess);
459 void iscsi_clear_all_transfer_task(struct spdk_iscsi_conn *conn,
460 				   struct spdk_scsi_lun *lun,
461 				   struct spdk_iscsi_pdu *pdu);
462 bool iscsi_del_transfer_task(struct spdk_iscsi_conn *conn, uint32_t CmdSN);
463 
464 uint32_t iscsi_pdu_calc_header_digest(struct spdk_iscsi_pdu *pdu);
465 uint32_t iscsi_pdu_calc_data_digest(struct spdk_iscsi_pdu *pdu);
466 
467 /* Memory management */
468 void iscsi_put_pdu(struct spdk_iscsi_pdu *pdu);
469 struct spdk_iscsi_pdu *iscsi_get_pdu(struct spdk_iscsi_conn *conn);
470 void iscsi_op_abort_task_set(struct spdk_iscsi_task *task,
471 			     uint8_t function);
472 void iscsi_queue_task(struct spdk_iscsi_conn *conn, struct spdk_iscsi_task *task);
473 
474 static inline struct spdk_mobj *
475 iscsi_datapool_get(struct spdk_mempool *pool)
476 {
477 	return spdk_mempool_get(pool);
478 }
479 
480 static inline void
481 iscsi_datapool_put(struct spdk_mobj *mobj)
482 {
483 	assert(mobj != NULL);
484 
485 	mobj->data_len = 0;
486 	spdk_mempool_put(mobj->mp, (void *)mobj);
487 }
488 
489 static inline uint32_t
490 iscsi_get_max_immediate_data_size(void)
491 {
492 	/*
493 	 * Specify enough extra space in addition to FirstBurstLength to
494 	 *  account for a header digest, data digest and additional header
495 	 *  segments (AHS).  These are not normally used but they do not
496 	 *  take up much space and we need to make sure the worst-case scenario
497 	 *  can be satisfied by the size returned here.
498 	 */
499 	return g_iscsi.FirstBurstLength +
500 	       ISCSI_DIGEST_LEN + /* data digest */
501 	       ISCSI_DIGEST_LEN + /* header digest */
502 	       8 +		   /* bidirectional AHS */
503 	       52;		   /* extended CDB AHS (for a 64-byte CDB) */
504 }
505 
506 #endif /* SPDK_ISCSI_H */
507