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 #include "spdk/stdinc.h" 36 37 #include "spdk/base64.h" 38 #include "spdk/crc32.h" 39 #include "spdk/endian.h" 40 #include "spdk/env.h" 41 #include "spdk/likely.h" 42 #include "spdk/trace.h" 43 #include "spdk/string.h" 44 #include "spdk/queue.h" 45 #include "spdk/net.h" 46 47 #include "iscsi/md5.h" 48 #include "iscsi/iscsi.h" 49 #include "iscsi/param.h" 50 #include "iscsi/tgt_node.h" 51 #include "iscsi/task.h" 52 #include "iscsi/conn.h" 53 #include "spdk/scsi.h" 54 #include "spdk/bdev.h" 55 #include "iscsi/portal_grp.h" 56 #include "iscsi/acceptor.h" 57 58 #include "spdk_internal/log.h" 59 60 #define MAX_TMPBUF 1024 61 62 #define SPDK_CRC32C_INITIAL 0xffffffffUL 63 #define SPDK_CRC32C_XOR 0xffffffffUL 64 65 #ifdef __FreeBSD__ 66 #define HAVE_SRANDOMDEV 1 67 #define HAVE_ARC4RANDOM 1 68 #endif 69 70 struct spdk_iscsi_globals g_spdk_iscsi = { 71 .mutex = PTHREAD_MUTEX_INITIALIZER, 72 .portal_head = TAILQ_HEAD_INITIALIZER(g_spdk_iscsi.portal_head), 73 .pg_head = TAILQ_HEAD_INITIALIZER(g_spdk_iscsi.pg_head), 74 .ig_head = TAILQ_HEAD_INITIALIZER(g_spdk_iscsi.ig_head), 75 .target_head = TAILQ_HEAD_INITIALIZER(g_spdk_iscsi.target_head), 76 .auth_group_head = TAILQ_HEAD_INITIALIZER(g_spdk_iscsi.auth_group_head), 77 }; 78 79 /* random value generation */ 80 static void gen_random(uint8_t *buf, size_t len); 81 #ifndef HAVE_SRANDOMDEV 82 static void srandomdev(void); 83 #endif /* HAVE_SRANDOMDEV */ 84 #ifndef HAVE_ARC4RANDOM 85 /* static uint32_t arc4random(void); */ 86 #endif /* HAVE_ARC4RANDOM */ 87 88 static int add_transfer_task(struct spdk_iscsi_conn *conn, 89 struct spdk_iscsi_task *task); 90 91 static int iscsi_send_r2t(struct spdk_iscsi_conn *conn, 92 struct spdk_iscsi_task *task, int offset, 93 int len, uint32_t transfer_tag, uint32_t *R2TSN); 94 static int iscsi_send_r2t_recovery(struct spdk_iscsi_conn *conn, 95 struct spdk_iscsi_task *r2t_task, uint32_t r2t_sn, 96 bool send_new_r2tsn); 97 98 static int create_iscsi_sess(struct spdk_iscsi_conn *conn, 99 struct spdk_iscsi_tgt_node *target, enum session_type session_type); 100 static int append_iscsi_sess(struct spdk_iscsi_conn *conn, 101 const char *initiator_port_name, uint16_t tsih, uint16_t cid); 102 103 static void remove_acked_pdu(struct spdk_iscsi_conn *conn, uint32_t ExpStatSN); 104 105 static int iscsi_reject(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu, 106 int reason); 107 108 #define DMIN32(A,B) ((uint32_t) ((uint32_t)(A) > (uint32_t)(B) ? (uint32_t)(B) : (uint32_t)(A))) 109 #define DMIN64(A,B) ((uint64_t) ((A) > (B) ? (B) : (A))) 110 111 #define MATCH_DIGEST_WORD(BUF, CRC32C) \ 112 ( ((((uint32_t) *((uint8_t *)(BUF)+0)) << 0) \ 113 | (((uint32_t) *((uint8_t *)(BUF)+1)) << 8) \ 114 | (((uint32_t) *((uint8_t *)(BUF)+2)) << 16) \ 115 | (((uint32_t) *((uint8_t *)(BUF)+3)) << 24)) \ 116 == (CRC32C)) 117 118 #if 0 119 static int 120 match_digest_word(const uint8_t *buf, uint32_t crc32c) 121 { 122 uint32_t l; 123 124 l = (buf[0] & 0xffU) << 0; 125 l |= (buf[1] & 0xffU) << 8; 126 l |= (buf[2] & 0xffU) << 16; 127 l |= (buf[3] & 0xffU) << 24; 128 return (l == crc32c); 129 } 130 131 static uint8_t * 132 make_digest_word(uint8_t *buf, size_t len, uint32_t crc32c) 133 { 134 if (len < ISCSI_DIGEST_LEN) { 135 return NULL; 136 } 137 138 buf[0] = (crc32c >> 0) & 0xffU; 139 buf[1] = (crc32c >> 8) & 0xffU; 140 buf[2] = (crc32c >> 16) & 0xffU; 141 buf[3] = (crc32c >> 24) & 0xffU; 142 return buf; 143 } 144 #endif 145 146 #ifndef HAVE_SRANDOMDEV 147 static void 148 srandomdev(void) 149 { 150 unsigned long seed; 151 time_t now; 152 pid_t pid; 153 154 pid = getpid(); 155 now = time(NULL); 156 seed = pid ^ now; 157 srandom(seed); 158 } 159 #endif /* HAVE_SRANDOMDEV */ 160 161 #ifndef HAVE_ARC4RANDOM 162 static int g_arc4random_initialized = 0; 163 164 static uint32_t 165 arc4random(void) 166 { 167 uint32_t r; 168 uint32_t r1, r2; 169 170 if (!g_arc4random_initialized) { 171 srandomdev(); 172 g_arc4random_initialized = 1; 173 } 174 r1 = (uint32_t)(random() & 0xffff); 175 r2 = (uint32_t)(random() & 0xffff); 176 r = (r1 << 16) | r2; 177 return r; 178 } 179 #endif /* HAVE_ARC4RANDOM */ 180 181 static void 182 gen_random(uint8_t *buf, size_t len) 183 { 184 #ifdef USE_RANDOM 185 long l; 186 size_t idx; 187 188 srandomdev(); 189 for (idx = 0; idx < len; idx++) { 190 l = random(); 191 buf[idx] = (uint8_t) l; 192 } 193 #else 194 uint32_t r; 195 size_t idx; 196 197 for (idx = 0; idx < len; idx++) { 198 r = arc4random(); 199 buf[idx] = (uint8_t) r; 200 } 201 #endif /* USE_RANDOM */ 202 } 203 204 static uint64_t 205 iscsi_get_isid(const uint8_t isid[6]) 206 { 207 return (uint64_t)isid[0] << 40 | 208 (uint64_t)isid[1] << 32 | 209 (uint64_t)isid[2] << 24 | 210 (uint64_t)isid[3] << 16 | 211 (uint64_t)isid[4] << 8 | 212 (uint64_t)isid[5]; 213 } 214 215 static int 216 bin2hex(char *buf, size_t len, const uint8_t *data, size_t data_len) 217 { 218 const char *digits = "0123456789ABCDEF"; 219 size_t total = 0; 220 size_t idx; 221 222 if (len < 3) { 223 return -1; 224 } 225 buf[total] = '0'; 226 total++; 227 buf[total] = 'x'; 228 total++; 229 buf[total] = '\0'; 230 231 for (idx = 0; idx < data_len; idx++) { 232 if (total + 3 > len) { 233 buf[total] = '\0'; 234 return - 1; 235 } 236 buf[total] = digits[(data[idx] >> 4) & 0x0fU]; 237 total++; 238 buf[total] = digits[data[idx] & 0x0fU]; 239 total++; 240 } 241 buf[total] = '\0'; 242 return total; 243 } 244 245 static int 246 hex2bin(uint8_t *data, size_t data_len, const char *str) 247 { 248 const char *digits = "0123456789ABCDEF"; 249 const char *dp; 250 const char *p; 251 size_t total = 0; 252 int n0, n1; 253 254 p = str; 255 if (p[0] != '0' && (p[1] != 'x' && p[1] != 'X')) { 256 return -1; 257 } 258 p += 2; 259 260 while (p[0] != '\0' && p[1] != '\0') { 261 if (total >= data_len) { 262 return -1; 263 } 264 dp = strchr(digits, toupper((int) p[0])); 265 if (dp == NULL) { 266 return -1; 267 } 268 n0 = (int)(dp - digits); 269 dp = strchr(digits, toupper((int) p[1])); 270 if (dp == NULL) { 271 return -1; 272 } 273 n1 = (int)(dp - digits); 274 275 data[total] = (uint8_t)(((n0 & 0x0fU) << 4) | (n1 & 0x0fU)); 276 total++; 277 p += 2; 278 } 279 return total; 280 } 281 282 uint32_t 283 spdk_iscsi_pdu_calc_header_digest(struct spdk_iscsi_pdu *pdu) 284 { 285 uint32_t crc32c; 286 uint32_t ahs_len_bytes = pdu->bhs.total_ahs_len * 4; 287 288 crc32c = SPDK_CRC32C_INITIAL; 289 crc32c = spdk_crc32c_update(&pdu->bhs, ISCSI_BHS_LEN, crc32c); 290 291 if (ahs_len_bytes) { 292 crc32c = spdk_crc32c_update(pdu->ahs, ahs_len_bytes, crc32c); 293 } 294 295 /* BHS and AHS are always 4-byte multiples in length, so no padding is necessary. */ 296 crc32c = crc32c ^ SPDK_CRC32C_XOR; 297 return crc32c; 298 } 299 300 uint32_t 301 spdk_iscsi_pdu_calc_data_digest(struct spdk_iscsi_pdu *pdu) 302 { 303 uint32_t data_len = DGET24(pdu->bhs.data_segment_len); 304 uint32_t crc32c; 305 uint32_t mod; 306 307 crc32c = SPDK_CRC32C_INITIAL; 308 crc32c = spdk_crc32c_update(pdu->data, data_len, crc32c); 309 310 mod = data_len % ISCSI_ALIGNMENT; 311 if (mod != 0) { 312 uint32_t pad_length = ISCSI_ALIGNMENT - mod; 313 uint8_t pad[3] = {0, 0, 0}; 314 315 assert(pad_length > 0); 316 assert(pad_length <= sizeof(pad)); 317 crc32c = spdk_crc32c_update(pad, pad_length, crc32c); 318 } 319 320 crc32c = crc32c ^ SPDK_CRC32C_XOR; 321 return crc32c; 322 } 323 324 static bool 325 iscsi_check_data_segment_length(struct spdk_iscsi_conn *conn, 326 struct spdk_iscsi_pdu *pdu, int data_len) 327 { 328 int max_segment_len; 329 330 /* 331 * Determine the maximum segment length expected for this PDU. 332 * This will be used to make sure the initiator did not send 333 * us too much immediate data. 334 * 335 * This value is specified separately by the initiator and target, 336 * and not negotiated. So we can use the #define safely here, 337 * since the value is not dependent on the initiator's maximum 338 * segment lengths (FirstBurstLength/MaxRecvDataSegmentLength), 339 * and SPDK currently does not allow configuration of these values 340 * at runtime. 341 */ 342 if (conn->sess == NULL) { 343 /* 344 * If the connection does not yet have a session, then 345 * login is not complete and we use the 8KB default 346 * FirstBurstLength as our maximum data segment length 347 * value. 348 */ 349 max_segment_len = SPDK_ISCSI_FIRST_BURST_LENGTH; 350 } else if (pdu->bhs.opcode == ISCSI_OP_SCSI_DATAOUT || 351 pdu->bhs.opcode == ISCSI_OP_NOPOUT) { 352 max_segment_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH; 353 } else { 354 max_segment_len = spdk_get_max_immediate_data_size(); 355 } 356 if (data_len <= max_segment_len) { 357 return true; 358 } else { 359 SPDK_ERRLOG("Data(%d) > MaxSegment(%d)\n", data_len, max_segment_len); 360 return false; 361 } 362 } 363 364 static int 365 iscsi_conn_read_data_segment(struct spdk_iscsi_conn *conn, 366 struct spdk_iscsi_pdu *pdu, 367 uint32_t segment_len) 368 { 369 struct spdk_dif_ctx dif_ctx; 370 struct iovec buf_iov, iovs[32]; 371 int rc, _rc; 372 373 if (spdk_likely(!spdk_iscsi_get_dif_ctx(conn, pdu, &dif_ctx))) { 374 return spdk_iscsi_conn_read_data(conn, 375 segment_len - pdu->data_valid_bytes, 376 pdu->data_buf + pdu->data_valid_bytes); 377 } else { 378 pdu->dif_insert_or_strip = true; 379 buf_iov.iov_base = pdu->data_buf; 380 buf_iov.iov_len = pdu->data_buf_len; 381 rc = spdk_dif_set_md_interleave_iovs(iovs, 32, &buf_iov, 1, 382 pdu->data_valid_bytes, segment_len, NULL, 383 &dif_ctx); 384 if (rc > 0) { 385 rc = spdk_iscsi_conn_readv_data(conn, iovs, rc); 386 if (rc > 0) { 387 _rc = spdk_dif_generate_stream(&buf_iov, 1, 388 pdu->data_valid_bytes, rc, 389 &dif_ctx); 390 if (_rc != 0) { 391 SPDK_ERRLOG("DIF generate failed\n"); 392 rc = _rc; 393 } 394 } 395 } else { 396 SPDK_ERRLOG("Setup iovs for interleaved metadata failed\n"); 397 } 398 return rc; 399 } 400 } 401 402 int 403 spdk_iscsi_read_pdu(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu **_pdu) 404 { 405 struct spdk_iscsi_pdu *pdu; 406 struct spdk_mempool *pool; 407 uint32_t crc32c; 408 int ahs_len; 409 int data_len; 410 int rc; 411 412 if (conn->pdu_in_progress == NULL) { 413 conn->pdu_in_progress = spdk_get_pdu(); 414 if (conn->pdu_in_progress == NULL) { 415 return SPDK_ISCSI_CONNECTION_FATAL; 416 } 417 } 418 419 pdu = conn->pdu_in_progress; 420 421 if (pdu->bhs_valid_bytes < ISCSI_BHS_LEN) { 422 rc = spdk_iscsi_conn_read_data(conn, 423 ISCSI_BHS_LEN - pdu->bhs_valid_bytes, 424 (uint8_t *)&pdu->bhs + pdu->bhs_valid_bytes); 425 if (rc < 0) { 426 *_pdu = NULL; 427 spdk_put_pdu(pdu); 428 conn->pdu_in_progress = NULL; 429 return rc; 430 } 431 pdu->bhs_valid_bytes += rc; 432 if (pdu->bhs_valid_bytes < ISCSI_BHS_LEN) { 433 *_pdu = NULL; 434 return SPDK_SUCCESS; 435 } 436 } 437 438 data_len = ISCSI_ALIGN(DGET24(pdu->bhs.data_segment_len)); 439 440 /* AHS */ 441 ahs_len = pdu->bhs.total_ahs_len * 4; 442 assert(ahs_len <= ISCSI_AHS_LEN); 443 if (pdu->ahs_valid_bytes < ahs_len) { 444 rc = spdk_iscsi_conn_read_data(conn, 445 ahs_len - pdu->ahs_valid_bytes, 446 pdu->ahs + pdu->ahs_valid_bytes); 447 if (rc < 0) { 448 *_pdu = NULL; 449 spdk_put_pdu(pdu); 450 conn->pdu_in_progress = NULL; 451 return rc; 452 } 453 454 pdu->ahs_valid_bytes += rc; 455 if (pdu->ahs_valid_bytes < ahs_len) { 456 *_pdu = NULL; 457 return SPDK_SUCCESS; 458 } 459 } 460 461 /* Header Digest */ 462 if (conn->header_digest && 463 pdu->hdigest_valid_bytes < ISCSI_DIGEST_LEN) { 464 rc = spdk_iscsi_conn_read_data(conn, 465 ISCSI_DIGEST_LEN - pdu->hdigest_valid_bytes, 466 pdu->header_digest + pdu->hdigest_valid_bytes); 467 if (rc < 0) { 468 *_pdu = NULL; 469 spdk_put_pdu(pdu); 470 conn->pdu_in_progress = NULL; 471 return rc; 472 } 473 474 pdu->hdigest_valid_bytes += rc; 475 if (pdu->hdigest_valid_bytes < ISCSI_DIGEST_LEN) { 476 *_pdu = NULL; 477 return SPDK_SUCCESS; 478 } 479 } 480 481 /* copy the actual data into local buffer */ 482 if (pdu->data_valid_bytes < data_len) { 483 if (pdu->data_buf == NULL) { 484 if (data_len <= spdk_get_max_immediate_data_size()) { 485 pool = g_spdk_iscsi.pdu_immediate_data_pool; 486 pdu->data_buf_len = SPDK_BDEV_BUF_SIZE_WITH_MD(spdk_get_max_immediate_data_size()); 487 } else if (data_len <= SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH) { 488 pool = g_spdk_iscsi.pdu_data_out_pool; 489 pdu->data_buf_len = SPDK_BDEV_BUF_SIZE_WITH_MD(SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH); 490 } else { 491 SPDK_ERRLOG("Data(%d) > MaxSegment(%d)\n", 492 data_len, SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH); 493 *_pdu = NULL; 494 spdk_put_pdu(pdu); 495 conn->pdu_in_progress = NULL; 496 return SPDK_ISCSI_CONNECTION_FATAL; 497 } 498 pdu->mobj = spdk_mempool_get(pool); 499 if (pdu->mobj == NULL) { 500 *_pdu = NULL; 501 return SPDK_SUCCESS; 502 } 503 pdu->data_buf = pdu->mobj->buf; 504 } 505 506 rc = iscsi_conn_read_data_segment(conn, pdu, data_len); 507 if (rc < 0) { 508 *_pdu = NULL; 509 spdk_put_pdu(pdu); 510 conn->pdu_in_progress = NULL; 511 return rc; 512 } 513 514 pdu->data_valid_bytes += rc; 515 if (pdu->data_valid_bytes < data_len) { 516 *_pdu = NULL; 517 return SPDK_SUCCESS; 518 } 519 } 520 521 /* copy out the data digest */ 522 if (conn->data_digest && data_len != 0 && 523 pdu->ddigest_valid_bytes < ISCSI_DIGEST_LEN) { 524 rc = spdk_iscsi_conn_read_data(conn, 525 ISCSI_DIGEST_LEN - pdu->ddigest_valid_bytes, 526 pdu->data_digest + pdu->ddigest_valid_bytes); 527 if (rc < 0) { 528 *_pdu = NULL; 529 spdk_put_pdu(pdu); 530 conn->pdu_in_progress = NULL; 531 return rc; 532 } 533 534 pdu->ddigest_valid_bytes += rc; 535 if (pdu->ddigest_valid_bytes < ISCSI_DIGEST_LEN) { 536 *_pdu = NULL; 537 return SPDK_SUCCESS; 538 } 539 } 540 541 /* All data for this PDU has now been read from the socket. */ 542 conn->pdu_in_progress = NULL; 543 544 spdk_trace_record(TRACE_ISCSI_READ_PDU, conn->id, pdu->data_valid_bytes, 545 (uintptr_t)pdu, pdu->bhs.opcode); 546 547 /* Data Segment */ 548 if (data_len != 0) { 549 if (!iscsi_check_data_segment_length(conn, pdu, data_len)) { 550 rc = iscsi_reject(conn, pdu, ISCSI_REASON_PROTOCOL_ERROR); 551 spdk_put_pdu(pdu); 552 553 /* 554 * If spdk_iscsi_reject() was not able to reject the PDU, 555 * treat it as a fatal connection error. Otherwise, 556 * return SUCCESS here so that the caller will continue 557 * to attempt to read PDUs. 558 */ 559 rc = (rc < 0) ? SPDK_ISCSI_CONNECTION_FATAL : SPDK_SUCCESS; 560 return rc; 561 } 562 563 pdu->data = pdu->data_buf; 564 pdu->data_from_mempool = true; 565 pdu->data_segment_len = data_len; 566 } 567 568 /* check digest */ 569 if (conn->header_digest) { 570 crc32c = spdk_iscsi_pdu_calc_header_digest(pdu); 571 rc = MATCH_DIGEST_WORD(pdu->header_digest, crc32c); 572 if (rc == 0) { 573 SPDK_ERRLOG("header digest error (%s)\n", conn->initiator_name); 574 spdk_put_pdu(pdu); 575 return SPDK_ISCSI_CONNECTION_FATAL; 576 } 577 } 578 if (conn->data_digest && data_len != 0) { 579 crc32c = spdk_iscsi_pdu_calc_data_digest(pdu); 580 rc = MATCH_DIGEST_WORD(pdu->data_digest, crc32c); 581 if (rc == 0) { 582 SPDK_ERRLOG("data digest error (%s)\n", conn->initiator_name); 583 spdk_put_pdu(pdu); 584 return SPDK_ISCSI_CONNECTION_FATAL; 585 } 586 } 587 588 *_pdu = pdu; 589 return 1; 590 } 591 592 struct _iov_ctx { 593 struct iovec *iov; 594 int num_iovs; 595 uint32_t iov_offset; 596 int iovcnt; 597 uint32_t mapped_len; 598 }; 599 600 static inline void 601 _iov_ctx_init(struct _iov_ctx *ctx, struct iovec *iovs, int num_iovs, 602 uint32_t iov_offset) 603 { 604 ctx->iov = iovs; 605 ctx->num_iovs = num_iovs; 606 ctx->iov_offset = iov_offset; 607 ctx->iovcnt = 0; 608 ctx->mapped_len = 0; 609 } 610 611 static inline bool 612 _iov_ctx_set_iov(struct _iov_ctx *ctx, uint8_t *data, uint32_t data_len) 613 { 614 if (ctx->iov_offset >= data_len) { 615 ctx->iov_offset -= data_len; 616 } else { 617 ctx->iov->iov_base = data + ctx->iov_offset; 618 ctx->iov->iov_len = data_len - ctx->iov_offset; 619 ctx->mapped_len += data_len - ctx->iov_offset; 620 ctx->iov_offset = 0; 621 ctx->iov++; 622 ctx->iovcnt++; 623 if (ctx->iovcnt == ctx->num_iovs) { 624 return false; 625 } 626 } 627 628 return true; 629 } 630 631 /* Build iovec array to leave metadata space for every data block 632 * when reading data segment from socket. 633 */ 634 static inline bool 635 _iov_ctx_set_md_interleave_iov(struct _iov_ctx *ctx, 636 void *buf, uint32_t buf_len, uint32_t data_len, 637 struct spdk_dif_ctx *dif_ctx) 638 { 639 int rc; 640 uint32_t mapped_len = 0; 641 struct iovec buf_iov; 642 643 if (ctx->iov_offset >= data_len) { 644 ctx->iov_offset -= buf_len; 645 } else { 646 buf_iov.iov_base = buf; 647 buf_iov.iov_len = buf_len; 648 rc = spdk_dif_set_md_interleave_iovs(ctx->iov, ctx->num_iovs - ctx->iovcnt, 649 &buf_iov, 1, 650 ctx->iov_offset, data_len, &mapped_len, 651 dif_ctx); 652 if (rc < 0) { 653 SPDK_ERRLOG("Failed to setup iovs for DIF strip\n"); 654 return false; 655 } 656 657 ctx->mapped_len += mapped_len; 658 ctx->iov_offset = 0; 659 ctx->iovcnt += rc; 660 661 if (ctx->iovcnt == ctx->num_iovs) { 662 return false; 663 } 664 } 665 666 return true; 667 } 668 669 int 670 spdk_iscsi_build_iovs(struct spdk_iscsi_conn *conn, struct iovec *iovs, int num_iovs, 671 struct spdk_iscsi_pdu *pdu, uint32_t *_mapped_length) 672 { 673 struct _iov_ctx ctx; 674 int enable_digest; 675 uint32_t total_ahs_len; 676 uint32_t data_len; 677 678 if (num_iovs == 0) { 679 return 0; 680 } 681 682 total_ahs_len = pdu->bhs.total_ahs_len; 683 data_len = DGET24(pdu->bhs.data_segment_len); 684 data_len = ISCSI_ALIGN(data_len); 685 686 enable_digest = 1; 687 if (pdu->bhs.opcode == ISCSI_OP_LOGIN_RSP) { 688 /* this PDU should be sent without digest */ 689 enable_digest = 0; 690 } 691 692 _iov_ctx_init(&ctx, iovs, num_iovs, pdu->writev_offset); 693 694 /* BHS */ 695 if (!_iov_ctx_set_iov(&ctx, (uint8_t *)&pdu->bhs, ISCSI_BHS_LEN)) { 696 goto end; 697 } 698 /* AHS */ 699 if (total_ahs_len > 0) { 700 if (!_iov_ctx_set_iov(&ctx, pdu->ahs, 4 * total_ahs_len)) { 701 goto end; 702 } 703 } 704 705 /* Header Digest */ 706 if (enable_digest && conn->header_digest) { 707 if (!_iov_ctx_set_iov(&ctx, pdu->header_digest, ISCSI_DIGEST_LEN)) { 708 goto end; 709 } 710 } 711 712 /* Data Segment */ 713 if (data_len > 0) { 714 if (!pdu->dif_insert_or_strip) { 715 if (!_iov_ctx_set_iov(&ctx, pdu->data, data_len)) { 716 goto end; 717 } 718 } else { 719 if (!_iov_ctx_set_md_interleave_iov(&ctx, pdu->data, pdu->data_buf_len, 720 data_len, &pdu->dif_ctx)) { 721 goto end; 722 } 723 } 724 } 725 726 /* Data Digest */ 727 if (enable_digest && conn->data_digest && data_len != 0) { 728 _iov_ctx_set_iov(&ctx, pdu->data_digest, ISCSI_DIGEST_LEN); 729 } 730 731 end: 732 if (_mapped_length != NULL) { 733 *_mapped_length = ctx.mapped_len; 734 } 735 736 return ctx.iovcnt; 737 } 738 739 static int 740 iscsi_append_text(struct spdk_iscsi_conn *conn __attribute__((__unused__)), 741 const char *key, const char *val, uint8_t *data, 742 int alloc_len, int data_len) 743 { 744 int total; 745 int len; 746 747 total = data_len; 748 if (alloc_len < 1) { 749 return 0; 750 } 751 if (total > alloc_len) { 752 total = alloc_len; 753 data[total - 1] = '\0'; 754 return total; 755 } 756 757 if (alloc_len - total < 1) { 758 SPDK_ERRLOG("data space small %d\n", alloc_len); 759 return total; 760 } 761 len = snprintf((char *) data + total, alloc_len - total, "%s=%s", key, val); 762 total += len + 1; 763 764 return total; 765 } 766 767 static int 768 iscsi_append_param(struct spdk_iscsi_conn *conn, const char *key, 769 uint8_t *data, int alloc_len, int data_len) 770 { 771 struct iscsi_param *param; 772 int rc; 773 774 param = spdk_iscsi_param_find(conn->params, key); 775 if (param == NULL) { 776 param = spdk_iscsi_param_find(conn->sess->params, key); 777 if (param == NULL) { 778 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "no key %.64s\n", key); 779 return data_len; 780 } 781 } 782 rc = iscsi_append_text(conn, param->key, param->val, data, 783 alloc_len, data_len); 784 return rc; 785 } 786 787 static int 788 iscsi_get_authinfo(struct spdk_iscsi_conn *conn, const char *authuser) 789 { 790 int ag_tag; 791 int rc; 792 793 if (conn->sess->target != NULL) { 794 ag_tag = conn->sess->target->chap_group; 795 } else { 796 ag_tag = -1; 797 } 798 if (ag_tag < 0) { 799 ag_tag = g_spdk_iscsi.chap_group; 800 } 801 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "ag_tag=%d\n", ag_tag); 802 803 rc = spdk_iscsi_chap_get_authinfo(&conn->auth, authuser, ag_tag); 804 if (rc < 0) { 805 SPDK_ERRLOG("chap_get_authinfo() failed\n"); 806 return -1; 807 } 808 return 0; 809 } 810 811 static int 812 iscsi_auth_params(struct spdk_iscsi_conn *conn, 813 struct iscsi_param *params, const char *method, uint8_t *data, 814 int alloc_len, int data_len) 815 { 816 char *in_val; 817 char *in_next; 818 char *new_val; 819 const char *algorithm; 820 const char *name; 821 const char *response; 822 const char *identifier; 823 const char *challenge; 824 int total; 825 int rc; 826 827 if (conn == NULL || params == NULL || method == NULL) { 828 return -1; 829 } 830 if (strcasecmp(method, "CHAP") == 0) { 831 /* method OK */ 832 } else { 833 SPDK_ERRLOG("unsupported AuthMethod %.64s\n", method); 834 return -1; 835 } 836 837 total = data_len; 838 if (alloc_len < 1) { 839 return 0; 840 } 841 if (total > alloc_len) { 842 total = alloc_len; 843 data[total - 1] = '\0'; 844 return total; 845 } 846 847 /* for temporary store */ 848 in_val = malloc(ISCSI_TEXT_MAX_VAL_LEN + 1); 849 if (!in_val) { 850 SPDK_ERRLOG("malloc() failed for temporary store\n"); 851 return -ENOMEM; 852 } 853 854 /* CHAP method (RFC1994) */ 855 if ((algorithm = spdk_iscsi_param_get_val(params, "CHAP_A")) != NULL) { 856 if (conn->auth.chap_phase != ISCSI_CHAP_PHASE_WAIT_A) { 857 SPDK_ERRLOG("CHAP sequence error\n"); 858 goto error_return; 859 } 860 861 /* CHAP_A is LIST type */ 862 snprintf(in_val, ISCSI_TEXT_MAX_VAL_LEN + 1, "%s", algorithm); 863 in_next = in_val; 864 while ((new_val = spdk_strsepq(&in_next, ",")) != NULL) { 865 if (strcasecmp(new_val, "5") == 0) { 866 /* CHAP with MD5 */ 867 break; 868 } 869 } 870 if (new_val == NULL) { 871 snprintf(in_val, ISCSI_TEXT_MAX_VAL_LEN + 1, "%s", "Reject"); 872 new_val = in_val; 873 iscsi_append_text(conn, "CHAP_A", new_val, 874 data, alloc_len, total); 875 goto error_return; 876 } 877 /* selected algorithm is 5 (MD5) */ 878 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "got CHAP_A=%s\n", new_val); 879 total = iscsi_append_text(conn, "CHAP_A", new_val, 880 data, alloc_len, total); 881 882 /* Identifier is one octet */ 883 gen_random(conn->auth.chap_id, 1); 884 snprintf(in_val, ISCSI_TEXT_MAX_VAL_LEN, "%d", 885 (int) conn->auth.chap_id[0]); 886 total = iscsi_append_text(conn, "CHAP_I", in_val, 887 data, alloc_len, total); 888 889 /* Challenge Value is a variable stream of octets */ 890 /* (binary length MUST not exceed 1024 bytes) */ 891 conn->auth.chap_challenge_len = ISCSI_CHAP_CHALLENGE_LEN; 892 gen_random(conn->auth.chap_challenge, conn->auth.chap_challenge_len); 893 bin2hex(in_val, ISCSI_TEXT_MAX_VAL_LEN, 894 conn->auth.chap_challenge, conn->auth.chap_challenge_len); 895 total = iscsi_append_text(conn, "CHAP_C", in_val, 896 data, alloc_len, total); 897 898 conn->auth.chap_phase = ISCSI_CHAP_PHASE_WAIT_NR; 899 } else if ((name = spdk_iscsi_param_get_val(params, "CHAP_N")) != NULL) { 900 uint8_t resmd5[SPDK_MD5DIGEST_LEN]; 901 uint8_t tgtmd5[SPDK_MD5DIGEST_LEN]; 902 struct spdk_md5ctx md5ctx; 903 size_t decoded_len = 0; 904 905 if (conn->auth.chap_phase != ISCSI_CHAP_PHASE_WAIT_NR) { 906 SPDK_ERRLOG("CHAP sequence error\n"); 907 goto error_return; 908 } 909 910 response = spdk_iscsi_param_get_val(params, "CHAP_R"); 911 if (response == NULL) { 912 SPDK_ERRLOG("no response\n"); 913 goto error_return; 914 } 915 if (response[0] == '0' && 916 (response[1] == 'x' || response[1] == 'X')) { 917 rc = hex2bin(resmd5, SPDK_MD5DIGEST_LEN, response); 918 if (rc < 0 || rc != SPDK_MD5DIGEST_LEN) { 919 SPDK_ERRLOG("response format error\n"); 920 goto error_return; 921 } 922 } else if (response[0] == '0' && 923 (response[1] == 'b' || response[1] == 'B')) { 924 response += 2; 925 rc = spdk_base64_decode(resmd5, &decoded_len, response); 926 if (rc < 0 || decoded_len != SPDK_MD5DIGEST_LEN) { 927 SPDK_ERRLOG("response format error\n"); 928 goto error_return; 929 } 930 } else { 931 SPDK_ERRLOG("response format error\n"); 932 goto error_return; 933 } 934 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "got CHAP_N/CHAP_R\n"); 935 936 rc = iscsi_get_authinfo(conn, name); 937 if (rc < 0) { 938 /* SPDK_ERRLOG("auth user or secret is missing\n"); */ 939 SPDK_ERRLOG("iscsi_get_authinfo() failed\n"); 940 goto error_return; 941 } 942 if (conn->auth.user[0] == '\0' || conn->auth.secret[0] == '\0') { 943 /* SPDK_ERRLOG("auth user or secret is missing\n"); */ 944 SPDK_ERRLOG("auth failed (name %.64s)\n", name); 945 goto error_return; 946 } 947 948 spdk_md5init(&md5ctx); 949 /* Identifier */ 950 spdk_md5update(&md5ctx, conn->auth.chap_id, 1); 951 /* followed by secret */ 952 spdk_md5update(&md5ctx, conn->auth.secret, 953 strlen(conn->auth.secret)); 954 /* followed by Challenge Value */ 955 spdk_md5update(&md5ctx, conn->auth.chap_challenge, 956 conn->auth.chap_challenge_len); 957 /* tgtmd5 is expecting Response Value */ 958 spdk_md5final(tgtmd5, &md5ctx); 959 960 bin2hex(in_val, ISCSI_TEXT_MAX_VAL_LEN, tgtmd5, SPDK_MD5DIGEST_LEN); 961 962 #if 0 963 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "tgtmd5=%s, resmd5=%s\n", in_val, response); 964 spdk_dump("tgtmd5", tgtmd5, SPDK_MD5DIGEST_LEN); 965 spdk_dump("resmd5", resmd5, SPDK_MD5DIGEST_LEN); 966 #endif 967 968 /* compare MD5 digest */ 969 if (memcmp(tgtmd5, resmd5, SPDK_MD5DIGEST_LEN) != 0) { 970 /* not match */ 971 /* SPDK_ERRLOG("auth user or secret is missing\n"); */ 972 SPDK_ERRLOG("auth failed (name %.64s)\n", name); 973 goto error_return; 974 } 975 /* OK initiator's secret */ 976 conn->authenticated = true; 977 978 /* mutual CHAP? */ 979 identifier = spdk_iscsi_param_get_val(params, "CHAP_I"); 980 if (identifier != NULL) { 981 conn->auth.chap_mid[0] = (uint8_t) strtol(identifier, NULL, 10); 982 challenge = spdk_iscsi_param_get_val(params, "CHAP_C"); 983 if (challenge == NULL) { 984 SPDK_ERRLOG("CHAP sequence error\n"); 985 goto error_return; 986 } 987 if (challenge[0] == '0' && 988 (challenge[1] == 'x' || challenge[1] == 'X')) { 989 rc = hex2bin(conn->auth.chap_mchallenge, 990 ISCSI_CHAP_CHALLENGE_LEN, challenge); 991 if (rc < 0) { 992 SPDK_ERRLOG("challenge format error\n"); 993 goto error_return; 994 } 995 conn->auth.chap_mchallenge_len = rc; 996 } else if (challenge[0] == '0' && 997 (challenge[1] == 'b' || challenge[1] == 'B')) { 998 challenge += 2; 999 rc = spdk_base64_decode(conn->auth.chap_mchallenge, 1000 &decoded_len, challenge); 1001 if (rc < 0) { 1002 SPDK_ERRLOG("challenge format error\n"); 1003 goto error_return; 1004 } 1005 conn->auth.chap_mchallenge_len = decoded_len; 1006 } else { 1007 SPDK_ERRLOG("challenge format error\n"); 1008 goto error_return; 1009 } 1010 #if 0 1011 spdk_dump("MChallenge", conn->auth.chap_mchallenge, 1012 conn->auth.chap_mchallenge_len); 1013 #endif 1014 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "got CHAP_I/CHAP_C\n"); 1015 1016 if (conn->auth.muser[0] == '\0' || conn->auth.msecret[0] == '\0') { 1017 /* SPDK_ERRLOG("mutual auth user or secret is missing\n"); */ 1018 SPDK_ERRLOG("auth failed (name %.64s)\n", name); 1019 goto error_return; 1020 } 1021 1022 spdk_md5init(&md5ctx); 1023 /* Identifier */ 1024 spdk_md5update(&md5ctx, conn->auth.chap_mid, 1); 1025 /* followed by secret */ 1026 spdk_md5update(&md5ctx, conn->auth.msecret, 1027 strlen(conn->auth.msecret)); 1028 /* followed by Challenge Value */ 1029 spdk_md5update(&md5ctx, conn->auth.chap_mchallenge, 1030 conn->auth.chap_mchallenge_len); 1031 /* tgtmd5 is Response Value */ 1032 spdk_md5final(tgtmd5, &md5ctx); 1033 1034 bin2hex(in_val, ISCSI_TEXT_MAX_VAL_LEN, tgtmd5, SPDK_MD5DIGEST_LEN); 1035 1036 total = iscsi_append_text(conn, "CHAP_N", 1037 conn->auth.muser, data, alloc_len, total); 1038 total = iscsi_append_text(conn, "CHAP_R", 1039 in_val, data, alloc_len, total); 1040 } else { 1041 /* not mutual */ 1042 if (conn->mutual_chap) { 1043 SPDK_ERRLOG("required mutual CHAP\n"); 1044 goto error_return; 1045 } 1046 } 1047 1048 conn->auth.chap_phase = ISCSI_CHAP_PHASE_END; 1049 } else { 1050 /* not found CHAP keys */ 1051 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "start CHAP\n"); 1052 conn->auth.chap_phase = ISCSI_CHAP_PHASE_WAIT_A; 1053 } 1054 1055 free(in_val); 1056 return total; 1057 1058 error_return: 1059 conn->auth.chap_phase = ISCSI_CHAP_PHASE_WAIT_A; 1060 free(in_val); 1061 return -1; 1062 } 1063 1064 static int 1065 iscsi_reject(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu, 1066 int reason) 1067 { 1068 struct spdk_iscsi_pdu *rsp_pdu; 1069 struct iscsi_bhs_reject *rsph; 1070 uint8_t *data; 1071 int total_ahs_len; 1072 int data_len; 1073 int alloc_len; 1074 1075 total_ahs_len = pdu->bhs.total_ahs_len; 1076 data_len = 0; 1077 alloc_len = ISCSI_BHS_LEN + (4 * total_ahs_len); 1078 1079 if (conn->header_digest) { 1080 alloc_len += ISCSI_DIGEST_LEN; 1081 } 1082 1083 data = calloc(1, alloc_len); 1084 if (!data) { 1085 SPDK_ERRLOG("calloc() failed for data segment\n"); 1086 return -ENOMEM; 1087 } 1088 1089 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "Reject PDU reason=%d\n", reason); 1090 1091 if (conn->sess != NULL) { 1092 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, 1093 "StatSN=%u, ExpCmdSN=%u, MaxCmdSN=%u\n", 1094 conn->StatSN, conn->sess->ExpCmdSN, 1095 conn->sess->MaxCmdSN); 1096 } else { 1097 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "StatSN=%u\n", conn->StatSN); 1098 } 1099 1100 memcpy(data, &pdu->bhs, ISCSI_BHS_LEN); 1101 data_len += ISCSI_BHS_LEN; 1102 1103 if (total_ahs_len != 0) { 1104 memcpy(data + data_len, pdu->ahs, (4 * total_ahs_len)); 1105 data_len += (4 * total_ahs_len); 1106 } 1107 1108 if (conn->header_digest) { 1109 memcpy(data + data_len, pdu->header_digest, ISCSI_DIGEST_LEN); 1110 data_len += ISCSI_DIGEST_LEN; 1111 } 1112 1113 rsp_pdu = spdk_get_pdu(); 1114 if (rsp_pdu == NULL) { 1115 free(data); 1116 return -ENOMEM; 1117 } 1118 1119 rsph = (struct iscsi_bhs_reject *)&rsp_pdu->bhs; 1120 rsp_pdu->data = data; 1121 rsph->opcode = ISCSI_OP_REJECT; 1122 rsph->flags |= 0x80; /* bit 0 is default to 1 */ 1123 rsph->reason = reason; 1124 DSET24(rsph->data_segment_len, data_len); 1125 1126 rsph->ffffffff = 0xffffffffU; 1127 to_be32(&rsph->stat_sn, conn->StatSN); 1128 conn->StatSN++; 1129 1130 if (conn->sess != NULL) { 1131 to_be32(&rsph->exp_cmd_sn, conn->sess->ExpCmdSN); 1132 to_be32(&rsph->max_cmd_sn, conn->sess->MaxCmdSN); 1133 } else { 1134 to_be32(&rsph->exp_cmd_sn, 1); 1135 to_be32(&rsph->max_cmd_sn, 1); 1136 } 1137 1138 SPDK_LOGDUMP(SPDK_LOG_ISCSI, "PDU", (void *)&rsp_pdu->bhs, ISCSI_BHS_LEN); 1139 1140 spdk_iscsi_conn_write_pdu(conn, rsp_pdu); 1141 1142 return 0; 1143 } 1144 1145 static int 1146 iscsi_check_values(struct spdk_iscsi_conn *conn) 1147 { 1148 if (conn->sess->FirstBurstLength > conn->sess->MaxBurstLength) { 1149 SPDK_ERRLOG("FirstBurstLength(%d) > MaxBurstLength(%d)\n", 1150 conn->sess->FirstBurstLength, 1151 conn->sess->MaxBurstLength); 1152 return -1; 1153 } 1154 if (conn->sess->FirstBurstLength > g_spdk_iscsi.FirstBurstLength) { 1155 SPDK_ERRLOG("FirstBurstLength(%d) > iSCSI target restriction(%d)\n", 1156 conn->sess->FirstBurstLength, g_spdk_iscsi.FirstBurstLength); 1157 return -1; 1158 } 1159 if (conn->sess->MaxBurstLength > 0x00ffffff) { 1160 SPDK_ERRLOG("MaxBurstLength(%d) > 0x00ffffff\n", 1161 conn->sess->MaxBurstLength); 1162 return -1; 1163 } 1164 1165 if (conn->MaxRecvDataSegmentLength < 512) { 1166 SPDK_ERRLOG("MaxRecvDataSegmentLength(%d) < 512\n", 1167 conn->MaxRecvDataSegmentLength); 1168 return -1; 1169 } 1170 if (conn->MaxRecvDataSegmentLength > 0x00ffffff) { 1171 SPDK_ERRLOG("MaxRecvDataSegmentLength(%d) > 0x00ffffff\n", 1172 conn->MaxRecvDataSegmentLength); 1173 return -1; 1174 } 1175 return 0; 1176 } 1177 1178 /* 1179 * The response function of spdk_iscsi_op_login 1180 * return: 1181 * 0:success; 1182 * -1:error; 1183 */ 1184 static int 1185 iscsi_op_login_response(struct spdk_iscsi_conn *conn, 1186 struct spdk_iscsi_pdu *rsp_pdu, struct iscsi_param *params) 1187 { 1188 struct iscsi_bhs_login_rsp *rsph; 1189 int rc; 1190 1191 rsph = (struct iscsi_bhs_login_rsp *)&rsp_pdu->bhs; 1192 rsph->version_max = ISCSI_VERSION; 1193 rsph->version_act = ISCSI_VERSION; 1194 DSET24(rsph->data_segment_len, rsp_pdu->data_segment_len); 1195 1196 to_be32(&rsph->stat_sn, conn->StatSN); 1197 conn->StatSN++; 1198 1199 if (conn->sess != NULL) { 1200 to_be32(&rsph->exp_cmd_sn, conn->sess->ExpCmdSN); 1201 to_be32(&rsph->max_cmd_sn, conn->sess->MaxCmdSN); 1202 } else { 1203 to_be32(&rsph->exp_cmd_sn, rsp_pdu->cmd_sn); 1204 to_be32(&rsph->max_cmd_sn, rsp_pdu->cmd_sn); 1205 } 1206 1207 SPDK_LOGDUMP(SPDK_LOG_ISCSI, "PDU", (uint8_t *)rsph, ISCSI_BHS_LEN); 1208 SPDK_LOGDUMP(SPDK_LOG_ISCSI, "DATA", rsp_pdu->data, rsp_pdu->data_segment_len); 1209 1210 /* Set T/CSG/NSG to reserved if login error. */ 1211 if (rsph->status_class != 0) { 1212 rsph->flags &= ~ISCSI_LOGIN_TRANSIT; 1213 rsph->flags &= ~ISCSI_LOGIN_CURRENT_STAGE_MASK; 1214 rsph->flags &= ~ISCSI_LOGIN_NEXT_STAGE_MASK; 1215 } 1216 spdk_iscsi_conn_write_pdu(conn, rsp_pdu); 1217 1218 /* after send PDU digest on/off */ 1219 if (conn->full_feature) { 1220 /* update internal variables */ 1221 rc = spdk_iscsi_copy_param2var(conn); 1222 if (rc < 0) { 1223 SPDK_ERRLOG("spdk_iscsi_copy_param2var() failed\n"); 1224 spdk_iscsi_param_free(params); 1225 return -1; 1226 } 1227 /* check value */ 1228 rc = iscsi_check_values(conn); 1229 if (rc < 0) { 1230 SPDK_ERRLOG("iscsi_check_values() failed\n"); 1231 spdk_iscsi_param_free(params); 1232 return -1; 1233 } 1234 } 1235 1236 spdk_iscsi_param_free(params); 1237 return 0; 1238 } 1239 1240 /* 1241 * This function is used to del the original param and update it with new 1242 * value 1243 * return: 1244 * 0: success 1245 * otherwise: error 1246 */ 1247 static int 1248 iscsi_op_login_update_param(struct spdk_iscsi_conn *conn, 1249 const char *key, const char *value, 1250 const char *list) 1251 { 1252 int rc = 0; 1253 struct iscsi_param *new_param, *orig_param; 1254 int index; 1255 1256 orig_param = spdk_iscsi_param_find(conn->params, key); 1257 if (orig_param == NULL) { 1258 SPDK_ERRLOG("orig_param %s not found\n", key); 1259 return SPDK_ISCSI_LOGIN_ERROR_PARAMETER; 1260 } 1261 1262 index = orig_param->state_index; 1263 rc = spdk_iscsi_param_del(&conn->params, key); 1264 if (rc < 0) { 1265 SPDK_ERRLOG("iscsi_param_del(%s) failed\n", key); 1266 return SPDK_ISCSI_LOGIN_ERROR_PARAMETER; 1267 } 1268 rc = spdk_iscsi_param_add(&conn->params, key, value, list, ISPT_LIST); 1269 if (rc < 0) { 1270 SPDK_ERRLOG("iscsi_param_add() failed\n"); 1271 return SPDK_ISCSI_LOGIN_ERROR_PARAMETER; 1272 } 1273 new_param = spdk_iscsi_param_find(conn->params, key); 1274 if (new_param == NULL) { 1275 SPDK_ERRLOG("spdk_iscsi_param_find() failed\n"); 1276 return SPDK_ISCSI_LOGIN_ERROR_PARAMETER; 1277 } 1278 new_param->state_index = index; 1279 return rc; 1280 } 1281 1282 static int 1283 iscsi_negotiate_chap_param(struct spdk_iscsi_conn *conn, bool disable_chap, 1284 bool require_chap, bool mutual_chap) 1285 { 1286 int rc = 0; 1287 1288 if (disable_chap) { 1289 conn->require_chap = false; 1290 rc = iscsi_op_login_update_param(conn, "AuthMethod", "None", "None"); 1291 if (rc < 0) { 1292 return rc; 1293 } 1294 } else if (require_chap) { 1295 conn->require_chap = true; 1296 rc = iscsi_op_login_update_param(conn, "AuthMethod", "CHAP", "CHAP"); 1297 if (rc < 0) { 1298 return rc; 1299 } 1300 } 1301 if (mutual_chap) { 1302 conn->mutual_chap = true; 1303 } 1304 1305 return rc; 1306 } 1307 1308 /* 1309 * The function which is used to handle the part of session discovery 1310 * return: 1311 * 0, success; 1312 * otherwise: error; 1313 */ 1314 static int 1315 iscsi_op_login_session_discovery_chap(struct spdk_iscsi_conn *conn) 1316 { 1317 return iscsi_negotiate_chap_param(conn, g_spdk_iscsi.disable_chap, 1318 g_spdk_iscsi.require_chap, 1319 g_spdk_iscsi.mutual_chap); 1320 } 1321 1322 /* 1323 * This function is used to update the param related with chap 1324 * return: 1325 * 0: success 1326 * otherwise: error 1327 */ 1328 static int 1329 iscsi_op_login_negotiate_chap_param(struct spdk_iscsi_conn *conn, 1330 struct spdk_iscsi_tgt_node *target) 1331 { 1332 return iscsi_negotiate_chap_param(conn, target->disable_chap, 1333 target->require_chap, 1334 target->mutual_chap); 1335 } 1336 1337 static int 1338 iscsi_op_login_negotiate_digest_param(struct spdk_iscsi_conn *conn, 1339 struct spdk_iscsi_tgt_node *target) 1340 { 1341 int rc; 1342 1343 if (target->header_digest) { 1344 /* 1345 * User specified header digests, so update the list of 1346 * HeaderDigest values to remove "None" so that only 1347 * initiators who support CRC32C can connect. 1348 */ 1349 rc = iscsi_op_login_update_param(conn, "HeaderDigest", "CRC32C", "CRC32C"); 1350 if (rc < 0) { 1351 return rc; 1352 } 1353 } 1354 1355 if (target->data_digest) { 1356 /* 1357 * User specified data digests, so update the list of 1358 * DataDigest values to remove "None" so that only 1359 * initiators who support CRC32C can connect. 1360 */ 1361 rc = iscsi_op_login_update_param(conn, "DataDigest", "CRC32C", "CRC32C"); 1362 if (rc < 0) { 1363 return rc; 1364 } 1365 } 1366 1367 return 0; 1368 } 1369 1370 /* 1371 * This function use to check the session 1372 * return: 1373 * 0, success 1374 * otherwise: error 1375 */ 1376 static int 1377 iscsi_op_login_check_session(struct spdk_iscsi_conn *conn, 1378 struct spdk_iscsi_pdu *rsp_pdu, 1379 char *initiator_port_name, int cid) 1380 1381 { 1382 int rc = 0; 1383 struct iscsi_bhs_login_rsp *rsph; 1384 1385 rsph = (struct iscsi_bhs_login_rsp *)&rsp_pdu->bhs; 1386 /* check existing session */ 1387 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "isid=%"PRIx64", tsih=%u, cid=%u\n", 1388 iscsi_get_isid(rsph->isid), from_be16(&rsph->tsih), cid); 1389 if (rsph->tsih != 0) { 1390 /* multiple connections */ 1391 rc = append_iscsi_sess(conn, initiator_port_name, 1392 from_be16(&rsph->tsih), cid); 1393 if (rc < 0) { 1394 SPDK_ERRLOG("isid=%"PRIx64", tsih=%u, cid=%u:" 1395 "spdk_append_iscsi_sess() failed\n", 1396 iscsi_get_isid(rsph->isid), from_be16(&rsph->tsih), 1397 cid); 1398 /* Can't include in session */ 1399 rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR; 1400 rsph->status_detail = ISCSI_LOGIN_CONN_ADD_FAIL; 1401 return SPDK_ISCSI_LOGIN_ERROR_RESPONSE; 1402 } 1403 } else if (!g_spdk_iscsi.AllowDuplicateIsid) { 1404 /* new session, drop old sess by the initiator */ 1405 spdk_iscsi_drop_conns(conn, initiator_port_name, 0 /* drop old */); 1406 } 1407 1408 return rc; 1409 } 1410 1411 /* 1412 * This function is used to check the target info 1413 * return: 1414 * 0: success 1415 * otherwise: error 1416 */ 1417 static int 1418 iscsi_op_login_check_target(struct spdk_iscsi_conn *conn, 1419 struct spdk_iscsi_pdu *rsp_pdu, 1420 const char *target_name, 1421 struct spdk_iscsi_tgt_node **target) 1422 { 1423 bool result; 1424 struct iscsi_bhs_login_rsp *rsph; 1425 1426 rsph = (struct iscsi_bhs_login_rsp *)&rsp_pdu->bhs; 1427 *target = spdk_iscsi_find_tgt_node(target_name); 1428 if (*target == NULL) { 1429 SPDK_WARNLOG("target %s not found\n", target_name); 1430 /* Not found */ 1431 rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR; 1432 rsph->status_detail = ISCSI_LOGIN_TARGET_NOT_FOUND; 1433 return SPDK_ISCSI_LOGIN_ERROR_RESPONSE; 1434 } 1435 if (spdk_iscsi_tgt_node_is_destructed(*target)) { 1436 SPDK_ERRLOG("target %s is removed\n", target_name); 1437 rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR; 1438 rsph->status_detail = ISCSI_LOGIN_TARGET_REMOVED; 1439 return SPDK_ISCSI_LOGIN_ERROR_RESPONSE; 1440 } 1441 result = spdk_iscsi_tgt_node_access(conn, *target, 1442 conn->initiator_name, 1443 conn->initiator_addr); 1444 if (!result) { 1445 SPDK_ERRLOG("access denied\n"); 1446 /* Not found */ 1447 rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR; 1448 rsph->status_detail = ISCSI_LOGIN_TARGET_NOT_FOUND; 1449 return SPDK_ISCSI_LOGIN_ERROR_RESPONSE; 1450 } 1451 1452 return 0; 1453 } 1454 1455 /* 1456 * The function which is used to handle the part of normal login session 1457 * return: 1458 * 0, success; 1459 * SPDK_ISCSI_LOGIN_ERROR_PARAMETER, parameter error; 1460 */ 1461 static int 1462 iscsi_op_login_session_normal(struct spdk_iscsi_conn *conn, 1463 struct spdk_iscsi_pdu *rsp_pdu, 1464 char *initiator_port_name, 1465 struct iscsi_param *params, 1466 struct spdk_iscsi_tgt_node **target, 1467 int cid) 1468 { 1469 const char *target_name; 1470 const char *target_short_name; 1471 struct iscsi_bhs_login_rsp *rsph; 1472 int rc = 0; 1473 1474 rsph = (struct iscsi_bhs_login_rsp *)&rsp_pdu->bhs; 1475 target_name = spdk_iscsi_param_get_val(params, "TargetName"); 1476 1477 if (target_name == NULL) { 1478 SPDK_ERRLOG("TargetName is empty\n"); 1479 /* Missing parameter */ 1480 rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR; 1481 rsph->status_detail = ISCSI_LOGIN_MISSING_PARMS; 1482 return SPDK_ISCSI_LOGIN_ERROR_RESPONSE; 1483 } 1484 1485 memset(conn->target_short_name, 0, MAX_TARGET_NAME); 1486 target_short_name = strstr(target_name, ":"); 1487 if (target_short_name != NULL) { 1488 target_short_name++; /* Advance past the ':' */ 1489 if (strlen(target_short_name) >= MAX_TARGET_NAME) { 1490 SPDK_ERRLOG("Target Short Name (%s) is more than %u characters\n", 1491 target_short_name, MAX_TARGET_NAME); 1492 return rc; 1493 } 1494 snprintf(conn->target_short_name, MAX_TARGET_NAME, "%s", 1495 target_short_name); 1496 } 1497 1498 pthread_mutex_lock(&g_spdk_iscsi.mutex); 1499 rc = iscsi_op_login_check_target(conn, rsp_pdu, target_name, target); 1500 pthread_mutex_unlock(&g_spdk_iscsi.mutex); 1501 1502 if (rc < 0) { 1503 return rc; 1504 } 1505 1506 conn->target = *target; 1507 conn->dev = (*target)->dev; 1508 conn->target_port = spdk_scsi_dev_find_port_by_id((*target)->dev, 1509 conn->portal->group->tag); 1510 1511 rc = iscsi_op_login_check_session(conn, rsp_pdu, 1512 initiator_port_name, cid); 1513 if (rc < 0) { 1514 return rc; 1515 } 1516 1517 /* force target flags */ 1518 pthread_mutex_lock(&((*target)->mutex)); 1519 rc = iscsi_op_login_negotiate_chap_param(conn, *target); 1520 pthread_mutex_unlock(&((*target)->mutex)); 1521 1522 if (rc != 0) { 1523 return rc; 1524 } 1525 1526 return iscsi_op_login_negotiate_digest_param(conn, *target); 1527 } 1528 1529 /* 1530 * This function is used to judge the session type 1531 * return 1532 * 0: success 1533 * otherwise, error 1534 */ 1535 static int 1536 iscsi_op_login_session_type(struct spdk_iscsi_conn *conn, 1537 struct spdk_iscsi_pdu *rsp_pdu, 1538 enum session_type *session_type, 1539 struct iscsi_param *params) 1540 { 1541 const char *session_type_str; 1542 struct iscsi_bhs_login_rsp *rsph; 1543 1544 rsph = (struct iscsi_bhs_login_rsp *)&rsp_pdu->bhs; 1545 session_type_str = spdk_iscsi_param_get_val(params, "SessionType"); 1546 if (session_type_str == NULL) { 1547 if (rsph->tsih != 0) { 1548 *session_type = SESSION_TYPE_NORMAL; 1549 } else { 1550 SPDK_ERRLOG("SessionType is empty\n"); 1551 /* Missing parameter */ 1552 rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR; 1553 rsph->status_detail = ISCSI_LOGIN_MISSING_PARMS; 1554 return SPDK_ISCSI_LOGIN_ERROR_RESPONSE; 1555 } 1556 } else { 1557 if (strcasecmp(session_type_str, "Discovery") == 0) { 1558 *session_type = SESSION_TYPE_DISCOVERY; 1559 } else if (strcasecmp(session_type_str, "Normal") == 0) { 1560 *session_type = SESSION_TYPE_NORMAL; 1561 } else { 1562 *session_type = SESSION_TYPE_INVALID; 1563 SPDK_ERRLOG("SessionType is invalid\n"); 1564 /* Missing parameter */ 1565 rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR; 1566 rsph->status_detail = ISCSI_LOGIN_MISSING_PARMS; 1567 return SPDK_ISCSI_LOGIN_ERROR_RESPONSE; 1568 } 1569 } 1570 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "Session Type: %s\n", session_type_str); 1571 1572 return 0; 1573 } 1574 /* 1575 * This function is used to initialize the port info 1576 * return 1577 * 0: success 1578 * otherwise: error 1579 */ 1580 static int 1581 iscsi_op_login_initialize_port(struct spdk_iscsi_conn *conn, 1582 struct spdk_iscsi_pdu *rsp_pdu, 1583 char *initiator_port_name, 1584 uint32_t name_length, 1585 struct iscsi_param *params) 1586 { 1587 const char *val; 1588 struct iscsi_bhs_login_rsp *rsph; 1589 rsph = (struct iscsi_bhs_login_rsp *)&rsp_pdu->bhs; 1590 1591 /* Initiator Name and Port */ 1592 val = spdk_iscsi_param_get_val(params, "InitiatorName"); 1593 if (val == NULL) { 1594 SPDK_ERRLOG("InitiatorName is empty\n"); 1595 /* Missing parameter */ 1596 rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR; 1597 rsph->status_detail = ISCSI_LOGIN_MISSING_PARMS; 1598 return SPDK_ISCSI_LOGIN_ERROR_RESPONSE; 1599 } 1600 snprintf(conn->initiator_name, sizeof(conn->initiator_name), "%s", val); 1601 snprintf(initiator_port_name, name_length, 1602 "%s,i,0x%12.12" PRIx64, val, iscsi_get_isid(rsph->isid)); 1603 spdk_strlwr(conn->initiator_name); 1604 spdk_strlwr(initiator_port_name); 1605 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "Initiator name: %s\n", conn->initiator_name); 1606 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "Initiator port: %s\n", initiator_port_name); 1607 1608 return 0; 1609 } 1610 1611 /* 1612 * This function is used to set the info in the connection data structure 1613 * return 1614 * 0: success 1615 * otherwise: error 1616 */ 1617 static int 1618 iscsi_op_login_set_conn_info(struct spdk_iscsi_conn *conn, 1619 struct spdk_iscsi_pdu *rsp_pdu, 1620 char *initiator_port_name, 1621 enum session_type session_type, 1622 struct spdk_iscsi_tgt_node *target, int cid) 1623 { 1624 int rc = 0; 1625 struct iscsi_bhs_login_rsp *rsph; 1626 struct spdk_scsi_port *initiator_port; 1627 1628 rsph = (struct iscsi_bhs_login_rsp *)&rsp_pdu->bhs; 1629 conn->authenticated = false; 1630 conn->auth.chap_phase = ISCSI_CHAP_PHASE_WAIT_A; 1631 conn->cid = cid; 1632 1633 if (conn->sess == NULL) { 1634 /* create initiator port */ 1635 initiator_port = spdk_scsi_port_create(iscsi_get_isid(rsph->isid), 0, initiator_port_name); 1636 if (initiator_port == NULL) { 1637 SPDK_ERRLOG("create_port() failed\n"); 1638 rsph->status_class = ISCSI_CLASS_TARGET_ERROR; 1639 rsph->status_detail = ISCSI_LOGIN_STATUS_NO_RESOURCES; 1640 return SPDK_ISCSI_LOGIN_ERROR_RESPONSE; 1641 } 1642 1643 /* new session */ 1644 rc = create_iscsi_sess(conn, target, session_type); 1645 if (rc < 0) { 1646 spdk_scsi_port_free(&initiator_port); 1647 SPDK_ERRLOG("create_sess() failed\n"); 1648 rsph->status_class = ISCSI_CLASS_TARGET_ERROR; 1649 rsph->status_detail = ISCSI_LOGIN_STATUS_NO_RESOURCES; 1650 return SPDK_ISCSI_LOGIN_ERROR_RESPONSE; 1651 } 1652 /* initialize parameters */ 1653 conn->sess->initiator_port = initiator_port; 1654 conn->StatSN = from_be32(&rsph->stat_sn); 1655 conn->sess->isid = iscsi_get_isid(rsph->isid); 1656 1657 /* Initiator port TransportID */ 1658 spdk_scsi_port_set_iscsi_transport_id(conn->sess->initiator_port, 1659 conn->initiator_name, 1660 conn->sess->isid); 1661 1662 /* Discovery sessions will not have a target. */ 1663 if (target != NULL) { 1664 conn->sess->queue_depth = target->queue_depth; 1665 } else { 1666 /* 1667 * Assume discovery sessions have an effective command 1668 * windows size of 1. 1669 */ 1670 conn->sess->queue_depth = 1; 1671 } 1672 conn->sess->ExpCmdSN = rsp_pdu->cmd_sn; 1673 conn->sess->MaxCmdSN = rsp_pdu->cmd_sn + conn->sess->queue_depth - 1; 1674 } 1675 1676 conn->initiator_port = conn->sess->initiator_port; 1677 1678 return 0; 1679 } 1680 1681 /* 1682 * This function is used to set the target info 1683 * return 1684 * 0: success 1685 * otherwise: error 1686 */ 1687 static int 1688 iscsi_op_login_set_target_info(struct spdk_iscsi_conn *conn, 1689 struct spdk_iscsi_pdu *rsp_pdu, 1690 enum session_type session_type, 1691 int alloc_len, 1692 struct spdk_iscsi_tgt_node *target) 1693 { 1694 char buf[MAX_TMPBUF]; 1695 const char *val; 1696 int rc = 0; 1697 struct spdk_iscsi_portal *portal = conn->portal; 1698 1699 /* declarative parameters */ 1700 if (target != NULL) { 1701 pthread_mutex_lock(&target->mutex); 1702 if (target->alias != NULL) { 1703 snprintf(buf, sizeof buf, "%s", target->alias); 1704 } else { 1705 snprintf(buf, sizeof buf, "%s", ""); 1706 } 1707 pthread_mutex_unlock(&target->mutex); 1708 rc = spdk_iscsi_param_set(conn->sess->params, "TargetAlias", buf); 1709 if (rc < 0) { 1710 SPDK_ERRLOG("iscsi_param_set() failed\n"); 1711 return SPDK_ISCSI_LOGIN_ERROR_PARAMETER; 1712 } 1713 } 1714 snprintf(buf, sizeof buf, "%s:%s,%d", portal->host, portal->port, 1715 portal->group->tag); 1716 rc = spdk_iscsi_param_set(conn->sess->params, "TargetAddress", buf); 1717 if (rc < 0) { 1718 SPDK_ERRLOG("iscsi_param_set() failed\n"); 1719 return SPDK_ISCSI_LOGIN_ERROR_PARAMETER; 1720 } 1721 snprintf(buf, sizeof buf, "%d", portal->group->tag); 1722 rc = spdk_iscsi_param_set(conn->sess->params, "TargetPortalGroupTag", buf); 1723 if (rc < 0) { 1724 SPDK_ERRLOG("iscsi_param_set() failed\n"); 1725 return SPDK_ISCSI_LOGIN_ERROR_PARAMETER; 1726 } 1727 1728 /* write in response */ 1729 if (target != NULL) { 1730 val = spdk_iscsi_param_get_val(conn->sess->params, "TargetAlias"); 1731 if (val != NULL && strlen(val) != 0) { 1732 rsp_pdu->data_segment_len = iscsi_append_param(conn, 1733 "TargetAlias", 1734 rsp_pdu->data, 1735 alloc_len, 1736 rsp_pdu->data_segment_len); 1737 } 1738 if (session_type == SESSION_TYPE_DISCOVERY) { 1739 rsp_pdu->data_segment_len = iscsi_append_param(conn, 1740 "TargetAddress", 1741 rsp_pdu->data, 1742 alloc_len, 1743 rsp_pdu->data_segment_len); 1744 } 1745 rsp_pdu->data_segment_len = iscsi_append_param(conn, 1746 "TargetPortalGroupTag", 1747 rsp_pdu->data, 1748 alloc_len, 1749 rsp_pdu->data_segment_len); 1750 } 1751 1752 return rc; 1753 } 1754 1755 /* 1756 * This function is used to handle the login of iscsi initiator when there is 1757 * no session 1758 * return: 1759 * 0, success; 1760 * SPDK_ISCSI_LOGIN_ERROR_PARAMETER, parameter error; 1761 * SPDK_ISCSI_LOGIN_ERROR_RESPONSE, used to notify the login fail. 1762 */ 1763 static int 1764 iscsi_op_login_phase_none(struct spdk_iscsi_conn *conn, 1765 struct spdk_iscsi_pdu *rsp_pdu, 1766 struct iscsi_param *params, 1767 int alloc_len, int cid) 1768 { 1769 enum session_type session_type; 1770 char initiator_port_name[MAX_INITIATOR_PORT_NAME]; 1771 struct iscsi_bhs_login_rsp *rsph; 1772 struct spdk_iscsi_tgt_node *target = NULL; 1773 int rc = 0; 1774 rsph = (struct iscsi_bhs_login_rsp *)&rsp_pdu->bhs; 1775 1776 conn->target = NULL; 1777 conn->dev = NULL; 1778 1779 rc = iscsi_op_login_initialize_port(conn, rsp_pdu, initiator_port_name, 1780 MAX_INITIATOR_PORT_NAME, params); 1781 if (rc < 0) { 1782 return rc; 1783 } 1784 1785 rc = iscsi_op_login_session_type(conn, rsp_pdu, &session_type, params); 1786 if (rc < 0) { 1787 return rc; 1788 } 1789 1790 /* Target Name and Port */ 1791 if (session_type == SESSION_TYPE_NORMAL) { 1792 rc = iscsi_op_login_session_normal(conn, rsp_pdu, 1793 initiator_port_name, 1794 params, &target, cid); 1795 if (rc < 0) { 1796 return rc; 1797 } 1798 1799 } else if (session_type == SESSION_TYPE_DISCOVERY) { 1800 target = NULL; 1801 rsph->tsih = 0; 1802 1803 /* force target flags */ 1804 pthread_mutex_lock(&g_spdk_iscsi.mutex); 1805 rc = iscsi_op_login_session_discovery_chap(conn); 1806 pthread_mutex_unlock(&g_spdk_iscsi.mutex); 1807 if (rc < 0) { 1808 return rc; 1809 } 1810 } else { 1811 SPDK_ERRLOG("unknown session type\n"); 1812 /* Missing parameter */ 1813 rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR; 1814 rsph->status_detail = ISCSI_LOGIN_MISSING_PARMS; 1815 return SPDK_ISCSI_LOGIN_ERROR_RESPONSE; 1816 } 1817 1818 rc = iscsi_op_login_set_conn_info(conn, rsp_pdu, initiator_port_name, 1819 session_type, target, cid); 1820 if (rc < 0) { 1821 return rc; 1822 } 1823 1824 /* limit conns on discovery session */ 1825 if (session_type == SESSION_TYPE_DISCOVERY) { 1826 conn->sess->MaxConnections = 1; 1827 rc = spdk_iscsi_param_set_int(conn->sess->params, 1828 "MaxConnections", 1829 conn->sess->MaxConnections); 1830 if (rc < 0) { 1831 SPDK_ERRLOG("iscsi_param_set_int() failed\n"); 1832 return SPDK_ISCSI_LOGIN_ERROR_PARAMETER; 1833 } 1834 } 1835 1836 return iscsi_op_login_set_target_info(conn, rsp_pdu, session_type, 1837 alloc_len, target); 1838 } 1839 1840 /* 1841 * The function which is used to initialize the internal response data 1842 * structure of iscsi login function. 1843 * return: 1844 * 0, success; 1845 * otherwise, error; 1846 */ 1847 static int 1848 iscsi_op_login_rsp_init(struct spdk_iscsi_conn *conn, 1849 struct spdk_iscsi_pdu *pdu, struct spdk_iscsi_pdu *rsp_pdu, 1850 struct iscsi_param **params, int *alloc_len, int *cid) 1851 { 1852 struct iscsi_bhs_login_req *reqh; 1853 struct iscsi_bhs_login_rsp *rsph; 1854 int rc; 1855 1856 rsph = (struct iscsi_bhs_login_rsp *)&rsp_pdu->bhs; 1857 rsph->opcode = ISCSI_OP_LOGIN_RSP; 1858 rsph->status_class = ISCSI_CLASS_SUCCESS; 1859 rsph->status_detail = ISCSI_LOGIN_ACCEPT; 1860 rsp_pdu->data_segment_len = 0; 1861 1862 /* Default MaxRecvDataSegmentLength - RFC3720(12.12) */ 1863 if (conn->MaxRecvDataSegmentLength < 8192) { 1864 *alloc_len = 8192; 1865 } else { 1866 *alloc_len = conn->MaxRecvDataSegmentLength; 1867 } 1868 1869 rsp_pdu->data = calloc(1, *alloc_len); 1870 if (!rsp_pdu->data) { 1871 SPDK_ERRLOG("calloc() failed for data segment\n"); 1872 return -ENOMEM; 1873 } 1874 1875 reqh = (struct iscsi_bhs_login_req *)&pdu->bhs; 1876 rsph->flags |= (reqh->flags & ISCSI_LOGIN_TRANSIT); 1877 rsph->flags |= (reqh->flags & ISCSI_LOGIN_CONTINUE); 1878 rsph->flags |= (reqh->flags & ISCSI_LOGIN_CURRENT_STAGE_MASK); 1879 if (ISCSI_BHS_LOGIN_GET_TBIT(rsph->flags)) { 1880 rsph->flags |= (reqh->flags & ISCSI_LOGIN_NEXT_STAGE_MASK); 1881 } 1882 1883 /* We don't need to convert from network byte order. Just store it */ 1884 memcpy(&rsph->isid, reqh->isid, 6); 1885 rsph->tsih = reqh->tsih; 1886 rsph->itt = reqh->itt; 1887 rsp_pdu->cmd_sn = from_be32(&reqh->cmd_sn); 1888 *cid = from_be16(&reqh->cid); 1889 1890 if (rsph->tsih) { 1891 rsph->stat_sn = reqh->exp_stat_sn; 1892 } 1893 1894 SPDK_LOGDUMP(SPDK_LOG_ISCSI, "PDU", (uint8_t *)&pdu->bhs, ISCSI_BHS_LEN); 1895 1896 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, 1897 "T=%d, C=%d, CSG=%d, NSG=%d, Min=%d, Max=%d, ITT=%x\n", 1898 ISCSI_BHS_LOGIN_GET_TBIT(rsph->flags), 1899 ISCSI_BHS_LOGIN_GET_CBIT(rsph->flags), 1900 ISCSI_BHS_LOGIN_GET_CSG(rsph->flags), 1901 ISCSI_BHS_LOGIN_GET_NSG(rsph->flags), 1902 reqh->version_min, reqh->version_max, from_be32(&rsph->itt)); 1903 1904 if (conn->sess != NULL) { 1905 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, 1906 "CmdSN=%u, ExpStatSN=%u, StatSN=%u, ExpCmdSN=%u," 1907 "MaxCmdSN=%u\n", rsp_pdu->cmd_sn, 1908 from_be32(&rsph->stat_sn), conn->StatSN, 1909 conn->sess->ExpCmdSN, 1910 conn->sess->MaxCmdSN); 1911 } else { 1912 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, 1913 "CmdSN=%u, ExpStatSN=%u, StatSN=%u\n", 1914 rsp_pdu->cmd_sn, from_be32(&rsph->stat_sn), 1915 conn->StatSN); 1916 } 1917 1918 if (ISCSI_BHS_LOGIN_GET_TBIT(rsph->flags) && 1919 ISCSI_BHS_LOGIN_GET_CBIT(rsph->flags)) { 1920 SPDK_ERRLOG("transit error\n"); 1921 return SPDK_ISCSI_LOGIN_ERROR_RESPONSE; 1922 } 1923 /* make sure reqh->version_max < ISCSI_VERSION */ 1924 if (reqh->version_min > ISCSI_VERSION) { 1925 SPDK_ERRLOG("unsupported version %d/%d\n", reqh->version_min, 1926 reqh->version_max); 1927 /* Unsupported version */ 1928 /* set all reserved flag to zero */ 1929 rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR; 1930 rsph->status_detail = ISCSI_LOGIN_UNSUPPORTED_VERSION; 1931 return SPDK_ISCSI_LOGIN_ERROR_RESPONSE; 1932 } 1933 1934 if ((ISCSI_BHS_LOGIN_GET_NSG(rsph->flags) == ISCSI_NSG_RESERVED_CODE) && 1935 ISCSI_BHS_LOGIN_GET_TBIT(rsph->flags)) { 1936 /* set NSG to zero */ 1937 rsph->flags &= ~ISCSI_LOGIN_NEXT_STAGE_MASK; 1938 /* also set other bits to zero */ 1939 rsph->flags &= ~ISCSI_LOGIN_TRANSIT; 1940 rsph->flags &= ~ISCSI_LOGIN_CURRENT_STAGE_MASK; 1941 SPDK_ERRLOG("Received reserved NSG code: %d\n", ISCSI_NSG_RESERVED_CODE); 1942 /* Initiator error */ 1943 rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR; 1944 rsph->status_detail = ISCSI_LOGIN_INITIATOR_ERROR; 1945 return SPDK_ISCSI_LOGIN_ERROR_RESPONSE; 1946 } 1947 1948 /* store incoming parameters */ 1949 rc = spdk_iscsi_parse_params(params, pdu->data, 1950 pdu->data_segment_len, ISCSI_BHS_LOGIN_GET_CBIT(reqh->flags), 1951 &conn->partial_text_parameter); 1952 if (rc < 0) { 1953 SPDK_ERRLOG("iscsi_parse_params() failed\n"); 1954 rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR; 1955 rsph->status_detail = ISCSI_LOGIN_INITIATOR_ERROR; 1956 return SPDK_ISCSI_LOGIN_ERROR_PARAMETER; 1957 } 1958 return 0; 1959 } 1960 1961 /* 1962 * This function is used to set the csg bit case in rsp 1963 * return: 1964 * 0, success 1965 * otherwise: error 1966 */ 1967 static int 1968 iscsi_op_login_rsp_handle_csg_bit(struct spdk_iscsi_conn *conn, 1969 struct spdk_iscsi_pdu *rsp_pdu, 1970 struct iscsi_param *params, int alloc_len) 1971 { 1972 const char *auth_method; 1973 int rc; 1974 struct iscsi_bhs_login_rsp *rsph; 1975 rsph = (struct iscsi_bhs_login_rsp *)&rsp_pdu->bhs; 1976 1977 switch (ISCSI_BHS_LOGIN_GET_CSG(rsph->flags)) { 1978 case ISCSI_SECURITY_NEGOTIATION_PHASE: 1979 /* SecurityNegotiation */ 1980 auth_method = spdk_iscsi_param_get_val(conn->params, "AuthMethod"); 1981 if (auth_method == NULL) { 1982 SPDK_ERRLOG("AuthMethod is empty\n"); 1983 /* Missing parameter */ 1984 rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR; 1985 rsph->status_detail = ISCSI_LOGIN_MISSING_PARMS; 1986 return SPDK_ISCSI_LOGIN_ERROR_RESPONSE; 1987 } 1988 if (strcasecmp(auth_method, "None") == 0) { 1989 conn->authenticated = true; 1990 } else { 1991 rc = iscsi_auth_params(conn, params, auth_method, 1992 rsp_pdu->data, alloc_len, 1993 rsp_pdu->data_segment_len); 1994 if (rc < 0) { 1995 SPDK_ERRLOG("iscsi_auth_params() failed\n"); 1996 /* Authentication failure */ 1997 rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR; 1998 rsph->status_detail = ISCSI_LOGIN_AUTHENT_FAIL; 1999 return SPDK_ISCSI_LOGIN_ERROR_RESPONSE; 2000 } 2001 rsp_pdu->data_segment_len = rc; 2002 if (!conn->authenticated) { 2003 /* not complete */ 2004 rsph->flags &= ~ISCSI_LOGIN_TRANSIT; 2005 } else { 2006 if (conn->auth.chap_phase != ISCSI_CHAP_PHASE_END) { 2007 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "CHAP phase not complete"); 2008 } 2009 } 2010 2011 SPDK_LOGDUMP(SPDK_LOG_ISCSI, "Negotiated Auth Params", 2012 rsp_pdu->data, rsp_pdu->data_segment_len); 2013 } 2014 break; 2015 2016 case ISCSI_OPERATIONAL_NEGOTIATION_PHASE: 2017 /* LoginOperationalNegotiation */ 2018 if (conn->state == ISCSI_CONN_STATE_INVALID) { 2019 if (conn->require_chap) { 2020 /* Authentication failure */ 2021 rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR; 2022 rsph->status_detail = ISCSI_LOGIN_AUTHENT_FAIL; 2023 return SPDK_ISCSI_LOGIN_ERROR_RESPONSE; 2024 } else { 2025 /* AuthMethod=None */ 2026 conn->authenticated = true; 2027 } 2028 } 2029 if (!conn->authenticated) { 2030 SPDK_ERRLOG("authentication error\n"); 2031 /* Authentication failure */ 2032 rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR; 2033 rsph->status_detail = ISCSI_LOGIN_AUTHENT_FAIL; 2034 return SPDK_ISCSI_LOGIN_ERROR_RESPONSE; 2035 } 2036 break; 2037 2038 case ISCSI_FULL_FEATURE_PHASE: 2039 /* FullFeaturePhase */ 2040 SPDK_ERRLOG("XXX Login in FullFeaturePhase\n"); 2041 /* Initiator error */ 2042 rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR; 2043 rsph->status_detail = ISCSI_LOGIN_INITIATOR_ERROR; 2044 return SPDK_ISCSI_LOGIN_ERROR_RESPONSE; 2045 2046 default: 2047 SPDK_ERRLOG("unknown stage\n"); 2048 /* Initiator error */ 2049 rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR; 2050 rsph->status_detail = ISCSI_LOGIN_INITIATOR_ERROR; 2051 return SPDK_ISCSI_LOGIN_ERROR_RESPONSE; 2052 } 2053 2054 return 0; 2055 } 2056 2057 /* This function is used to notify the session info 2058 * return 2059 * 0: success 2060 * otherwise: error 2061 */ 2062 static int 2063 iscsi_op_login_notify_session_info(struct spdk_iscsi_conn *conn, 2064 struct spdk_iscsi_pdu *rsp_pdu) 2065 { 2066 struct iscsi_bhs_login_rsp *rsph; 2067 2068 rsph = (struct iscsi_bhs_login_rsp *)&rsp_pdu->bhs; 2069 if (conn->sess->session_type == SESSION_TYPE_NORMAL) { 2070 /* normal session */ 2071 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "Login from %s (%s) on %s tgt_node%d" 2072 " (%s:%s,%d), ISID=%"PRIx64", TSIH=%u," 2073 " CID=%u, HeaderDigest=%s, DataDigest=%s\n", 2074 conn->initiator_name, conn->initiator_addr, 2075 conn->target->name, conn->target->num, 2076 conn->portal->host, conn->portal->port, conn->portal->group->tag, 2077 conn->sess->isid, conn->sess->tsih, conn->cid, 2078 (spdk_iscsi_param_eq_val(conn->params, "HeaderDigest", "CRC32C") 2079 ? "on" : "off"), 2080 (spdk_iscsi_param_eq_val(conn->params, "DataDigest", "CRC32C") 2081 ? "on" : "off")); 2082 } else if (conn->sess->session_type == SESSION_TYPE_DISCOVERY) { 2083 /* discovery session */ 2084 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "Login(discovery) from %s (%s) on" 2085 " (%s:%s,%d), ISID=%"PRIx64", TSIH=%u," 2086 " CID=%u, HeaderDigest=%s, DataDigest=%s\n", 2087 conn->initiator_name, conn->initiator_addr, 2088 conn->portal->host, conn->portal->port, conn->portal->group->tag, 2089 conn->sess->isid, conn->sess->tsih, conn->cid, 2090 (spdk_iscsi_param_eq_val(conn->params, "HeaderDigest", "CRC32C") 2091 ? "on" : "off"), 2092 (spdk_iscsi_param_eq_val(conn->params, "DataDigest", "CRC32C") 2093 ? "on" : "off")); 2094 } else { 2095 SPDK_ERRLOG("unknown session type\n"); 2096 /* Initiator error */ 2097 rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR; 2098 rsph->status_detail = ISCSI_LOGIN_INITIATOR_ERROR; 2099 return SPDK_ISCSI_LOGIN_ERROR_RESPONSE; 2100 } 2101 2102 return 0; 2103 } 2104 2105 /* 2106 * This function is to handle the tbit cases 2107 * return 2108 * 0: success 2109 * otherwise error 2110 */ 2111 static int 2112 iscsi_op_login_rsp_handle_t_bit(struct spdk_iscsi_conn *conn, 2113 struct spdk_iscsi_pdu *rsp_pdu) 2114 { 2115 int rc; 2116 struct iscsi_bhs_login_rsp *rsph; 2117 rsph = (struct iscsi_bhs_login_rsp *)&rsp_pdu->bhs; 2118 2119 switch (ISCSI_BHS_LOGIN_GET_NSG(rsph->flags)) { 2120 case ISCSI_SECURITY_NEGOTIATION_PHASE: 2121 /* SecurityNegotiation */ 2122 conn->login_phase = ISCSI_SECURITY_NEGOTIATION_PHASE; 2123 break; 2124 2125 case ISCSI_OPERATIONAL_NEGOTIATION_PHASE: 2126 /* LoginOperationalNegotiation */ 2127 conn->login_phase = ISCSI_OPERATIONAL_NEGOTIATION_PHASE; 2128 break; 2129 2130 case ISCSI_FULL_FEATURE_PHASE: 2131 /* FullFeaturePhase */ 2132 conn->login_phase = ISCSI_FULL_FEATURE_PHASE; 2133 to_be16(&rsph->tsih, conn->sess->tsih); 2134 2135 rc = iscsi_op_login_notify_session_info(conn, rsp_pdu); 2136 if (rc < 0) { 2137 return rc; 2138 } 2139 2140 conn->full_feature = 1; 2141 spdk_iscsi_conn_schedule(conn); 2142 break; 2143 2144 default: 2145 SPDK_ERRLOG("unknown stage\n"); 2146 /* Initiator error */ 2147 rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR; 2148 rsph->status_detail = ISCSI_LOGIN_INITIATOR_ERROR; 2149 return SPDK_ISCSI_LOGIN_ERROR_RESPONSE; 2150 } 2151 2152 return 0; 2153 } 2154 2155 /* 2156 * This function is used to set the values of the internal data structure used 2157 * by spdk_iscsi_op_login function 2158 * return: 2159 * 0, used to notify the a successful login 2160 * SPDK_ISCSI_LOGIN_ERROR_RESPONSE, used to notify a failure login. 2161 */ 2162 static int 2163 iscsi_op_login_rsp_handle(struct spdk_iscsi_conn *conn, 2164 struct spdk_iscsi_pdu *rsp_pdu, struct iscsi_param **params, 2165 int alloc_len) 2166 { 2167 int rc; 2168 struct iscsi_bhs_login_rsp *rsph; 2169 rsph = (struct iscsi_bhs_login_rsp *)&rsp_pdu->bhs; 2170 2171 /* negotiate parameters */ 2172 rc = spdk_iscsi_negotiate_params(conn, params, rsp_pdu->data, alloc_len, 2173 rsp_pdu->data_segment_len); 2174 if (rc < 0) { 2175 /* 2176 * spdk_iscsi_negotiate_params just returns -1 on failure, 2177 * so translate this into meaningful response codes and 2178 * return values. 2179 */ 2180 rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR; 2181 rsph->status_detail = ISCSI_LOGIN_INITIATOR_ERROR; 2182 return SPDK_ISCSI_LOGIN_ERROR_RESPONSE; 2183 } 2184 2185 rsp_pdu->data_segment_len = rc; 2186 SPDK_LOGDUMP(SPDK_LOG_ISCSI, "Negotiated Params", rsp_pdu->data, rc); 2187 2188 /* handle the CSG bit case */ 2189 rc = iscsi_op_login_rsp_handle_csg_bit(conn, rsp_pdu, *params, 2190 alloc_len); 2191 if (rc < 0) { 2192 return rc; 2193 } 2194 2195 /* handle the T bit case */ 2196 if (ISCSI_BHS_LOGIN_GET_TBIT(rsph->flags)) { 2197 rc = iscsi_op_login_rsp_handle_t_bit(conn, rsp_pdu); 2198 } 2199 2200 return rc; 2201 } 2202 2203 static int 2204 iscsi_op_login(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu) 2205 { 2206 int rc; 2207 struct spdk_iscsi_pdu *rsp_pdu; 2208 struct iscsi_param *params = NULL; 2209 struct iscsi_param **params_p = ¶ms; 2210 int alloc_len; 2211 int cid; 2212 2213 if (conn->full_feature && conn->sess != NULL && 2214 conn->sess->session_type == SESSION_TYPE_DISCOVERY) { 2215 return SPDK_ISCSI_CONNECTION_FATAL; 2216 } 2217 2218 rsp_pdu = spdk_get_pdu(); 2219 if (rsp_pdu == NULL) { 2220 return SPDK_ISCSI_CONNECTION_FATAL; 2221 } 2222 rc = iscsi_op_login_rsp_init(conn, pdu, rsp_pdu, params_p, 2223 &alloc_len, &cid); 2224 if (rc == SPDK_ISCSI_LOGIN_ERROR_RESPONSE || rc == SPDK_ISCSI_LOGIN_ERROR_PARAMETER) { 2225 iscsi_op_login_response(conn, rsp_pdu, *params_p); 2226 return rc; 2227 } 2228 2229 /* For other values, we need to directly return */ 2230 if (rc < 0) { 2231 spdk_put_pdu(rsp_pdu); 2232 return rc; 2233 } 2234 2235 if (conn->state == ISCSI_CONN_STATE_INVALID) { 2236 rc = iscsi_op_login_phase_none(conn, rsp_pdu, *params_p, 2237 alloc_len, cid); 2238 if (rc == SPDK_ISCSI_LOGIN_ERROR_RESPONSE || rc == SPDK_ISCSI_LOGIN_ERROR_PARAMETER) { 2239 iscsi_op_login_response(conn, rsp_pdu, *params_p); 2240 return rc; 2241 } 2242 } 2243 2244 rc = iscsi_op_login_rsp_handle(conn, rsp_pdu, params_p, alloc_len); 2245 if (rc == SPDK_ISCSI_LOGIN_ERROR_RESPONSE) { 2246 iscsi_op_login_response(conn, rsp_pdu, *params_p); 2247 return rc; 2248 } 2249 2250 rc = iscsi_op_login_response(conn, rsp_pdu, *params_p); 2251 if (rc == 0) { 2252 conn->state = ISCSI_CONN_STATE_RUNNING; 2253 } else { 2254 SPDK_ERRLOG("login error - connection will be destroyed\n"); 2255 } 2256 2257 return rc; 2258 } 2259 2260 static int 2261 iscsi_op_text(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu) 2262 { 2263 struct iscsi_param *params = NULL; 2264 struct iscsi_param **params_p = ¶ms; 2265 struct spdk_iscsi_pdu *rsp_pdu; 2266 uint8_t *data; 2267 uint64_t lun; 2268 uint32_t task_tag; 2269 uint32_t CmdSN; 2270 uint32_t ExpStatSN; 2271 const char *val; 2272 int F_bit, C_bit; 2273 int data_len; 2274 int alloc_len; 2275 int rc; 2276 struct iscsi_bhs_text_req *reqh; 2277 struct iscsi_bhs_text_resp *rsph; 2278 2279 data_len = 0; 2280 alloc_len = conn->MaxRecvDataSegmentLength; 2281 2282 reqh = (struct iscsi_bhs_text_req *)&pdu->bhs; 2283 2284 F_bit = !!(reqh->flags & ISCSI_FLAG_FINAL); 2285 C_bit = !!(reqh->flags & ISCSI_TEXT_CONTINUE); 2286 lun = from_be64(&reqh->lun); 2287 task_tag = from_be32(&reqh->itt); 2288 CmdSN = from_be32(&reqh->cmd_sn); 2289 pdu->cmd_sn = CmdSN; 2290 ExpStatSN = from_be32(&reqh->exp_stat_sn); 2291 2292 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "I=%d, F=%d, C=%d, ITT=%x, TTT=%x\n", 2293 reqh->immediate, F_bit, C_bit, task_tag, from_be32(&reqh->ttt)); 2294 2295 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, 2296 "CmdSN=%u, ExpStatSN=%u, StatSN=%u, ExpCmdSN=%u, MaxCmdSN=%u\n", 2297 CmdSN, ExpStatSN, conn->StatSN, conn->sess->ExpCmdSN, 2298 conn->sess->MaxCmdSN); 2299 2300 if (ExpStatSN != conn->StatSN) { 2301 #if 0 2302 SPDK_ERRLOG("StatSN(%u) error\n", ExpStatSN); 2303 return -1; 2304 #else 2305 /* StarPort have a bug */ 2306 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "StatSN(%u) rewound\n", ExpStatSN); 2307 conn->StatSN = ExpStatSN; 2308 #endif 2309 } 2310 2311 if (F_bit && C_bit) { 2312 SPDK_ERRLOG("final and continue\n"); 2313 return -1; 2314 } 2315 2316 /* 2317 * If this is the first text op in a sequence, save the ITT so we can 2318 * compare it against the ITT for subsequent ops in the same sequence. 2319 * If a subsequent text op in same sequence has a different ITT, reject 2320 * that PDU. 2321 */ 2322 if (conn->sess->current_text_itt == 0xffffffffU) { 2323 conn->sess->current_text_itt = task_tag; 2324 } else if (conn->sess->current_text_itt != task_tag) { 2325 SPDK_ERRLOG("The correct itt is %u, and the current itt is %u...\n", 2326 conn->sess->current_text_itt, task_tag); 2327 return iscsi_reject(conn, pdu, ISCSI_REASON_PROTOCOL_ERROR); 2328 } 2329 2330 /* store incoming parameters */ 2331 rc = spdk_iscsi_parse_params(¶ms, pdu->data, pdu->data_segment_len, 2332 C_bit, &conn->partial_text_parameter); 2333 if (rc < 0) { 2334 SPDK_ERRLOG("iscsi_parse_params() failed\n"); 2335 spdk_iscsi_param_free(params); 2336 return -1; 2337 } 2338 2339 data = calloc(1, alloc_len); 2340 if (!data) { 2341 SPDK_ERRLOG("calloc() failed for data segment\n"); 2342 spdk_iscsi_param_free(params); 2343 return -ENOMEM; 2344 } 2345 2346 /* negotiate parameters */ 2347 data_len = spdk_iscsi_negotiate_params(conn, params_p, 2348 data, alloc_len, data_len); 2349 if (data_len < 0) { 2350 SPDK_ERRLOG("spdk_iscsi_negotiate_params() failed\n"); 2351 spdk_iscsi_param_free(*params_p); 2352 free(data); 2353 return -1; 2354 } 2355 2356 /* sendtargets is special case */ 2357 val = spdk_iscsi_param_get_val(*params_p, "SendTargets"); 2358 if (val != NULL) { 2359 if (spdk_iscsi_param_eq_val(conn->sess->params, 2360 "SessionType", "Discovery")) { 2361 if (strcasecmp(val, "") == 0) { 2362 val = "ALL"; 2363 } 2364 2365 data_len = spdk_iscsi_send_tgts(conn, 2366 conn->initiator_name, 2367 conn->initiator_addr, 2368 val, data, alloc_len, 2369 data_len); 2370 } else { 2371 if (strcasecmp(val, "") == 0) { 2372 val = conn->target->name; 2373 } 2374 2375 if (strcasecmp(val, "ALL") == 0) { 2376 /* not in discovery session */ 2377 data_len = iscsi_append_text(conn, 2378 "SendTargets", 2379 "Reject", data, 2380 alloc_len, data_len); 2381 } else { 2382 data_len = spdk_iscsi_send_tgts(conn, 2383 conn->initiator_name, 2384 conn->initiator_addr, 2385 val, data, alloc_len, 2386 data_len); 2387 } 2388 } 2389 } else { 2390 if (spdk_iscsi_param_eq_val(conn->sess->params, "SessionType", "Discovery")) { 2391 spdk_iscsi_param_free(*params_p); 2392 free(data); 2393 return SPDK_ISCSI_CONNECTION_FATAL; 2394 } 2395 } 2396 2397 SPDK_LOGDUMP(SPDK_LOG_ISCSI, "Negotiated Params", data, data_len); 2398 2399 /* response PDU */ 2400 rsp_pdu = spdk_get_pdu(); 2401 if (rsp_pdu == NULL) { 2402 spdk_iscsi_param_free(*params_p); 2403 free(data); 2404 return SPDK_ISCSI_CONNECTION_FATAL; 2405 } 2406 rsph = (struct iscsi_bhs_text_resp *)&rsp_pdu->bhs; 2407 2408 rsp_pdu->data = data; 2409 rsph->opcode = ISCSI_OP_TEXT_RSP; 2410 2411 if (F_bit) { 2412 rsph->flags |= ISCSI_FLAG_FINAL; 2413 } 2414 2415 if (C_bit) { 2416 rsph->flags |= ISCSI_TEXT_CONTINUE; 2417 } 2418 2419 DSET24(rsph->data_segment_len, data_len); 2420 to_be64(&rsph->lun, lun); 2421 to_be32(&rsph->itt, task_tag); 2422 2423 if (F_bit) { 2424 rsph->ttt = 0xffffffffU; 2425 conn->sess->current_text_itt = 0xffffffffU; 2426 } else { 2427 to_be32(&rsph->ttt, 1 + conn->id); 2428 } 2429 2430 to_be32(&rsph->stat_sn, conn->StatSN); 2431 conn->StatSN++; 2432 2433 if (reqh->immediate == 0) { 2434 conn->sess->MaxCmdSN++; 2435 } 2436 2437 to_be32(&rsph->exp_cmd_sn, conn->sess->ExpCmdSN); 2438 to_be32(&rsph->max_cmd_sn, conn->sess->MaxCmdSN); 2439 2440 spdk_iscsi_conn_write_pdu(conn, rsp_pdu); 2441 2442 /* update internal variables */ 2443 rc = spdk_iscsi_copy_param2var(conn); 2444 if (rc < 0) { 2445 SPDK_ERRLOG("spdk_iscsi_copy_param2var() failed\n"); 2446 spdk_iscsi_param_free(*params_p); 2447 return -1; 2448 } 2449 2450 /* check value */ 2451 rc = iscsi_check_values(conn); 2452 if (rc < 0) { 2453 SPDK_ERRLOG("iscsi_check_values() failed\n"); 2454 spdk_iscsi_param_free(*params_p); 2455 return -1; 2456 } 2457 2458 spdk_iscsi_param_free(*params_p); 2459 return 0; 2460 } 2461 2462 static int 2463 iscsi_op_logout(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu) 2464 { 2465 struct spdk_iscsi_pdu *rsp_pdu; 2466 uint32_t task_tag; 2467 uint32_t CmdSN; 2468 uint32_t ExpStatSN; 2469 int response; 2470 struct iscsi_bhs_logout_req *reqh; 2471 struct iscsi_bhs_logout_resp *rsph; 2472 uint16_t cid; 2473 2474 reqh = (struct iscsi_bhs_logout_req *)&pdu->bhs; 2475 2476 cid = from_be16(&reqh->cid); 2477 task_tag = from_be32(&reqh->itt); 2478 CmdSN = from_be32(&reqh->cmd_sn); 2479 pdu->cmd_sn = CmdSN; 2480 ExpStatSN = from_be32(&reqh->exp_stat_sn); 2481 2482 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "reason=%d, ITT=%x, cid=%d\n", 2483 reqh->reason, task_tag, cid); 2484 2485 if (reqh->reason != 0 && conn->sess->session_type == SESSION_TYPE_DISCOVERY) { 2486 SPDK_ERRLOG("only logout with close the session reason can be in discovery session"); 2487 return SPDK_ISCSI_CONNECTION_FATAL; 2488 } 2489 2490 if (conn->sess != NULL) { 2491 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, 2492 "CmdSN=%u, ExpStatSN=%u, StatSN=%u, ExpCmdSN=%u, MaxCmdSN=%u\n", 2493 CmdSN, ExpStatSN, conn->StatSN, 2494 conn->sess->ExpCmdSN, conn->sess->MaxCmdSN); 2495 2496 if (CmdSN != conn->sess->ExpCmdSN) { 2497 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "CmdSN(%u) might have dropped\n", CmdSN); 2498 /* ignore error */ 2499 } 2500 } else { 2501 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "CmdSN=%u, ExpStatSN=%u, StatSN=%u\n", 2502 CmdSN, ExpStatSN, conn->StatSN); 2503 } 2504 2505 if (ExpStatSN != conn->StatSN) { 2506 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "StatSN(%u/%u) might have dropped\n", 2507 ExpStatSN, conn->StatSN); 2508 /* ignore error */ 2509 } 2510 2511 if (conn->id == cid) { 2512 /* connection or session closed successfully */ 2513 response = 0; 2514 spdk_iscsi_conn_logout(conn); 2515 } else { 2516 response = 1; 2517 } 2518 2519 /* response PDU */ 2520 rsp_pdu = spdk_get_pdu(); 2521 if (rsp_pdu == NULL) { 2522 return SPDK_ISCSI_CONNECTION_FATAL; 2523 } 2524 rsph = (struct iscsi_bhs_logout_resp *)&rsp_pdu->bhs; 2525 rsp_pdu->data = NULL; 2526 rsph->opcode = ISCSI_OP_LOGOUT_RSP; 2527 rsph->flags |= 0x80; /* bit 0 must be 1 */ 2528 rsph->response = response; 2529 DSET24(rsph->data_segment_len, 0); 2530 to_be32(&rsph->itt, task_tag); 2531 2532 if (conn->sess != NULL) { 2533 to_be32(&rsph->stat_sn, conn->StatSN); 2534 conn->StatSN++; 2535 2536 if (conn->sess->connections == 1) { 2537 conn->sess->MaxCmdSN++; 2538 } 2539 2540 to_be32(&rsph->exp_cmd_sn, conn->sess->ExpCmdSN); 2541 to_be32(&rsph->max_cmd_sn, conn->sess->MaxCmdSN); 2542 } else { 2543 to_be32(&rsph->stat_sn, conn->StatSN); 2544 conn->StatSN++; 2545 to_be32(&rsph->exp_cmd_sn, CmdSN); 2546 to_be32(&rsph->max_cmd_sn, CmdSN); 2547 } 2548 2549 rsph->time_2_wait = 0; 2550 rsph->time_2_retain = 0; 2551 2552 spdk_iscsi_conn_write_pdu(conn, rsp_pdu); 2553 2554 if (conn->sess == NULL) { 2555 /* 2556 * login failed but initiator still sent a logout rather than 2557 * just closing the TCP connection. 2558 */ 2559 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "Logout(login failed) from %s (%s) on" 2560 " (%s:%s,%d)\n", 2561 conn->initiator_name, conn->initiator_addr, 2562 conn->portal_host, conn->portal_port, conn->pg_tag); 2563 } else if (spdk_iscsi_param_eq_val(conn->sess->params, "SessionType", "Normal")) { 2564 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "Logout from %s (%s) on %s tgt_node%d" 2565 " (%s:%s,%d), ISID=%"PRIx64", TSIH=%u," 2566 " CID=%u, HeaderDigest=%s, DataDigest=%s\n", 2567 conn->initiator_name, conn->initiator_addr, 2568 conn->target->name, conn->target->num, 2569 conn->portal_host, conn->portal_port, conn->pg_tag, 2570 conn->sess->isid, conn->sess->tsih, conn->cid, 2571 (spdk_iscsi_param_eq_val(conn->params, "HeaderDigest", "CRC32C") 2572 ? "on" : "off"), 2573 (spdk_iscsi_param_eq_val(conn->params, "DataDigest", "CRC32C") 2574 ? "on" : "off")); 2575 } else { 2576 /* discovery session */ 2577 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "Logout(discovery) from %s (%s) on" 2578 " (%s:%s,%d), ISID=%"PRIx64", TSIH=%u," 2579 " CID=%u, HeaderDigest=%s, DataDigest=%s\n", 2580 conn->initiator_name, conn->initiator_addr, 2581 conn->portal_host, conn->portal_port, conn->pg_tag, 2582 conn->sess->isid, conn->sess->tsih, conn->cid, 2583 (spdk_iscsi_param_eq_val(conn->params, "HeaderDigest", "CRC32C") 2584 ? "on" : "off"), 2585 (spdk_iscsi_param_eq_val(conn->params, "DataDigest", "CRC32C") 2586 ? "on" : "off")); 2587 } 2588 2589 return 0; 2590 } 2591 2592 /* This function returns the spdk_scsi_task by searching the snack list via 2593 * task transfertag and the pdu's opcode 2594 */ 2595 static struct spdk_iscsi_task * 2596 get_scsi_task_from_ttt(struct spdk_iscsi_conn *conn, uint32_t transfer_tag) 2597 { 2598 struct spdk_iscsi_pdu *pdu; 2599 struct iscsi_bhs_data_in *datain_bhs; 2600 2601 TAILQ_FOREACH(pdu, &conn->snack_pdu_list, tailq) { 2602 if (pdu->bhs.opcode == ISCSI_OP_SCSI_DATAIN) { 2603 datain_bhs = (struct iscsi_bhs_data_in *)&pdu->bhs; 2604 if (from_be32(&datain_bhs->ttt) == transfer_tag) { 2605 return pdu->task; 2606 } 2607 } 2608 } 2609 2610 return NULL; 2611 } 2612 2613 /* This function returns the spdk_scsi_task by searching the snack list via 2614 * initiator task tag and the pdu's opcode 2615 */ 2616 static struct spdk_iscsi_task * 2617 get_scsi_task_from_itt(struct spdk_iscsi_conn *conn, 2618 uint32_t task_tag, enum iscsi_op opcode) 2619 { 2620 struct spdk_iscsi_pdu *pdu; 2621 2622 TAILQ_FOREACH(pdu, &conn->snack_pdu_list, tailq) { 2623 if (pdu->bhs.opcode == opcode && 2624 pdu->task != NULL && 2625 pdu->task->tag == task_tag) { 2626 return pdu->task; 2627 } 2628 } 2629 2630 return NULL; 2631 } 2632 2633 static int 2634 iscsi_send_datain(struct spdk_iscsi_conn *conn, 2635 struct spdk_iscsi_task *task, int datain_flag, 2636 int residual_len, int offset, int DataSN, int len) 2637 { 2638 struct spdk_iscsi_pdu *rsp_pdu; 2639 struct iscsi_bhs_data_in *rsph; 2640 uint32_t task_tag; 2641 uint32_t transfer_tag; 2642 int F_bit, U_bit, O_bit, S_bit; 2643 struct spdk_iscsi_task *primary; 2644 2645 primary = spdk_iscsi_task_get_primary(task); 2646 2647 /* DATA PDU */ 2648 rsp_pdu = spdk_get_pdu(); 2649 rsph = (struct iscsi_bhs_data_in *)&rsp_pdu->bhs; 2650 rsp_pdu->data = task->scsi.iovs[0].iov_base + offset; 2651 rsp_pdu->data_buf_len = task->scsi.iovs[0].iov_len - offset; 2652 rsp_pdu->data_from_mempool = true; 2653 2654 task_tag = task->tag; 2655 transfer_tag = 0xffffffffU; 2656 2657 F_bit = datain_flag & ISCSI_FLAG_FINAL; 2658 O_bit = datain_flag & ISCSI_DATAIN_OVERFLOW; 2659 U_bit = datain_flag & ISCSI_DATAIN_UNDERFLOW; 2660 S_bit = datain_flag & ISCSI_DATAIN_STATUS; 2661 2662 /* 2663 * we need to hold onto this task/cmd because until the 2664 * PDU has been written out 2665 */ 2666 rsp_pdu->task = task; 2667 task->scsi.ref++; 2668 2669 rsph->opcode = ISCSI_OP_SCSI_DATAIN; 2670 2671 if (F_bit) { 2672 rsph->flags |= ISCSI_FLAG_FINAL; 2673 } 2674 2675 /* we leave the A_bit clear */ 2676 2677 if (F_bit && S_bit) { 2678 if (O_bit) { 2679 rsph->flags |= ISCSI_DATAIN_OVERFLOW; 2680 } 2681 2682 if (U_bit) { 2683 rsph->flags |= ISCSI_DATAIN_UNDERFLOW; 2684 } 2685 } 2686 2687 if (S_bit) { 2688 rsph->flags |= ISCSI_DATAIN_STATUS; 2689 rsph->status = task->scsi.status; 2690 } 2691 2692 DSET24(rsph->data_segment_len, len); 2693 2694 to_be32(&rsph->itt, task_tag); 2695 to_be32(&rsph->ttt, transfer_tag); 2696 2697 if (S_bit) { 2698 to_be32(&rsph->stat_sn, conn->StatSN); 2699 conn->StatSN++; 2700 } 2701 2702 if (F_bit && S_bit && !spdk_iscsi_task_is_immediate(primary)) { 2703 conn->sess->MaxCmdSN++; 2704 } 2705 2706 to_be32(&rsph->exp_cmd_sn, conn->sess->ExpCmdSN); 2707 to_be32(&rsph->max_cmd_sn, conn->sess->MaxCmdSN); 2708 2709 to_be32(&rsph->data_sn, DataSN); 2710 2711 if (conn->sess->ErrorRecoveryLevel >= 1) { 2712 primary->datain_datasn = DataSN; 2713 } 2714 DataSN++; 2715 2716 if (task->parent) { 2717 offset += primary->scsi.data_transferred; 2718 } 2719 to_be32(&rsph->buffer_offset, (uint32_t)offset); 2720 2721 if (F_bit && S_bit) { 2722 to_be32(&rsph->res_cnt, residual_len); 2723 } 2724 2725 spdk_iscsi_conn_write_pdu(conn, rsp_pdu); 2726 2727 return DataSN; 2728 } 2729 2730 static int 2731 iscsi_transfer_in(struct spdk_iscsi_conn *conn, struct spdk_iscsi_task *task) 2732 { 2733 uint32_t DataSN; 2734 int transfer_len; 2735 int data_len; 2736 int segment_len; 2737 int offset; 2738 int residual_len = 0; 2739 int sent_status; 2740 int len; 2741 int datain_flag = 0; 2742 int datain_seq_cnt; 2743 int i; 2744 int sequence_end; 2745 struct spdk_iscsi_task *primary; 2746 2747 primary = spdk_iscsi_task_get_primary(task); 2748 segment_len = conn->MaxRecvDataSegmentLength; 2749 data_len = task->scsi.data_transferred; 2750 transfer_len = task->scsi.length; 2751 2752 if (task->scsi.status != SPDK_SCSI_STATUS_GOOD) { 2753 if (task != primary) { 2754 conn->data_in_cnt--; 2755 /* Handle the case when primary task return success but the subtask failed */ 2756 if (primary->bytes_completed == primary->scsi.transfer_len && 2757 primary->scsi.status == SPDK_SCSI_STATUS_GOOD) { 2758 conn->data_in_cnt--; 2759 } 2760 } else { 2761 /* handle the case that it is a primary task which has subtasks */ 2762 if (primary->scsi.transfer_len != primary->scsi.length) { 2763 conn->data_in_cnt--; 2764 } 2765 } 2766 2767 return 0; 2768 } 2769 2770 if (data_len < transfer_len) { 2771 /* underflow */ 2772 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "Underflow %u/%u\n", data_len, transfer_len); 2773 residual_len = transfer_len - data_len; 2774 transfer_len = data_len; 2775 datain_flag |= ISCSI_DATAIN_UNDERFLOW; 2776 } else if (data_len > transfer_len) { 2777 /* overflow */ 2778 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "Overflow %u/%u\n", data_len, transfer_len); 2779 residual_len = data_len - transfer_len; 2780 datain_flag |= ISCSI_DATAIN_OVERFLOW; 2781 } else { 2782 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "Transfer %u\n", transfer_len); 2783 residual_len = 0; 2784 } 2785 2786 DataSN = primary->datain_datasn; 2787 sent_status = 0; 2788 2789 /* calculate the number of sequences for all data-in pdus */ 2790 datain_seq_cnt = 1 + ((transfer_len - 1) / (int)conn->sess->MaxBurstLength); 2791 for (i = 0; i < datain_seq_cnt; i++) { 2792 offset = i * conn->sess->MaxBurstLength; 2793 sequence_end = DMIN32(((i + 1) * conn->sess->MaxBurstLength), 2794 transfer_len); 2795 2796 /* send data splitted by segment_len */ 2797 for (; offset < sequence_end; offset += segment_len) { 2798 len = DMIN32(segment_len, (sequence_end - offset)); 2799 2800 datain_flag &= ~ISCSI_FLAG_FINAL; 2801 datain_flag &= ~ISCSI_DATAIN_STATUS; 2802 2803 if (offset + len == sequence_end) { 2804 /* last PDU in a sequence */ 2805 datain_flag |= ISCSI_FLAG_FINAL; 2806 if (task->scsi.sense_data_len == 0) { 2807 /* The last pdu in all data-in pdus */ 2808 if ((offset + len) == transfer_len && 2809 (primary->bytes_completed == primary->scsi.transfer_len)) { 2810 datain_flag |= ISCSI_DATAIN_STATUS; 2811 sent_status = 1; 2812 } 2813 } 2814 } 2815 2816 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "Transfer=%d, Offset=%d, Len=%d\n", 2817 sequence_end, offset, len); 2818 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "StatSN=%u, DataSN=%u, Offset=%u, Len=%d\n", 2819 conn->StatSN, DataSN, offset, len); 2820 2821 DataSN = iscsi_send_datain(conn, task, datain_flag, residual_len, 2822 offset, DataSN, len); 2823 } 2824 } 2825 2826 if (task != primary) { 2827 primary->scsi.data_transferred += task->scsi.data_transferred; 2828 } 2829 primary->datain_datasn = DataSN; 2830 2831 return sent_status; 2832 } 2833 2834 /* 2835 * This function compare the input pdu's bhs with the pdu's bhs associated by 2836 * active_r2t_tasks and queued_r2t_tasks in a connection 2837 */ 2838 static bool 2839 iscsi_compare_pdu_bhs_within_existed_r2t_tasks(struct spdk_iscsi_conn *conn, 2840 struct spdk_iscsi_pdu *pdu) 2841 { 2842 struct spdk_iscsi_task *task; 2843 2844 TAILQ_FOREACH(task, &conn->active_r2t_tasks, link) { 2845 if (!memcmp(&pdu->bhs, spdk_iscsi_task_get_bhs(task), ISCSI_BHS_LEN)) { 2846 return true; 2847 } 2848 } 2849 2850 TAILQ_FOREACH(task, &conn->queued_r2t_tasks, link) { 2851 if (!memcmp(&pdu->bhs, spdk_iscsi_task_get_bhs(task), ISCSI_BHS_LEN)) { 2852 return true; 2853 } 2854 } 2855 2856 return false; 2857 } 2858 2859 static void 2860 iscsi_queue_task(struct spdk_iscsi_conn *conn, struct spdk_iscsi_task *task) 2861 { 2862 spdk_trace_record(TRACE_ISCSI_TASK_QUEUE, conn->id, task->scsi.length, 2863 (uintptr_t)task, (uintptr_t)task->pdu); 2864 task->is_queued = true; 2865 spdk_scsi_dev_queue_task(conn->dev, &task->scsi); 2866 } 2867 2868 static void 2869 iscsi_queue_mgmt_task(struct spdk_iscsi_conn *conn, struct spdk_iscsi_task *task) 2870 { 2871 spdk_scsi_dev_queue_mgmt_task(conn->dev, &task->scsi); 2872 } 2873 2874 int spdk_iscsi_conn_handle_queued_datain_tasks(struct spdk_iscsi_conn *conn) 2875 { 2876 struct spdk_iscsi_task *task; 2877 2878 while (!TAILQ_EMPTY(&conn->queued_datain_tasks) && 2879 conn->data_in_cnt < MAX_LARGE_DATAIN_PER_CONNECTION) { 2880 task = TAILQ_FIRST(&conn->queued_datain_tasks); 2881 assert(task->current_datain_offset <= task->scsi.transfer_len); 2882 2883 if (task->current_datain_offset == 0) { 2884 task->scsi.lun = spdk_scsi_dev_get_lun(conn->dev, task->lun_id); 2885 if (task->scsi.lun == NULL) { 2886 TAILQ_REMOVE(&conn->queued_datain_tasks, task, link); 2887 spdk_scsi_task_process_null_lun(&task->scsi); 2888 spdk_iscsi_task_cpl(&task->scsi); 2889 return 0; 2890 } 2891 task->current_datain_offset = task->scsi.length; 2892 conn->data_in_cnt++; 2893 iscsi_queue_task(conn, task); 2894 continue; 2895 } 2896 if (task->current_datain_offset < task->scsi.transfer_len) { 2897 struct spdk_iscsi_task *subtask; 2898 uint32_t remaining_size = 0; 2899 2900 remaining_size = task->scsi.transfer_len - task->current_datain_offset; 2901 subtask = spdk_iscsi_task_get(conn, task, spdk_iscsi_task_cpl); 2902 assert(subtask != NULL); 2903 subtask->scsi.offset = task->current_datain_offset; 2904 subtask->scsi.length = DMIN32(SPDK_BDEV_LARGE_BUF_MAX_SIZE, remaining_size); 2905 spdk_scsi_task_set_data(&subtask->scsi, NULL, 0); 2906 task->current_datain_offset += subtask->scsi.length; 2907 conn->data_in_cnt++; 2908 2909 task->scsi.lun = spdk_scsi_dev_get_lun(conn->dev, task->lun_id); 2910 if (task->scsi.lun == NULL) { 2911 /* Remove the primary task from the list if this is the last subtask */ 2912 if (task->current_datain_offset == task->scsi.transfer_len) { 2913 TAILQ_REMOVE(&conn->queued_datain_tasks, task, link); 2914 } 2915 subtask->scsi.transfer_len = subtask->scsi.length; 2916 spdk_scsi_task_process_null_lun(&subtask->scsi); 2917 spdk_iscsi_task_cpl(&subtask->scsi); 2918 return 0; 2919 } 2920 2921 iscsi_queue_task(conn, subtask); 2922 } 2923 if (task->current_datain_offset == task->scsi.transfer_len) { 2924 TAILQ_REMOVE(&conn->queued_datain_tasks, task, link); 2925 } 2926 } 2927 return 0; 2928 } 2929 2930 static int 2931 iscsi_op_scsi_read(struct spdk_iscsi_conn *conn, struct spdk_iscsi_task *task) 2932 { 2933 int32_t remaining_size; 2934 2935 TAILQ_INIT(&task->subtask_list); 2936 task->scsi.dxfer_dir = SPDK_SCSI_DIR_FROM_DEV; 2937 task->parent = NULL; 2938 task->scsi.offset = 0; 2939 task->scsi.length = DMIN32(SPDK_BDEV_LARGE_BUF_MAX_SIZE, task->scsi.transfer_len); 2940 spdk_scsi_task_set_data(&task->scsi, NULL, 0); 2941 2942 remaining_size = task->scsi.transfer_len - task->scsi.length; 2943 task->current_datain_offset = 0; 2944 2945 if (remaining_size == 0) { 2946 iscsi_queue_task(conn, task); 2947 return 0; 2948 } 2949 2950 TAILQ_INSERT_TAIL(&conn->queued_datain_tasks, task, link); 2951 2952 return spdk_iscsi_conn_handle_queued_datain_tasks(conn); 2953 } 2954 2955 static int 2956 iscsi_op_scsi(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu) 2957 { 2958 struct spdk_iscsi_task *task; 2959 struct spdk_scsi_dev *dev; 2960 uint8_t *cdb; 2961 uint64_t lun; 2962 uint32_t task_tag; 2963 uint32_t transfer_len; 2964 uint32_t scsi_data_len; 2965 int F_bit, R_bit, W_bit; 2966 int lun_i, rc; 2967 struct iscsi_bhs_scsi_req *reqh; 2968 2969 if (conn->sess->session_type != SESSION_TYPE_NORMAL) { 2970 SPDK_ERRLOG("ISCSI_OP_SCSI not allowed in discovery and invalid session\n"); 2971 return SPDK_ISCSI_CONNECTION_FATAL; 2972 } 2973 2974 reqh = (struct iscsi_bhs_scsi_req *)&pdu->bhs; 2975 2976 F_bit = reqh->final_bit; 2977 R_bit = reqh->read_bit; 2978 W_bit = reqh->write_bit; 2979 lun = from_be64(&reqh->lun); 2980 task_tag = from_be32(&reqh->itt); 2981 transfer_len = from_be32(&reqh->expected_data_xfer_len); 2982 cdb = reqh->cdb; 2983 2984 SPDK_LOGDUMP(SPDK_LOG_ISCSI, "CDB", cdb, 16); 2985 2986 task = spdk_iscsi_task_get(conn, NULL, spdk_iscsi_task_cpl); 2987 if (!task) { 2988 SPDK_ERRLOG("Unable to acquire task\n"); 2989 return SPDK_ISCSI_CONNECTION_FATAL; 2990 } 2991 2992 spdk_iscsi_task_associate_pdu(task, pdu); 2993 lun_i = spdk_scsi_lun_id_fmt_to_int(lun); 2994 task->lun_id = lun_i; 2995 dev = conn->dev; 2996 task->scsi.lun = spdk_scsi_dev_get_lun(dev, lun_i); 2997 2998 if ((R_bit != 0) && (W_bit != 0)) { 2999 SPDK_ERRLOG("Bidirectional CDB is not supported\n"); 3000 spdk_iscsi_task_put(task); 3001 return SPDK_ISCSI_CONNECTION_FATAL; 3002 } 3003 3004 task->scsi.cdb = cdb; 3005 task->tag = task_tag; 3006 task->scsi.transfer_len = transfer_len; 3007 task->scsi.target_port = conn->target_port; 3008 task->scsi.initiator_port = conn->initiator_port; 3009 task->parent = NULL; 3010 task->rsp_scsi_status = SPDK_SCSI_STATUS_GOOD; 3011 3012 if (task->scsi.lun == NULL) { 3013 spdk_scsi_task_process_null_lun(&task->scsi); 3014 spdk_iscsi_task_cpl(&task->scsi); 3015 return 0; 3016 } 3017 3018 /* no bi-directional support */ 3019 if (R_bit) { 3020 return iscsi_op_scsi_read(conn, task); 3021 } else if (W_bit) { 3022 task->scsi.dxfer_dir = SPDK_SCSI_DIR_TO_DEV; 3023 3024 if ((conn->sess->ErrorRecoveryLevel >= 1) && 3025 (iscsi_compare_pdu_bhs_within_existed_r2t_tasks(conn, pdu))) { 3026 spdk_iscsi_task_response(conn, task); 3027 spdk_iscsi_task_put(task); 3028 return 0; 3029 } 3030 3031 if (pdu->data_segment_len > transfer_len) { 3032 SPDK_ERRLOG("data segment len(=%d) > task transfer len(=%d)\n", 3033 (int)pdu->data_segment_len, transfer_len); 3034 spdk_iscsi_task_put(task); 3035 return iscsi_reject(conn, pdu, ISCSI_REASON_PROTOCOL_ERROR); 3036 } 3037 3038 /* check the ImmediateData and also pdu->data_segment_len */ 3039 if ((!conn->sess->ImmediateData && (pdu->data_segment_len > 0)) || 3040 (pdu->data_segment_len > conn->sess->FirstBurstLength)) { 3041 spdk_iscsi_task_put(task); 3042 return iscsi_reject(conn, pdu, ISCSI_REASON_PROTOCOL_ERROR); 3043 } 3044 3045 if (spdk_likely(!pdu->dif_insert_or_strip)) { 3046 scsi_data_len = pdu->data_segment_len; 3047 } else { 3048 scsi_data_len = pdu->data_buf_len; 3049 } 3050 3051 if (F_bit && pdu->data_segment_len < transfer_len) { 3052 /* needs R2T */ 3053 rc = add_transfer_task(conn, task); 3054 if (rc < 0) { 3055 SPDK_ERRLOG("add_transfer_task() failed\n"); 3056 spdk_iscsi_task_put(task); 3057 return SPDK_ISCSI_CONNECTION_FATAL; 3058 } 3059 3060 /* Non-immediate writes */ 3061 if (pdu->data_segment_len == 0) { 3062 return 0; 3063 } else { 3064 /* we are doing the first partial write task */ 3065 task->scsi.ref++; 3066 spdk_scsi_task_set_data(&task->scsi, pdu->data, scsi_data_len); 3067 task->scsi.length = pdu->data_segment_len; 3068 } 3069 } 3070 3071 if (pdu->data_segment_len == transfer_len) { 3072 /* we are doing small writes with no R2T */ 3073 spdk_scsi_task_set_data(&task->scsi, pdu->data, scsi_data_len); 3074 task->scsi.length = transfer_len; 3075 } 3076 } else { 3077 /* neither R nor W bit set */ 3078 task->scsi.dxfer_dir = SPDK_SCSI_DIR_NONE; 3079 if (transfer_len > 0) { 3080 spdk_iscsi_task_put(task); 3081 SPDK_ERRLOG("Reject scsi cmd with EDTL > 0 but (R | W) == 0\n"); 3082 return iscsi_reject(conn, pdu, ISCSI_REASON_INVALID_PDU_FIELD); 3083 } 3084 } 3085 3086 iscsi_queue_task(conn, task); 3087 return 0; 3088 } 3089 3090 static void 3091 abort_transfer_task_in_task_mgmt_resp(struct spdk_iscsi_conn *conn, 3092 struct spdk_iscsi_task *task) 3093 { 3094 struct spdk_iscsi_pdu *pdu; 3095 3096 pdu = spdk_iscsi_task_get_pdu(task); 3097 3098 switch (task->scsi.function) { 3099 /* abort task identified by Reference Task Tag field */ 3100 case ISCSI_TASK_FUNC_ABORT_TASK: 3101 spdk_del_transfer_task(conn, task->scsi.abort_id); 3102 break; 3103 3104 /* abort all tasks issued via this session on the LUN */ 3105 case ISCSI_TASK_FUNC_ABORT_TASK_SET: 3106 spdk_clear_all_transfer_task(conn, task->scsi.lun, pdu); 3107 break; 3108 3109 case ISCSI_TASK_FUNC_LOGICAL_UNIT_RESET: 3110 spdk_clear_all_transfer_task(conn, task->scsi.lun, pdu); 3111 break; 3112 } 3113 } 3114 3115 void 3116 spdk_iscsi_task_mgmt_response(struct spdk_iscsi_conn *conn, 3117 struct spdk_iscsi_task *task) 3118 { 3119 struct spdk_iscsi_pdu *rsp_pdu; 3120 struct iscsi_bhs_task_req *reqh; 3121 struct iscsi_bhs_task_resp *rsph; 3122 3123 if (task->pdu == NULL) { 3124 /* 3125 * This was an internally generated task management command, 3126 * usually from LUN cleanup when a connection closes. 3127 */ 3128 return; 3129 } 3130 3131 reqh = (struct iscsi_bhs_task_req *)&task->pdu->bhs; 3132 /* response PDU */ 3133 rsp_pdu = spdk_get_pdu(); 3134 rsph = (struct iscsi_bhs_task_resp *)&rsp_pdu->bhs; 3135 rsph->opcode = ISCSI_OP_TASK_RSP; 3136 rsph->flags |= 0x80; /* bit 0 default to 1 */ 3137 switch (task->scsi.response) { 3138 case SPDK_SCSI_TASK_MGMT_RESP_COMPLETE: 3139 abort_transfer_task_in_task_mgmt_resp(conn, task); 3140 rsph->response = ISCSI_TASK_FUNC_RESP_COMPLETE; 3141 break; 3142 case SPDK_SCSI_TASK_MGMT_RESP_SUCCESS: 3143 abort_transfer_task_in_task_mgmt_resp(conn, task); 3144 rsph->response = ISCSI_TASK_FUNC_RESP_COMPLETE; 3145 break; 3146 case SPDK_SCSI_TASK_MGMT_RESP_REJECT: 3147 rsph->response = ISCSI_TASK_FUNC_REJECTED; 3148 break; 3149 case SPDK_SCSI_TASK_MGMT_RESP_INVALID_LUN: 3150 rsph->response = ISCSI_TASK_FUNC_RESP_LUN_NOT_EXIST; 3151 break; 3152 case SPDK_SCSI_TASK_MGMT_RESP_TARGET_FAILURE: 3153 rsph->response = ISCSI_TASK_FUNC_REJECTED; 3154 break; 3155 case SPDK_SCSI_TASK_MGMT_RESP_REJECT_FUNC_NOT_SUPPORTED: 3156 rsph->response = ISCSI_TASK_FUNC_RESP_FUNC_NOT_SUPPORTED; 3157 break; 3158 } 3159 rsph->itt = reqh->itt; 3160 3161 to_be32(&rsph->stat_sn, conn->StatSN); 3162 conn->StatSN++; 3163 3164 if (reqh->immediate == 0) { 3165 conn->sess->MaxCmdSN++; 3166 } 3167 3168 to_be32(&rsph->exp_cmd_sn, conn->sess->ExpCmdSN); 3169 to_be32(&rsph->max_cmd_sn, conn->sess->MaxCmdSN); 3170 3171 spdk_iscsi_conn_write_pdu(conn, rsp_pdu); 3172 } 3173 3174 void spdk_iscsi_task_response(struct spdk_iscsi_conn *conn, 3175 struct spdk_iscsi_task *task) 3176 { 3177 struct spdk_iscsi_pdu *rsp_pdu; 3178 struct iscsi_bhs_scsi_resp *rsph; 3179 uint32_t task_tag; 3180 uint32_t transfer_len; 3181 size_t residual_len; 3182 size_t data_len; 3183 int O_bit, U_bit; 3184 int rc; 3185 struct spdk_iscsi_task *primary; 3186 3187 primary = spdk_iscsi_task_get_primary(task); 3188 3189 transfer_len = primary->scsi.transfer_len; 3190 task_tag = task->tag; 3191 3192 /* transfer data from logical unit */ 3193 /* (direction is view of initiator side) */ 3194 if (spdk_iscsi_task_is_read(primary)) { 3195 rc = iscsi_transfer_in(conn, task); 3196 if (rc > 0) { 3197 /* sent status by last DATAIN PDU */ 3198 return; 3199 } 3200 3201 if (primary->bytes_completed != primary->scsi.transfer_len) { 3202 return; 3203 } 3204 } 3205 3206 O_bit = U_bit = 0; 3207 residual_len = 0; 3208 data_len = primary->scsi.data_transferred; 3209 3210 if ((transfer_len != 0) && 3211 (task->scsi.status == SPDK_SCSI_STATUS_GOOD)) { 3212 if (data_len < transfer_len) { 3213 /* underflow */ 3214 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "Underflow %zu/%u\n", data_len, transfer_len); 3215 residual_len = transfer_len - data_len; 3216 U_bit = 1; 3217 } else if (data_len > transfer_len) { 3218 /* overflow */ 3219 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "Overflow %zu/%u\n", data_len, transfer_len); 3220 residual_len = data_len - transfer_len; 3221 O_bit = 1; 3222 } else { 3223 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "Transfer %u\n", transfer_len); 3224 } 3225 } 3226 3227 /* response PDU */ 3228 rsp_pdu = spdk_get_pdu(); 3229 assert(rsp_pdu != NULL); 3230 rsph = (struct iscsi_bhs_scsi_resp *)&rsp_pdu->bhs; 3231 assert(task->scsi.sense_data_len <= sizeof(rsp_pdu->sense.data)); 3232 memcpy(rsp_pdu->sense.data, task->scsi.sense_data, task->scsi.sense_data_len); 3233 to_be16(&rsp_pdu->sense.length, task->scsi.sense_data_len); 3234 rsp_pdu->data = (uint8_t *)&rsp_pdu->sense; 3235 rsp_pdu->data_from_mempool = true; 3236 3237 /* 3238 * we need to hold onto this task/cmd because until the 3239 * PDU has been written out 3240 */ 3241 rsp_pdu->task = task; 3242 task->scsi.ref++; 3243 3244 rsph->opcode = ISCSI_OP_SCSI_RSP; 3245 rsph->flags |= 0x80; /* bit 0 is default to 1 */ 3246 3247 if (O_bit) { 3248 rsph->flags |= ISCSI_SCSI_OVERFLOW; 3249 } 3250 3251 if (U_bit) { 3252 rsph->flags |= ISCSI_SCSI_UNDERFLOW; 3253 } 3254 3255 rsph->status = task->scsi.status; 3256 if (task->scsi.sense_data_len) { 3257 /* SenseLength (2 bytes) + SenseData */ 3258 DSET24(rsph->data_segment_len, 2 + task->scsi.sense_data_len); 3259 } 3260 to_be32(&rsph->itt, task_tag); 3261 3262 to_be32(&rsph->stat_sn, conn->StatSN); 3263 conn->StatSN++; 3264 3265 if (!spdk_iscsi_task_is_immediate(primary)) { 3266 conn->sess->MaxCmdSN++; 3267 } 3268 3269 to_be32(&rsph->exp_cmd_sn, conn->sess->ExpCmdSN); 3270 to_be32(&rsph->max_cmd_sn, conn->sess->MaxCmdSN); 3271 3272 to_be32(&rsph->bi_read_res_cnt, 0); 3273 to_be32(&rsph->res_cnt, residual_len); 3274 3275 spdk_iscsi_conn_write_pdu(conn, rsp_pdu); 3276 } 3277 3278 static struct spdk_iscsi_task * 3279 get_transfer_task(struct spdk_iscsi_conn *conn, uint32_t transfer_tag) 3280 { 3281 int i; 3282 3283 for (i = 0; i < conn->pending_r2t; i++) { 3284 if (conn->outstanding_r2t_tasks[i]->ttt == transfer_tag) { 3285 return (conn->outstanding_r2t_tasks[i]); 3286 } 3287 } 3288 3289 return NULL; 3290 } 3291 3292 static int 3293 _iscsi_conn_abort_queued_datain_task(struct spdk_iscsi_conn *conn, 3294 struct spdk_iscsi_task *task) 3295 { 3296 struct spdk_iscsi_task *subtask; 3297 uint32_t remaining_size; 3298 3299 while (conn->data_in_cnt < MAX_LARGE_DATAIN_PER_CONNECTION) { 3300 assert(task->current_datain_offset <= task->scsi.transfer_len); 3301 3302 /* If no IO is submitted yet, just abort the primary task. */ 3303 if (task->current_datain_offset == 0) { 3304 TAILQ_REMOVE(&conn->queued_datain_tasks, task, link); 3305 spdk_scsi_task_process_abort(&task->scsi); 3306 spdk_iscsi_task_cpl(&task->scsi); 3307 return 0; 3308 } 3309 3310 /* If any IO is submitted already, abort all subtasks by repetition. */ 3311 if (task->current_datain_offset < task->scsi.transfer_len) { 3312 remaining_size = task->scsi.transfer_len - task->current_datain_offset; 3313 subtask = spdk_iscsi_task_get(conn, task, spdk_iscsi_task_cpl); 3314 assert(subtask != NULL); 3315 subtask->scsi.offset = task->current_datain_offset; 3316 subtask->scsi.length = DMIN32(SPDK_BDEV_LARGE_BUF_MAX_SIZE, remaining_size); 3317 spdk_scsi_task_set_data(&subtask->scsi, NULL, 0); 3318 task->current_datain_offset += subtask->scsi.length; 3319 conn->data_in_cnt++; 3320 3321 subtask->scsi.transfer_len = subtask->scsi.length; 3322 spdk_scsi_task_process_abort(&subtask->scsi); 3323 spdk_iscsi_task_cpl(&subtask->scsi); 3324 } 3325 3326 /* Remove the primary task from the list if this is the last subtask */ 3327 if (task->current_datain_offset == task->scsi.transfer_len) { 3328 TAILQ_REMOVE(&conn->queued_datain_tasks, task, link); 3329 return 0; 3330 } 3331 } 3332 3333 return -1; 3334 } 3335 3336 static int 3337 iscsi_conn_abort_queued_datain_task(struct spdk_iscsi_conn *conn, 3338 uint32_t ref_task_tag) 3339 { 3340 struct spdk_iscsi_task *task; 3341 3342 TAILQ_FOREACH(task, &conn->queued_datain_tasks, link) { 3343 if (task->tag == ref_task_tag) { 3344 return _iscsi_conn_abort_queued_datain_task(conn, task); 3345 } 3346 } 3347 3348 return 0; 3349 } 3350 3351 static int 3352 iscsi_conn_abort_queued_datain_tasks(struct spdk_iscsi_conn *conn, 3353 struct spdk_scsi_lun *lun, 3354 struct spdk_iscsi_pdu *pdu) 3355 { 3356 struct spdk_iscsi_task *task, *task_tmp; 3357 struct spdk_iscsi_pdu *pdu_tmp; 3358 int rc; 3359 3360 TAILQ_FOREACH_SAFE(task, &conn->queued_datain_tasks, link, task_tmp) { 3361 pdu_tmp = spdk_iscsi_task_get_pdu(task); 3362 if ((lun == NULL || lun == task->scsi.lun) && 3363 (pdu == NULL || (SN32_LT(pdu_tmp->cmd_sn, pdu->cmd_sn)))) { 3364 rc = _iscsi_conn_abort_queued_datain_task(conn, task); 3365 if (rc != 0) { 3366 return rc; 3367 } 3368 } 3369 } 3370 3371 return 0; 3372 } 3373 3374 static int 3375 _iscsi_op_abort_task(void *arg) 3376 { 3377 struct spdk_iscsi_task *task = arg; 3378 int rc; 3379 3380 rc = iscsi_conn_abort_queued_datain_task(task->conn, task->scsi.abort_id); 3381 if (rc != 0) { 3382 return 1; 3383 } 3384 3385 spdk_poller_unregister(&task->mgmt_poller); 3386 iscsi_queue_mgmt_task(task->conn, task); 3387 return 1; 3388 } 3389 3390 static void 3391 iscsi_op_abort_task(struct spdk_iscsi_task *task, uint32_t ref_task_tag) 3392 { 3393 task->scsi.abort_id = ref_task_tag; 3394 task->scsi.function = SPDK_SCSI_TASK_FUNC_ABORT_TASK; 3395 task->mgmt_poller = spdk_poller_register(_iscsi_op_abort_task, task, 10); 3396 } 3397 3398 static int 3399 _iscsi_op_abort_task_set(void *arg) 3400 { 3401 struct spdk_iscsi_task *task = arg; 3402 int rc; 3403 3404 rc = iscsi_conn_abort_queued_datain_tasks(task->conn, task->scsi.lun, 3405 task->pdu); 3406 if (rc != 0) { 3407 return 1; 3408 } 3409 3410 spdk_poller_unregister(&task->mgmt_poller); 3411 iscsi_queue_mgmt_task(task->conn, task); 3412 return 1; 3413 } 3414 3415 void 3416 spdk_iscsi_op_abort_task_set(struct spdk_iscsi_task *task, uint8_t function) 3417 { 3418 task->scsi.function = function; 3419 task->mgmt_poller = spdk_poller_register(_iscsi_op_abort_task_set, task, 10); 3420 } 3421 3422 static int 3423 iscsi_op_task(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu) 3424 { 3425 struct iscsi_bhs_task_req *reqh; 3426 uint64_t lun; 3427 uint32_t task_tag; 3428 uint32_t ref_task_tag; 3429 uint8_t function; 3430 int lun_i; 3431 struct spdk_iscsi_task *task; 3432 struct spdk_scsi_dev *dev; 3433 3434 if (conn->sess->session_type != SESSION_TYPE_NORMAL) { 3435 SPDK_ERRLOG("ISCSI_OP_TASK not allowed in discovery and invalid session\n"); 3436 return SPDK_ISCSI_CONNECTION_FATAL; 3437 } 3438 3439 reqh = (struct iscsi_bhs_task_req *)&pdu->bhs; 3440 function = reqh->flags & ISCSI_TASK_FUNCTION_MASK; 3441 lun = from_be64(&reqh->lun); 3442 task_tag = from_be32(&reqh->itt); 3443 ref_task_tag = from_be32(&reqh->ref_task_tag); 3444 3445 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "I=%d, func=%d, ITT=%x, ref TT=%x, LUN=0x%16.16"PRIx64"\n", 3446 reqh->immediate, function, task_tag, ref_task_tag, lun); 3447 3448 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "StatSN=%u, ExpCmdSN=%u, MaxCmdSN=%u\n", 3449 conn->StatSN, conn->sess->ExpCmdSN, conn->sess->MaxCmdSN); 3450 3451 lun_i = spdk_scsi_lun_id_fmt_to_int(lun); 3452 dev = conn->dev; 3453 3454 task = spdk_iscsi_task_get(conn, NULL, spdk_iscsi_task_mgmt_cpl); 3455 if (!task) { 3456 SPDK_ERRLOG("Unable to acquire task\n"); 3457 return SPDK_ISCSI_CONNECTION_FATAL; 3458 } 3459 3460 spdk_iscsi_task_associate_pdu(task, pdu); 3461 task->scsi.target_port = conn->target_port; 3462 task->scsi.initiator_port = conn->initiator_port; 3463 task->tag = task_tag; 3464 task->scsi.lun = spdk_scsi_dev_get_lun(dev, lun_i); 3465 3466 if (task->scsi.lun == NULL) { 3467 task->scsi.response = SPDK_SCSI_TASK_MGMT_RESP_INVALID_LUN; 3468 spdk_iscsi_task_mgmt_response(conn, task); 3469 spdk_iscsi_task_put(task); 3470 return 0; 3471 } 3472 3473 switch (function) { 3474 /* abort task identified by Referenced Task Tag field */ 3475 case ISCSI_TASK_FUNC_ABORT_TASK: 3476 SPDK_NOTICELOG("ABORT_TASK\n"); 3477 3478 iscsi_op_abort_task(task, ref_task_tag); 3479 return SPDK_SUCCESS; 3480 3481 /* abort all tasks issued via this session on the LUN */ 3482 case ISCSI_TASK_FUNC_ABORT_TASK_SET: 3483 SPDK_NOTICELOG("ABORT_TASK_SET\n"); 3484 3485 spdk_iscsi_op_abort_task_set(task, SPDK_SCSI_TASK_FUNC_ABORT_TASK_SET); 3486 return SPDK_SUCCESS; 3487 3488 case ISCSI_TASK_FUNC_CLEAR_TASK_SET: 3489 task->scsi.response = SPDK_SCSI_TASK_MGMT_RESP_REJECT_FUNC_NOT_SUPPORTED; 3490 SPDK_NOTICELOG("CLEAR_TASK_SET (Unsupported)\n"); 3491 break; 3492 3493 case ISCSI_TASK_FUNC_CLEAR_ACA: 3494 task->scsi.response = SPDK_SCSI_TASK_MGMT_RESP_REJECT_FUNC_NOT_SUPPORTED; 3495 SPDK_NOTICELOG("CLEAR_ACA (Unsupported)\n"); 3496 break; 3497 3498 case ISCSI_TASK_FUNC_LOGICAL_UNIT_RESET: 3499 SPDK_NOTICELOG("LOGICAL_UNIT_RESET\n"); 3500 3501 spdk_iscsi_op_abort_task_set(task, SPDK_SCSI_TASK_FUNC_LUN_RESET); 3502 return SPDK_SUCCESS; 3503 3504 case ISCSI_TASK_FUNC_TARGET_WARM_RESET: 3505 SPDK_NOTICELOG("TARGET_WARM_RESET (Unsupported)\n"); 3506 3507 #if 0 3508 spdk_iscsi_drop_conns(conn, conn->initiator_name, 1 /* drop all */); 3509 rc = spdk_iscsi_tgt_node_reset(conn->sess->target, lun); 3510 if (rc < 0) { 3511 SPDK_ERRLOG("tgt_node reset failed\n"); 3512 } 3513 #else 3514 task->scsi.response = SPDK_SCSI_TASK_MGMT_RESP_REJECT_FUNC_NOT_SUPPORTED; 3515 #endif 3516 break; 3517 3518 case ISCSI_TASK_FUNC_TARGET_COLD_RESET: 3519 SPDK_NOTICELOG("TARGET_COLD_RESET\n"); 3520 3521 #if 0 3522 spdk_iscsi_drop_conns(conn, conn->initiator_name, 1 /* drop all */); 3523 3524 rc = spdk_iscsi_tgt_node_reset(conn->sess->target, lun); 3525 if (rc < 0) { 3526 SPDK_ERRLOG("tgt_node reset failed\n"); 3527 } 3528 3529 conn->state = ISCSI_CONN_STATE_EXITING; 3530 #else 3531 task->scsi.response = SPDK_SCSI_TASK_MGMT_RESP_REJECT_FUNC_NOT_SUPPORTED; 3532 #endif 3533 break; 3534 3535 case ISCSI_TASK_FUNC_TASK_REASSIGN: 3536 SPDK_NOTICELOG("TASK_REASSIGN (Unsupported)\n"); 3537 task->scsi.response = SPDK_SCSI_TASK_MGMT_RESP_REJECT_FUNC_NOT_SUPPORTED; 3538 break; 3539 3540 default: 3541 SPDK_ERRLOG("unsupported function %d\n", function); 3542 task->scsi.response = SPDK_SCSI_TASK_MGMT_RESP_REJECT; 3543 break; 3544 } 3545 3546 spdk_iscsi_task_mgmt_response(conn, task); 3547 spdk_iscsi_task_put(task); 3548 return 0; 3549 } 3550 3551 static int 3552 iscsi_op_nopout(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu) 3553 { 3554 struct spdk_iscsi_pdu *rsp_pdu; 3555 struct iscsi_bhs_nop_out *reqh; 3556 struct iscsi_bhs_nop_in *rsph; 3557 uint8_t *data; 3558 uint64_t lun; 3559 uint32_t task_tag; 3560 uint32_t transfer_tag; 3561 uint32_t CmdSN; 3562 int I_bit; 3563 int data_len; 3564 3565 if (conn->sess->session_type == SESSION_TYPE_DISCOVERY) { 3566 SPDK_ERRLOG("ISCSI_OP_NOPOUT not allowed in discovery session\n"); 3567 return SPDK_ISCSI_CONNECTION_FATAL; 3568 } 3569 3570 reqh = (struct iscsi_bhs_nop_out *)&pdu->bhs; 3571 I_bit = reqh->immediate; 3572 3573 data_len = DGET24(reqh->data_segment_len); 3574 if (data_len > conn->MaxRecvDataSegmentLength) { 3575 data_len = conn->MaxRecvDataSegmentLength; 3576 } 3577 3578 lun = from_be64(&reqh->lun); 3579 task_tag = from_be32(&reqh->itt); 3580 transfer_tag = from_be32(&reqh->ttt); 3581 CmdSN = from_be32(&reqh->cmd_sn); 3582 pdu->cmd_sn = CmdSN; 3583 3584 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "I=%d, ITT=%x, TTT=%x\n", 3585 I_bit, task_tag, transfer_tag); 3586 3587 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "CmdSN=%u, StatSN=%u, ExpCmdSN=%u, MaxCmdSN=%u\n", 3588 CmdSN, conn->StatSN, conn->sess->ExpCmdSN, 3589 conn->sess->MaxCmdSN); 3590 3591 if (transfer_tag != 0xFFFFFFFF && transfer_tag != (uint32_t)conn->id) { 3592 SPDK_ERRLOG("invalid transfer tag 0x%x\n", transfer_tag); 3593 /* 3594 * Technically we should probably fail the connection here, but for now 3595 * just print the error message and continue. 3596 */ 3597 } 3598 3599 /* 3600 * We don't actually check to see if this is a response to the NOP-In 3601 * that we sent. Our goal is to just verify that the initiator is 3602 * alive and responding to commands, not to verify that it tags 3603 * NOP-Outs correctly 3604 */ 3605 conn->nop_outstanding = false; 3606 3607 if (task_tag == 0xffffffffU) { 3608 if (I_bit == 1) { 3609 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "got NOPOUT ITT=0xffffffff\n"); 3610 return SPDK_SUCCESS; 3611 } else { 3612 SPDK_ERRLOG("got NOPOUT ITT=0xffffffff, I=0\n"); 3613 return SPDK_ISCSI_CONNECTION_FATAL; 3614 } 3615 } 3616 3617 data = calloc(1, data_len); 3618 if (!data) { 3619 SPDK_ERRLOG("calloc() failed for ping data\n"); 3620 return SPDK_ISCSI_CONNECTION_FATAL; 3621 } 3622 3623 /* response of NOPOUT */ 3624 if (data_len > 0) { 3625 /* copy ping data */ 3626 memcpy(data, pdu->data, data_len); 3627 } 3628 3629 transfer_tag = 0xffffffffU; 3630 3631 /* response PDU */ 3632 rsp_pdu = spdk_get_pdu(); 3633 if (rsp_pdu == NULL) { 3634 free(data); 3635 return SPDK_ISCSI_CONNECTION_FATAL; 3636 } 3637 rsph = (struct iscsi_bhs_nop_in *)&rsp_pdu->bhs; 3638 rsp_pdu->data = data; 3639 rsph->opcode = ISCSI_OP_NOPIN; 3640 rsph->flags |= 0x80; /* bit 0 default to 1 */ 3641 DSET24(rsph->data_segment_len, data_len); 3642 to_be64(&rsph->lun, lun); 3643 to_be32(&rsph->itt, task_tag); 3644 to_be32(&rsph->ttt, transfer_tag); 3645 3646 to_be32(&rsph->stat_sn, conn->StatSN); 3647 conn->StatSN++; 3648 3649 if (I_bit == 0) { 3650 conn->sess->MaxCmdSN++; 3651 } 3652 3653 to_be32(&rsph->exp_cmd_sn, conn->sess->ExpCmdSN); 3654 to_be32(&rsph->max_cmd_sn, conn->sess->MaxCmdSN); 3655 3656 spdk_iscsi_conn_write_pdu(conn, rsp_pdu); 3657 conn->last_nopin = spdk_get_ticks(); 3658 3659 return SPDK_SUCCESS; 3660 } 3661 3662 static int 3663 add_transfer_task(struct spdk_iscsi_conn *conn, struct spdk_iscsi_task *task) 3664 { 3665 uint32_t transfer_len; 3666 size_t max_burst_len; 3667 size_t segment_len; 3668 size_t data_len; 3669 int len; 3670 int idx; 3671 int rc; 3672 int data_out_req; 3673 3674 transfer_len = task->scsi.transfer_len; 3675 data_len = spdk_iscsi_task_get_pdu(task)->data_segment_len; 3676 max_burst_len = conn->sess->MaxBurstLength; 3677 segment_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH; 3678 data_out_req = 1 + (transfer_len - data_len - 1) / segment_len; 3679 task->data_out_cnt = data_out_req; 3680 3681 /* 3682 * If we already have too many tasks using R2T, then queue this task 3683 * and start sending R2T for it after some of the tasks using R2T/data 3684 * out buffers complete. 3685 */ 3686 if (conn->pending_r2t >= DEFAULT_MAXR2T) { 3687 TAILQ_INSERT_TAIL(&conn->queued_r2t_tasks, task, link); 3688 return SPDK_SUCCESS; 3689 } 3690 3691 conn->data_out_cnt += data_out_req; 3692 idx = conn->pending_r2t++; 3693 3694 conn->outstanding_r2t_tasks[idx] = task; 3695 task->next_expected_r2t_offset = data_len; 3696 task->current_r2t_length = 0; 3697 task->R2TSN = 0; 3698 /* According to RFC3720 10.8.5, 0xffffffff is 3699 * reserved for TTT in R2T. 3700 */ 3701 if (++conn->ttt == 0xffffffffu) { 3702 conn->ttt = 0; 3703 } 3704 task->ttt = conn->ttt; 3705 3706 while (data_len != transfer_len) { 3707 len = DMIN32(max_burst_len, (transfer_len - data_len)); 3708 rc = iscsi_send_r2t(conn, task, data_len, len, 3709 task->ttt, &task->R2TSN); 3710 if (rc < 0) { 3711 SPDK_ERRLOG("iscsi_send_r2t() failed\n"); 3712 return rc; 3713 } 3714 data_len += len; 3715 task->next_r2t_offset = data_len; 3716 task->outstanding_r2t++; 3717 if (conn->sess->MaxOutstandingR2T == task->outstanding_r2t) { 3718 break; 3719 } 3720 } 3721 3722 TAILQ_INSERT_TAIL(&conn->active_r2t_tasks, task, link); 3723 return SPDK_SUCCESS; 3724 } 3725 3726 /* If there are additional large writes queued for R2Ts, start them now. 3727 * This is called when a large write is just completed or when multiple LUNs 3728 * are attached and large write tasks for the specific LUN are cleared. 3729 */ 3730 static void 3731 start_queued_transfer_tasks(struct spdk_iscsi_conn *conn) 3732 { 3733 struct spdk_iscsi_task *task, *tmp; 3734 3735 TAILQ_FOREACH_SAFE(task, &conn->queued_r2t_tasks, link, tmp) { 3736 if (conn->pending_r2t < DEFAULT_MAXR2T) { 3737 TAILQ_REMOVE(&conn->queued_r2t_tasks, task, link); 3738 add_transfer_task(conn, task); 3739 } else { 3740 break; 3741 } 3742 } 3743 } 3744 3745 void spdk_del_transfer_task(struct spdk_iscsi_conn *conn, uint32_t task_tag) 3746 { 3747 struct spdk_iscsi_task *task; 3748 int i; 3749 3750 for (i = 0; i < conn->pending_r2t; i++) { 3751 if (conn->outstanding_r2t_tasks[i]->tag == task_tag) { 3752 task = conn->outstanding_r2t_tasks[i]; 3753 conn->data_out_cnt -= task->data_out_cnt; 3754 3755 conn->pending_r2t--; 3756 for (; i < conn->pending_r2t; i++) { 3757 conn->outstanding_r2t_tasks[i] = conn->outstanding_r2t_tasks[i + 1]; 3758 } 3759 conn->outstanding_r2t_tasks[conn->pending_r2t] = NULL; 3760 break; 3761 } 3762 } 3763 3764 start_queued_transfer_tasks(conn); 3765 } 3766 3767 static void 3768 del_connection_queued_task(struct spdk_iscsi_conn *conn, void *tailq, 3769 struct spdk_scsi_lun *lun, 3770 struct spdk_iscsi_pdu *pdu) 3771 { 3772 struct spdk_iscsi_task *task, *task_tmp; 3773 struct spdk_iscsi_pdu *pdu_tmp; 3774 3775 /* 3776 * Temporary used to index spdk_scsi_task related 3777 * queues of the connection. 3778 */ 3779 TAILQ_HEAD(queued_tasks, spdk_iscsi_task) *head; 3780 head = (struct queued_tasks *)tailq; 3781 3782 TAILQ_FOREACH_SAFE(task, head, link, task_tmp) { 3783 pdu_tmp = spdk_iscsi_task_get_pdu(task); 3784 if ((lun == NULL || lun == task->scsi.lun) && 3785 (pdu == NULL || SN32_LT(pdu_tmp->cmd_sn, pdu->cmd_sn))) { 3786 TAILQ_REMOVE(head, task, link); 3787 if (lun != NULL && spdk_scsi_lun_is_removing(lun)) { 3788 spdk_scsi_task_process_null_lun(&task->scsi); 3789 spdk_iscsi_task_response(conn, task); 3790 } 3791 spdk_iscsi_task_put(task); 3792 } 3793 } 3794 } 3795 3796 void spdk_clear_all_transfer_task(struct spdk_iscsi_conn *conn, 3797 struct spdk_scsi_lun *lun, 3798 struct spdk_iscsi_pdu *pdu) 3799 { 3800 int i, j, pending_r2t; 3801 struct spdk_iscsi_task *task; 3802 struct spdk_iscsi_pdu *pdu_tmp; 3803 3804 pending_r2t = conn->pending_r2t; 3805 for (i = 0; i < pending_r2t; i++) { 3806 task = conn->outstanding_r2t_tasks[i]; 3807 pdu_tmp = spdk_iscsi_task_get_pdu(task); 3808 if ((lun == NULL || lun == task->scsi.lun) && 3809 (pdu == NULL || SN32_LT(pdu_tmp->cmd_sn, pdu->cmd_sn))) { 3810 conn->outstanding_r2t_tasks[i] = NULL; 3811 task->outstanding_r2t = 0; 3812 task->next_r2t_offset = 0; 3813 task->next_expected_r2t_offset = 0; 3814 conn->data_out_cnt -= task->data_out_cnt; 3815 conn->pending_r2t--; 3816 } 3817 } 3818 3819 for (i = 0; i < pending_r2t; i++) { 3820 if (conn->outstanding_r2t_tasks[i] != NULL) { 3821 continue; 3822 } 3823 for (j = i + 1; j < pending_r2t; j++) { 3824 if (conn->outstanding_r2t_tasks[j] != NULL) { 3825 conn->outstanding_r2t_tasks[i] = conn->outstanding_r2t_tasks[j]; 3826 conn->outstanding_r2t_tasks[j] = NULL; 3827 break; 3828 } 3829 } 3830 } 3831 3832 del_connection_queued_task(conn, &conn->active_r2t_tasks, lun, pdu); 3833 del_connection_queued_task(conn, &conn->queued_r2t_tasks, lun, pdu); 3834 3835 start_queued_transfer_tasks(conn); 3836 } 3837 3838 /* This function is used to handle the r2t snack */ 3839 static int 3840 iscsi_handle_r2t_snack(struct spdk_iscsi_conn *conn, 3841 struct spdk_iscsi_task *task, 3842 struct spdk_iscsi_pdu *pdu, uint32_t beg_run, 3843 uint32_t run_length, int32_t task_tag) 3844 { 3845 int32_t last_r2tsn; 3846 int i; 3847 3848 if (beg_run < task->acked_r2tsn) { 3849 SPDK_ERRLOG("ITT: 0x%08x, R2T SNACK requests retransmission of" 3850 "R2TSN: from 0x%08x to 0x%08x. But it has already" 3851 "ack to R2TSN:0x%08x, protocol error.\n", 3852 task_tag, beg_run, (beg_run + run_length), 3853 (task->acked_r2tsn - 1)); 3854 return iscsi_reject(conn, pdu, ISCSI_REASON_PROTOCOL_ERROR); 3855 } 3856 3857 if (run_length) { 3858 if ((beg_run + run_length) > task->R2TSN) { 3859 SPDK_ERRLOG("ITT: 0x%08x, received R2T SNACK with" 3860 "BegRun: 0x%08x, RunLength: 0x%08x, exceeds" 3861 "current R2TSN: 0x%08x, protocol error.\n", 3862 task_tag, beg_run, run_length, 3863 task->R2TSN); 3864 3865 return iscsi_reject(conn, pdu, ISCSI_REASON_INVALID_PDU_FIELD); 3866 } 3867 last_r2tsn = (beg_run + run_length); 3868 } else { 3869 last_r2tsn = task->R2TSN; 3870 } 3871 3872 for (i = beg_run; i < last_r2tsn; i++) { 3873 if (iscsi_send_r2t_recovery(conn, task, i, false) < 0) { 3874 SPDK_ERRLOG("The r2t_sn=%d of r2t_task=%p is not sent\n", i, task); 3875 } 3876 } 3877 return 0; 3878 } 3879 3880 /* This function is used to recover the data in packet */ 3881 static int 3882 iscsi_handle_recovery_datain(struct spdk_iscsi_conn *conn, 3883 struct spdk_iscsi_task *task, 3884 struct spdk_iscsi_pdu *pdu, uint32_t beg_run, 3885 uint32_t run_length, uint32_t task_tag) 3886 { 3887 struct spdk_iscsi_pdu *old_pdu, *pdu_temp; 3888 uint32_t i; 3889 struct iscsi_bhs_data_in *datain_header; 3890 uint32_t last_statsn; 3891 3892 task = spdk_iscsi_task_get_primary(task); 3893 3894 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "iscsi_handle_recovery_datain\n"); 3895 3896 if (beg_run < task->acked_data_sn) { 3897 SPDK_ERRLOG("ITT: 0x%08x, DATA IN SNACK requests retransmission of" 3898 "DATASN: from 0x%08x to 0x%08x but already acked to " 3899 "DATASN: 0x%08x protocol error\n", 3900 task_tag, beg_run, 3901 (beg_run + run_length), (task->acked_data_sn - 1)); 3902 3903 return iscsi_reject(conn, pdu, ISCSI_REASON_PROTOCOL_ERROR); 3904 } 3905 3906 if (run_length == 0) { 3907 /* as the DataSN begins at 0 */ 3908 run_length = task->datain_datasn + 1; 3909 } 3910 3911 if ((beg_run + run_length - 1) > task->datain_datasn) { 3912 SPDK_ERRLOG("Initiator requests BegRun: 0x%08x, RunLength:" 3913 "0x%08x greater than maximum DataSN: 0x%08x.\n", 3914 beg_run, run_length, task->datain_datasn); 3915 3916 return -1; 3917 } else { 3918 last_statsn = beg_run + run_length - 1; 3919 } 3920 3921 for (i = beg_run; i <= last_statsn; i++) { 3922 TAILQ_FOREACH_SAFE(old_pdu, &conn->snack_pdu_list, tailq, pdu_temp) { 3923 if (old_pdu->bhs.opcode == ISCSI_OP_SCSI_DATAIN) { 3924 datain_header = (struct iscsi_bhs_data_in *)&old_pdu->bhs; 3925 if (from_be32(&datain_header->itt) == task_tag && 3926 from_be32(&datain_header->data_sn) == i) { 3927 TAILQ_REMOVE(&conn->snack_pdu_list, old_pdu, tailq); 3928 spdk_iscsi_conn_write_pdu(conn, old_pdu); 3929 break; 3930 } 3931 } 3932 } 3933 } 3934 return 0; 3935 } 3936 3937 /* This function is used to handle the status snack */ 3938 static int 3939 iscsi_handle_status_snack(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu) 3940 { 3941 uint32_t beg_run; 3942 uint32_t run_length; 3943 struct iscsi_bhs_snack_req *reqh; 3944 uint32_t i; 3945 uint32_t last_statsn; 3946 bool found_pdu; 3947 struct spdk_iscsi_pdu *old_pdu; 3948 3949 reqh = (struct iscsi_bhs_snack_req *)&pdu->bhs; 3950 beg_run = from_be32(&reqh->beg_run); 3951 run_length = from_be32(&reqh->run_len); 3952 3953 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "beg_run=%d, run_length=%d, conn->StatSN=" 3954 "%d, conn->exp_statsn=%d\n", beg_run, run_length, 3955 conn->StatSN, conn->exp_statsn); 3956 3957 if (!beg_run) { 3958 beg_run = conn->exp_statsn; 3959 } else if (beg_run < conn->exp_statsn) { 3960 SPDK_ERRLOG("Got Status SNACK Begrun: 0x%08x, RunLength: 0x%08x " 3961 "but already got ExpStatSN: 0x%08x on CID:%hu.\n", 3962 beg_run, run_length, conn->StatSN, conn->cid); 3963 3964 return iscsi_reject(conn, pdu, ISCSI_REASON_INVALID_PDU_FIELD); 3965 } 3966 3967 last_statsn = (!run_length) ? conn->StatSN : (beg_run + run_length); 3968 3969 for (i = beg_run; i < last_statsn; i++) { 3970 found_pdu = false; 3971 TAILQ_FOREACH(old_pdu, &conn->snack_pdu_list, tailq) { 3972 if (from_be32(&old_pdu->bhs.stat_sn) == i) { 3973 found_pdu = true; 3974 break; 3975 } 3976 } 3977 3978 if (!found_pdu) { 3979 SPDK_ERRLOG("Unable to find StatSN: 0x%08x. For a Status" 3980 "SNACK, assuming this is a proactive SNACK " 3981 "for an untransmitted StatSN, ignoring.\n", 3982 beg_run); 3983 } else { 3984 TAILQ_REMOVE(&conn->snack_pdu_list, old_pdu, tailq); 3985 spdk_iscsi_conn_write_pdu(conn, old_pdu); 3986 } 3987 } 3988 3989 return 0; 3990 } 3991 3992 /* This function is used to handle the data ack snack */ 3993 static int 3994 iscsi_handle_data_ack(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu) 3995 { 3996 uint32_t transfer_tag; 3997 uint32_t beg_run; 3998 uint32_t run_length; 3999 struct spdk_iscsi_pdu *old_pdu; 4000 uint32_t old_datasn; 4001 struct iscsi_bhs_snack_req *reqh; 4002 struct spdk_iscsi_task *task; 4003 struct iscsi_bhs_data_in *datain_header; 4004 struct spdk_iscsi_task *primary; 4005 4006 reqh = (struct iscsi_bhs_snack_req *)&pdu->bhs; 4007 transfer_tag = from_be32(&reqh->ttt); 4008 beg_run = from_be32(&reqh->beg_run); 4009 run_length = from_be32(&reqh->run_len); 4010 task = NULL; 4011 datain_header = NULL; 4012 4013 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "beg_run=%d,transfer_tag=%d,run_len=%d\n", 4014 beg_run, transfer_tag, run_length); 4015 4016 task = get_scsi_task_from_ttt(conn, transfer_tag); 4017 if (!task) { 4018 SPDK_ERRLOG("Data ACK SNACK for TTT: 0x%08x is invalid.\n", 4019 transfer_tag); 4020 goto reject_return; 4021 } 4022 4023 primary = spdk_iscsi_task_get_primary(task); 4024 if ((run_length != 0) || (beg_run < primary->acked_data_sn)) { 4025 SPDK_ERRLOG("TTT: 0x%08x Data ACK SNACK BegRUN: %d is less than " 4026 "the next expected acked DataSN: %d\n", 4027 transfer_tag, beg_run, primary->acked_data_sn); 4028 goto reject_return; 4029 } 4030 4031 primary->acked_data_sn = beg_run; 4032 4033 /* To free the pdu */ 4034 TAILQ_FOREACH(old_pdu, &conn->snack_pdu_list, tailq) { 4035 if (old_pdu->bhs.opcode == ISCSI_OP_SCSI_DATAIN) { 4036 datain_header = (struct iscsi_bhs_data_in *) &old_pdu->bhs; 4037 old_datasn = from_be32(&datain_header->data_sn); 4038 if ((from_be32(&datain_header->ttt) == transfer_tag) && 4039 (old_datasn == beg_run - 1)) { 4040 TAILQ_REMOVE(&conn->snack_pdu_list, old_pdu, tailq); 4041 if (old_pdu->task) { 4042 spdk_iscsi_task_put(old_pdu->task); 4043 } 4044 spdk_put_pdu(old_pdu); 4045 break; 4046 } 4047 } 4048 } 4049 4050 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "Received Data ACK SNACK for TTT: 0x%08x," 4051 " updated acked DataSN to 0x%08x.\n", transfer_tag, 4052 (task->acked_data_sn - 1)); 4053 4054 return 0; 4055 4056 reject_return: 4057 return iscsi_reject(conn, pdu, ISCSI_REASON_INVALID_SNACK); 4058 } 4059 4060 /* This function is used to remove the r2t pdu from snack_pdu_list by < task, r2t_sn> info */ 4061 static struct spdk_iscsi_pdu * 4062 iscsi_remove_r2t_pdu_from_snack_list(struct spdk_iscsi_conn *conn, 4063 struct spdk_iscsi_task *task, 4064 uint32_t r2t_sn) 4065 { 4066 struct spdk_iscsi_pdu *pdu; 4067 struct iscsi_bhs_r2t *r2t_header; 4068 4069 TAILQ_FOREACH(pdu, &conn->snack_pdu_list, tailq) { 4070 if (pdu->bhs.opcode == ISCSI_OP_R2T) { 4071 r2t_header = (struct iscsi_bhs_r2t *)&pdu->bhs; 4072 if (pdu->task == task && 4073 from_be32(&r2t_header->r2t_sn) == r2t_sn) { 4074 TAILQ_REMOVE(&conn->snack_pdu_list, pdu, tailq); 4075 return pdu; 4076 } 4077 } 4078 } 4079 4080 return NULL; 4081 } 4082 4083 /* This function is used re-send the r2t packet */ 4084 static int 4085 iscsi_send_r2t_recovery(struct spdk_iscsi_conn *conn, 4086 struct spdk_iscsi_task *task, uint32_t r2t_sn, 4087 bool send_new_r2tsn) 4088 { 4089 struct spdk_iscsi_pdu *pdu; 4090 struct iscsi_bhs_r2t *rsph; 4091 uint32_t transfer_len; 4092 uint32_t len; 4093 int rc; 4094 4095 /* remove the r2t pdu from the snack_list */ 4096 pdu = iscsi_remove_r2t_pdu_from_snack_list(conn, task, r2t_sn); 4097 if (!pdu) { 4098 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "No pdu is found\n"); 4099 return -1; 4100 } 4101 4102 /* flag 4103 * false: only need to re-send the old r2t with changing statsn 4104 * true: we send a r2t with new r2tsn 4105 */ 4106 if (!send_new_r2tsn) { 4107 to_be32(&pdu->bhs.stat_sn, conn->StatSN); 4108 spdk_iscsi_conn_write_pdu(conn, pdu); 4109 } else { 4110 rsph = (struct iscsi_bhs_r2t *)&pdu->bhs; 4111 transfer_len = from_be32(&rsph->desired_xfer_len); 4112 4113 /* still need to increase the acked r2tsn */ 4114 task->acked_r2tsn++; 4115 len = DMIN32(conn->sess->MaxBurstLength, (transfer_len - 4116 task->next_expected_r2t_offset)); 4117 4118 /* remove the old_r2t_pdu */ 4119 if (pdu->task) { 4120 spdk_iscsi_task_put(pdu->task); 4121 } 4122 spdk_put_pdu(pdu); 4123 4124 /* re-send a new r2t pdu */ 4125 rc = iscsi_send_r2t(conn, task, task->next_expected_r2t_offset, 4126 len, task->ttt, &task->R2TSN); 4127 if (rc < 0) { 4128 return SPDK_ISCSI_CONNECTION_FATAL; 4129 } 4130 } 4131 4132 return 0; 4133 } 4134 4135 /* This function is used to handle the snack request from the initiator */ 4136 static int 4137 iscsi_op_snack(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu) 4138 { 4139 struct iscsi_bhs_snack_req *reqh; 4140 struct spdk_iscsi_task *task; 4141 int type; 4142 uint32_t task_tag; 4143 uint32_t beg_run; 4144 uint32_t run_length; 4145 int rc; 4146 4147 if (conn->sess->session_type == SESSION_TYPE_DISCOVERY) { 4148 SPDK_ERRLOG("ISCSI_OP_SNACK not allowed in discovery session\n"); 4149 return SPDK_ISCSI_CONNECTION_FATAL; 4150 } 4151 4152 reqh = (struct iscsi_bhs_snack_req *)&pdu->bhs; 4153 if (!conn->sess->ErrorRecoveryLevel) { 4154 SPDK_ERRLOG("Got a SNACK request in ErrorRecoveryLevel=0\n"); 4155 return iscsi_reject(conn, pdu, ISCSI_REASON_PROTOCOL_ERROR); 4156 } 4157 4158 type = reqh->flags & ISCSI_FLAG_SNACK_TYPE_MASK; 4159 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "The value of type is %d\n", type); 4160 4161 switch (type) { 4162 case 0: 4163 reqh = (struct iscsi_bhs_snack_req *)&pdu->bhs; 4164 task_tag = from_be32(&reqh->itt); 4165 beg_run = from_be32(&reqh->beg_run); 4166 run_length = from_be32(&reqh->run_len); 4167 4168 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "beg_run=%d, run_length=%d, " 4169 "task_tag=%x, transfer_tag=%u\n", beg_run, 4170 run_length, task_tag, from_be32(&reqh->ttt)); 4171 4172 task = get_scsi_task_from_itt(conn, task_tag, 4173 ISCSI_OP_SCSI_DATAIN); 4174 if (task) { 4175 return iscsi_handle_recovery_datain(conn, task, pdu, 4176 beg_run, run_length, task_tag); 4177 } 4178 task = get_scsi_task_from_itt(conn, task_tag, ISCSI_OP_R2T); 4179 if (task) { 4180 return iscsi_handle_r2t_snack(conn, task, pdu, beg_run, 4181 run_length, task_tag); 4182 } 4183 SPDK_ERRLOG("It is Neither datain nor r2t recovery request\n"); 4184 rc = -1; 4185 break; 4186 case ISCSI_FLAG_SNACK_TYPE_STATUS: 4187 rc = iscsi_handle_status_snack(conn, pdu); 4188 break; 4189 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK: 4190 rc = iscsi_handle_data_ack(conn, pdu); 4191 break; 4192 case ISCSI_FLAG_SNACK_TYPE_RDATA: 4193 SPDK_ERRLOG("R-Data SNACK is Not Supported int spdk\n"); 4194 rc = iscsi_reject(conn, pdu, ISCSI_REASON_PROTOCOL_ERROR); 4195 break; 4196 default: 4197 SPDK_ERRLOG("Unknown SNACK type %d, protocol error\n", type); 4198 rc = iscsi_reject(conn, pdu, ISCSI_REASON_PROTOCOL_ERROR); 4199 break; 4200 } 4201 4202 return rc; 4203 } 4204 4205 /* This function is used to refree the pdu when it is acknowledged */ 4206 static void 4207 remove_acked_pdu(struct spdk_iscsi_conn *conn, uint32_t ExpStatSN) 4208 { 4209 struct spdk_iscsi_pdu *pdu, *pdu_temp; 4210 uint32_t stat_sn; 4211 4212 conn->exp_statsn = DMIN32(ExpStatSN, conn->StatSN); 4213 TAILQ_FOREACH_SAFE(pdu, &conn->snack_pdu_list, tailq, pdu_temp) { 4214 stat_sn = from_be32(&pdu->bhs.stat_sn); 4215 if (SN32_LT(stat_sn, conn->exp_statsn)) { 4216 TAILQ_REMOVE(&conn->snack_pdu_list, pdu, tailq); 4217 spdk_iscsi_conn_free_pdu(conn, pdu); 4218 } 4219 } 4220 } 4221 4222 static int 4223 iscsi_op_data(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu) 4224 { 4225 struct spdk_iscsi_task *task, *subtask; 4226 struct iscsi_bhs_data_out *reqh; 4227 struct spdk_scsi_lun *lun_dev; 4228 uint32_t transfer_tag; 4229 uint32_t task_tag; 4230 uint32_t transfer_len; 4231 uint32_t DataSN; 4232 uint32_t buffer_offset; 4233 uint32_t len; 4234 int F_bit; 4235 int rc; 4236 int reject_reason = ISCSI_REASON_INVALID_PDU_FIELD; 4237 4238 if (conn->sess->session_type == SESSION_TYPE_DISCOVERY) { 4239 SPDK_ERRLOG("ISCSI_OP_SCSI_DATAOUT not allowed in discovery session\n"); 4240 return SPDK_ISCSI_CONNECTION_FATAL; 4241 } 4242 4243 reqh = (struct iscsi_bhs_data_out *)&pdu->bhs; 4244 F_bit = !!(reqh->flags & ISCSI_FLAG_FINAL); 4245 transfer_tag = from_be32(&reqh->ttt); 4246 task_tag = from_be32(&reqh->itt); 4247 DataSN = from_be32(&reqh->data_sn); 4248 buffer_offset = from_be32(&reqh->buffer_offset); 4249 4250 task = get_transfer_task(conn, transfer_tag); 4251 if (task == NULL) { 4252 SPDK_ERRLOG("Not found task for transfer_tag=%x\n", transfer_tag); 4253 goto reject_return; 4254 } 4255 4256 lun_dev = spdk_scsi_dev_get_lun(conn->dev, task->lun_id); 4257 4258 if (pdu->data_segment_len > task->desired_data_transfer_length) { 4259 SPDK_ERRLOG("the dataout pdu data length is larger than the value sent by R2T PDU\n"); 4260 return SPDK_ISCSI_CONNECTION_FATAL; 4261 } 4262 4263 if (task->tag != task_tag) { 4264 SPDK_ERRLOG("The r2t task tag is %u, and the dataout task tag is %u\n", 4265 task->tag, task_tag); 4266 goto reject_return; 4267 } 4268 4269 if (DataSN != task->r2t_datasn) { 4270 SPDK_ERRLOG("DataSN(%u) exp=%d error\n", DataSN, task->r2t_datasn); 4271 if (conn->sess->ErrorRecoveryLevel >= 1) { 4272 goto send_r2t_recovery_return; 4273 } else { 4274 reject_reason = ISCSI_REASON_PROTOCOL_ERROR; 4275 goto reject_return; 4276 } 4277 } 4278 4279 if (buffer_offset != task->next_expected_r2t_offset) { 4280 SPDK_ERRLOG("offset(%u) error\n", buffer_offset); 4281 return SPDK_ISCSI_CONNECTION_FATAL; 4282 } 4283 4284 transfer_len = task->scsi.transfer_len; 4285 task->current_r2t_length += pdu->data_segment_len; 4286 task->next_expected_r2t_offset += pdu->data_segment_len; 4287 task->r2t_datasn++; 4288 4289 if (task->current_r2t_length > conn->sess->MaxBurstLength) { 4290 SPDK_ERRLOG("R2T burst(%u) > MaxBurstLength(%u)\n", 4291 task->current_r2t_length, 4292 conn->sess->MaxBurstLength); 4293 return SPDK_ISCSI_CONNECTION_FATAL; 4294 } 4295 4296 if (F_bit) { 4297 /* 4298 * This R2T burst is done. Clear the length before we 4299 * receive a PDU for the next R2T burst. 4300 */ 4301 task->current_r2t_length = 0; 4302 } 4303 4304 subtask = spdk_iscsi_task_get(conn, task, spdk_iscsi_task_cpl); 4305 if (subtask == NULL) { 4306 SPDK_ERRLOG("Unable to acquire subtask\n"); 4307 return SPDK_ISCSI_CONNECTION_FATAL; 4308 } 4309 subtask->scsi.offset = buffer_offset; 4310 subtask->scsi.length = pdu->data_segment_len; 4311 if (spdk_likely(!pdu->dif_insert_or_strip)) { 4312 spdk_scsi_task_set_data(&subtask->scsi, pdu->data, pdu->data_segment_len); 4313 } else { 4314 spdk_scsi_task_set_data(&subtask->scsi, pdu->data, pdu->data_buf_len); 4315 } 4316 spdk_iscsi_task_associate_pdu(subtask, pdu); 4317 4318 if (task->next_expected_r2t_offset == transfer_len) { 4319 task->acked_r2tsn++; 4320 } else if (F_bit && (task->next_r2t_offset < transfer_len)) { 4321 task->acked_r2tsn++; 4322 len = DMIN32(conn->sess->MaxBurstLength, (transfer_len - 4323 task->next_r2t_offset)); 4324 rc = iscsi_send_r2t(conn, task, task->next_r2t_offset, len, 4325 task->ttt, &task->R2TSN); 4326 if (rc < 0) { 4327 SPDK_ERRLOG("iscsi_send_r2t() failed\n"); 4328 } 4329 task->next_r2t_offset += len; 4330 } 4331 4332 if (lun_dev == NULL) { 4333 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "LUN %d is removed, complete the task immediately\n", 4334 task->lun_id); 4335 subtask->scsi.transfer_len = subtask->scsi.length; 4336 spdk_scsi_task_process_null_lun(&subtask->scsi); 4337 spdk_iscsi_task_cpl(&subtask->scsi); 4338 return 0; 4339 } 4340 4341 iscsi_queue_task(conn, subtask); 4342 return 0; 4343 4344 send_r2t_recovery_return: 4345 rc = iscsi_send_r2t_recovery(conn, task, task->acked_r2tsn, true); 4346 if (rc == 0) { 4347 return 0; 4348 } 4349 4350 reject_return: 4351 return iscsi_reject(conn, pdu, reject_reason); 4352 } 4353 4354 static int 4355 iscsi_send_r2t(struct spdk_iscsi_conn *conn, 4356 struct spdk_iscsi_task *task, int offset, 4357 int len, uint32_t transfer_tag, uint32_t *R2TSN) 4358 { 4359 struct spdk_iscsi_pdu *rsp_pdu; 4360 struct iscsi_bhs_r2t *rsph; 4361 uint64_t fmt_lun; 4362 4363 /* R2T PDU */ 4364 rsp_pdu = spdk_get_pdu(); 4365 if (rsp_pdu == NULL) { 4366 return SPDK_ISCSI_CONNECTION_FATAL; 4367 } 4368 rsph = (struct iscsi_bhs_r2t *)&rsp_pdu->bhs; 4369 rsp_pdu->data = NULL; 4370 rsph->opcode = ISCSI_OP_R2T; 4371 rsph->flags |= 0x80; /* bit 0 is default to 1 */ 4372 fmt_lun = spdk_scsi_lun_id_int_to_fmt(task->lun_id); 4373 to_be64(&rsph->lun, fmt_lun); 4374 to_be32(&rsph->itt, task->tag); 4375 to_be32(&rsph->ttt, transfer_tag); 4376 4377 to_be32(&rsph->stat_sn, conn->StatSN); 4378 to_be32(&rsph->exp_cmd_sn, conn->sess->ExpCmdSN); 4379 to_be32(&rsph->max_cmd_sn, conn->sess->MaxCmdSN); 4380 4381 to_be32(&rsph->r2t_sn, *R2TSN); 4382 *R2TSN += 1; 4383 4384 task->r2t_datasn = 0; /* next expected datasn to ack */ 4385 4386 to_be32(&rsph->buffer_offset, (uint32_t)offset); 4387 to_be32(&rsph->desired_xfer_len, (uint32_t)len); 4388 task->desired_data_transfer_length = (size_t)len; 4389 4390 /* we need to hold onto this task/cmd because until the PDU has been 4391 * written out */ 4392 rsp_pdu->task = task; 4393 task->scsi.ref++; 4394 4395 spdk_iscsi_conn_write_pdu(conn, rsp_pdu); 4396 4397 return SPDK_SUCCESS; 4398 } 4399 4400 void spdk_iscsi_send_nopin(struct spdk_iscsi_conn *conn) 4401 { 4402 struct spdk_iscsi_pdu *rsp_pdu; 4403 struct iscsi_bhs_nop_in *rsp; 4404 4405 /* Only send nopin if we have logged in and are in a normal session. */ 4406 if (conn->sess == NULL || 4407 !conn->full_feature || 4408 !spdk_iscsi_param_eq_val(conn->sess->params, "SessionType", "Normal")) { 4409 return; 4410 } 4411 4412 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "send NOPIN isid=%"PRIx64", tsih=%u, cid=%u\n", 4413 conn->sess->isid, conn->sess->tsih, conn->cid); 4414 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "StatSN=%u, ExpCmdSN=%u, MaxCmdSN=%u\n", 4415 conn->StatSN, conn->sess->ExpCmdSN, 4416 conn->sess->MaxCmdSN); 4417 4418 rsp_pdu = spdk_get_pdu(); 4419 rsp = (struct iscsi_bhs_nop_in *) &rsp_pdu->bhs; 4420 rsp_pdu->data = NULL; 4421 4422 /* 4423 * spdk_get_pdu() memset's the PDU for us, so only fill out the needed 4424 * fields. 4425 */ 4426 rsp->opcode = ISCSI_OP_NOPIN; 4427 rsp->flags = 0x80; 4428 /* 4429 * Technically the to_be32() is not needed here, since 4430 * to_be32(0xFFFFFFFU) returns 0xFFFFFFFFU. 4431 */ 4432 to_be32(&rsp->itt, 0xFFFFFFFFU); 4433 to_be32(&rsp->ttt, conn->id); 4434 to_be32(&rsp->stat_sn, conn->StatSN); 4435 to_be32(&rsp->exp_cmd_sn, conn->sess->ExpCmdSN); 4436 to_be32(&rsp->max_cmd_sn, conn->sess->MaxCmdSN); 4437 4438 spdk_iscsi_conn_write_pdu(conn, rsp_pdu); 4439 conn->last_nopin = spdk_get_ticks(); 4440 conn->nop_outstanding = true; 4441 } 4442 4443 static void 4444 init_login_reject_response(struct spdk_iscsi_pdu *pdu, struct spdk_iscsi_pdu *rsp_pdu) 4445 { 4446 struct iscsi_bhs_login_rsp *rsph; 4447 4448 memset(rsp_pdu, 0, sizeof(struct spdk_iscsi_pdu)); 4449 rsph = (struct iscsi_bhs_login_rsp *)&rsp_pdu->bhs; 4450 rsph->version_max = ISCSI_VERSION; 4451 rsph->version_act = ISCSI_VERSION; 4452 rsph->opcode = ISCSI_OP_LOGIN_RSP; 4453 rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR; 4454 rsph->status_detail = ISCSI_LOGIN_INVALID_LOGIN_REQUEST; 4455 rsph->itt = pdu->bhs.itt; 4456 } 4457 4458 int 4459 spdk_iscsi_execute(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu) 4460 { 4461 int opcode; 4462 int rc; 4463 struct spdk_iscsi_pdu *rsp_pdu = NULL; 4464 uint32_t ExpStatSN; 4465 int I_bit; 4466 struct spdk_iscsi_sess *sess; 4467 struct iscsi_bhs_scsi_req *reqh; 4468 4469 if (pdu == NULL) { 4470 return -1; 4471 } 4472 4473 opcode = pdu->bhs.opcode; 4474 reqh = (struct iscsi_bhs_scsi_req *)&pdu->bhs; 4475 pdu->cmd_sn = from_be32(&reqh->cmd_sn); 4476 4477 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "opcode %x\n", opcode); 4478 4479 if (opcode == ISCSI_OP_LOGIN) { 4480 rc = iscsi_op_login(conn, pdu); 4481 if (rc < 0) { 4482 SPDK_ERRLOG("iscsi_op_login() failed\n"); 4483 } 4484 return rc; 4485 } 4486 4487 /* connection in login phase but receive non-login opcode 4488 * return response code 0x020b to initiator. 4489 * */ 4490 if (!conn->full_feature && conn->state == ISCSI_CONN_STATE_RUNNING) { 4491 rsp_pdu = spdk_get_pdu(); 4492 if (rsp_pdu == NULL) { 4493 return SPDK_ISCSI_CONNECTION_FATAL; 4494 } 4495 init_login_reject_response(pdu, rsp_pdu); 4496 spdk_iscsi_conn_write_pdu(conn, rsp_pdu); 4497 SPDK_ERRLOG("Received opcode %d in login phase\n", opcode); 4498 return SPDK_ISCSI_LOGIN_ERROR_RESPONSE; 4499 } else if (conn->state == ISCSI_CONN_STATE_INVALID) { 4500 SPDK_ERRLOG("before Full Feature\n"); 4501 return SPDK_ISCSI_CONNECTION_FATAL; 4502 } 4503 4504 sess = conn->sess; 4505 if (!sess) { 4506 SPDK_ERRLOG("Connection has no associated session!\n"); 4507 return SPDK_ISCSI_CONNECTION_FATAL; 4508 } 4509 I_bit = reqh->immediate; 4510 if (I_bit == 0) { 4511 if (SN32_LT(pdu->cmd_sn, sess->ExpCmdSN) || 4512 SN32_GT(pdu->cmd_sn, sess->MaxCmdSN)) { 4513 if (sess->session_type == SESSION_TYPE_NORMAL && 4514 opcode != ISCSI_OP_SCSI_DATAOUT) { 4515 SPDK_ERRLOG("CmdSN(%u) ignore (ExpCmdSN=%u, MaxCmdSN=%u)\n", 4516 pdu->cmd_sn, sess->ExpCmdSN, sess->MaxCmdSN); 4517 4518 if (sess->ErrorRecoveryLevel >= 1) { 4519 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "Skip the error in ERL 1 and 2\n"); 4520 } else { 4521 return SPDK_PDU_FATAL; 4522 } 4523 } 4524 } 4525 } else if (pdu->cmd_sn != sess->ExpCmdSN) { 4526 SPDK_ERRLOG("CmdSN(%u) error ExpCmdSN=%u\n", pdu->cmd_sn, sess->ExpCmdSN); 4527 4528 if (sess->ErrorRecoveryLevel >= 1) { 4529 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "Skip the error in ERL 1 and 2\n"); 4530 } else if (opcode != ISCSI_OP_NOPOUT) { 4531 /* 4532 * The Linux initiator does not send valid CmdSNs for 4533 * nopout under heavy load, so do not close the 4534 * connection in that case. 4535 */ 4536 return SPDK_ISCSI_CONNECTION_FATAL; 4537 } 4538 } 4539 4540 ExpStatSN = from_be32(&reqh->exp_stat_sn); 4541 if (SN32_GT(ExpStatSN, conn->StatSN)) { 4542 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "StatSN(%u) advanced\n", ExpStatSN); 4543 ExpStatSN = conn->StatSN; 4544 } 4545 4546 if (sess->ErrorRecoveryLevel >= 1) { 4547 remove_acked_pdu(conn, ExpStatSN); 4548 } 4549 4550 if (!I_bit && opcode != ISCSI_OP_SCSI_DATAOUT) { 4551 sess->ExpCmdSN++; 4552 } 4553 4554 switch (opcode) { 4555 case ISCSI_OP_NOPOUT: 4556 rc = iscsi_op_nopout(conn, pdu); 4557 if (rc < 0) { 4558 SPDK_ERRLOG("spdk_iscsi_op_nopout() failed\n"); 4559 return rc; 4560 } 4561 break; 4562 4563 case ISCSI_OP_SCSI: 4564 rc = iscsi_op_scsi(conn, pdu); 4565 if (rc < 0) { 4566 SPDK_ERRLOG("spdk_iscsi_op_scsi() failed\n"); 4567 return rc; 4568 } 4569 break; 4570 case ISCSI_OP_TASK: 4571 rc = iscsi_op_task(conn, pdu); 4572 if (rc < 0) { 4573 SPDK_ERRLOG("spdk_iscsi_op_task() failed\n"); 4574 return rc; 4575 } 4576 break; 4577 4578 case ISCSI_OP_TEXT: 4579 rc = iscsi_op_text(conn, pdu); 4580 if (rc < 0) { 4581 SPDK_ERRLOG("spdk_iscsi_op_text() failed\n"); 4582 return rc; 4583 } 4584 break; 4585 4586 case ISCSI_OP_LOGOUT: 4587 rc = iscsi_op_logout(conn, pdu); 4588 if (rc < 0) { 4589 SPDK_ERRLOG("spdk_iscsi_op_logout() failed\n"); 4590 return rc; 4591 } 4592 break; 4593 4594 case ISCSI_OP_SCSI_DATAOUT: 4595 rc = iscsi_op_data(conn, pdu); 4596 if (rc < 0) { 4597 SPDK_ERRLOG("spdk_iscsi_op_data() failed\n"); 4598 return rc; 4599 } 4600 break; 4601 4602 case ISCSI_OP_SNACK: 4603 rc = iscsi_op_snack(conn, pdu); 4604 if (rc < 0) { 4605 SPDK_ERRLOG("spdk_iscsi_op_snack() failed\n"); 4606 return rc; 4607 } 4608 break; 4609 4610 default: 4611 SPDK_ERRLOG("unsupported opcode %x\n", opcode); 4612 return iscsi_reject(conn, pdu, ISCSI_REASON_PROTOCOL_ERROR); 4613 } 4614 4615 return 0; 4616 } 4617 4618 bool 4619 spdk_iscsi_get_dif_ctx(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu, 4620 struct spdk_dif_ctx *dif_ctx) 4621 { 4622 struct iscsi_bhs *bhs; 4623 uint32_t offset = 0; 4624 uint8_t *cdb = NULL; 4625 uint64_t lun; 4626 int lun_id = 0; 4627 struct spdk_scsi_lun *lun_dev; 4628 4629 /* connection is not in full feature phase but non-login opcode 4630 * was received. 4631 */ 4632 if ((!conn->full_feature && conn->state == ISCSI_CONN_STATE_RUNNING) || 4633 conn->state == ISCSI_CONN_STATE_INVALID) { 4634 return false; 4635 } 4636 4637 /* SCSI Command is allowed only in normal session */ 4638 if (conn->sess == NULL || 4639 conn->sess->session_type != SESSION_TYPE_NORMAL) { 4640 return false; 4641 } 4642 4643 bhs = &pdu->bhs; 4644 4645 switch (bhs->opcode) { 4646 case ISCSI_OP_SCSI: { 4647 struct iscsi_bhs_scsi_req *sbhs; 4648 4649 sbhs = (struct iscsi_bhs_scsi_req *)bhs; 4650 offset = 0; 4651 cdb = sbhs->cdb; 4652 lun = from_be64(&sbhs->lun); 4653 lun_id = spdk_scsi_lun_id_fmt_to_int(lun); 4654 break; 4655 } 4656 case ISCSI_OP_SCSI_DATAOUT: { 4657 struct iscsi_bhs_data_out *dbhs; 4658 struct spdk_iscsi_task *task; 4659 int transfer_tag; 4660 4661 dbhs = (struct iscsi_bhs_data_out *)bhs; 4662 offset = from_be32(&dbhs->buffer_offset); 4663 transfer_tag = from_be32(&dbhs->ttt); 4664 task = get_transfer_task(conn, transfer_tag); 4665 if (task == NULL) { 4666 return false; 4667 } 4668 cdb = task->scsi.cdb; 4669 lun_id = task->lun_id; 4670 break; 4671 } 4672 case ISCSI_OP_SCSI_DATAIN: { 4673 struct iscsi_bhs_data_in *dbhs; 4674 struct spdk_iscsi_task *task; 4675 4676 dbhs = (struct iscsi_bhs_data_in *)bhs; 4677 offset = from_be32(&dbhs->buffer_offset); 4678 task = pdu->task; 4679 assert(task != NULL); 4680 cdb = task->scsi.cdb; 4681 lun_id = task->lun_id; 4682 break; 4683 } 4684 default: 4685 return false; 4686 } 4687 4688 lun_dev = spdk_scsi_dev_get_lun(conn->dev, lun_id); 4689 if (lun_dev == NULL) { 4690 return false; 4691 } 4692 4693 return spdk_scsi_lun_get_dif_ctx(lun_dev, cdb, offset, dif_ctx); 4694 } 4695 4696 void spdk_free_sess(struct spdk_iscsi_sess *sess) 4697 { 4698 if (sess == NULL) { 4699 return; 4700 } 4701 4702 sess->tag = 0; 4703 sess->target = NULL; 4704 sess->session_type = SESSION_TYPE_INVALID; 4705 spdk_iscsi_param_free(sess->params); 4706 free(sess->conns); 4707 spdk_scsi_port_free(&sess->initiator_port); 4708 spdk_mempool_put(g_spdk_iscsi.session_pool, (void *)sess); 4709 } 4710 4711 static int 4712 create_iscsi_sess(struct spdk_iscsi_conn *conn, 4713 struct spdk_iscsi_tgt_node *target, 4714 enum session_type session_type) 4715 { 4716 struct spdk_iscsi_sess *sess; 4717 int rc; 4718 4719 sess = spdk_mempool_get(g_spdk_iscsi.session_pool); 4720 if (!sess) { 4721 SPDK_ERRLOG("Unable to get session object\n"); 4722 SPDK_ERRLOG("MaxSessions set to %d\n", g_spdk_iscsi.MaxSessions); 4723 return -ENOMEM; 4724 } 4725 4726 /* configuration values */ 4727 pthread_mutex_lock(&g_spdk_iscsi.mutex); 4728 4729 sess->MaxConnections = g_spdk_iscsi.MaxConnectionsPerSession; 4730 sess->MaxOutstandingR2T = DEFAULT_MAXOUTSTANDINGR2T; 4731 4732 sess->DefaultTime2Wait = g_spdk_iscsi.DefaultTime2Wait; 4733 sess->DefaultTime2Retain = g_spdk_iscsi.DefaultTime2Retain; 4734 sess->FirstBurstLength = g_spdk_iscsi.FirstBurstLength; 4735 sess->MaxBurstLength = SPDK_ISCSI_MAX_BURST_LENGTH; 4736 sess->InitialR2T = DEFAULT_INITIALR2T; 4737 sess->ImmediateData = g_spdk_iscsi.ImmediateData; 4738 sess->DataPDUInOrder = DEFAULT_DATAPDUINORDER; 4739 sess->DataSequenceInOrder = DEFAULT_DATASEQUENCEINORDER; 4740 sess->ErrorRecoveryLevel = g_spdk_iscsi.ErrorRecoveryLevel; 4741 4742 pthread_mutex_unlock(&g_spdk_iscsi.mutex); 4743 4744 sess->tag = conn->portal->group->tag; 4745 4746 sess->conns = calloc(sess->MaxConnections, sizeof(*sess->conns)); 4747 if (!sess->conns) { 4748 SPDK_ERRLOG("calloc() failed for connection array\n"); 4749 return -ENOMEM; 4750 } 4751 4752 sess->connections = 0; 4753 4754 sess->conns[sess->connections] = conn; 4755 sess->connections++; 4756 4757 sess->params = NULL; 4758 sess->target = target; 4759 sess->isid = 0; 4760 sess->session_type = session_type; 4761 sess->current_text_itt = 0xffffffffU; 4762 4763 /* set default params */ 4764 rc = spdk_iscsi_sess_params_init(&sess->params); 4765 if (rc < 0) { 4766 SPDK_ERRLOG("iscsi_sess_params_init() failed\n"); 4767 goto error_return; 4768 } 4769 /* replace with config value */ 4770 rc = spdk_iscsi_param_set_int(sess->params, "MaxConnections", 4771 sess->MaxConnections); 4772 if (rc < 0) { 4773 SPDK_ERRLOG("iscsi_param_set_int() failed\n"); 4774 goto error_return; 4775 } 4776 4777 rc = spdk_iscsi_param_set_int(sess->params, "MaxOutstandingR2T", 4778 sess->MaxOutstandingR2T); 4779 if (rc < 0) { 4780 SPDK_ERRLOG("iscsi_param_set_int() failed\n"); 4781 goto error_return; 4782 } 4783 4784 rc = spdk_iscsi_param_set_int(sess->params, "DefaultTime2Wait", 4785 sess->DefaultTime2Wait); 4786 if (rc < 0) { 4787 SPDK_ERRLOG("iscsi_param_set_int() failed\n"); 4788 goto error_return; 4789 } 4790 4791 rc = spdk_iscsi_param_set_int(sess->params, "DefaultTime2Retain", 4792 sess->DefaultTime2Retain); 4793 if (rc < 0) { 4794 SPDK_ERRLOG("iscsi_param_set_int() failed\n"); 4795 goto error_return; 4796 } 4797 4798 rc = spdk_iscsi_param_set_int(sess->params, "FirstBurstLength", 4799 sess->FirstBurstLength); 4800 if (rc < 0) { 4801 SPDK_ERRLOG("iscsi_param_set_int() failed\n"); 4802 goto error_return; 4803 } 4804 4805 rc = spdk_iscsi_param_set_int(sess->params, "MaxBurstLength", 4806 sess->MaxBurstLength); 4807 if (rc < 0) { 4808 SPDK_ERRLOG("iscsi_param_set_int() failed\n"); 4809 goto error_return; 4810 } 4811 4812 rc = spdk_iscsi_param_set(sess->params, "InitialR2T", 4813 sess->InitialR2T ? "Yes" : "No"); 4814 if (rc < 0) { 4815 SPDK_ERRLOG("iscsi_param_set() failed\n"); 4816 goto error_return; 4817 } 4818 4819 rc = spdk_iscsi_param_set(sess->params, "ImmediateData", 4820 sess->ImmediateData ? "Yes" : "No"); 4821 if (rc < 0) { 4822 SPDK_ERRLOG("iscsi_param_set() failed\n"); 4823 goto error_return; 4824 } 4825 4826 rc = spdk_iscsi_param_set(sess->params, "DataPDUInOrder", 4827 sess->DataPDUInOrder ? "Yes" : "No"); 4828 if (rc < 0) { 4829 SPDK_ERRLOG("iscsi_param_set() failed\n"); 4830 goto error_return; 4831 } 4832 4833 rc = spdk_iscsi_param_set(sess->params, "DataSequenceInOrder", 4834 sess->DataSequenceInOrder ? "Yes" : "No"); 4835 if (rc < 0) { 4836 SPDK_ERRLOG("iscsi_param_set() failed\n"); 4837 goto error_return; 4838 } 4839 4840 rc = spdk_iscsi_param_set_int(sess->params, "ErrorRecoveryLevel", 4841 sess->ErrorRecoveryLevel); 4842 if (rc < 0) { 4843 SPDK_ERRLOG("iscsi_param_set_int() failed\n"); 4844 goto error_return; 4845 } 4846 4847 /* realloc buffer */ 4848 rc = spdk_iscsi_param_set_int(conn->params, "MaxRecvDataSegmentLength", 4849 conn->MaxRecvDataSegmentLength); 4850 if (rc < 0) { 4851 SPDK_ERRLOG("iscsi_param_set_int() failed\n"); 4852 goto error_return; 4853 } 4854 4855 /* sess for first connection of session */ 4856 conn->sess = sess; 4857 return 0; 4858 4859 error_return: 4860 spdk_free_sess(sess); 4861 conn->sess = NULL; 4862 return -1; 4863 } 4864 4865 static struct spdk_iscsi_sess * 4866 get_iscsi_sess_by_tsih(uint16_t tsih) 4867 { 4868 struct spdk_iscsi_sess *session; 4869 4870 if (tsih == 0 || tsih > g_spdk_iscsi.MaxSessions) { 4871 return NULL; 4872 } 4873 4874 session = g_spdk_iscsi.session[tsih - 1]; 4875 assert(tsih == session->tsih); 4876 4877 return session; 4878 } 4879 4880 static int 4881 append_iscsi_sess(struct spdk_iscsi_conn *conn, 4882 const char *initiator_port_name, uint16_t tsih, uint16_t cid) 4883 { 4884 struct spdk_iscsi_sess *sess; 4885 4886 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "append session: init port name=%s, tsih=%u, cid=%u\n", 4887 initiator_port_name, tsih, cid); 4888 4889 sess = get_iscsi_sess_by_tsih(tsih); 4890 if (sess == NULL) { 4891 SPDK_ERRLOG("spdk_get_iscsi_sess_by_tsih failed\n"); 4892 return -1; 4893 } 4894 if ((conn->portal->group->tag != sess->tag) || 4895 (strcasecmp(initiator_port_name, spdk_scsi_port_get_name(sess->initiator_port)) != 0) || 4896 (conn->target != sess->target)) { 4897 /* no match */ 4898 SPDK_ERRLOG("no MCS session for init port name=%s, tsih=%d, cid=%d\n", 4899 initiator_port_name, tsih, cid); 4900 return -1; 4901 } 4902 4903 if (sess->connections >= sess->MaxConnections) { 4904 /* no slot for connection */ 4905 SPDK_ERRLOG("too many connections for init port name=%s, tsih=%d, cid=%d\n", 4906 initiator_port_name, tsih, cid); 4907 return -1; 4908 } 4909 4910 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "Connections (tsih %d): %d\n", sess->tsih, sess->connections); 4911 conn->sess = sess; 4912 4913 /* 4914 * TODO: need a mutex or other sync mechanism to protect the session's 4915 * connection list. 4916 */ 4917 sess->conns[sess->connections] = conn; 4918 sess->connections++; 4919 4920 return 0; 4921 } 4922 4923 bool spdk_iscsi_is_deferred_free_pdu(struct spdk_iscsi_pdu *pdu) 4924 { 4925 if (pdu == NULL) { 4926 return false; 4927 } 4928 4929 if (pdu->bhs.opcode == ISCSI_OP_R2T || 4930 pdu->bhs.opcode == ISCSI_OP_SCSI_DATAIN) { 4931 return true; 4932 } 4933 4934 return false; 4935 } 4936