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