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