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