4 * Copyright (C) 2008-2012 Daisuke Aoyama <aoyama@peach.ne.jp>.
5 * Copyright (c) Intel Corporation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
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.
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.
38 #include "spdk/stdinc.h"
40 #include "spdk/bdev.h"
41 #include "spdk/iscsi_spec.h"
42 #include "spdk/thread.h"
43 #include "spdk/sock.h"
45 #include "spdk/scsi.h"
46 #include "iscsi/param.h"
48 #include "spdk/assert.h"
50 #include "spdk/util.h"
52 #define SPDK_ISCSI_DEFAULT_NODEBASE "iqn.2016-06.io.spdk"
54 #define DEFAULT_MAXR2T 4
55 #define MAX_INITIATOR_PORT_NAME 256
56 #define MAX_INITIATOR_NAME 223
57 #define MAX_TARGET_NAME 223
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
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
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.
85 #define SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH 65536
88 * Defines maximum number of data out buffers each connection can have in
89 * use at any given time.
91 #define MAX_DATA_OUT_PER_CONNECTION 16
94 * Defines 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.
98 #define MAX_LARGE_DATAIN_PER_CONNECTION 64
100 #define SPDK_ISCSI_MAX_BURST_LENGTH \
101 (SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH * MAX_DATA_OUT_PER_CONNECTION)
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.
108 #define SPDK_ISCSI_FIRST_BURST_LENGTH 8192
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
115 #define SPDK_ISCSI_MIN_FIRST_BURST_LENGTH 512
117 #define SPDK_ISCSI_MAX_FIRST_BURST_LENGTH 16777215
120 * Defines default maximum queue depth per connection and this can be
121 * changed by configuration file.
123 #define DEFAULT_MAX_QUEUE_DEPTH 64
125 /** Defines how long we should wait for a logout request when the target
126 * requests logout to the initiator asynchronously.
128 #define ISCSI_LOGOUT_REQUEST_TIMEOUT 30 /* in seconds */
130 /** Defines how long we should wait for a TCP close after responding to a
131 * logout request, before terminating the connection ourselves.
133 #define ISCSI_LOGOUT_TIMEOUT 5 /* in seconds */
135 /* For spdk_iscsi_login_in related function use, we need to avoid the conflict
138 #define SPDK_ISCSI_LOGIN_ERROR_RESPONSE -1000
139 #define SPDK_ISCSI_LOGIN_ERROR_PARAMETER -1001
140 #define SPDK_ISCSI_PARAMETER_EXCHANGE_NOT_ONCE -1002
142 #define ISCSI_AHS_LEN 60
145 struct spdk_mempool
*mp
;
150 * Maximum number of SGL elements, i.e.,
151 * BHS, AHS, Header Digest, Data Segment and Data Digest.
153 #define SPDK_ISCSI_MAX_SGL_DESCRIPTORS (5)
155 typedef void (*iscsi_conn_xfer_complete_cb
)(void *cb_arg
);
157 struct spdk_iscsi_pdu
{
158 struct iscsi_bhs bhs
;
159 struct spdk_mobj
*mobj
;
163 uint8_t header_digest
[ISCSI_DIGEST_LEN
];
164 uint8_t data_digest
[ISCSI_DIGEST_LEN
];
165 size_t data_segment_len
;
168 uint32_t data_valid_bytes
;
169 int hdigest_valid_bytes
;
170 int ddigest_valid_bytes
;
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 */
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 struct spdk_iscsi_conn
*conn
;
181 iscsi_conn_xfer_complete_cb cb_fn
;
184 /* The sock request ends with a 0 length iovec. Place the actual iovec immediately
185 * after it. There is a static assert below to check if the compiler inserted
186 * any unwanted padding */
187 int32_t mapped_length
;
188 struct spdk_sock_request sock_req
;
189 struct iovec iov
[SPDK_ISCSI_MAX_SGL_DESCRIPTORS
];
190 TAILQ_ENTRY(spdk_iscsi_pdu
) tailq
;
194 * 60 bytes of AHS should suffice for now.
195 * This should always be at the end of PDU data structure.
196 * we need to not zero this out when doing memory clear.
198 uint8_t ahs
[ISCSI_AHS_LEN
];
201 uint16_t length
; /* iSCSI SenseLength (big-endian) */
205 SPDK_STATIC_ASSERT(offsetof(struct spdk_iscsi_pdu
,
206 sock_req
) + sizeof(struct spdk_sock_request
) == offsetof(struct spdk_iscsi_pdu
, iov
),
207 "Compiler inserted padding between iov and sock_req");
209 enum iscsi_connection_state
{
210 ISCSI_CONN_STATE_INVALID
= 0,
211 ISCSI_CONN_STATE_RUNNING
= 1,
212 ISCSI_CONN_STATE_EXITING
= 2,
213 ISCSI_CONN_STATE_EXITED
= 3,
216 enum iscsi_chap_phase
{
217 ISCSI_CHAP_PHASE_NONE
= 0,
218 ISCSI_CHAP_PHASE_WAIT_A
= 1,
219 ISCSI_CHAP_PHASE_WAIT_NR
= 2,
220 ISCSI_CHAP_PHASE_END
= 3,
224 SESSION_TYPE_INVALID
= 0,
225 SESSION_TYPE_NORMAL
= 1,
226 SESSION_TYPE_DISCOVERY
= 2,
229 #define ISCSI_CHAP_CHALLENGE_LEN 1024
230 #define ISCSI_CHAP_MAX_USER_LEN 255
231 #define ISCSI_CHAP_MAX_SECRET_LEN 255
233 struct iscsi_chap_auth
{
234 enum iscsi_chap_phase chap_phase
;
236 char user
[ISCSI_CHAP_MAX_USER_LEN
+ 1];
237 char secret
[ISCSI_CHAP_MAX_SECRET_LEN
+ 1];
238 char muser
[ISCSI_CHAP_MAX_USER_LEN
+ 1];
239 char msecret
[ISCSI_CHAP_MAX_SECRET_LEN
+ 1];
243 int chap_challenge_len
;
244 uint8_t chap_challenge
[ISCSI_CHAP_CHALLENGE_LEN
];
245 int chap_mchallenge_len
;
246 uint8_t chap_mchallenge
[ISCSI_CHAP_CHALLENGE_LEN
];
249 struct spdk_iscsi_auth_secret
{
250 char user
[ISCSI_CHAP_MAX_USER_LEN
+ 1];
251 char secret
[ISCSI_CHAP_MAX_SECRET_LEN
+ 1];
252 char muser
[ISCSI_CHAP_MAX_USER_LEN
+ 1];
253 char msecret
[ISCSI_CHAP_MAX_SECRET_LEN
+ 1];
254 TAILQ_ENTRY(spdk_iscsi_auth_secret
) tailq
;
257 struct spdk_iscsi_auth_group
{
259 TAILQ_HEAD(, spdk_iscsi_auth_secret
) secret_head
;
260 TAILQ_ENTRY(spdk_iscsi_auth_group
) tailq
;
263 struct spdk_iscsi_sess
{
264 uint32_t connections
;
265 struct spdk_iscsi_conn
**conns
;
267 struct spdk_scsi_port
*initiator_port
;
272 struct spdk_iscsi_tgt_node
*target
;
275 struct iscsi_param
*params
;
277 enum session_type session_type
;
278 uint32_t MaxConnections
;
279 uint32_t MaxOutstandingR2T
;
280 uint32_t DefaultTime2Wait
;
281 uint32_t DefaultTime2Retain
;
282 uint32_t FirstBurstLength
;
283 uint32_t MaxBurstLength
;
287 bool DataSequenceInOrder
;
288 uint32_t ErrorRecoveryLevel
;
293 uint32_t current_text_itt
;
296 struct spdk_iscsi_poll_group
{
297 struct spdk_poller
*poller
;
298 struct spdk_poller
*nop_poller
;
299 STAILQ_HEAD(connections
, spdk_iscsi_conn
) connections
;
300 struct spdk_sock_group
*sock_group
;
301 TAILQ_ENTRY(spdk_iscsi_poll_group
) link
;
304 struct spdk_iscsi_opts
{
308 int32_t nopininterval
;
313 uint32_t MaxSessions
;
314 uint32_t MaxConnectionsPerSession
;
315 uint32_t MaxConnections
;
316 uint32_t MaxQueueDepth
;
317 uint32_t DefaultTime2Wait
;
318 uint32_t DefaultTime2Retain
;
319 uint32_t FirstBurstLength
;
321 uint32_t ErrorRecoveryLevel
;
322 bool AllowDuplicateIsid
;
325 struct spdk_iscsi_globals
{
328 pthread_mutex_t mutex
;
330 TAILQ_HEAD(, spdk_iscsi_portal
) portal_head
;
331 TAILQ_HEAD(, spdk_iscsi_portal_grp
) pg_head
;
332 TAILQ_HEAD(, spdk_iscsi_init_grp
) ig_head
;
333 TAILQ_HEAD(, spdk_iscsi_tgt_node
) target_head
;
334 TAILQ_HEAD(, spdk_iscsi_auth_group
) auth_group_head
;
335 TAILQ_HEAD(, spdk_iscsi_poll_group
) poll_group_head
;
338 int32_t nopininterval
;
344 uint32_t MaxSessions
;
345 uint32_t MaxConnectionsPerSession
;
346 uint32_t MaxConnections
;
347 uint32_t MaxQueueDepth
;
348 uint32_t DefaultTime2Wait
;
349 uint32_t DefaultTime2Retain
;
350 uint32_t FirstBurstLength
;
352 uint32_t ErrorRecoveryLevel
;
353 bool AllowDuplicateIsid
;
355 struct spdk_mempool
*pdu_pool
;
356 struct spdk_mempool
*pdu_immediate_data_pool
;
357 struct spdk_mempool
*pdu_data_out_pool
;
358 struct spdk_mempool
*session_pool
;
359 struct spdk_mempool
*task_pool
;
361 struct spdk_iscsi_sess
**session
;
364 #define ISCSI_SECURITY_NEGOTIATION_PHASE 0
365 #define ISCSI_OPERATIONAL_NEGOTIATION_PHASE 1
366 #define ISCSI_NSG_RESERVED_CODE 2
367 #define ISCSI_FULL_FEATURE_PHASE 3
370 #define ISCSI_LOGOUT_REASON_CLOSE_SESSION 0
371 #define ISCSI_LOGOUT_REASON_CLOSE_CONNECTION 1
372 #define ISCSI_LOGOUT_REASON_REMOVE_CONN_FOR_RECOVERY 2
374 enum spdk_error_codes
{
375 SPDK_ISCSI_CONNECTION_FATAL
= -1,
380 ((( (uint32_t) *((uint8_t *)(B)+0)) << 16) \
381 | (((uint32_t) *((uint8_t *)(B)+1)) << 8) \
382 | (((uint32_t) *((uint8_t *)(B)+2)) << 0))
384 #define DSET24(B,D) \
385 (((*((uint8_t *)(B)+0)) = (uint8_t)((uint32_t)(D) >> 16)), \
386 ((*((uint8_t *)(B)+1)) = (uint8_t)((uint32_t)(D) >> 8)), \
387 ((*((uint8_t *)(B)+2)) = (uint8_t)((uint32_t)(D) >> 0)))
389 #define xstrdup(s) (s ? strdup(s) : (char *)NULL)
391 extern struct spdk_iscsi_globals g_iscsi
;
392 extern struct spdk_iscsi_opts
*g_spdk_iscsi_opts
;
394 struct spdk_iscsi_task
;
395 struct spdk_json_write_ctx
;
397 typedef void (*spdk_iscsi_init_cb
)(void *cb_arg
, int rc
);
399 void spdk_iscsi_init(spdk_iscsi_init_cb cb_fn
, void *cb_arg
);
400 typedef void (*spdk_iscsi_fini_cb
)(void *arg
);
401 void spdk_iscsi_fini(spdk_iscsi_fini_cb cb_fn
, void *cb_arg
);
402 void shutdown_iscsi_conns_done(void);
403 void spdk_iscsi_config_text(FILE *fp
);
404 void spdk_iscsi_config_json(struct spdk_json_write_ctx
*w
);
406 struct spdk_iscsi_opts
*iscsi_opts_alloc(void);
407 void iscsi_opts_free(struct spdk_iscsi_opts
*opts
);
408 struct spdk_iscsi_opts
*iscsi_opts_copy(struct spdk_iscsi_opts
*src
);
409 void iscsi_opts_info_json(struct spdk_json_write_ctx
*w
);
410 int iscsi_set_discovery_auth(bool disable_chap
, bool require_chap
,
411 bool mutual_chap
, int32_t chap_group
);
412 int iscsi_chap_get_authinfo(struct iscsi_chap_auth
*auth
, const char *authuser
,
414 int iscsi_add_auth_group(int32_t tag
, struct spdk_iscsi_auth_group
**_group
);
415 struct spdk_iscsi_auth_group
*iscsi_find_auth_group_by_tag(int32_t tag
);
416 void iscsi_delete_auth_group(struct spdk_iscsi_auth_group
*group
);
417 int iscsi_auth_group_add_secret(struct spdk_iscsi_auth_group
*group
,
418 const char *user
, const char *secret
,
419 const char *muser
, const char *msecret
);
420 int iscsi_auth_group_delete_secret(struct spdk_iscsi_auth_group
*group
,
422 void iscsi_auth_groups_info_json(struct spdk_json_write_ctx
*w
);
424 void iscsi_task_response(struct spdk_iscsi_conn
*conn
,
425 struct spdk_iscsi_task
*task
);
426 int iscsi_build_iovs(struct spdk_iscsi_conn
*conn
, struct iovec
*iovs
, int iovcnt
,
427 struct spdk_iscsi_pdu
*pdu
, uint32_t *mapped_length
);
428 int iscsi_handle_incoming_pdus(struct spdk_iscsi_conn
*conn
);
429 void iscsi_task_mgmt_response(struct spdk_iscsi_conn
*conn
,
430 struct spdk_iscsi_task
*task
);
432 void iscsi_free_sess(struct spdk_iscsi_sess
*sess
);
433 void iscsi_clear_all_transfer_task(struct spdk_iscsi_conn
*conn
,
434 struct spdk_scsi_lun
*lun
,
435 struct spdk_iscsi_pdu
*pdu
);
436 bool iscsi_del_transfer_task(struct spdk_iscsi_conn
*conn
, uint32_t CmdSN
);
438 uint32_t iscsi_pdu_calc_header_digest(struct spdk_iscsi_pdu
*pdu
);
439 uint32_t iscsi_pdu_calc_data_digest(struct spdk_iscsi_pdu
*pdu
);
441 /* Memory management */
442 void iscsi_put_pdu(struct spdk_iscsi_pdu
*pdu
);
443 struct spdk_iscsi_pdu
*iscsi_get_pdu(struct spdk_iscsi_conn
*conn
);
444 void iscsi_op_abort_task_set(struct spdk_iscsi_task
*task
,
446 void iscsi_queue_task(struct spdk_iscsi_conn
*conn
, struct spdk_iscsi_task
*task
);
448 static inline uint32_t
449 iscsi_get_max_immediate_data_size(void)
452 * Specify enough extra space in addition to FirstBurstLength to
453 * account for a header digest, data digest and additional header
454 * segments (AHS). These are not normally used but they do not
455 * take up much space and we need to make sure the worst-case scenario
456 * can be satisified by the size returned here.
458 return g_iscsi
.FirstBurstLength
+
459 ISCSI_DIGEST_LEN
+ /* data digest */
460 ISCSI_DIGEST_LEN
+ /* header digest */
461 8 + /* bidirectional AHS */
462 52; /* extended CDB AHS (for a 64-byte CDB) */
465 #endif /* SPDK_ISCSI_H */