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