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