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