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_data_offset <= task->scsi.transfer_len); 968 /* Stop split and abort read I/O for remaining data. */ 969 if (task->current_data_offset < task->scsi.transfer_len) { 970 remaining_size = task->scsi.transfer_len - task->current_data_offset; 971 subtask = iscsi_task_get(conn, task, iscsi_task_cpl); 972 assert(subtask != NULL); 973 subtask->scsi.offset = task->current_data_offset; 974 subtask->scsi.length = remaining_size; 975 spdk_scsi_task_set_data(&subtask->scsi, NULL, 0); 976 task->current_data_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_data_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_data_offset <= task->scsi.transfer_len); 1038 if (task->current_data_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_data_offset; 1043 subtask = iscsi_task_get(conn, task, iscsi_task_cpl); 1044 assert(subtask != NULL); 1045 subtask->scsi.offset = task->current_data_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_data_offset += remaining_size; 1052 assert(task->current_data_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_data_offset += subtask->scsi.length; 1061 iscsi_queue_task(conn, subtask); 1062 } 1063 if (task->current_data_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 process_completed_read_subtask_list_in_order(struct spdk_iscsi_conn *conn, 1081 struct spdk_iscsi_task *primary) 1082 { 1083 struct spdk_iscsi_task *subtask, *tmp; 1084 1085 TAILQ_FOREACH_SAFE(subtask, &primary->subtask_list, subtask_link, tmp) { 1086 if (subtask->scsi.offset == primary->bytes_completed) { 1087 TAILQ_REMOVE(&primary->subtask_list, subtask, subtask_link); 1088 primary->bytes_completed += subtask->scsi.length; 1089 if (primary->bytes_completed == primary->scsi.transfer_len) { 1090 iscsi_task_put(primary); 1091 } 1092 iscsi_task_response(conn, subtask); 1093 iscsi_task_put(subtask); 1094 } else { 1095 break; 1096 } 1097 } 1098 } 1099 1100 static void 1101 process_read_task_completion(struct spdk_iscsi_conn *conn, 1102 struct spdk_iscsi_task *task, 1103 struct spdk_iscsi_task *primary) 1104 { 1105 struct spdk_iscsi_task *tmp; 1106 1107 if (task->scsi.status != SPDK_SCSI_STATUS_GOOD) { 1108 if (primary->scsi.status == SPDK_SCSI_STATUS_GOOD) { 1109 /* If the status of the completed subtask, task, is the 1110 * first failure, copy it to out-of-order subtasks, and 1111 * remember it as the status of the SCSI Read Command. 1112 */ 1113 TAILQ_FOREACH(tmp, &primary->subtask_list, subtask_link) { 1114 spdk_scsi_task_copy_status(&tmp->scsi, &task->scsi); 1115 } 1116 spdk_scsi_task_copy_status(&primary->scsi, &task->scsi); 1117 } 1118 } else if (primary->scsi.status != SPDK_SCSI_STATUS_GOOD) { 1119 /* Even if the status of the completed subtask is success, 1120 * if there are any failed subtask ever, copy the first failed 1121 * status to it. 1122 */ 1123 spdk_scsi_task_copy_status(&task->scsi, &primary->scsi); 1124 } 1125 1126 if (task == primary) { 1127 /* If read I/O size is not larger than SPDK_BDEV_LARGE_BUF_MAX_SIZE, 1128 * the primary task which processes the SCSI Read Command PDU is 1129 * submitted directly. Hence send SCSI Response PDU for the primary 1130 * task simply. 1131 */ 1132 primary->bytes_completed = task->scsi.length; 1133 assert(primary->bytes_completed == task->scsi.transfer_len); 1134 iscsi_task_response(conn, task); 1135 iscsi_task_put(task); 1136 } else if (!conn->sess->DataSequenceInOrder) { 1137 /* If DataSequenceInOrder is No, send SCSI Response PDU for the completed 1138 * subtask without any deferral. 1139 */ 1140 primary->bytes_completed += task->scsi.length; 1141 if (primary->bytes_completed == primary->scsi.transfer_len) { 1142 iscsi_task_put(primary); 1143 } 1144 iscsi_task_response(conn, task); 1145 iscsi_task_put(task); 1146 } else { 1147 /* If DataSequenceInOrder is Yes, if the completed subtask is out-of-order, 1148 * it is deferred until all preceding subtasks send SCSI Response PDU. 1149 */ 1150 if (task->scsi.offset != primary->bytes_completed) { 1151 TAILQ_FOREACH(tmp, &primary->subtask_list, subtask_link) { 1152 if (task->scsi.offset < tmp->scsi.offset) { 1153 TAILQ_INSERT_BEFORE(tmp, task, subtask_link); 1154 return; 1155 } 1156 } 1157 1158 TAILQ_INSERT_TAIL(&primary->subtask_list, task, subtask_link); 1159 } else { 1160 TAILQ_INSERT_HEAD(&primary->subtask_list, task, subtask_link); 1161 process_completed_read_subtask_list_in_order(conn, primary); 1162 } 1163 } 1164 } 1165 1166 static void 1167 process_non_read_task_completion(struct spdk_iscsi_conn *conn, 1168 struct spdk_iscsi_task *task, 1169 struct spdk_iscsi_task *primary) 1170 { 1171 primary->bytes_completed += task->scsi.length; 1172 1173 if (task == primary) { 1174 /* This was a small write with no R2T. */ 1175 iscsi_task_response(conn, task); 1176 iscsi_task_put(task); 1177 return; 1178 } 1179 1180 if (task->scsi.status == SPDK_SCSI_STATUS_GOOD) { 1181 primary->scsi.data_transferred += task->scsi.data_transferred; 1182 } else if (primary->scsi.status == SPDK_SCSI_STATUS_GOOD) { 1183 /* If the status of this subtask is the first failure, copy it to 1184 * the primary task. 1185 */ 1186 spdk_scsi_task_copy_status(&primary->scsi, &task->scsi); 1187 } 1188 1189 if (primary->bytes_completed == primary->scsi.transfer_len) { 1190 /* If LUN is removed in the middle of the iSCSI write sequence, 1191 * primary might complete the write to the initiator because it is not 1192 * ensured that the initiator will send all data requested by R2Ts. 1193 * 1194 * We check it and skip the following if primary is completed. (see 1195 * iscsi_clear_all_transfer_task() in iscsi.c.) 1196 */ 1197 if (primary->is_r2t_active) { 1198 iscsi_task_response(conn, primary); 1199 iscsi_del_transfer_task(conn, primary->tag); 1200 } else { 1201 iscsi_task_response(conn, task); 1202 } 1203 } 1204 iscsi_task_put(task); 1205 } 1206 1207 void 1208 iscsi_task_cpl(struct spdk_scsi_task *scsi_task) 1209 { 1210 struct spdk_iscsi_task *primary; 1211 struct spdk_iscsi_task *task = iscsi_task_from_scsi_task(scsi_task); 1212 struct spdk_iscsi_conn *conn = task->conn; 1213 struct spdk_iscsi_pdu *pdu = task->pdu; 1214 1215 spdk_trace_record(TRACE_ISCSI_TASK_DONE, conn->id, 0, (uintptr_t)task, 0); 1216 1217 task->is_queued = false; 1218 primary = iscsi_task_get_primary(task); 1219 1220 if (iscsi_task_is_read(primary)) { 1221 process_read_task_completion(conn, task, primary); 1222 } else { 1223 process_non_read_task_completion(conn, task, primary); 1224 } 1225 if (!task->parent) { 1226 spdk_trace_record(TRACE_ISCSI_PDU_COMPLETED, 0, 0, (uintptr_t)pdu, 0); 1227 } 1228 } 1229 1230 static void 1231 iscsi_conn_send_nopin(struct spdk_iscsi_conn *conn) 1232 { 1233 struct spdk_iscsi_pdu *rsp_pdu; 1234 struct iscsi_bhs_nop_in *rsp; 1235 /* Only send nopin if we have logged in and are in a normal session. */ 1236 if (conn->sess == NULL || 1237 !conn->full_feature || 1238 !iscsi_param_eq_val(conn->sess->params, "SessionType", "Normal")) { 1239 return; 1240 } 1241 SPDK_DEBUGLOG(iscsi, "send NOPIN isid=%"PRIx64", tsih=%u, cid=%u\n", 1242 conn->sess->isid, conn->sess->tsih, conn->cid); 1243 SPDK_DEBUGLOG(iscsi, "StatSN=%u, ExpCmdSN=%u, MaxCmdSN=%u\n", 1244 conn->StatSN, conn->sess->ExpCmdSN, 1245 conn->sess->MaxCmdSN); 1246 rsp_pdu = iscsi_get_pdu(conn); 1247 rsp = (struct iscsi_bhs_nop_in *) &rsp_pdu->bhs; 1248 rsp_pdu->data = NULL; 1249 /* 1250 * iscsi_get_pdu() memset's the PDU for us, so only fill out the needed 1251 * fields. 1252 */ 1253 rsp->opcode = ISCSI_OP_NOPIN; 1254 rsp->flags = 0x80; 1255 /* 1256 * Technically the to_be32() is not needed here, since 1257 * to_be32(0xFFFFFFFU) returns 0xFFFFFFFFU. 1258 */ 1259 to_be32(&rsp->itt, 0xFFFFFFFFU); 1260 to_be32(&rsp->ttt, conn->id); 1261 to_be32(&rsp->stat_sn, conn->StatSN); 1262 to_be32(&rsp->exp_cmd_sn, conn->sess->ExpCmdSN); 1263 to_be32(&rsp->max_cmd_sn, conn->sess->MaxCmdSN); 1264 iscsi_conn_write_pdu(conn, rsp_pdu, iscsi_conn_pdu_generic_complete, NULL); 1265 conn->last_nopin = spdk_get_ticks(); 1266 conn->nop_outstanding = true; 1267 } 1268 1269 void 1270 iscsi_conn_handle_nop(struct spdk_iscsi_conn *conn) 1271 { 1272 uint64_t tsc; 1273 1274 /** 1275 * This function will be executed by nop_poller of iSCSI polling group, so 1276 * we need to check the connection state first, then do the nop interval 1277 * expiration check work. 1278 */ 1279 if ((conn->state == ISCSI_CONN_STATE_EXITED) || 1280 (conn->state == ISCSI_CONN_STATE_EXITING)) { 1281 return; 1282 } 1283 1284 /* Check for nop interval expiration */ 1285 tsc = spdk_get_ticks(); 1286 if (conn->nop_outstanding) { 1287 if ((tsc - conn->last_nopin) > conn->timeout) { 1288 SPDK_ERRLOG("Timed out waiting for NOP-Out response from initiator\n"); 1289 SPDK_ERRLOG(" tsc=0x%" PRIx64 ", last_nopin=0x%" PRIx64 "\n", tsc, conn->last_nopin); 1290 SPDK_ERRLOG(" initiator=%s, target=%s\n", conn->initiator_name, 1291 conn->target_short_name); 1292 conn->state = ISCSI_CONN_STATE_EXITING; 1293 } 1294 } else if (tsc - conn->last_nopin > conn->nopininterval) { 1295 iscsi_conn_send_nopin(conn); 1296 } 1297 } 1298 1299 /** 1300 * \brief Reads data for the specified iSCSI connection from its TCP socket. 1301 * 1302 * The TCP socket is marked as non-blocking, so this function may not read 1303 * all data requested. 1304 * 1305 * Returns SPDK_ISCSI_CONNECTION_FATAL if the recv() operation indicates a fatal 1306 * error with the TCP connection (including if the TCP connection was closed 1307 * unexpectedly. 1308 * 1309 * Otherwise returns the number of bytes successfully read. 1310 */ 1311 int 1312 iscsi_conn_read_data(struct spdk_iscsi_conn *conn, int bytes, 1313 void *buf) 1314 { 1315 int ret; 1316 1317 if (bytes == 0) { 1318 return 0; 1319 } 1320 1321 ret = spdk_sock_recv(conn->sock, buf, bytes); 1322 1323 if (ret > 0) { 1324 spdk_trace_record(TRACE_ISCSI_READ_FROM_SOCKET_DONE, conn->id, ret, 0, 0); 1325 return ret; 1326 } 1327 1328 if (ret < 0) { 1329 if (errno == EAGAIN || errno == EWOULDBLOCK) { 1330 return 0; 1331 } 1332 1333 /* For connect reset issue, do not output error log */ 1334 if (errno == ECONNRESET) { 1335 SPDK_DEBUGLOG(iscsi, "spdk_sock_recv() failed, errno %d: %s\n", 1336 errno, spdk_strerror(errno)); 1337 } else { 1338 SPDK_ERRLOG("spdk_sock_recv() failed, errno %d: %s\n", 1339 errno, spdk_strerror(errno)); 1340 } 1341 } 1342 1343 /* connection closed */ 1344 return SPDK_ISCSI_CONNECTION_FATAL; 1345 } 1346 1347 int 1348 iscsi_conn_readv_data(struct spdk_iscsi_conn *conn, 1349 struct iovec *iov, int iovcnt) 1350 { 1351 int ret; 1352 1353 if (iov == NULL || iovcnt == 0) { 1354 return 0; 1355 } 1356 1357 if (iovcnt == 1) { 1358 return iscsi_conn_read_data(conn, iov[0].iov_len, 1359 iov[0].iov_base); 1360 } 1361 1362 ret = spdk_sock_readv(conn->sock, iov, iovcnt); 1363 1364 if (ret > 0) { 1365 spdk_trace_record(TRACE_ISCSI_READ_FROM_SOCKET_DONE, conn->id, ret, 0, 0); 1366 return ret; 1367 } 1368 1369 if (ret < 0) { 1370 if (errno == EAGAIN || errno == EWOULDBLOCK) { 1371 return 0; 1372 } 1373 1374 /* For connect reset issue, do not output error log */ 1375 if (errno == ECONNRESET) { 1376 SPDK_DEBUGLOG(iscsi, "spdk_sock_readv() failed, errno %d: %s\n", 1377 errno, spdk_strerror(errno)); 1378 } else { 1379 SPDK_ERRLOG("spdk_sock_readv() failed, errno %d: %s\n", 1380 errno, spdk_strerror(errno)); 1381 } 1382 } 1383 1384 /* connection closed */ 1385 return SPDK_ISCSI_CONNECTION_FATAL; 1386 } 1387 1388 static bool 1389 iscsi_is_free_pdu_deferred(struct spdk_iscsi_pdu *pdu) 1390 { 1391 if (pdu == NULL) { 1392 return false; 1393 } 1394 1395 if (pdu->bhs.opcode == ISCSI_OP_R2T || 1396 pdu->bhs.opcode == ISCSI_OP_SCSI_DATAIN) { 1397 return true; 1398 } 1399 1400 return false; 1401 } 1402 1403 static int 1404 iscsi_dif_verify(struct spdk_iscsi_pdu *pdu, struct spdk_dif_ctx *dif_ctx) 1405 { 1406 struct iovec iov; 1407 struct spdk_dif_error err_blk = {}; 1408 uint32_t num_blocks; 1409 int rc; 1410 1411 iov.iov_base = pdu->data; 1412 iov.iov_len = pdu->data_buf_len; 1413 num_blocks = pdu->data_buf_len / dif_ctx->block_size; 1414 1415 rc = spdk_dif_verify(&iov, 1, num_blocks, dif_ctx, &err_blk); 1416 if (rc != 0) { 1417 SPDK_ERRLOG("DIF error detected. type=%d, offset=%" PRIu32 "\n", 1418 err_blk.err_type, err_blk.err_offset); 1419 } 1420 1421 return rc; 1422 } 1423 1424 static void 1425 _iscsi_conn_pdu_write_done(void *cb_arg, int err) 1426 { 1427 struct spdk_iscsi_pdu *pdu = cb_arg; 1428 struct spdk_iscsi_conn *conn = pdu->conn; 1429 1430 assert(conn != NULL); 1431 1432 if (spdk_unlikely(conn->state >= ISCSI_CONN_STATE_EXITING)) { 1433 /* The other policy will recycle the resource */ 1434 return; 1435 } 1436 1437 TAILQ_REMOVE(&conn->write_pdu_list, pdu, tailq); 1438 1439 if (err != 0) { 1440 conn->state = ISCSI_CONN_STATE_EXITING; 1441 } else { 1442 spdk_trace_record(TRACE_ISCSI_FLUSH_WRITEBUF_DONE, conn->id, pdu->mapped_length, (uintptr_t)pdu, 0); 1443 } 1444 1445 if ((conn->full_feature) && 1446 (conn->sess->ErrorRecoveryLevel >= 1) && 1447 iscsi_is_free_pdu_deferred(pdu)) { 1448 SPDK_DEBUGLOG(iscsi, "stat_sn=%d\n", 1449 from_be32(&pdu->bhs.stat_sn)); 1450 TAILQ_INSERT_TAIL(&conn->snack_pdu_list, pdu, 1451 tailq); 1452 } else { 1453 iscsi_conn_free_pdu(conn, pdu); 1454 } 1455 } 1456 1457 void 1458 iscsi_conn_pdu_generic_complete(void *cb_arg) 1459 { 1460 } 1461 1462 void 1463 iscsi_conn_write_pdu(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu, 1464 iscsi_conn_xfer_complete_cb cb_fn, 1465 void *cb_arg) 1466 { 1467 uint32_t crc32c; 1468 ssize_t rc; 1469 1470 if (spdk_unlikely(pdu->dif_insert_or_strip)) { 1471 rc = iscsi_dif_verify(pdu, &pdu->dif_ctx); 1472 if (rc != 0) { 1473 iscsi_conn_free_pdu(conn, pdu); 1474 conn->state = ISCSI_CONN_STATE_EXITING; 1475 return; 1476 } 1477 } 1478 1479 if (pdu->bhs.opcode != ISCSI_OP_LOGIN_RSP) { 1480 /* Header Digest */ 1481 if (conn->header_digest) { 1482 crc32c = iscsi_pdu_calc_header_digest(pdu); 1483 MAKE_DIGEST_WORD(pdu->header_digest, crc32c); 1484 } 1485 1486 /* Data Digest */ 1487 if (conn->data_digest && DGET24(pdu->bhs.data_segment_len) != 0) { 1488 crc32c = iscsi_pdu_calc_data_digest(pdu); 1489 MAKE_DIGEST_WORD(pdu->data_digest, crc32c); 1490 } 1491 } 1492 1493 pdu->cb_fn = cb_fn; 1494 pdu->cb_arg = cb_arg; 1495 TAILQ_INSERT_TAIL(&conn->write_pdu_list, pdu, tailq); 1496 1497 if (spdk_unlikely(conn->state >= ISCSI_CONN_STATE_EXITING)) { 1498 return; 1499 } 1500 pdu->sock_req.iovcnt = iscsi_build_iovs(conn, pdu->iov, SPDK_COUNTOF(pdu->iov), pdu, 1501 &pdu->mapped_length); 1502 pdu->sock_req.cb_fn = _iscsi_conn_pdu_write_done; 1503 pdu->sock_req.cb_arg = pdu; 1504 1505 spdk_trace_record(TRACE_ISCSI_FLUSH_WRITEBUF_START, conn->id, pdu->mapped_length, (uintptr_t)pdu, 1506 pdu->sock_req.iovcnt); 1507 spdk_sock_writev_async(conn->sock, &pdu->sock_req); 1508 } 1509 1510 static void 1511 iscsi_conn_sock_cb(void *arg, struct spdk_sock_group *group, struct spdk_sock *sock) 1512 { 1513 struct spdk_iscsi_conn *conn = arg; 1514 int rc; 1515 1516 assert(conn != NULL); 1517 1518 if ((conn->state == ISCSI_CONN_STATE_EXITED) || 1519 (conn->state == ISCSI_CONN_STATE_EXITING)) { 1520 return; 1521 } 1522 1523 /* Handle incoming PDUs */ 1524 rc = iscsi_handle_incoming_pdus(conn); 1525 if (rc < 0) { 1526 conn->state = ISCSI_CONN_STATE_EXITING; 1527 } 1528 } 1529 1530 static void 1531 iscsi_conn_full_feature_migrate(void *arg) 1532 { 1533 struct spdk_iscsi_conn *conn = arg; 1534 1535 if (conn->state >= ISCSI_CONN_STATE_EXITING) { 1536 /* Connection is being exited before this callback is executed. */ 1537 SPDK_DEBUGLOG(iscsi, "Connection is already exited.\n"); 1538 return; 1539 } 1540 1541 if (conn->sess->session_type == SESSION_TYPE_NORMAL) { 1542 iscsi_conn_open_luns(conn); 1543 } 1544 1545 /* Add this connection to the assigned poll group. */ 1546 iscsi_poll_group_add_conn(conn->pg, conn); 1547 } 1548 1549 static struct spdk_iscsi_poll_group *g_next_pg = NULL; 1550 1551 void 1552 iscsi_conn_schedule(struct spdk_iscsi_conn *conn) 1553 { 1554 struct spdk_iscsi_poll_group *pg; 1555 struct spdk_iscsi_tgt_node *target; 1556 1557 if (conn->sess->session_type != SESSION_TYPE_NORMAL) { 1558 /* Leave all non-normal sessions on the acceptor 1559 * thread. */ 1560 return; 1561 } 1562 pthread_mutex_lock(&g_iscsi.mutex); 1563 1564 target = conn->sess->target; 1565 pthread_mutex_lock(&target->mutex); 1566 target->num_active_conns++; 1567 if (target->num_active_conns == 1) { 1568 /** 1569 * This is the only active connection for this target node. 1570 * Pick a poll group using round-robin. 1571 */ 1572 if (g_next_pg == NULL) { 1573 g_next_pg = TAILQ_FIRST(&g_iscsi.poll_group_head); 1574 assert(g_next_pg != NULL); 1575 } 1576 1577 pg = g_next_pg; 1578 g_next_pg = TAILQ_NEXT(g_next_pg, link); 1579 1580 /* Save the pg in the target node so it can be used for any other connections to this target node. */ 1581 target->pg = pg; 1582 } else { 1583 /** 1584 * There are other active connections for this target node. 1585 */ 1586 pg = target->pg; 1587 } 1588 1589 pthread_mutex_unlock(&target->mutex); 1590 pthread_mutex_unlock(&g_iscsi.mutex); 1591 1592 assert(spdk_io_channel_get_thread(spdk_io_channel_from_ctx(conn->pg)) == 1593 spdk_get_thread()); 1594 1595 /* Remove this connection from the previous poll group */ 1596 iscsi_poll_group_remove_conn(conn->pg, conn); 1597 1598 conn->pg = pg; 1599 1600 spdk_thread_send_msg(spdk_io_channel_get_thread(spdk_io_channel_from_ctx(pg)), 1601 iscsi_conn_full_feature_migrate, conn); 1602 } 1603 1604 static int 1605 logout_timeout(void *arg) 1606 { 1607 struct spdk_iscsi_conn *conn = arg; 1608 1609 if (conn->state < ISCSI_CONN_STATE_EXITING) { 1610 conn->state = ISCSI_CONN_STATE_EXITING; 1611 } 1612 1613 return SPDK_POLLER_BUSY; 1614 } 1615 1616 void 1617 iscsi_conn_logout(struct spdk_iscsi_conn *conn) 1618 { 1619 conn->is_logged_out = true; 1620 conn->logout_timer = SPDK_POLLER_REGISTER(logout_timeout, conn, ISCSI_LOGOUT_TIMEOUT * 1000000); 1621 } 1622 1623 static const char * 1624 iscsi_conn_get_state(struct spdk_iscsi_conn *conn) 1625 { 1626 switch (conn->state) { 1627 SPDK_ISCSI_CONNECTION_STATUS(ISCSI_CONN_STATE_INVALID, "invalid"); 1628 SPDK_ISCSI_CONNECTION_STATUS(ISCSI_CONN_STATE_RUNNING, "running"); 1629 SPDK_ISCSI_CONNECTION_STATUS(ISCSI_CONN_STATE_EXITING, "exiting"); 1630 SPDK_ISCSI_CONNECTION_STATUS(ISCSI_CONN_STATE_EXITED, "exited"); 1631 } 1632 return "unknown"; 1633 } 1634 1635 static const char * 1636 iscsi_conn_get_login_phase(struct spdk_iscsi_conn *conn) 1637 { 1638 switch (conn->login_phase) { 1639 SPDK_ISCSI_CONNECTION_STATUS(ISCSI_SECURITY_NEGOTIATION_PHASE, "security_negotiation_phase"); 1640 SPDK_ISCSI_CONNECTION_STATUS(ISCSI_OPERATIONAL_NEGOTIATION_PHASE, "operational_negotiation_phase"); 1641 SPDK_ISCSI_CONNECTION_STATUS(ISCSI_FULL_FEATURE_PHASE, "full_feature_phase"); 1642 } 1643 return "not_started"; 1644 } 1645 1646 SPDK_TRACE_REGISTER_FN(iscsi_conn_trace, "iscsi_conn", TRACE_GROUP_ISCSI) 1647 { 1648 spdk_trace_register_owner(OWNER_ISCSI_CONN, 'c'); 1649 spdk_trace_register_object(OBJECT_ISCSI_PDU, 'p'); 1650 spdk_trace_register_description("ISCSI_READ_DONE", TRACE_ISCSI_READ_FROM_SOCKET_DONE, 1651 OWNER_ISCSI_CONN, OBJECT_NONE, 0, 1652 SPDK_TRACE_ARG_TYPE_INT, ""); 1653 spdk_trace_register_description("ISCSI_WRITE_START", TRACE_ISCSI_FLUSH_WRITEBUF_START, 1654 OWNER_ISCSI_CONN, OBJECT_ISCSI_PDU, 1, 1655 SPDK_TRACE_ARG_TYPE_INT, "iovec"); 1656 spdk_trace_register_description("ISCSI_WRITE_DONE", TRACE_ISCSI_FLUSH_WRITEBUF_DONE, 1657 OWNER_ISCSI_CONN, OBJECT_ISCSI_PDU, 0, 1658 SPDK_TRACE_ARG_TYPE_INT, ""); 1659 spdk_trace_register_description("ISCSI_READ_PDU", TRACE_ISCSI_READ_PDU, 1660 OWNER_ISCSI_CONN, OBJECT_ISCSI_PDU, 1, 1661 SPDK_TRACE_ARG_TYPE_INT, "opc"); 1662 spdk_trace_register_description("ISCSI_TASK_DONE", TRACE_ISCSI_TASK_DONE, 1663 OWNER_ISCSI_CONN, OBJECT_SCSI_TASK, 0, 1664 SPDK_TRACE_ARG_TYPE_INT, ""); 1665 spdk_trace_register_description("ISCSI_TASK_QUEUE", TRACE_ISCSI_TASK_QUEUE, 1666 OWNER_ISCSI_CONN, OBJECT_SCSI_TASK, 1, 1667 SPDK_TRACE_ARG_TYPE_PTR, "pdu"); 1668 spdk_trace_register_description("ISCSI_TASK_EXECUTED", TRACE_ISCSI_TASK_EXECUTED, 1669 OWNER_ISCSI_CONN, OBJECT_ISCSI_PDU, 0, 1670 SPDK_TRACE_ARG_TYPE_INT, ""); 1671 spdk_trace_register_description("ISCSI_PDU_COMPLETED", TRACE_ISCSI_PDU_COMPLETED, 1672 OWNER_ISCSI_CONN, OBJECT_ISCSI_PDU, 0, 1673 SPDK_TRACE_ARG_TYPE_INT, ""); 1674 } 1675 1676 void 1677 iscsi_conn_info_json(struct spdk_json_write_ctx *w, struct spdk_iscsi_conn *conn) 1678 { 1679 uint16_t tsih; 1680 1681 if (!conn->is_valid) { 1682 return; 1683 } 1684 1685 spdk_json_write_object_begin(w); 1686 1687 spdk_json_write_named_int32(w, "id", conn->id); 1688 1689 spdk_json_write_named_int32(w, "cid", conn->cid); 1690 1691 /* 1692 * If we try to return data for a connection that has not 1693 * logged in yet, the session will not be set. So in this 1694 * case, return -1 for the tsih rather than segfaulting 1695 * on the null conn->sess. 1696 */ 1697 if (conn->sess == NULL) { 1698 tsih = -1; 1699 } else { 1700 tsih = conn->sess->tsih; 1701 } 1702 spdk_json_write_named_int32(w, "tsih", tsih); 1703 1704 spdk_json_write_named_string(w, "state", iscsi_conn_get_state(conn)); 1705 1706 spdk_json_write_named_string(w, "login_phase", iscsi_conn_get_login_phase(conn)); 1707 1708 spdk_json_write_named_string(w, "initiator_addr", conn->initiator_addr); 1709 1710 spdk_json_write_named_string(w, "target_addr", conn->target_addr); 1711 1712 spdk_json_write_named_string(w, "target_node_name", conn->target_short_name); 1713 1714 spdk_json_write_named_string(w, "thread_name", 1715 spdk_thread_get_name(spdk_get_thread())); 1716 1717 spdk_json_write_object_end(w); 1718 } 1719