xref: /spdk/lib/iscsi/iscsi.h (revision fa2d95b3fe66e7f5c543eaef89fa00d4eaa0e6e7)
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 #include "spdk/event.h"
43 #include "spdk/thread.h"
44 
45 #include "iscsi/param.h"
46 #include "iscsi/tgt_node.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 
64 #define DEFAULT_PORT 3260
65 #define DEFAULT_MAX_SESSIONS 128
66 #define DEFAULT_MAX_CONNECTIONS_PER_SESSION 2
67 #define DEFAULT_MAXOUTSTANDINGR2T 1
68 #define DEFAULT_DEFAULTTIME2WAIT 2
69 #define DEFAULT_DEFAULTTIME2RETAIN 20
70 #define DEFAULT_INITIALR2T true
71 #define DEFAULT_IMMEDIATEDATA true
72 #define DEFAULT_DATAPDUINORDER true
73 #define DEFAULT_DATASEQUENCEINORDER true
74 #define DEFAULT_ERRORRECOVERYLEVEL 0
75 #define DEFAULT_TIMEOUT 60
76 #define MAX_NOPININTERVAL 60
77 #define DEFAULT_NOPININTERVAL 30
78 #define DEFAULT_CONNECTIONS_PER_LCORE 4
79 
80 /*
81  * SPDK iSCSI target currently only supports 64KB as the maximum data segment length
82  *  it can receive from initiators.  Other values may work, but no guarantees.
83  */
84 #define SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH  65536
85 
86 /*
87  * Defines maximum number of data out buffers each connection can have in
88  *  use at any given time.
89  */
90 #define MAX_DATA_OUT_PER_CONNECTION 16
91 
92 /*
93  * Defines maximum number of data in buffers each connection can have in
94  *  use at any given time. So this limit does not affect I/O smaller than
95  *  SPDK_BDEV_SMALL_BUF_MAX_SIZE.
96  */
97 #define MAX_LARGE_DATAIN_PER_CONNECTION 64
98 
99 #define SPDK_ISCSI_MAX_BURST_LENGTH	\
100 		(SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH * MAX_DATA_OUT_PER_CONNECTION)
101 
102 /*
103  * Defines default maximum amount in bytes of unsolicited data the iSCSI
104  *  initiator may send to the SPDK iSCSI target during the execution of
105  *  a single SCSI command. And it is smaller than the MaxBurstLength.
106  */
107 #define SPDK_ISCSI_FIRST_BURST_LENGTH	8192
108 
109 /*
110  * Defines minimum amount in bytes of unsolicited data the iSCSI initiator
111  *  may send to the SPDK iSCSI target during the execution of a single
112  *  SCSI command.
113  */
114 #define SPDK_ISCSI_MIN_FIRST_BURST_LENGTH	512
115 
116 #define SPDK_ISCSI_MAX_FIRST_BURST_LENGTH	16777215
117 
118 /*
119  * Defines default maximum queue depth per connection and this can be
120  * changed by configuration file.
121  */
122 #define DEFAULT_MAX_QUEUE_DEPTH	64
123 
124 /** Defines how long we should wait for a TCP close after responding to a
125  *   logout request, before terminating the connection ourselves.
126  */
127 #define ISCSI_LOGOUT_TIMEOUT 5 /* in seconds */
128 
129 /* according to RFC1982 */
130 #define SN32_CMPMAX (((uint32_t)1U) << (32 - 1))
131 #define SN32_LT(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 #define SN32_GT(S1,S2) \
138 	(((uint32_t)(S1) != (uint32_t)(S2))				\
139 	    && (((uint32_t)(S1) < (uint32_t)(S2)			\
140 		    && ((uint32_t)(S2) - (uint32_t)(S1) > SN32_CMPMAX))	\
141 		|| ((uint32_t)(S1) > (uint32_t)(S2)			\
142 		    && ((uint32_t)(S1) - (uint32_t)(S2) < SN32_CMPMAX))))
143 
144 /* For spdk_iscsi_login_in related function use, we need to avoid the conflict
145  * with other errors
146  * */
147 #define SPDK_ISCSI_LOGIN_ERROR_RESPONSE -1000
148 #define SPDK_ISCSI_LOGIN_ERROR_PARAMETER -1001
149 #define SPDK_ISCSI_PARAMETER_EXCHANGE_NOT_ONCE -1002
150 
151 #define ISCSI_AHS_LEN 60
152 
153 struct spdk_mobj {
154 	struct spdk_mempool *mp;
155 	void *buf;
156 };
157 
158 struct spdk_iscsi_pdu {
159 	struct iscsi_bhs bhs;
160 	struct spdk_mobj *mobj;
161 	uint8_t *data_buf;
162 	uint8_t *data;
163 	uint8_t header_digest[ISCSI_DIGEST_LEN];
164 	uint8_t data_digest[ISCSI_DIGEST_LEN];
165 	size_t data_segment_len;
166 	int bhs_valid_bytes;
167 	int ahs_valid_bytes;
168 	int data_valid_bytes;
169 	int hdigest_valid_bytes;
170 	int ddigest_valid_bytes;
171 	int ref;
172 	bool data_from_mempool;  /* indicate whether the data buffer is allocated from mempool */
173 	struct spdk_iscsi_task *task; /* data tied to a task buffer */
174 	uint32_t cmd_sn;
175 	uint32_t writev_offset;
176 	uint32_t data_buf_len;
177 	bool dif_insert_or_strip;
178 	struct spdk_dif_ctx dif_ctx;
179 	TAILQ_ENTRY(spdk_iscsi_pdu)	tailq;
180 
181 
182 	/*
183 	 * 60 bytes of AHS should suffice for now.
184 	 * This should always be at the end of PDU data structure.
185 	 * we need to not zero this out when doing memory clear.
186 	 */
187 	uint8_t ahs[ISCSI_AHS_LEN];
188 
189 	struct {
190 		uint16_t length; /* iSCSI SenseLength (big-endian) */
191 		uint8_t data[32];
192 	} sense;
193 };
194 
195 enum iscsi_connection_state {
196 	ISCSI_CONN_STATE_INVALID = 0,
197 	ISCSI_CONN_STATE_RUNNING = 1,
198 	ISCSI_CONN_STATE_LOGGED_OUT = 2,
199 	ISCSI_CONN_STATE_EXITING = 3,
200 	ISCSI_CONN_STATE_EXITED = 4,
201 };
202 
203 enum iscsi_chap_phase {
204 	ISCSI_CHAP_PHASE_NONE = 0,
205 	ISCSI_CHAP_PHASE_WAIT_A = 1,
206 	ISCSI_CHAP_PHASE_WAIT_NR = 2,
207 	ISCSI_CHAP_PHASE_END = 3,
208 };
209 
210 enum session_type {
211 	SESSION_TYPE_INVALID = 0,
212 	SESSION_TYPE_NORMAL = 1,
213 	SESSION_TYPE_DISCOVERY = 2,
214 };
215 
216 #define ISCSI_CHAP_CHALLENGE_LEN	1024
217 #define ISCSI_CHAP_MAX_USER_LEN		255
218 #define ISCSI_CHAP_MAX_SECRET_LEN	255
219 
220 struct iscsi_chap_auth {
221 	enum iscsi_chap_phase chap_phase;
222 
223 	char user[ISCSI_CHAP_MAX_USER_LEN + 1];
224 	char secret[ISCSI_CHAP_MAX_SECRET_LEN + 1];
225 	char muser[ISCSI_CHAP_MAX_USER_LEN + 1];
226 	char msecret[ISCSI_CHAP_MAX_SECRET_LEN + 1];
227 
228 	uint8_t chap_id[1];
229 	uint8_t chap_mid[1];
230 	int chap_challenge_len;
231 	uint8_t chap_challenge[ISCSI_CHAP_CHALLENGE_LEN];
232 	int chap_mchallenge_len;
233 	uint8_t chap_mchallenge[ISCSI_CHAP_CHALLENGE_LEN];
234 };
235 
236 struct spdk_iscsi_auth_secret {
237 	char user[ISCSI_CHAP_MAX_USER_LEN + 1];
238 	char secret[ISCSI_CHAP_MAX_SECRET_LEN + 1];
239 	char muser[ISCSI_CHAP_MAX_USER_LEN + 1];
240 	char msecret[ISCSI_CHAP_MAX_SECRET_LEN + 1];
241 	TAILQ_ENTRY(spdk_iscsi_auth_secret) tailq;
242 };
243 
244 struct spdk_iscsi_auth_group {
245 	int32_t tag;
246 	TAILQ_HEAD(, spdk_iscsi_auth_secret) secret_head;
247 	TAILQ_ENTRY(spdk_iscsi_auth_group) tailq;
248 };
249 
250 struct spdk_iscsi_sess {
251 	uint32_t connections;
252 	struct spdk_iscsi_conn **conns;
253 
254 	struct spdk_scsi_port *initiator_port;
255 	int tag;
256 
257 	uint64_t isid;
258 	uint16_t tsih;
259 	struct spdk_iscsi_tgt_node *target;
260 	int queue_depth;
261 
262 	struct iscsi_param *params;
263 
264 	enum session_type session_type;
265 	uint32_t MaxConnections;
266 	uint32_t MaxOutstandingR2T;
267 	uint32_t DefaultTime2Wait;
268 	uint32_t DefaultTime2Retain;
269 	uint32_t FirstBurstLength;
270 	uint32_t MaxBurstLength;
271 	bool InitialR2T;
272 	bool ImmediateData;
273 	bool DataPDUInOrder;
274 	bool DataSequenceInOrder;
275 	uint32_t ErrorRecoveryLevel;
276 
277 	uint32_t ExpCmdSN;
278 	uint32_t MaxCmdSN;
279 
280 	uint32_t current_text_itt;
281 };
282 
283 struct spdk_iscsi_poll_group {
284 	uint32_t					core;
285 	struct spdk_poller				*poller;
286 	struct spdk_poller				*nop_poller;
287 	STAILQ_HEAD(connections, spdk_iscsi_conn)	connections;
288 	struct spdk_sock_group				*sock_group;
289 };
290 
291 struct spdk_iscsi_opts {
292 	char *authfile;
293 	char *nodebase;
294 	int32_t timeout;
295 	int32_t nopininterval;
296 	bool disable_chap;
297 	bool require_chap;
298 	bool mutual_chap;
299 	int32_t chap_group;
300 	uint32_t MaxSessions;
301 	uint32_t MaxConnectionsPerSession;
302 	uint32_t MaxConnections;
303 	uint32_t MaxQueueDepth;
304 	uint32_t DefaultTime2Wait;
305 	uint32_t DefaultTime2Retain;
306 	uint32_t FirstBurstLength;
307 	bool ImmediateData;
308 	uint32_t ErrorRecoveryLevel;
309 	bool AllowDuplicateIsid;
310 	uint32_t min_connections_per_core;
311 };
312 
313 struct spdk_iscsi_globals {
314 	char *authfile;
315 	char *nodebase;
316 	pthread_mutex_t mutex;
317 	TAILQ_HEAD(, spdk_iscsi_portal)		portal_head;
318 	TAILQ_HEAD(, spdk_iscsi_portal_grp)	pg_head;
319 	TAILQ_HEAD(, spdk_iscsi_init_grp)	ig_head;
320 	TAILQ_HEAD(, spdk_iscsi_tgt_node)	target_head;
321 	TAILQ_HEAD(, spdk_iscsi_auth_group)	auth_group_head;
322 
323 	int32_t timeout;
324 	int32_t nopininterval;
325 	bool disable_chap;
326 	bool require_chap;
327 	bool mutual_chap;
328 	int32_t chap_group;
329 
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 
341 	struct spdk_mempool *pdu_pool;
342 	struct spdk_mempool *pdu_immediate_data_pool;
343 	struct spdk_mempool *pdu_data_out_pool;
344 	struct spdk_mempool *session_pool;
345 	struct spdk_mempool *task_pool;
346 
347 	struct spdk_iscsi_sess	**session;
348 	struct spdk_iscsi_poll_group *poll_group;
349 };
350 
351 #define ISCSI_SECURITY_NEGOTIATION_PHASE	0
352 #define ISCSI_OPERATIONAL_NEGOTIATION_PHASE	1
353 #define ISCSI_NSG_RESERVED_CODE			2
354 #define ISCSI_FULL_FEATURE_PHASE		3
355 
356 enum spdk_error_codes {
357 	SPDK_SUCCESS		= 0,
358 	SPDK_ISCSI_CONNECTION_FATAL	= -1,
359 	SPDK_PDU_FATAL		= -2,
360 };
361 
362 #define DGET24(B)											\
363 	(((  (uint32_t) *((uint8_t *)(B)+0)) << 16)				\
364 	 | (((uint32_t) *((uint8_t *)(B)+1)) << 8)				\
365 	 | (((uint32_t) *((uint8_t *)(B)+2)) << 0))
366 
367 #define DSET24(B,D)													\
368 	(((*((uint8_t *)(B)+0)) = (uint8_t)((uint32_t)(D) >> 16)),		\
369 	 ((*((uint8_t *)(B)+1)) = (uint8_t)((uint32_t)(D) >> 8)),		\
370 	 ((*((uint8_t *)(B)+2)) = (uint8_t)((uint32_t)(D) >> 0)))
371 
372 #define xstrdup(s) (s ? strdup(s) : (char *)NULL)
373 
374 extern struct spdk_iscsi_globals g_spdk_iscsi;
375 extern struct spdk_iscsi_opts *g_spdk_iscsi_opts;
376 
377 struct spdk_iscsi_task;
378 struct spdk_json_write_ctx;
379 
380 typedef void (*spdk_iscsi_init_cb)(void *cb_arg, int rc);
381 
382 void spdk_iscsi_init(spdk_iscsi_init_cb cb_fn, void *cb_arg);
383 typedef void (*spdk_iscsi_fini_cb)(void *arg);
384 void spdk_iscsi_fini(spdk_iscsi_fini_cb cb_fn, void *cb_arg);
385 void spdk_shutdown_iscsi_conns_done(void);
386 void spdk_iscsi_config_text(FILE *fp);
387 void spdk_iscsi_config_json(struct spdk_json_write_ctx *w);
388 
389 struct spdk_iscsi_opts *spdk_iscsi_opts_alloc(void);
390 void spdk_iscsi_opts_free(struct spdk_iscsi_opts *opts);
391 struct spdk_iscsi_opts *spdk_iscsi_opts_copy(struct spdk_iscsi_opts *src);
392 void spdk_iscsi_opts_info_json(struct spdk_json_write_ctx *w);
393 int spdk_iscsi_set_discovery_auth(bool disable_chap, bool require_chap,
394 				  bool mutual_chap, int32_t chap_group);
395 int spdk_iscsi_chap_get_authinfo(struct iscsi_chap_auth *auth, const char *authuser,
396 				 int ag_tag);
397 int spdk_iscsi_add_auth_group(int32_t tag, struct spdk_iscsi_auth_group **_group);
398 struct spdk_iscsi_auth_group *spdk_iscsi_find_auth_group_by_tag(int32_t tag);
399 void spdk_iscsi_delete_auth_group(struct spdk_iscsi_auth_group *group);
400 int spdk_iscsi_auth_group_add_secret(struct spdk_iscsi_auth_group *group,
401 				     const char *user, const char *secret,
402 				     const char *muser, const char *msecret);
403 int spdk_iscsi_auth_group_delete_secret(struct spdk_iscsi_auth_group *group,
404 					const char *user);
405 void spdk_iscsi_auth_groups_info_json(struct spdk_json_write_ctx *w);
406 
407 void spdk_iscsi_send_nopin(struct spdk_iscsi_conn *conn);
408 void spdk_iscsi_task_response(struct spdk_iscsi_conn *conn,
409 			      struct spdk_iscsi_task *task);
410 int spdk_iscsi_execute(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu);
411 int spdk_iscsi_build_iovs(struct spdk_iscsi_conn *conn, struct iovec *iovs, int num_iovs,
412 			  struct spdk_iscsi_pdu *pdu, uint32_t *mapped_length);
413 int spdk_iscsi_read_pdu(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu **_pdu);
414 bool spdk_iscsi_get_dif_ctx(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu,
415 			    struct spdk_dif_ctx *dif_ctx);
416 void spdk_iscsi_task_mgmt_response(struct spdk_iscsi_conn *conn,
417 				   struct spdk_iscsi_task *task);
418 
419 int spdk_iscsi_conn_params_init(struct iscsi_param **params);
420 int spdk_iscsi_sess_params_init(struct iscsi_param **params);
421 
422 void spdk_free_sess(struct spdk_iscsi_sess *sess);
423 void spdk_clear_all_transfer_task(struct spdk_iscsi_conn *conn,
424 				  struct spdk_scsi_lun *lun,
425 				  struct spdk_iscsi_pdu *pdu);
426 void spdk_del_transfer_task(struct spdk_iscsi_conn *conn, uint32_t CmdSN);
427 bool spdk_iscsi_is_deferred_free_pdu(struct spdk_iscsi_pdu *pdu);
428 
429 int spdk_iscsi_negotiate_params(struct spdk_iscsi_conn *conn,
430 				struct iscsi_param **params_p, uint8_t *data,
431 				int alloc_len, int data_len);
432 int spdk_iscsi_copy_param2var(struct spdk_iscsi_conn *conn);
433 
434 void spdk_iscsi_task_cpl(struct spdk_scsi_task *scsi_task);
435 void spdk_iscsi_task_mgmt_cpl(struct spdk_scsi_task *scsi_task);
436 uint32_t spdk_iscsi_pdu_calc_header_digest(struct spdk_iscsi_pdu *pdu);
437 uint32_t spdk_iscsi_pdu_calc_data_digest(struct spdk_iscsi_pdu *pdu);
438 
439 /* Memory management */
440 void spdk_put_pdu(struct spdk_iscsi_pdu *pdu);
441 struct spdk_iscsi_pdu *spdk_get_pdu(void);
442 int spdk_iscsi_conn_handle_queued_datain_tasks(struct spdk_iscsi_conn *conn);
443 void spdk_iscsi_op_abort_task_set(struct spdk_iscsi_task *task,
444 				  uint8_t function);
445 
446 static inline int
447 spdk_get_max_immediate_data_size(void)
448 {
449 	/*
450 	 * Specify enough extra space in addition to FirstBurstLength to
451 	 *  account for a header digest, data digest and additional header
452 	 *  segments (AHS).  These are not normally used but they do not
453 	 *  take up much space and we need to make sure the worst-case scenario
454 	 *  can be satisified by the size returned here.
455 	 */
456 	return g_spdk_iscsi.FirstBurstLength +
457 	       ISCSI_DIGEST_LEN + /* data digest */
458 	       ISCSI_DIGEST_LEN + /* header digest */
459 	       8 +		   /* bidirectional AHS */
460 	       52;		   /* extended CDB AHS (for a 64-byte CDB) */
461 }
462 
463 #endif /* SPDK_ISCSI_H */
464