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