1 /*- 2 * BSD LICENSE 3 * 4 * Copyright (C) 2008-2012 Daisuke Aoyama <aoyama@peach.ne.jp>. 5 * Copyright (c) Intel Corporation. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * * Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * * Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * * Neither the name of Intel Corporation nor the names of its 19 * contributors may be used to endorse or promote products derived 20 * from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 #include "spdk/stdinc.h" 36 37 #include "spdk/endian.h" 38 #include "spdk/env.h" 39 #include "spdk/likely.h" 40 #include "spdk/thread.h" 41 #include "spdk/queue.h" 42 #include "spdk/trace.h" 43 #include "spdk/net.h" 44 #include "spdk/sock.h" 45 #include "spdk/string.h" 46 47 #include "spdk/log.h" 48 49 #include "iscsi/task.h" 50 #include "iscsi/conn.h" 51 #include "iscsi/tgt_node.h" 52 #include "iscsi/portal_grp.h" 53 54 #define MAKE_DIGEST_WORD(BUF, CRC32C) \ 55 ( ((*((uint8_t *)(BUF)+0)) = (uint8_t)((uint32_t)(CRC32C) >> 0)), \ 56 ((*((uint8_t *)(BUF)+1)) = (uint8_t)((uint32_t)(CRC32C) >> 8)), \ 57 ((*((uint8_t *)(BUF)+2)) = (uint8_t)((uint32_t)(CRC32C) >> 16)), \ 58 ((*((uint8_t *)(BUF)+3)) = (uint8_t)((uint32_t)(CRC32C) >> 24))) 59 60 #define SPDK_ISCSI_CONNECTION_MEMSET(conn) \ 61 memset(&(conn)->portal, 0, sizeof(*(conn)) - \ 62 offsetof(struct spdk_iscsi_conn, portal)); 63 64 #define SPDK_ISCSI_CONNECTION_STATUS(status, rnstr) case(status): return(rnstr) 65 66 static struct spdk_iscsi_conn *g_conns_array = NULL; 67 68 static TAILQ_HEAD(, spdk_iscsi_conn) g_free_conns = TAILQ_HEAD_INITIALIZER(g_free_conns); 69 static TAILQ_HEAD(, spdk_iscsi_conn) g_active_conns = TAILQ_HEAD_INITIALIZER(g_active_conns); 70 71 static pthread_mutex_t g_conns_mutex = PTHREAD_MUTEX_INITIALIZER; 72 73 static struct spdk_poller *g_shutdown_timer = NULL; 74 75 static void iscsi_conn_sock_cb(void *arg, struct spdk_sock_group *group, 76 struct spdk_sock *sock); 77 78 static struct spdk_iscsi_conn * 79 allocate_conn(void) 80 { 81 struct spdk_iscsi_conn *conn; 82 83 pthread_mutex_lock(&g_conns_mutex); 84 conn = TAILQ_FIRST(&g_free_conns); 85 if (conn != NULL) { 86 assert(!conn->is_valid); 87 TAILQ_REMOVE(&g_free_conns, conn, conn_link); 88 SPDK_ISCSI_CONNECTION_MEMSET(conn); 89 conn->is_valid = 1; 90 91 TAILQ_INSERT_TAIL(&g_active_conns, conn, conn_link); 92 } 93 pthread_mutex_unlock(&g_conns_mutex); 94 95 return conn; 96 } 97 98 static void 99 _free_conn(struct spdk_iscsi_conn *conn) 100 { 101 TAILQ_REMOVE(&g_active_conns, conn, conn_link); 102 103 memset(conn->portal_host, 0, sizeof(conn->portal_host)); 104 memset(conn->portal_port, 0, sizeof(conn->portal_port)); 105 conn->is_valid = 0; 106 107 TAILQ_INSERT_TAIL(&g_free_conns, conn, conn_link); 108 } 109 110 static void 111 free_conn(struct spdk_iscsi_conn *conn) 112 { 113 pthread_mutex_lock(&g_conns_mutex); 114 _free_conn(conn); 115 pthread_mutex_unlock(&g_conns_mutex); 116 } 117 118 static void 119 _iscsi_conns_cleanup(void) 120 { 121 free(g_conns_array); 122 } 123 124 int initialize_iscsi_conns(void) 125 { 126 uint32_t i; 127 128 SPDK_DEBUGLOG(iscsi, "spdk_iscsi_init\n"); 129 130 g_conns_array = calloc(MAX_ISCSI_CONNECTIONS, sizeof(struct spdk_iscsi_conn)); 131 if (g_conns_array == NULL) { 132 return -ENOMEM; 133 } 134 135 for (i = 0; i < MAX_ISCSI_CONNECTIONS; i++) { 136 g_conns_array[i].id = i; 137 TAILQ_INSERT_TAIL(&g_free_conns, &g_conns_array[i], conn_link); 138 } 139 140 return 0; 141 } 142 143 static void 144 iscsi_poll_group_add_conn(struct spdk_iscsi_poll_group *pg, struct spdk_iscsi_conn *conn) 145 { 146 int rc; 147 148 rc = spdk_sock_group_add_sock(pg->sock_group, conn->sock, iscsi_conn_sock_cb, conn); 149 if (rc < 0) { 150 SPDK_ERRLOG("Failed to add sock=%p of conn=%p\n", conn->sock, conn); 151 return; 152 } 153 154 conn->is_stopped = false; 155 STAILQ_INSERT_TAIL(&pg->connections, conn, pg_link); 156 } 157 158 static void 159 iscsi_poll_group_remove_conn(struct spdk_iscsi_poll_group *pg, struct spdk_iscsi_conn *conn) 160 { 161 int rc; 162 163 assert(conn->sock != NULL); 164 rc = spdk_sock_group_remove_sock(pg->sock_group, conn->sock); 165 if (rc < 0) { 166 SPDK_ERRLOG("Failed to remove sock=%p of conn=%p\n", conn->sock, conn); 167 } 168 169 conn->is_stopped = true; 170 STAILQ_REMOVE(&pg->connections, conn, spdk_iscsi_conn, pg_link); 171 } 172 173 static int 174 login_timeout(void *arg) 175 { 176 struct spdk_iscsi_conn *conn = arg; 177 178 if (conn->state < ISCSI_CONN_STATE_EXITING) { 179 conn->state = ISCSI_CONN_STATE_EXITING; 180 } 181 spdk_poller_unregister(&conn->login_timer); 182 183 return SPDK_POLLER_BUSY; 184 } 185 186 static void 187 iscsi_conn_start(void *ctx) 188 { 189 struct spdk_iscsi_conn *conn = ctx; 190 191 iscsi_poll_group_add_conn(conn->pg, conn); 192 193 conn->login_timer = SPDK_POLLER_REGISTER(login_timeout, conn, ISCSI_LOGIN_TIMEOUT * 1000000); 194 } 195 196 int 197 iscsi_conn_construct(struct spdk_iscsi_portal *portal, 198 struct spdk_sock *sock) 199 { 200 struct spdk_iscsi_poll_group *pg; 201 struct spdk_iscsi_conn *conn; 202 int i, rc; 203 204 conn = allocate_conn(); 205 if (conn == NULL) { 206 SPDK_ERRLOG("Could not allocate connection.\n"); 207 return -1; 208 } 209 210 pthread_mutex_lock(&g_iscsi.mutex); 211 conn->timeout = g_iscsi.timeout * spdk_get_ticks_hz(); /* seconds to TSC */ 212 conn->nopininterval = g_iscsi.nopininterval; 213 conn->nopininterval *= spdk_get_ticks_hz(); /* seconds to TSC */ 214 conn->last_nopin = spdk_get_ticks(); 215 conn->nop_outstanding = false; 216 conn->data_out_cnt = 0; 217 conn->data_in_cnt = 0; 218 conn->disable_chap = portal->group->disable_chap; 219 conn->require_chap = portal->group->require_chap; 220 conn->mutual_chap = portal->group->mutual_chap; 221 conn->chap_group = portal->group->chap_group; 222 pthread_mutex_unlock(&g_iscsi.mutex); 223 conn->MaxRecvDataSegmentLength = 8192; /* RFC3720(12.12) */ 224 225 conn->portal = portal; 226 conn->pg_tag = portal->group->tag; 227 memcpy(conn->portal_host, portal->host, strlen(portal->host)); 228 memcpy(conn->portal_port, portal->port, strlen(portal->port)); 229 conn->sock = sock; 230 231 conn->state = ISCSI_CONN_STATE_INVALID; 232 conn->login_phase = ISCSI_SECURITY_NEGOTIATION_PHASE; 233 conn->ttt = 0; 234 235 conn->partial_text_parameter = NULL; 236 237 for (i = 0; i < MAX_CONNECTION_PARAMS; i++) { 238 conn->conn_param_state_negotiated[i] = false; 239 } 240 241 for (i = 0; i < MAX_SESSION_PARAMS; i++) { 242 conn->sess_param_state_negotiated[i] = false; 243 } 244 245 conn->pdu_recv_state = ISCSI_PDU_RECV_STATE_AWAIT_PDU_READY; 246 247 TAILQ_INIT(&conn->write_pdu_list); 248 TAILQ_INIT(&conn->snack_pdu_list); 249 TAILQ_INIT(&conn->queued_r2t_tasks); 250 TAILQ_INIT(&conn->active_r2t_tasks); 251 TAILQ_INIT(&conn->queued_datain_tasks); 252 memset(&conn->luns, 0, sizeof(conn->luns)); 253 254 rc = spdk_sock_getaddr(sock, conn->target_addr, sizeof conn->target_addr, NULL, 255 conn->initiator_addr, sizeof conn->initiator_addr, NULL); 256 if (rc < 0) { 257 SPDK_ERRLOG("spdk_sock_getaddr() failed\n"); 258 goto error_return; 259 } 260 261 /* set low water mark */ 262 rc = spdk_sock_set_recvlowat(conn->sock, 1); 263 if (rc != 0) { 264 SPDK_ERRLOG("spdk_sock_set_recvlowat() failed\n"); 265 goto error_return; 266 } 267 268 /* set default params */ 269 rc = iscsi_conn_params_init(&conn->params); 270 if (rc < 0) { 271 SPDK_ERRLOG("iscsi_conn_params_init() failed\n"); 272 goto error_return; 273 } 274 conn->logout_request_timer = NULL; 275 conn->logout_timer = NULL; 276 conn->shutdown_timer = NULL; 277 SPDK_DEBUGLOG(iscsi, "Launching connection on acceptor thread\n"); 278 conn->pending_task_cnt = 0; 279 280 /* Get the first poll group. */ 281 pg = TAILQ_FIRST(&g_iscsi.poll_group_head); 282 if (pg == NULL) { 283 SPDK_ERRLOG("There is no poll group.\n"); 284 assert(false); 285 goto error_return; 286 } 287 288 conn->pg = pg; 289 spdk_thread_send_msg(spdk_io_channel_get_thread(spdk_io_channel_from_ctx(pg)), 290 iscsi_conn_start, conn); 291 return 0; 292 293 error_return: 294 iscsi_param_free(conn->params); 295 free_conn(conn); 296 return -1; 297 } 298 299 void 300 iscsi_conn_free_pdu(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu) 301 { 302 iscsi_conn_xfer_complete_cb cb_fn; 303 void *cb_arg; 304 305 cb_fn = pdu->cb_fn; 306 cb_arg = pdu->cb_arg; 307 308 assert(cb_fn != NULL); 309 pdu->cb_fn = NULL; 310 311 if (pdu->task) { 312 iscsi_task_put(pdu->task); 313 } 314 iscsi_put_pdu(pdu); 315 316 cb_fn(cb_arg); 317 } 318 319 static int 320 iscsi_conn_free_tasks(struct spdk_iscsi_conn *conn) 321 { 322 struct spdk_iscsi_pdu *pdu, *tmp_pdu; 323 struct spdk_iscsi_task *iscsi_task, *tmp_iscsi_task; 324 325 TAILQ_FOREACH_SAFE(pdu, &conn->snack_pdu_list, tailq, tmp_pdu) { 326 TAILQ_REMOVE(&conn->snack_pdu_list, pdu, tailq); 327 iscsi_conn_free_pdu(conn, pdu); 328 } 329 330 TAILQ_FOREACH_SAFE(iscsi_task, &conn->queued_datain_tasks, link, tmp_iscsi_task) { 331 if (!iscsi_task->is_queued) { 332 TAILQ_REMOVE(&conn->queued_datain_tasks, iscsi_task, link); 333 iscsi_task_put(iscsi_task); 334 } 335 } 336 337 /* We have to parse conn->write_pdu_list in the end. In iscsi_conn_free_pdu(), 338 * iscsi_conn_handle_queued_datain_tasks() may be called, and 339 * iscsi_conn_handle_queued_datain_tasks() will parse conn->queued_datain_tasks 340 * and may stack some PDUs to conn->write_pdu_list. Hence when we come here, we 341 * have to ensure there is no associated task in conn->queued_datain_tasks. 342 */ 343 TAILQ_FOREACH_SAFE(pdu, &conn->write_pdu_list, tailq, tmp_pdu) { 344 TAILQ_REMOVE(&conn->write_pdu_list, pdu, tailq); 345 iscsi_conn_free_pdu(conn, pdu); 346 } 347 348 if (conn->pending_task_cnt) { 349 return -1; 350 } 351 352 return 0; 353 } 354 355 static void 356 iscsi_conn_cleanup_backend(struct spdk_iscsi_conn *conn) 357 { 358 int rc; 359 struct spdk_iscsi_tgt_node *target; 360 361 if (conn->sess->connections > 1) { 362 /* connection specific cleanup */ 363 } else if (!g_iscsi.AllowDuplicateIsid) { 364 /* clean up all tasks to all LUNs for session */ 365 target = conn->sess->target; 366 if (target != NULL) { 367 rc = iscsi_tgt_node_cleanup_luns(conn, target); 368 if (rc < 0) { 369 SPDK_ERRLOG("target abort failed\n"); 370 } 371 } 372 } 373 } 374 375 static void 376 iscsi_conn_free(struct spdk_iscsi_conn *conn) 377 { 378 struct spdk_iscsi_sess *sess; 379 int idx; 380 uint32_t i; 381 382 pthread_mutex_lock(&g_conns_mutex); 383 384 if (conn->sess == NULL) { 385 goto end; 386 } 387 388 idx = -1; 389 sess = conn->sess; 390 conn->sess = NULL; 391 392 for (i = 0; i < sess->connections; i++) { 393 if (sess->conns[i] == conn) { 394 idx = i; 395 break; 396 } 397 } 398 399 if (idx < 0) { 400 SPDK_ERRLOG("remove conn not found\n"); 401 } else { 402 for (i = idx; i < sess->connections - 1; i++) { 403 sess->conns[i] = sess->conns[i + 1]; 404 } 405 sess->conns[sess->connections - 1] = NULL; 406 sess->connections--; 407 408 if (sess->connections == 0) { 409 /* cleanup last connection */ 410 SPDK_DEBUGLOG(iscsi, 411 "cleanup last conn free sess\n"); 412 iscsi_free_sess(sess); 413 } 414 } 415 416 SPDK_DEBUGLOG(iscsi, "Terminating connections(tsih %d): %d\n", 417 sess->tsih, sess->connections); 418 419 end: 420 SPDK_DEBUGLOG(iscsi, "cleanup free conn\n"); 421 iscsi_param_free(conn->params); 422 _free_conn(conn); 423 424 pthread_mutex_unlock(&g_conns_mutex); 425 } 426 427 static void 428 iscsi_conn_close_lun(struct spdk_iscsi_conn *conn, int lun_id) 429 { 430 struct spdk_iscsi_lun *iscsi_lun; 431 432 iscsi_lun = conn->luns[lun_id]; 433 if (iscsi_lun == NULL) { 434 return; 435 } 436 437 spdk_scsi_lun_free_io_channel(iscsi_lun->desc); 438 spdk_scsi_lun_close(iscsi_lun->desc); 439 spdk_poller_unregister(&iscsi_lun->remove_poller); 440 free(iscsi_lun); 441 442 conn->luns[lun_id] = NULL; 443 } 444 445 static void 446 iscsi_conn_close_luns(struct spdk_iscsi_conn *conn) 447 { 448 int i; 449 450 for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) { 451 iscsi_conn_close_lun(conn, i); 452 } 453 } 454 455 static bool 456 iscsi_conn_check_tasks_for_lun(struct spdk_iscsi_conn *conn, 457 struct spdk_scsi_lun *lun) 458 { 459 struct spdk_iscsi_pdu *pdu, *tmp_pdu; 460 struct spdk_iscsi_task *task; 461 462 assert(lun != NULL); 463 464 /* We can remove deferred PDUs safely because they are already flushed. */ 465 TAILQ_FOREACH_SAFE(pdu, &conn->snack_pdu_list, tailq, tmp_pdu) { 466 if (lun == pdu->task->scsi.lun) { 467 TAILQ_REMOVE(&conn->snack_pdu_list, pdu, tailq); 468 iscsi_conn_free_pdu(conn, pdu); 469 } 470 } 471 472 TAILQ_FOREACH(task, &conn->queued_datain_tasks, link) { 473 if (lun == task->scsi.lun) { 474 return false; 475 } 476 } 477 478 /* This check loop works even when connection exits in the middle of LUN hotplug 479 * because all PDUs in write_pdu_list are removed in iscsi_conn_free_tasks(). 480 */ 481 TAILQ_FOREACH(pdu, &conn->write_pdu_list, tailq) { 482 if (pdu->task && lun == pdu->task->scsi.lun) { 483 return false; 484 } 485 } 486 487 return true; 488 } 489 490 static int 491 iscsi_conn_remove_lun(void *ctx) 492 { 493 struct spdk_iscsi_lun *iscsi_lun = ctx; 494 struct spdk_iscsi_conn *conn = iscsi_lun->conn; 495 struct spdk_scsi_lun *lun = iscsi_lun->lun; 496 int lun_id = spdk_scsi_lun_get_id(lun); 497 498 if (!iscsi_conn_check_tasks_for_lun(conn, lun)) { 499 return SPDK_POLLER_BUSY; 500 } 501 iscsi_conn_close_lun(conn, lun_id); 502 return SPDK_POLLER_BUSY; 503 } 504 505 static void 506 _iscsi_conn_hotremove_lun(void *ctx) 507 { 508 struct spdk_iscsi_lun *iscsi_lun = ctx; 509 struct spdk_iscsi_conn *conn = iscsi_lun->conn; 510 struct spdk_scsi_lun *lun = iscsi_lun->lun; 511 512 assert(spdk_io_channel_get_thread(spdk_io_channel_from_ctx(conn->pg)) == 513 spdk_get_thread()); 514 515 /* If a connection is already in stating status, just return */ 516 if (conn->state >= ISCSI_CONN_STATE_EXITING) { 517 return; 518 } 519 520 iscsi_clear_all_transfer_task(conn, lun, NULL); 521 522 iscsi_lun->remove_poller = SPDK_POLLER_REGISTER(iscsi_conn_remove_lun, iscsi_lun, 523 1000); 524 } 525 526 static void 527 iscsi_conn_hotremove_lun(struct spdk_scsi_lun *lun, void *remove_ctx) 528 { 529 struct spdk_iscsi_conn *conn = remove_ctx; 530 int lun_id = spdk_scsi_lun_get_id(lun); 531 struct spdk_iscsi_lun *iscsi_lun; 532 533 iscsi_lun = conn->luns[lun_id]; 534 if (iscsi_lun == NULL) { 535 SPDK_ERRLOG("LUN hotplug was notified to the unallocated LUN %d.\n", lun_id); 536 return; 537 } 538 539 spdk_thread_send_msg(spdk_io_channel_get_thread(spdk_io_channel_from_ctx(conn->pg)), 540 _iscsi_conn_hotremove_lun, iscsi_lun); 541 } 542 543 static int 544 iscsi_conn_open_lun(struct spdk_iscsi_conn *conn, int lun_id, 545 struct spdk_scsi_lun *lun) 546 { 547 int rc; 548 struct spdk_iscsi_lun *iscsi_lun; 549 550 iscsi_lun = calloc(1, sizeof(*iscsi_lun)); 551 if (iscsi_lun == NULL) { 552 return -ENOMEM; 553 } 554 555 iscsi_lun->conn = conn; 556 iscsi_lun->lun = lun; 557 558 rc = spdk_scsi_lun_open(lun, iscsi_conn_hotremove_lun, conn, &iscsi_lun->desc); 559 if (rc != 0) { 560 free(iscsi_lun); 561 return rc; 562 } 563 564 rc = spdk_scsi_lun_allocate_io_channel(iscsi_lun->desc); 565 if (rc != 0) { 566 spdk_scsi_lun_close(iscsi_lun->desc); 567 free(iscsi_lun); 568 return rc; 569 } 570 571 conn->luns[lun_id] = iscsi_lun; 572 573 return 0; 574 } 575 576 static void 577 iscsi_conn_open_luns(struct spdk_iscsi_conn *conn) 578 { 579 int i, rc; 580 struct spdk_scsi_lun *lun; 581 582 for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) { 583 lun = spdk_scsi_dev_get_lun(conn->dev, i); 584 if (lun == NULL) { 585 continue; 586 } 587 588 rc = iscsi_conn_open_lun(conn, i, lun); 589 if (rc != 0) { 590 goto error; 591 } 592 } 593 594 return; 595 596 error: 597 iscsi_conn_close_luns(conn); 598 } 599 600 /** 601 * This function will stop executing the specified connection. 602 */ 603 static void 604 iscsi_conn_stop(struct spdk_iscsi_conn *conn) 605 { 606 struct spdk_iscsi_tgt_node *target; 607 608 assert(conn->state == ISCSI_CONN_STATE_EXITED); 609 assert(conn->data_in_cnt == 0); 610 assert(conn->data_out_cnt == 0); 611 612 if (conn->sess != NULL && 613 conn->sess->session_type == SESSION_TYPE_NORMAL && 614 conn->full_feature) { 615 target = conn->sess->target; 616 pthread_mutex_lock(&target->mutex); 617 target->num_active_conns--; 618 pthread_mutex_unlock(&target->mutex); 619 620 iscsi_conn_close_luns(conn); 621 } 622 623 assert(spdk_io_channel_get_thread(spdk_io_channel_from_ctx(conn->pg)) == 624 spdk_get_thread()); 625 } 626 627 static int 628 _iscsi_conn_check_shutdown(void *arg) 629 { 630 struct spdk_iscsi_conn *conn = arg; 631 int rc; 632 633 rc = iscsi_conn_free_tasks(conn); 634 if (rc < 0) { 635 return SPDK_POLLER_BUSY; 636 } 637 638 spdk_poller_unregister(&conn->shutdown_timer); 639 640 iscsi_conn_stop(conn); 641 iscsi_conn_free(conn); 642 643 return SPDK_POLLER_BUSY; 644 } 645 646 static void 647 _iscsi_conn_destruct(struct spdk_iscsi_conn *conn) 648 { 649 int rc; 650 651 iscsi_poll_group_remove_conn(conn->pg, conn); 652 spdk_sock_close(&conn->sock); 653 iscsi_clear_all_transfer_task(conn, NULL, NULL); 654 spdk_poller_unregister(&conn->logout_request_timer); 655 spdk_poller_unregister(&conn->logout_timer); 656 657 rc = iscsi_conn_free_tasks(conn); 658 if (rc < 0) { 659 /* The connection cannot be freed yet. Check back later. */ 660 conn->shutdown_timer = SPDK_POLLER_REGISTER(_iscsi_conn_check_shutdown, conn, 1000); 661 } else { 662 iscsi_conn_stop(conn); 663 iscsi_conn_free(conn); 664 } 665 } 666 667 static int 668 _iscsi_conn_check_pending_tasks(void *arg) 669 { 670 struct spdk_iscsi_conn *conn = arg; 671 672 if (conn->dev != NULL && 673 spdk_scsi_dev_has_pending_tasks(conn->dev, conn->initiator_port)) { 674 return SPDK_POLLER_BUSY; 675 } 676 677 spdk_poller_unregister(&conn->shutdown_timer); 678 679 _iscsi_conn_destruct(conn); 680 681 return SPDK_POLLER_BUSY; 682 } 683 684 void 685 iscsi_conn_destruct(struct spdk_iscsi_conn *conn) 686 { 687 struct spdk_iscsi_pdu *pdu; 688 struct spdk_iscsi_task *task; 689 int opcode; 690 691 /* If a connection is already in exited status, just return */ 692 if (conn->state >= ISCSI_CONN_STATE_EXITED) { 693 return; 694 } 695 696 conn->state = ISCSI_CONN_STATE_EXITED; 697 698 /* 699 * Each connection pre-allocates its next PDU - make sure these get 700 * freed here. 701 */ 702 pdu = conn->pdu_in_progress; 703 if (pdu) { 704 /* remove the task left in the PDU too. */ 705 task = pdu->task; 706 if (task) { 707 opcode = pdu->bhs.opcode; 708 switch (opcode) { 709 case ISCSI_OP_SCSI: 710 case ISCSI_OP_SCSI_DATAOUT: 711 spdk_scsi_task_process_abort(&task->scsi); 712 iscsi_task_cpl(&task->scsi); 713 break; 714 default: 715 SPDK_ERRLOG("unexpected opcode %x\n", opcode); 716 iscsi_task_put(task); 717 break; 718 } 719 } 720 iscsi_put_pdu(pdu); 721 conn->pdu_in_progress = NULL; 722 } 723 724 if (conn->sess != NULL && conn->pending_task_cnt > 0) { 725 iscsi_conn_cleanup_backend(conn); 726 } 727 728 if (conn->dev != NULL && 729 spdk_scsi_dev_has_pending_tasks(conn->dev, conn->initiator_port)) { 730 conn->shutdown_timer = SPDK_POLLER_REGISTER(_iscsi_conn_check_pending_tasks, conn, 1000); 731 } else { 732 _iscsi_conn_destruct(conn); 733 } 734 } 735 736 int 737 iscsi_get_active_conns(struct spdk_iscsi_tgt_node *target) 738 { 739 struct spdk_iscsi_conn *conn; 740 int num = 0; 741 742 if (g_conns_array == MAP_FAILED) { 743 return 0; 744 } 745 746 pthread_mutex_lock(&g_conns_mutex); 747 TAILQ_FOREACH(conn, &g_active_conns, conn_link) { 748 if (target == NULL || conn->target == target) { 749 num++; 750 } 751 } 752 pthread_mutex_unlock(&g_conns_mutex); 753 return num; 754 } 755 756 static void 757 iscsi_conn_check_shutdown_cb(void *arg1) 758 { 759 _iscsi_conns_cleanup(); 760 shutdown_iscsi_conns_done(); 761 } 762 763 static int 764 iscsi_conn_check_shutdown(void *arg) 765 { 766 if (iscsi_get_active_conns(NULL) != 0) { 767 return SPDK_POLLER_BUSY; 768 } 769 770 spdk_poller_unregister(&g_shutdown_timer); 771 772 spdk_thread_send_msg(spdk_get_thread(), iscsi_conn_check_shutdown_cb, NULL); 773 774 return SPDK_POLLER_BUSY; 775 } 776 777 static void 778 iscsi_send_logout_request(struct spdk_iscsi_conn *conn) 779 { 780 struct spdk_iscsi_pdu *rsp_pdu; 781 struct iscsi_bhs_async *rsph; 782 783 rsp_pdu = iscsi_get_pdu(conn); 784 assert(rsp_pdu != NULL); 785 786 rsph = (struct iscsi_bhs_async *)&rsp_pdu->bhs; 787 rsp_pdu->data = NULL; 788 789 rsph->opcode = ISCSI_OP_ASYNC; 790 to_be32(&rsph->ffffffff, 0xFFFFFFFF); 791 rsph->async_event = 1; 792 to_be16(&rsph->param3, ISCSI_LOGOUT_REQUEST_TIMEOUT); 793 794 to_be32(&rsph->stat_sn, conn->StatSN); 795 conn->StatSN++; 796 to_be32(&rsph->exp_cmd_sn, conn->sess->ExpCmdSN); 797 to_be32(&rsph->max_cmd_sn, conn->sess->MaxCmdSN); 798 799 iscsi_conn_write_pdu(conn, rsp_pdu, iscsi_conn_pdu_generic_complete, NULL); 800 } 801 802 static int 803 logout_request_timeout(void *arg) 804 { 805 struct spdk_iscsi_conn *conn = arg; 806 807 if (conn->state < ISCSI_CONN_STATE_EXITING) { 808 conn->state = ISCSI_CONN_STATE_EXITING; 809 } 810 811 return SPDK_POLLER_BUSY; 812 } 813 814 /* If the connection is running and logout is not requested yet, request logout 815 * to initiator and wait for the logout process to start. 816 */ 817 static void 818 _iscsi_conn_request_logout(void *ctx) 819 { 820 struct spdk_iscsi_conn *conn = ctx; 821 822 if (conn->state > ISCSI_CONN_STATE_RUNNING || 823 conn->logout_request_timer != NULL) { 824 return; 825 } 826 827 iscsi_send_logout_request(conn); 828 829 conn->logout_request_timer = SPDK_POLLER_REGISTER(logout_request_timeout, 830 conn, ISCSI_LOGOUT_REQUEST_TIMEOUT * 1000000); 831 } 832 833 static void 834 iscsi_conn_request_logout(struct spdk_iscsi_conn *conn) 835 { 836 struct spdk_thread *thread; 837 838 if (conn->state == ISCSI_CONN_STATE_INVALID) { 839 /* Move it to EXITING state if the connection is in login. */ 840 conn->state = ISCSI_CONN_STATE_EXITING; 841 } else if (conn->state == ISCSI_CONN_STATE_RUNNING && 842 conn->logout_request_timer == NULL) { 843 thread = spdk_io_channel_get_thread(spdk_io_channel_from_ctx(conn->pg)); 844 spdk_thread_send_msg(thread, _iscsi_conn_request_logout, conn); 845 } 846 } 847 848 void 849 iscsi_conns_request_logout(struct spdk_iscsi_tgt_node *target, int pg_tag) 850 { 851 struct spdk_iscsi_conn *conn; 852 853 if (g_conns_array == MAP_FAILED) { 854 return; 855 } 856 857 pthread_mutex_lock(&g_conns_mutex); 858 TAILQ_FOREACH(conn, &g_active_conns, conn_link) { 859 if ((target == NULL) || 860 (conn->target == target && (pg_tag < 0 || conn->pg_tag == pg_tag))) { 861 iscsi_conn_request_logout(conn); 862 } 863 } 864 pthread_mutex_unlock(&g_conns_mutex); 865 } 866 867 void 868 shutdown_iscsi_conns(void) 869 { 870 iscsi_conns_request_logout(NULL, -1); 871 872 g_shutdown_timer = SPDK_POLLER_REGISTER(iscsi_conn_check_shutdown, NULL, 1000); 873 } 874 875 /* Do not set conn->state if the connection has already started exiting. 876 * This ensures we do not move a connection from EXITED state back to EXITING. 877 */ 878 static void 879 _iscsi_conn_drop(void *ctx) 880 { 881 struct spdk_iscsi_conn *conn = ctx; 882 883 if (conn->state < ISCSI_CONN_STATE_EXITING) { 884 conn->state = ISCSI_CONN_STATE_EXITING; 885 } 886 } 887 888 int 889 iscsi_drop_conns(struct spdk_iscsi_conn *conn, const char *conn_match, 890 int drop_all) 891 { 892 struct spdk_iscsi_conn *xconn; 893 const char *xconn_match; 894 struct spdk_thread *thread; 895 int num; 896 897 SPDK_DEBUGLOG(iscsi, "iscsi_drop_conns\n"); 898 899 num = 0; 900 pthread_mutex_lock(&g_conns_mutex); 901 if (g_conns_array == MAP_FAILED) { 902 goto exit; 903 } 904 905 TAILQ_FOREACH(xconn, &g_active_conns, conn_link) { 906 if (xconn == conn) { 907 continue; 908 } 909 910 if (!drop_all && xconn->initiator_port == NULL) { 911 continue; 912 } 913 914 xconn_match = 915 drop_all ? xconn->initiator_name : spdk_scsi_port_get_name(xconn->initiator_port); 916 917 if (!strcasecmp(conn_match, xconn_match) && 918 conn->target == xconn->target) { 919 920 if (num == 0) { 921 /* 922 * Only print this message before we report the 923 * first dropped connection. 924 */ 925 SPDK_ERRLOG("drop old connections %s by %s\n", 926 conn->target->name, conn_match); 927 } 928 929 SPDK_ERRLOG("exiting conn by %s (%s)\n", 930 xconn_match, xconn->initiator_addr); 931 if (xconn->sess != NULL) { 932 SPDK_DEBUGLOG(iscsi, "TSIH=%u\n", xconn->sess->tsih); 933 } else { 934 SPDK_DEBUGLOG(iscsi, "TSIH=xx\n"); 935 } 936 937 SPDK_DEBUGLOG(iscsi, "CID=%u\n", xconn->cid); 938 939 thread = spdk_io_channel_get_thread(spdk_io_channel_from_ctx(xconn->pg)); 940 spdk_thread_send_msg(thread, _iscsi_conn_drop, xconn); 941 942 num++; 943 } 944 } 945 946 exit: 947 pthread_mutex_unlock(&g_conns_mutex); 948 949 if (num != 0) { 950 SPDK_ERRLOG("exiting %d conns\n", num); 951 } 952 953 return 0; 954 } 955 956 static int 957 _iscsi_conn_abort_queued_datain_task(struct spdk_iscsi_conn *conn, 958 struct spdk_iscsi_task *task) 959 { 960 struct spdk_iscsi_task *subtask; 961 uint32_t remaining_size; 962 963 if (conn->data_in_cnt >= g_iscsi.MaxLargeDataInPerConnection) { 964 return -1; 965 } 966 967 assert(task->current_datain_offset <= task->scsi.transfer_len); 968 /* Stop split and abort read I/O for remaining data. */ 969 if (task->current_datain_offset < task->scsi.transfer_len) { 970 remaining_size = task->scsi.transfer_len - task->current_datain_offset; 971 subtask = iscsi_task_get(conn, task, iscsi_task_cpl); 972 assert(subtask != NULL); 973 subtask->scsi.offset = task->current_datain_offset; 974 subtask->scsi.length = remaining_size; 975 spdk_scsi_task_set_data(&subtask->scsi, NULL, 0); 976 task->current_datain_offset += subtask->scsi.length; 977 978 subtask->scsi.transfer_len = subtask->scsi.length; 979 spdk_scsi_task_process_abort(&subtask->scsi); 980 iscsi_task_cpl(&subtask->scsi); 981 } 982 983 /* Remove the primary task from the list because all subtasks are submitted 984 * or aborted. 985 */ 986 assert(task->current_datain_offset == task->scsi.transfer_len); 987 TAILQ_REMOVE(&conn->queued_datain_tasks, task, link); 988 return 0; 989 } 990 991 int 992 iscsi_conn_abort_queued_datain_task(struct spdk_iscsi_conn *conn, 993 uint32_t ref_task_tag) 994 { 995 struct spdk_iscsi_task *task; 996 997 TAILQ_FOREACH(task, &conn->queued_datain_tasks, link) { 998 if (task->tag == ref_task_tag) { 999 return _iscsi_conn_abort_queued_datain_task(conn, task); 1000 } 1001 } 1002 1003 return 0; 1004 } 1005 1006 int 1007 iscsi_conn_abort_queued_datain_tasks(struct spdk_iscsi_conn *conn, 1008 struct spdk_scsi_lun *lun, 1009 struct spdk_iscsi_pdu *pdu) 1010 { 1011 struct spdk_iscsi_task *task, *task_tmp; 1012 struct spdk_iscsi_pdu *pdu_tmp; 1013 int rc; 1014 1015 TAILQ_FOREACH_SAFE(task, &conn->queued_datain_tasks, link, task_tmp) { 1016 pdu_tmp = iscsi_task_get_pdu(task); 1017 if ((lun == NULL || lun == task->scsi.lun) && 1018 (pdu == NULL || (spdk_sn32_lt(pdu_tmp->cmd_sn, pdu->cmd_sn)))) { 1019 rc = _iscsi_conn_abort_queued_datain_task(conn, task); 1020 if (rc != 0) { 1021 return rc; 1022 } 1023 } 1024 } 1025 1026 return 0; 1027 } 1028 1029 int 1030 iscsi_conn_handle_queued_datain_tasks(struct spdk_iscsi_conn *conn) 1031 { 1032 struct spdk_iscsi_task *task; 1033 1034 while (!TAILQ_EMPTY(&conn->queued_datain_tasks) && 1035 conn->data_in_cnt < g_iscsi.MaxLargeDataInPerConnection) { 1036 task = TAILQ_FIRST(&conn->queued_datain_tasks); 1037 assert(task->current_datain_offset <= task->scsi.transfer_len); 1038 if (task->current_datain_offset < task->scsi.transfer_len) { 1039 struct spdk_iscsi_task *subtask; 1040 uint32_t remaining_size = 0; 1041 1042 remaining_size = task->scsi.transfer_len - task->current_datain_offset; 1043 subtask = iscsi_task_get(conn, task, iscsi_task_cpl); 1044 assert(subtask != NULL); 1045 subtask->scsi.offset = task->current_datain_offset; 1046 spdk_scsi_task_set_data(&subtask->scsi, NULL, 0); 1047 1048 if (spdk_scsi_dev_get_lun(conn->dev, task->lun_id) == NULL) { 1049 /* Stop submitting split read I/Os for remaining data. */ 1050 TAILQ_REMOVE(&conn->queued_datain_tasks, task, link); 1051 task->current_datain_offset += remaining_size; 1052 assert(task->current_datain_offset == task->scsi.transfer_len); 1053 subtask->scsi.transfer_len = remaining_size; 1054 spdk_scsi_task_process_null_lun(&subtask->scsi); 1055 iscsi_task_cpl(&subtask->scsi); 1056 return 0; 1057 } 1058 1059 subtask->scsi.length = spdk_min(SPDK_BDEV_LARGE_BUF_MAX_SIZE, remaining_size); 1060 task->current_datain_offset += subtask->scsi.length; 1061 iscsi_queue_task(conn, subtask); 1062 } 1063 if (task->current_datain_offset == task->scsi.transfer_len) { 1064 TAILQ_REMOVE(&conn->queued_datain_tasks, task, link); 1065 } 1066 } 1067 return 0; 1068 } 1069 1070 void 1071 iscsi_task_mgmt_cpl(struct spdk_scsi_task *scsi_task) 1072 { 1073 struct spdk_iscsi_task *task = iscsi_task_from_scsi_task(scsi_task); 1074 1075 iscsi_task_mgmt_response(task->conn, task); 1076 iscsi_task_put(task); 1077 } 1078 1079 static void 1080 iscsi_task_copy_to_rsp_scsi_status(struct spdk_iscsi_task *primary, 1081 struct spdk_scsi_task *task) 1082 { 1083 memcpy(primary->rsp_sense_data, task->sense_data, task->sense_data_len); 1084 primary->rsp_sense_data_len = task->sense_data_len; 1085 primary->rsp_scsi_status = task->status; 1086 } 1087 1088 static void 1089 iscsi_task_copy_from_rsp_scsi_status(struct spdk_scsi_task *task, 1090 struct spdk_iscsi_task *primary) 1091 { 1092 memcpy(task->sense_data, primary->rsp_sense_data, 1093 primary->rsp_sense_data_len); 1094 task->sense_data_len = primary->rsp_sense_data_len; 1095 task->status = primary->rsp_scsi_status; 1096 } 1097 1098 static void 1099 process_completed_read_subtask_list_in_order(struct spdk_iscsi_conn *conn, 1100 struct spdk_iscsi_task *primary) 1101 { 1102 struct spdk_iscsi_task *subtask, *tmp; 1103 1104 TAILQ_FOREACH_SAFE(subtask, &primary->subtask_list, subtask_link, tmp) { 1105 if (subtask->scsi.offset == primary->bytes_completed) { 1106 TAILQ_REMOVE(&primary->subtask_list, subtask, subtask_link); 1107 primary->bytes_completed += subtask->scsi.length; 1108 if (primary->bytes_completed == primary->scsi.transfer_len) { 1109 iscsi_task_put(primary); 1110 } 1111 iscsi_task_response(conn, subtask); 1112 iscsi_task_put(subtask); 1113 } else { 1114 break; 1115 } 1116 } 1117 } 1118 1119 static void 1120 process_read_task_completion(struct spdk_iscsi_conn *conn, 1121 struct spdk_iscsi_task *task, 1122 struct spdk_iscsi_task *primary) 1123 { 1124 struct spdk_iscsi_task *tmp; 1125 1126 /* If the status of the completed subtask is the first failure, 1127 * copy it to out-of-order subtasks and remember it as the status 1128 * of the command, 1129 * 1130 * Even if the status of the completed task is success, 1131 * there are any failed subtask ever, copy the first failed status 1132 * to it. 1133 */ 1134 if (task->scsi.status != SPDK_SCSI_STATUS_GOOD) { 1135 if (primary->rsp_scsi_status == SPDK_SCSI_STATUS_GOOD) { 1136 TAILQ_FOREACH(tmp, &primary->subtask_list, subtask_link) { 1137 spdk_scsi_task_copy_status(&tmp->scsi, &task->scsi); 1138 } 1139 iscsi_task_copy_to_rsp_scsi_status(primary, &task->scsi); 1140 } 1141 } else if (primary->rsp_scsi_status != SPDK_SCSI_STATUS_GOOD) { 1142 iscsi_task_copy_from_rsp_scsi_status(&task->scsi, primary); 1143 } 1144 1145 if (task == primary) { 1146 primary->bytes_completed = task->scsi.length; 1147 /* For non split read I/O */ 1148 assert(primary->bytes_completed == task->scsi.transfer_len); 1149 iscsi_task_response(conn, task); 1150 iscsi_task_put(task); 1151 } else if (!conn->sess->DataSequenceInOrder) { 1152 primary->bytes_completed += task->scsi.length; 1153 if (primary->bytes_completed == primary->scsi.transfer_len) { 1154 iscsi_task_put(primary); 1155 } 1156 iscsi_task_response(conn, task); 1157 iscsi_task_put(task); 1158 } else { 1159 1160 if (task->scsi.offset != primary->bytes_completed) { 1161 TAILQ_FOREACH(tmp, &primary->subtask_list, subtask_link) { 1162 if (task->scsi.offset < tmp->scsi.offset) { 1163 TAILQ_INSERT_BEFORE(tmp, task, subtask_link); 1164 return; 1165 } 1166 } 1167 1168 TAILQ_INSERT_TAIL(&primary->subtask_list, task, subtask_link); 1169 } else { 1170 TAILQ_INSERT_HEAD(&primary->subtask_list, task, subtask_link); 1171 process_completed_read_subtask_list_in_order(conn, primary); 1172 } 1173 } 1174 } 1175 1176 static void 1177 process_non_read_task_completion(struct spdk_iscsi_conn *conn, 1178 struct spdk_iscsi_task *task, 1179 struct spdk_iscsi_task *primary) 1180 { 1181 primary->bytes_completed += task->scsi.length; 1182 1183 /* If the status of the subtask is the first failure, remember it as 1184 * the status of the command and set it to the status of the primary 1185 * task later. 1186 * 1187 * If the first failed task is the primary, two copies can be avoided 1188 * but code simplicity is prioritized. 1189 */ 1190 if (task->scsi.status == SPDK_SCSI_STATUS_GOOD) { 1191 if (task != primary) { 1192 primary->scsi.data_transferred += task->scsi.data_transferred; 1193 } 1194 } else if (primary->rsp_scsi_status == SPDK_SCSI_STATUS_GOOD) { 1195 iscsi_task_copy_to_rsp_scsi_status(primary, &task->scsi); 1196 } 1197 1198 if (primary->bytes_completed == primary->scsi.transfer_len) { 1199 /* 1200 * Check if this is the last task completed for an iSCSI write 1201 * that required child subtasks. If task != primary, we know 1202 * for sure that it was part of an iSCSI write with child subtasks. 1203 * The trickier case is when the last task completed was the initial 1204 * task - in this case the task will have a smaller length than 1205 * the overall transfer length. 1206 */ 1207 if (task != primary || task->scsi.length != task->scsi.transfer_len) { 1208 /* If LUN is removed in the middle of the iSCSI write sequence, 1209 * primary might complete the write to the initiator because it is not 1210 * ensured that the initiator will send all data requested by R2Ts. 1211 * 1212 * We check it and skip the following if primary is completed. (see 1213 * iscsi_clear_all_transfer_task() in iscsi.c.) 1214 */ 1215 if (primary->is_r2t_active) { 1216 if (primary->rsp_scsi_status != SPDK_SCSI_STATUS_GOOD) { 1217 iscsi_task_copy_from_rsp_scsi_status(&primary->scsi, primary); 1218 } 1219 iscsi_task_response(conn, primary); 1220 iscsi_del_transfer_task(conn, primary->tag); 1221 } 1222 } else { 1223 iscsi_task_response(conn, task); 1224 } 1225 } 1226 iscsi_task_put(task); 1227 } 1228 1229 void 1230 iscsi_task_cpl(struct spdk_scsi_task *scsi_task) 1231 { 1232 struct spdk_iscsi_task *primary; 1233 struct spdk_iscsi_task *task = iscsi_task_from_scsi_task(scsi_task); 1234 struct spdk_iscsi_conn *conn = task->conn; 1235 struct spdk_iscsi_pdu *pdu = task->pdu; 1236 1237 spdk_trace_record(TRACE_ISCSI_TASK_DONE, conn->id, 0, (uintptr_t)task, 0); 1238 1239 task->is_queued = false; 1240 primary = iscsi_task_get_primary(task); 1241 1242 if (iscsi_task_is_read(primary)) { 1243 process_read_task_completion(conn, task, primary); 1244 } else { 1245 process_non_read_task_completion(conn, task, primary); 1246 } 1247 if (!task->parent) { 1248 spdk_trace_record(TRACE_ISCSI_PDU_COMPLETED, 0, 0, (uintptr_t)pdu, 0); 1249 } 1250 } 1251 1252 static void 1253 iscsi_conn_send_nopin(struct spdk_iscsi_conn *conn) 1254 { 1255 struct spdk_iscsi_pdu *rsp_pdu; 1256 struct iscsi_bhs_nop_in *rsp; 1257 /* Only send nopin if we have logged in and are in a normal session. */ 1258 if (conn->sess == NULL || 1259 !conn->full_feature || 1260 !iscsi_param_eq_val(conn->sess->params, "SessionType", "Normal")) { 1261 return; 1262 } 1263 SPDK_DEBUGLOG(iscsi, "send NOPIN isid=%"PRIx64", tsih=%u, cid=%u\n", 1264 conn->sess->isid, conn->sess->tsih, conn->cid); 1265 SPDK_DEBUGLOG(iscsi, "StatSN=%u, ExpCmdSN=%u, MaxCmdSN=%u\n", 1266 conn->StatSN, conn->sess->ExpCmdSN, 1267 conn->sess->MaxCmdSN); 1268 rsp_pdu = iscsi_get_pdu(conn); 1269 rsp = (struct iscsi_bhs_nop_in *) &rsp_pdu->bhs; 1270 rsp_pdu->data = NULL; 1271 /* 1272 * iscsi_get_pdu() memset's the PDU for us, so only fill out the needed 1273 * fields. 1274 */ 1275 rsp->opcode = ISCSI_OP_NOPIN; 1276 rsp->flags = 0x80; 1277 /* 1278 * Technically the to_be32() is not needed here, since 1279 * to_be32(0xFFFFFFFU) returns 0xFFFFFFFFU. 1280 */ 1281 to_be32(&rsp->itt, 0xFFFFFFFFU); 1282 to_be32(&rsp->ttt, conn->id); 1283 to_be32(&rsp->stat_sn, conn->StatSN); 1284 to_be32(&rsp->exp_cmd_sn, conn->sess->ExpCmdSN); 1285 to_be32(&rsp->max_cmd_sn, conn->sess->MaxCmdSN); 1286 iscsi_conn_write_pdu(conn, rsp_pdu, iscsi_conn_pdu_generic_complete, NULL); 1287 conn->last_nopin = spdk_get_ticks(); 1288 conn->nop_outstanding = true; 1289 } 1290 1291 void 1292 iscsi_conn_handle_nop(struct spdk_iscsi_conn *conn) 1293 { 1294 uint64_t tsc; 1295 1296 /** 1297 * This function will be executed by nop_poller of iSCSI polling group, so 1298 * we need to check the connection state first, then do the nop interval 1299 * expiration check work. 1300 */ 1301 if ((conn->state == ISCSI_CONN_STATE_EXITED) || 1302 (conn->state == ISCSI_CONN_STATE_EXITING)) { 1303 return; 1304 } 1305 1306 /* Check for nop interval expiration */ 1307 tsc = spdk_get_ticks(); 1308 if (conn->nop_outstanding) { 1309 if ((tsc - conn->last_nopin) > conn->timeout) { 1310 SPDK_ERRLOG("Timed out waiting for NOP-Out response from initiator\n"); 1311 SPDK_ERRLOG(" tsc=0x%" PRIx64 ", last_nopin=0x%" PRIx64 "\n", tsc, conn->last_nopin); 1312 SPDK_ERRLOG(" initiator=%s, target=%s\n", conn->initiator_name, 1313 conn->target_short_name); 1314 conn->state = ISCSI_CONN_STATE_EXITING; 1315 } 1316 } else if (tsc - conn->last_nopin > conn->nopininterval) { 1317 iscsi_conn_send_nopin(conn); 1318 } 1319 } 1320 1321 /** 1322 * \brief Reads data for the specified iSCSI connection from its TCP socket. 1323 * 1324 * The TCP socket is marked as non-blocking, so this function may not read 1325 * all data requested. 1326 * 1327 * Returns SPDK_ISCSI_CONNECTION_FATAL if the recv() operation indicates a fatal 1328 * error with the TCP connection (including if the TCP connection was closed 1329 * unexpectedly. 1330 * 1331 * Otherwise returns the number of bytes successfully read. 1332 */ 1333 int 1334 iscsi_conn_read_data(struct spdk_iscsi_conn *conn, int bytes, 1335 void *buf) 1336 { 1337 int ret; 1338 1339 if (bytes == 0) { 1340 return 0; 1341 } 1342 1343 ret = spdk_sock_recv(conn->sock, buf, bytes); 1344 1345 if (ret > 0) { 1346 spdk_trace_record(TRACE_ISCSI_READ_FROM_SOCKET_DONE, conn->id, ret, 0, 0); 1347 return ret; 1348 } 1349 1350 if (ret < 0) { 1351 if (errno == EAGAIN || errno == EWOULDBLOCK) { 1352 return 0; 1353 } 1354 1355 /* For connect reset issue, do not output error log */ 1356 if (errno == ECONNRESET) { 1357 SPDK_DEBUGLOG(iscsi, "spdk_sock_recv() failed, errno %d: %s\n", 1358 errno, spdk_strerror(errno)); 1359 } else { 1360 SPDK_ERRLOG("spdk_sock_recv() failed, errno %d: %s\n", 1361 errno, spdk_strerror(errno)); 1362 } 1363 } 1364 1365 /* connection closed */ 1366 return SPDK_ISCSI_CONNECTION_FATAL; 1367 } 1368 1369 int 1370 iscsi_conn_readv_data(struct spdk_iscsi_conn *conn, 1371 struct iovec *iov, int iovcnt) 1372 { 1373 int ret; 1374 1375 if (iov == NULL || iovcnt == 0) { 1376 return 0; 1377 } 1378 1379 if (iovcnt == 1) { 1380 return iscsi_conn_read_data(conn, iov[0].iov_len, 1381 iov[0].iov_base); 1382 } 1383 1384 ret = spdk_sock_readv(conn->sock, iov, iovcnt); 1385 1386 if (ret > 0) { 1387 spdk_trace_record(TRACE_ISCSI_READ_FROM_SOCKET_DONE, conn->id, ret, 0, 0); 1388 return ret; 1389 } 1390 1391 if (ret < 0) { 1392 if (errno == EAGAIN || errno == EWOULDBLOCK) { 1393 return 0; 1394 } 1395 1396 /* For connect reset issue, do not output error log */ 1397 if (errno == ECONNRESET) { 1398 SPDK_DEBUGLOG(iscsi, "spdk_sock_readv() failed, errno %d: %s\n", 1399 errno, spdk_strerror(errno)); 1400 } else { 1401 SPDK_ERRLOG("spdk_sock_readv() failed, errno %d: %s\n", 1402 errno, spdk_strerror(errno)); 1403 } 1404 } 1405 1406 /* connection closed */ 1407 return SPDK_ISCSI_CONNECTION_FATAL; 1408 } 1409 1410 static bool 1411 iscsi_is_free_pdu_deferred(struct spdk_iscsi_pdu *pdu) 1412 { 1413 if (pdu == NULL) { 1414 return false; 1415 } 1416 1417 if (pdu->bhs.opcode == ISCSI_OP_R2T || 1418 pdu->bhs.opcode == ISCSI_OP_SCSI_DATAIN) { 1419 return true; 1420 } 1421 1422 return false; 1423 } 1424 1425 static int 1426 iscsi_dif_verify(struct spdk_iscsi_pdu *pdu, struct spdk_dif_ctx *dif_ctx) 1427 { 1428 struct iovec iov; 1429 struct spdk_dif_error err_blk = {}; 1430 uint32_t num_blocks; 1431 int rc; 1432 1433 iov.iov_base = pdu->data; 1434 iov.iov_len = pdu->data_buf_len; 1435 num_blocks = pdu->data_buf_len / dif_ctx->block_size; 1436 1437 rc = spdk_dif_verify(&iov, 1, num_blocks, dif_ctx, &err_blk); 1438 if (rc != 0) { 1439 SPDK_ERRLOG("DIF error detected. type=%d, offset=%" PRIu32 "\n", 1440 err_blk.err_type, err_blk.err_offset); 1441 } 1442 1443 return rc; 1444 } 1445 1446 static void 1447 _iscsi_conn_pdu_write_done(void *cb_arg, int err) 1448 { 1449 struct spdk_iscsi_pdu *pdu = cb_arg; 1450 struct spdk_iscsi_conn *conn = pdu->conn; 1451 1452 assert(conn != NULL); 1453 1454 if (spdk_unlikely(conn->state >= ISCSI_CONN_STATE_EXITING)) { 1455 /* The other policy will recycle the resource */ 1456 return; 1457 } 1458 1459 TAILQ_REMOVE(&conn->write_pdu_list, pdu, tailq); 1460 1461 if (err != 0) { 1462 conn->state = ISCSI_CONN_STATE_EXITING; 1463 } else { 1464 spdk_trace_record(TRACE_ISCSI_FLUSH_WRITEBUF_DONE, conn->id, pdu->mapped_length, (uintptr_t)pdu, 0); 1465 } 1466 1467 if ((conn->full_feature) && 1468 (conn->sess->ErrorRecoveryLevel >= 1) && 1469 iscsi_is_free_pdu_deferred(pdu)) { 1470 SPDK_DEBUGLOG(iscsi, "stat_sn=%d\n", 1471 from_be32(&pdu->bhs.stat_sn)); 1472 TAILQ_INSERT_TAIL(&conn->snack_pdu_list, pdu, 1473 tailq); 1474 } else { 1475 iscsi_conn_free_pdu(conn, pdu); 1476 } 1477 } 1478 1479 void 1480 iscsi_conn_pdu_generic_complete(void *cb_arg) 1481 { 1482 } 1483 1484 void 1485 iscsi_conn_write_pdu(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu, 1486 iscsi_conn_xfer_complete_cb cb_fn, 1487 void *cb_arg) 1488 { 1489 uint32_t crc32c; 1490 ssize_t rc; 1491 1492 if (spdk_unlikely(pdu->dif_insert_or_strip)) { 1493 rc = iscsi_dif_verify(pdu, &pdu->dif_ctx); 1494 if (rc != 0) { 1495 iscsi_conn_free_pdu(conn, pdu); 1496 conn->state = ISCSI_CONN_STATE_EXITING; 1497 return; 1498 } 1499 } 1500 1501 if (pdu->bhs.opcode != ISCSI_OP_LOGIN_RSP) { 1502 /* Header Digest */ 1503 if (conn->header_digest) { 1504 crc32c = iscsi_pdu_calc_header_digest(pdu); 1505 MAKE_DIGEST_WORD(pdu->header_digest, crc32c); 1506 } 1507 1508 /* Data Digest */ 1509 if (conn->data_digest && DGET24(pdu->bhs.data_segment_len) != 0) { 1510 crc32c = iscsi_pdu_calc_data_digest(pdu); 1511 MAKE_DIGEST_WORD(pdu->data_digest, crc32c); 1512 } 1513 } 1514 1515 pdu->cb_fn = cb_fn; 1516 pdu->cb_arg = cb_arg; 1517 TAILQ_INSERT_TAIL(&conn->write_pdu_list, pdu, tailq); 1518 1519 if (spdk_unlikely(conn->state >= ISCSI_CONN_STATE_EXITING)) { 1520 return; 1521 } 1522 pdu->sock_req.iovcnt = iscsi_build_iovs(conn, pdu->iov, SPDK_COUNTOF(pdu->iov), pdu, 1523 &pdu->mapped_length); 1524 pdu->sock_req.cb_fn = _iscsi_conn_pdu_write_done; 1525 pdu->sock_req.cb_arg = pdu; 1526 1527 spdk_trace_record(TRACE_ISCSI_FLUSH_WRITEBUF_START, conn->id, pdu->mapped_length, (uintptr_t)pdu, 1528 pdu->sock_req.iovcnt); 1529 spdk_sock_writev_async(conn->sock, &pdu->sock_req); 1530 } 1531 1532 static void 1533 iscsi_conn_sock_cb(void *arg, struct spdk_sock_group *group, struct spdk_sock *sock) 1534 { 1535 struct spdk_iscsi_conn *conn = arg; 1536 int rc; 1537 1538 assert(conn != NULL); 1539 1540 if ((conn->state == ISCSI_CONN_STATE_EXITED) || 1541 (conn->state == ISCSI_CONN_STATE_EXITING)) { 1542 return; 1543 } 1544 1545 /* Handle incoming PDUs */ 1546 rc = iscsi_handle_incoming_pdus(conn); 1547 if (rc < 0) { 1548 conn->state = ISCSI_CONN_STATE_EXITING; 1549 } 1550 } 1551 1552 static void 1553 iscsi_conn_full_feature_migrate(void *arg) 1554 { 1555 struct spdk_iscsi_conn *conn = arg; 1556 1557 if (conn->state >= ISCSI_CONN_STATE_EXITING) { 1558 /* Connection is being exited before this callback is executed. */ 1559 SPDK_DEBUGLOG(iscsi, "Connection is already exited.\n"); 1560 return; 1561 } 1562 1563 if (conn->sess->session_type == SESSION_TYPE_NORMAL) { 1564 iscsi_conn_open_luns(conn); 1565 } 1566 1567 /* Add this connection to the assigned poll group. */ 1568 iscsi_poll_group_add_conn(conn->pg, conn); 1569 } 1570 1571 static struct spdk_iscsi_poll_group *g_next_pg = NULL; 1572 1573 void 1574 iscsi_conn_schedule(struct spdk_iscsi_conn *conn) 1575 { 1576 struct spdk_iscsi_poll_group *pg; 1577 struct spdk_iscsi_tgt_node *target; 1578 1579 if (conn->sess->session_type != SESSION_TYPE_NORMAL) { 1580 /* Leave all non-normal sessions on the acceptor 1581 * thread. */ 1582 return; 1583 } 1584 pthread_mutex_lock(&g_iscsi.mutex); 1585 1586 target = conn->sess->target; 1587 pthread_mutex_lock(&target->mutex); 1588 target->num_active_conns++; 1589 if (target->num_active_conns == 1) { 1590 /** 1591 * This is the only active connection for this target node. 1592 * Pick a poll group using round-robin. 1593 */ 1594 if (g_next_pg == NULL) { 1595 g_next_pg = TAILQ_FIRST(&g_iscsi.poll_group_head); 1596 assert(g_next_pg != NULL); 1597 } 1598 1599 pg = g_next_pg; 1600 g_next_pg = TAILQ_NEXT(g_next_pg, link); 1601 1602 /* Save the pg in the target node so it can be used for any other connections to this target node. */ 1603 target->pg = pg; 1604 } else { 1605 /** 1606 * There are other active connections for this target node. 1607 */ 1608 pg = target->pg; 1609 } 1610 1611 pthread_mutex_unlock(&target->mutex); 1612 pthread_mutex_unlock(&g_iscsi.mutex); 1613 1614 assert(spdk_io_channel_get_thread(spdk_io_channel_from_ctx(conn->pg)) == 1615 spdk_get_thread()); 1616 1617 /* Remove this connection from the previous poll group */ 1618 iscsi_poll_group_remove_conn(conn->pg, conn); 1619 1620 conn->pg = pg; 1621 1622 spdk_thread_send_msg(spdk_io_channel_get_thread(spdk_io_channel_from_ctx(pg)), 1623 iscsi_conn_full_feature_migrate, conn); 1624 } 1625 1626 static int 1627 logout_timeout(void *arg) 1628 { 1629 struct spdk_iscsi_conn *conn = arg; 1630 1631 if (conn->state < ISCSI_CONN_STATE_EXITING) { 1632 conn->state = ISCSI_CONN_STATE_EXITING; 1633 } 1634 1635 return SPDK_POLLER_BUSY; 1636 } 1637 1638 void 1639 iscsi_conn_logout(struct spdk_iscsi_conn *conn) 1640 { 1641 conn->is_logged_out = true; 1642 conn->logout_timer = SPDK_POLLER_REGISTER(logout_timeout, conn, ISCSI_LOGOUT_TIMEOUT * 1000000); 1643 } 1644 1645 static const char * 1646 iscsi_conn_get_state(struct spdk_iscsi_conn *conn) 1647 { 1648 switch (conn->state) { 1649 SPDK_ISCSI_CONNECTION_STATUS(ISCSI_CONN_STATE_INVALID, "invalid"); 1650 SPDK_ISCSI_CONNECTION_STATUS(ISCSI_CONN_STATE_RUNNING, "running"); 1651 SPDK_ISCSI_CONNECTION_STATUS(ISCSI_CONN_STATE_EXITING, "exiting"); 1652 SPDK_ISCSI_CONNECTION_STATUS(ISCSI_CONN_STATE_EXITED, "exited"); 1653 } 1654 return "unknown"; 1655 } 1656 1657 static const char * 1658 iscsi_conn_get_login_phase(struct spdk_iscsi_conn *conn) 1659 { 1660 switch (conn->login_phase) { 1661 SPDK_ISCSI_CONNECTION_STATUS(ISCSI_SECURITY_NEGOTIATION_PHASE, "security_negotiation_phase"); 1662 SPDK_ISCSI_CONNECTION_STATUS(ISCSI_OPERATIONAL_NEGOTIATION_PHASE, "operational_negotiation_phase"); 1663 SPDK_ISCSI_CONNECTION_STATUS(ISCSI_FULL_FEATURE_PHASE, "full_feature_phase"); 1664 } 1665 return "not_started"; 1666 } 1667 1668 SPDK_TRACE_REGISTER_FN(iscsi_conn_trace, "iscsi_conn", TRACE_GROUP_ISCSI) 1669 { 1670 spdk_trace_register_owner(OWNER_ISCSI_CONN, 'c'); 1671 spdk_trace_register_object(OBJECT_ISCSI_PDU, 'p'); 1672 spdk_trace_register_description("ISCSI_READ_DONE", TRACE_ISCSI_READ_FROM_SOCKET_DONE, 1673 OWNER_ISCSI_CONN, OBJECT_NONE, 0, 0, ""); 1674 spdk_trace_register_description("ISCSI_WRITE_START", TRACE_ISCSI_FLUSH_WRITEBUF_START, 1675 OWNER_ISCSI_CONN, OBJECT_NONE, 0, 0, "iovec: "); 1676 spdk_trace_register_description("ISCSI_WRITE_DONE", TRACE_ISCSI_FLUSH_WRITEBUF_DONE, 1677 OWNER_ISCSI_CONN, OBJECT_NONE, 0, 0, ""); 1678 spdk_trace_register_description("ISCSI_READ_PDU", TRACE_ISCSI_READ_PDU, 1679 OWNER_ISCSI_CONN, OBJECT_ISCSI_PDU, 1, 0, "opc: "); 1680 spdk_trace_register_description("ISCSI_TASK_DONE", TRACE_ISCSI_TASK_DONE, 1681 OWNER_ISCSI_CONN, OBJECT_SCSI_TASK, 0, 0, ""); 1682 spdk_trace_register_description("ISCSI_TASK_QUEUE", TRACE_ISCSI_TASK_QUEUE, 1683 OWNER_ISCSI_CONN, OBJECT_SCSI_TASK, 1, 1, "pdu: "); 1684 spdk_trace_register_description("ISCSI_TASK_EXECUTED", TRACE_ISCSI_TASK_EXECUTED, 1685 OWNER_ISCSI_CONN, OBJECT_ISCSI_PDU, 0, 0, ""); 1686 spdk_trace_register_description("ISCSI_PDU_COMPLETED", TRACE_ISCSI_PDU_COMPLETED, 1687 OWNER_ISCSI_CONN, OBJECT_ISCSI_PDU, 0, 0, ""); 1688 } 1689 1690 void 1691 iscsi_conn_info_json(struct spdk_json_write_ctx *w, struct spdk_iscsi_conn *conn) 1692 { 1693 uint16_t tsih; 1694 1695 if (!conn->is_valid) { 1696 return; 1697 } 1698 1699 spdk_json_write_object_begin(w); 1700 1701 spdk_json_write_named_int32(w, "id", conn->id); 1702 1703 spdk_json_write_named_int32(w, "cid", conn->cid); 1704 1705 /* 1706 * If we try to return data for a connection that has not 1707 * logged in yet, the session will not be set. So in this 1708 * case, return -1 for the tsih rather than segfaulting 1709 * on the null conn->sess. 1710 */ 1711 if (conn->sess == NULL) { 1712 tsih = -1; 1713 } else { 1714 tsih = conn->sess->tsih; 1715 } 1716 spdk_json_write_named_int32(w, "tsih", tsih); 1717 1718 spdk_json_write_named_string(w, "state", iscsi_conn_get_state(conn)); 1719 1720 spdk_json_write_named_string(w, "login_phase", iscsi_conn_get_login_phase(conn)); 1721 1722 spdk_json_write_named_string(w, "initiator_addr", conn->initiator_addr); 1723 1724 spdk_json_write_named_string(w, "target_addr", conn->target_addr); 1725 1726 spdk_json_write_named_string(w, "target_node_name", conn->target_short_name); 1727 1728 spdk_json_write_named_string(w, "thread_name", 1729 spdk_thread_get_name(spdk_get_thread())); 1730 1731 spdk_json_write_object_end(w); 1732 } 1733