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 616 rc = iscsi_conn_free_tasks(conn); 617 if (rc < 0) { 618 /* The connection cannot be freed yet. Check back later. */ 619 conn->shutdown_timer = SPDK_POLLER_REGISTER(_iscsi_conn_check_shutdown, conn, 1000); 620 } else { 621 iscsi_conn_stop(conn); 622 iscsi_conn_free(conn); 623 } 624 } 625 626 static int 627 _iscsi_conn_check_pending_tasks(void *arg) 628 { 629 struct spdk_iscsi_conn *conn = arg; 630 631 if (conn->dev != NULL && 632 spdk_scsi_dev_has_pending_tasks(conn->dev, conn->initiator_port)) { 633 return SPDK_POLLER_BUSY; 634 } 635 636 spdk_poller_unregister(&conn->shutdown_timer); 637 638 _iscsi_conn_destruct(conn); 639 640 return SPDK_POLLER_BUSY; 641 } 642 643 void 644 iscsi_conn_destruct(struct spdk_iscsi_conn *conn) 645 { 646 struct spdk_iscsi_pdu *pdu; 647 struct spdk_iscsi_task *task; 648 int opcode; 649 650 /* If a connection is already in exited status, just return */ 651 if (conn->state >= ISCSI_CONN_STATE_EXITED) { 652 return; 653 } 654 655 conn->state = ISCSI_CONN_STATE_EXITED; 656 657 /* 658 * Each connection pre-allocates its next PDU - make sure these get 659 * freed here. 660 */ 661 pdu = conn->pdu_in_progress; 662 if (pdu) { 663 /* remove the task left in the PDU too. */ 664 task = pdu->task; 665 if (task) { 666 opcode = pdu->bhs.opcode; 667 switch (opcode) { 668 case ISCSI_OP_SCSI: 669 case ISCSI_OP_SCSI_DATAOUT: 670 spdk_scsi_task_process_abort(&task->scsi); 671 iscsi_task_cpl(&task->scsi); 672 break; 673 default: 674 SPDK_ERRLOG("unexpected opcode %x\n", opcode); 675 iscsi_task_put(task); 676 break; 677 } 678 } 679 iscsi_put_pdu(pdu); 680 conn->pdu_in_progress = NULL; 681 } 682 683 if (conn->sess != NULL && conn->pending_task_cnt > 0) { 684 iscsi_conn_cleanup_backend(conn); 685 } 686 687 if (conn->dev != NULL && 688 spdk_scsi_dev_has_pending_tasks(conn->dev, conn->initiator_port)) { 689 conn->shutdown_timer = SPDK_POLLER_REGISTER(_iscsi_conn_check_pending_tasks, conn, 1000); 690 } else { 691 _iscsi_conn_destruct(conn); 692 } 693 } 694 695 int 696 iscsi_get_active_conns(struct spdk_iscsi_tgt_node *target) 697 { 698 struct spdk_iscsi_conn *conn; 699 int num = 0; 700 701 if (g_conns_array == MAP_FAILED) { 702 return 0; 703 } 704 705 pthread_mutex_lock(&g_conns_mutex); 706 TAILQ_FOREACH(conn, &g_active_conns, conn_link) { 707 if (target == NULL || conn->target == target) { 708 num++; 709 } 710 } 711 pthread_mutex_unlock(&g_conns_mutex); 712 return num; 713 } 714 715 static void 716 iscsi_conn_check_shutdown_cb(void *arg1) 717 { 718 _iscsi_conns_cleanup(); 719 shutdown_iscsi_conns_done(); 720 } 721 722 static int 723 iscsi_conn_check_shutdown(void *arg) 724 { 725 if (iscsi_get_active_conns(NULL) != 0) { 726 return SPDK_POLLER_BUSY; 727 } 728 729 spdk_poller_unregister(&g_shutdown_timer); 730 731 spdk_thread_send_msg(spdk_get_thread(), iscsi_conn_check_shutdown_cb, NULL); 732 733 return SPDK_POLLER_BUSY; 734 } 735 736 static void 737 iscsi_send_logout_request(struct spdk_iscsi_conn *conn) 738 { 739 struct spdk_iscsi_pdu *rsp_pdu; 740 struct iscsi_bhs_async *rsph; 741 742 rsp_pdu = iscsi_get_pdu(conn); 743 assert(rsp_pdu != NULL); 744 745 rsph = (struct iscsi_bhs_async *)&rsp_pdu->bhs; 746 rsp_pdu->data = NULL; 747 748 rsph->opcode = ISCSI_OP_ASYNC; 749 to_be32(&rsph->ffffffff, 0xFFFFFFFF); 750 rsph->async_event = 1; 751 to_be16(&rsph->param3, ISCSI_LOGOUT_REQUEST_TIMEOUT); 752 753 to_be32(&rsph->stat_sn, conn->StatSN); 754 conn->StatSN++; 755 to_be32(&rsph->exp_cmd_sn, conn->sess->ExpCmdSN); 756 to_be32(&rsph->max_cmd_sn, conn->sess->MaxCmdSN); 757 758 iscsi_conn_write_pdu(conn, rsp_pdu, iscsi_conn_pdu_generic_complete, NULL); 759 } 760 761 static int 762 logout_request_timeout(void *arg) 763 { 764 struct spdk_iscsi_conn *conn = arg; 765 766 if (conn->state < ISCSI_CONN_STATE_EXITING) { 767 conn->state = ISCSI_CONN_STATE_EXITING; 768 } 769 770 return SPDK_POLLER_BUSY; 771 } 772 773 /* If the connection is running and logout is not requested yet, request logout 774 * to initiator and wait for the logout process to start. 775 */ 776 static void 777 _iscsi_conn_request_logout(void *ctx) 778 { 779 struct spdk_iscsi_conn *conn = ctx; 780 781 if (conn->state > ISCSI_CONN_STATE_RUNNING || 782 conn->logout_request_timer != NULL) { 783 return; 784 } 785 786 iscsi_send_logout_request(conn); 787 788 conn->logout_request_timer = SPDK_POLLER_REGISTER(logout_request_timeout, 789 conn, ISCSI_LOGOUT_REQUEST_TIMEOUT * 1000000); 790 } 791 792 static void 793 iscsi_conn_request_logout(struct spdk_iscsi_conn *conn) 794 { 795 struct spdk_thread *thread; 796 797 if (conn->state == ISCSI_CONN_STATE_INVALID) { 798 /* Move it to EXITING state if the connection is in login. */ 799 conn->state = ISCSI_CONN_STATE_EXITING; 800 } else if (conn->state == ISCSI_CONN_STATE_RUNNING && 801 conn->logout_request_timer == NULL) { 802 thread = spdk_io_channel_get_thread(spdk_io_channel_from_ctx(conn->pg)); 803 spdk_thread_send_msg(thread, _iscsi_conn_request_logout, conn); 804 } 805 } 806 807 void 808 iscsi_conns_request_logout(struct spdk_iscsi_tgt_node *target, int pg_tag) 809 { 810 struct spdk_iscsi_conn *conn; 811 812 if (g_conns_array == MAP_FAILED) { 813 return; 814 } 815 816 pthread_mutex_lock(&g_conns_mutex); 817 TAILQ_FOREACH(conn, &g_active_conns, conn_link) { 818 if ((target == NULL) || 819 (conn->target == target && (pg_tag < 0 || conn->pg_tag == pg_tag))) { 820 iscsi_conn_request_logout(conn); 821 } 822 } 823 pthread_mutex_unlock(&g_conns_mutex); 824 } 825 826 void 827 shutdown_iscsi_conns(void) 828 { 829 iscsi_conns_request_logout(NULL, -1); 830 831 g_shutdown_timer = SPDK_POLLER_REGISTER(iscsi_conn_check_shutdown, NULL, 1000); 832 } 833 834 /* Do not set conn->state if the connection has already started exiting. 835 * This ensures we do not move a connection from EXITED state back to EXITING. 836 */ 837 static void 838 _iscsi_conn_drop(void *ctx) 839 { 840 struct spdk_iscsi_conn *conn = ctx; 841 842 if (conn->state < ISCSI_CONN_STATE_EXITING) { 843 conn->state = ISCSI_CONN_STATE_EXITING; 844 } 845 } 846 847 int 848 iscsi_drop_conns(struct spdk_iscsi_conn *conn, const char *conn_match, 849 int drop_all) 850 { 851 struct spdk_iscsi_conn *xconn; 852 const char *xconn_match; 853 struct spdk_thread *thread; 854 int num; 855 856 SPDK_DEBUGLOG(iscsi, "iscsi_drop_conns\n"); 857 858 num = 0; 859 pthread_mutex_lock(&g_conns_mutex); 860 if (g_conns_array == MAP_FAILED) { 861 goto exit; 862 } 863 864 TAILQ_FOREACH(xconn, &g_active_conns, conn_link) { 865 if (xconn == conn) { 866 continue; 867 } 868 869 if (!drop_all && xconn->initiator_port == NULL) { 870 continue; 871 } 872 873 xconn_match = 874 drop_all ? xconn->initiator_name : spdk_scsi_port_get_name(xconn->initiator_port); 875 876 if (!strcasecmp(conn_match, xconn_match) && 877 conn->target == xconn->target) { 878 879 if (num == 0) { 880 /* 881 * Only print this message before we report the 882 * first dropped connection. 883 */ 884 SPDK_ERRLOG("drop old connections %s by %s\n", 885 conn->target->name, conn_match); 886 } 887 888 SPDK_ERRLOG("exiting conn by %s (%s)\n", 889 xconn_match, xconn->initiator_addr); 890 if (xconn->sess != NULL) { 891 SPDK_DEBUGLOG(iscsi, "TSIH=%u\n", xconn->sess->tsih); 892 } else { 893 SPDK_DEBUGLOG(iscsi, "TSIH=xx\n"); 894 } 895 896 SPDK_DEBUGLOG(iscsi, "CID=%u\n", xconn->cid); 897 898 thread = spdk_io_channel_get_thread(spdk_io_channel_from_ctx(xconn->pg)); 899 spdk_thread_send_msg(thread, _iscsi_conn_drop, xconn); 900 901 num++; 902 } 903 } 904 905 exit: 906 pthread_mutex_unlock(&g_conns_mutex); 907 908 if (num != 0) { 909 SPDK_ERRLOG("exiting %d conns\n", num); 910 } 911 912 return 0; 913 } 914 915 static int 916 _iscsi_conn_abort_queued_datain_task(struct spdk_iscsi_conn *conn, 917 struct spdk_iscsi_task *task) 918 { 919 struct spdk_iscsi_task *subtask; 920 uint32_t remaining_size; 921 922 if (conn->data_in_cnt >= g_iscsi.MaxLargeDataInPerConnection) { 923 return -1; 924 } 925 926 assert(task->current_data_offset <= task->scsi.transfer_len); 927 /* Stop split and abort read I/O for remaining data. */ 928 if (task->current_data_offset < task->scsi.transfer_len) { 929 remaining_size = task->scsi.transfer_len - task->current_data_offset; 930 subtask = iscsi_task_get(conn, task, iscsi_task_cpl); 931 assert(subtask != NULL); 932 subtask->scsi.offset = task->current_data_offset; 933 subtask->scsi.length = remaining_size; 934 spdk_scsi_task_set_data(&subtask->scsi, NULL, 0); 935 task->current_data_offset += subtask->scsi.length; 936 937 subtask->scsi.transfer_len = subtask->scsi.length; 938 spdk_scsi_task_process_abort(&subtask->scsi); 939 iscsi_task_cpl(&subtask->scsi); 940 } 941 942 /* Remove the primary task from the list because all subtasks are submitted 943 * or aborted. 944 */ 945 assert(task->current_data_offset == task->scsi.transfer_len); 946 TAILQ_REMOVE(&conn->queued_datain_tasks, task, link); 947 return 0; 948 } 949 950 int 951 iscsi_conn_abort_queued_datain_task(struct spdk_iscsi_conn *conn, 952 uint32_t ref_task_tag) 953 { 954 struct spdk_iscsi_task *task; 955 956 TAILQ_FOREACH(task, &conn->queued_datain_tasks, link) { 957 if (task->tag == ref_task_tag) { 958 return _iscsi_conn_abort_queued_datain_task(conn, task); 959 } 960 } 961 962 return 0; 963 } 964 965 int 966 iscsi_conn_abort_queued_datain_tasks(struct spdk_iscsi_conn *conn, 967 struct spdk_scsi_lun *lun, 968 struct spdk_iscsi_pdu *pdu) 969 { 970 struct spdk_iscsi_task *task, *task_tmp; 971 struct spdk_iscsi_pdu *pdu_tmp; 972 int rc; 973 974 TAILQ_FOREACH_SAFE(task, &conn->queued_datain_tasks, link, task_tmp) { 975 pdu_tmp = iscsi_task_get_pdu(task); 976 if ((lun == NULL || lun == task->scsi.lun) && 977 (pdu == NULL || (spdk_sn32_lt(pdu_tmp->cmd_sn, pdu->cmd_sn)))) { 978 rc = _iscsi_conn_abort_queued_datain_task(conn, task); 979 if (rc != 0) { 980 return rc; 981 } 982 } 983 } 984 985 return 0; 986 } 987 988 int 989 iscsi_conn_handle_queued_datain_tasks(struct spdk_iscsi_conn *conn) 990 { 991 struct spdk_iscsi_task *task; 992 993 while (!TAILQ_EMPTY(&conn->queued_datain_tasks) && 994 conn->data_in_cnt < g_iscsi.MaxLargeDataInPerConnection) { 995 task = TAILQ_FIRST(&conn->queued_datain_tasks); 996 assert(task->current_data_offset <= task->scsi.transfer_len); 997 if (task->current_data_offset < task->scsi.transfer_len) { 998 struct spdk_iscsi_task *subtask; 999 uint32_t remaining_size = 0; 1000 1001 remaining_size = task->scsi.transfer_len - task->current_data_offset; 1002 subtask = iscsi_task_get(conn, task, iscsi_task_cpl); 1003 assert(subtask != NULL); 1004 subtask->scsi.offset = task->current_data_offset; 1005 spdk_scsi_task_set_data(&subtask->scsi, NULL, 0); 1006 1007 if (spdk_scsi_dev_get_lun(conn->dev, task->lun_id) == NULL) { 1008 /* Stop submitting split read I/Os for remaining data. */ 1009 TAILQ_REMOVE(&conn->queued_datain_tasks, task, link); 1010 task->current_data_offset += remaining_size; 1011 assert(task->current_data_offset == task->scsi.transfer_len); 1012 subtask->scsi.transfer_len = remaining_size; 1013 spdk_scsi_task_process_null_lun(&subtask->scsi); 1014 iscsi_task_cpl(&subtask->scsi); 1015 return 0; 1016 } 1017 1018 subtask->scsi.length = spdk_min(SPDK_BDEV_LARGE_BUF_MAX_SIZE, remaining_size); 1019 task->current_data_offset += subtask->scsi.length; 1020 iscsi_queue_task(conn, subtask); 1021 } 1022 if (task->current_data_offset == task->scsi.transfer_len) { 1023 TAILQ_REMOVE(&conn->queued_datain_tasks, task, link); 1024 } 1025 } 1026 return 0; 1027 } 1028 1029 void 1030 iscsi_task_mgmt_cpl(struct spdk_scsi_task *scsi_task) 1031 { 1032 struct spdk_iscsi_task *task = iscsi_task_from_scsi_task(scsi_task); 1033 1034 iscsi_task_mgmt_response(task->conn, task); 1035 iscsi_task_put(task); 1036 } 1037 1038 static void 1039 process_completed_read_subtask_list_in_order(struct spdk_iscsi_conn *conn, 1040 struct spdk_iscsi_task *primary) 1041 { 1042 struct spdk_iscsi_task *subtask, *tmp; 1043 1044 TAILQ_FOREACH_SAFE(subtask, &primary->subtask_list, subtask_link, tmp) { 1045 if (subtask->scsi.offset == primary->bytes_completed) { 1046 TAILQ_REMOVE(&primary->subtask_list, subtask, subtask_link); 1047 primary->bytes_completed += subtask->scsi.length; 1048 if (primary->bytes_completed == primary->scsi.transfer_len) { 1049 iscsi_task_put(primary); 1050 } 1051 iscsi_task_response(conn, subtask); 1052 iscsi_task_put(subtask); 1053 } else { 1054 break; 1055 } 1056 } 1057 } 1058 1059 static void 1060 process_read_task_completion(struct spdk_iscsi_conn *conn, 1061 struct spdk_iscsi_task *task, 1062 struct spdk_iscsi_task *primary) 1063 { 1064 struct spdk_iscsi_task *tmp; 1065 1066 if (task->scsi.status != SPDK_SCSI_STATUS_GOOD) { 1067 if (primary->scsi.status == SPDK_SCSI_STATUS_GOOD) { 1068 /* If the status of the completed subtask, task, is the 1069 * first failure, copy it to out-of-order subtasks, and 1070 * remember it as the status of the SCSI Read Command. 1071 */ 1072 TAILQ_FOREACH(tmp, &primary->subtask_list, subtask_link) { 1073 spdk_scsi_task_copy_status(&tmp->scsi, &task->scsi); 1074 } 1075 spdk_scsi_task_copy_status(&primary->scsi, &task->scsi); 1076 } 1077 } else if (primary->scsi.status != SPDK_SCSI_STATUS_GOOD) { 1078 /* Even if the status of the completed subtask is success, 1079 * if there are any failed subtask ever, copy the first failed 1080 * status to it. 1081 */ 1082 spdk_scsi_task_copy_status(&task->scsi, &primary->scsi); 1083 } 1084 1085 if (task == primary) { 1086 /* If read I/O size is not larger than SPDK_BDEV_LARGE_BUF_MAX_SIZE, 1087 * the primary task which processes the SCSI Read Command PDU is 1088 * submitted directly. Hence send SCSI Response PDU for the primary 1089 * task simply. 1090 */ 1091 primary->bytes_completed = task->scsi.length; 1092 assert(primary->bytes_completed == task->scsi.transfer_len); 1093 iscsi_task_response(conn, task); 1094 iscsi_task_put(task); 1095 } else if (!conn->sess->DataSequenceInOrder) { 1096 /* If DataSequenceInOrder is No, send SCSI Response PDU for the completed 1097 * subtask without any deferral. 1098 */ 1099 primary->bytes_completed += task->scsi.length; 1100 if (primary->bytes_completed == primary->scsi.transfer_len) { 1101 iscsi_task_put(primary); 1102 } 1103 iscsi_task_response(conn, task); 1104 iscsi_task_put(task); 1105 } else { 1106 /* If DataSequenceInOrder is Yes, if the completed subtask is out-of-order, 1107 * it is deferred until all preceding subtasks send SCSI Response PDU. 1108 */ 1109 if (task->scsi.offset != primary->bytes_completed) { 1110 TAILQ_FOREACH(tmp, &primary->subtask_list, subtask_link) { 1111 if (task->scsi.offset < tmp->scsi.offset) { 1112 TAILQ_INSERT_BEFORE(tmp, task, subtask_link); 1113 return; 1114 } 1115 } 1116 1117 TAILQ_INSERT_TAIL(&primary->subtask_list, task, subtask_link); 1118 } else { 1119 TAILQ_INSERT_HEAD(&primary->subtask_list, task, subtask_link); 1120 process_completed_read_subtask_list_in_order(conn, primary); 1121 } 1122 } 1123 } 1124 1125 static void 1126 process_non_read_task_completion(struct spdk_iscsi_conn *conn, 1127 struct spdk_iscsi_task *task, 1128 struct spdk_iscsi_task *primary) 1129 { 1130 primary->bytes_completed += task->scsi.length; 1131 1132 if (task == primary) { 1133 /* This was a small write with no R2T. */ 1134 iscsi_task_response(conn, task); 1135 iscsi_task_put(task); 1136 return; 1137 } 1138 1139 if (task->scsi.status == SPDK_SCSI_STATUS_GOOD) { 1140 primary->scsi.data_transferred += task->scsi.data_transferred; 1141 } else if (primary->scsi.status == SPDK_SCSI_STATUS_GOOD) { 1142 /* If the status of this subtask is the first failure, copy it to 1143 * the primary task. 1144 */ 1145 spdk_scsi_task_copy_status(&primary->scsi, &task->scsi); 1146 } 1147 1148 if (primary->bytes_completed == primary->scsi.transfer_len) { 1149 /* If LUN is removed in the middle of the iSCSI write sequence, 1150 * primary might complete the write to the initiator because it is not 1151 * ensured that the initiator will send all data requested by R2Ts. 1152 * 1153 * We check it and skip the following if primary is completed. (see 1154 * iscsi_clear_all_transfer_task() in iscsi.c.) 1155 */ 1156 if (primary->is_r2t_active) { 1157 iscsi_task_response(conn, primary); 1158 iscsi_del_transfer_task(conn, primary->tag); 1159 } else { 1160 iscsi_task_response(conn, task); 1161 } 1162 } 1163 iscsi_task_put(task); 1164 } 1165 1166 void 1167 iscsi_task_cpl(struct spdk_scsi_task *scsi_task) 1168 { 1169 struct spdk_iscsi_task *primary; 1170 struct spdk_iscsi_task *task = iscsi_task_from_scsi_task(scsi_task); 1171 struct spdk_iscsi_conn *conn = task->conn; 1172 struct spdk_iscsi_pdu *pdu = task->pdu; 1173 1174 spdk_trace_record(TRACE_ISCSI_TASK_DONE, conn->id, 0, (uintptr_t)task); 1175 1176 task->is_queued = false; 1177 primary = iscsi_task_get_primary(task); 1178 1179 if (iscsi_task_is_read(primary)) { 1180 process_read_task_completion(conn, task, primary); 1181 } else { 1182 process_non_read_task_completion(conn, task, primary); 1183 } 1184 if (!task->parent) { 1185 spdk_trace_record(TRACE_ISCSI_PDU_COMPLETED, 0, 0, (uintptr_t)pdu); 1186 } 1187 } 1188 1189 static void 1190 iscsi_conn_send_nopin(struct spdk_iscsi_conn *conn) 1191 { 1192 struct spdk_iscsi_pdu *rsp_pdu; 1193 struct iscsi_bhs_nop_in *rsp; 1194 /* Only send nopin if we have logged in and are in a normal session. */ 1195 if (conn->sess == NULL || 1196 !conn->full_feature || 1197 !iscsi_param_eq_val(conn->sess->params, "SessionType", "Normal")) { 1198 return; 1199 } 1200 SPDK_DEBUGLOG(iscsi, "send NOPIN isid=%"PRIx64", tsih=%u, cid=%u\n", 1201 conn->sess->isid, conn->sess->tsih, conn->cid); 1202 SPDK_DEBUGLOG(iscsi, "StatSN=%u, ExpCmdSN=%u, MaxCmdSN=%u\n", 1203 conn->StatSN, conn->sess->ExpCmdSN, 1204 conn->sess->MaxCmdSN); 1205 rsp_pdu = iscsi_get_pdu(conn); 1206 rsp = (struct iscsi_bhs_nop_in *) &rsp_pdu->bhs; 1207 rsp_pdu->data = NULL; 1208 /* 1209 * iscsi_get_pdu() memset's the PDU for us, so only fill out the needed 1210 * fields. 1211 */ 1212 rsp->opcode = ISCSI_OP_NOPIN; 1213 rsp->flags = 0x80; 1214 /* 1215 * Technically the to_be32() is not needed here, since 1216 * to_be32(0xFFFFFFFU) returns 0xFFFFFFFFU. 1217 */ 1218 to_be32(&rsp->itt, 0xFFFFFFFFU); 1219 to_be32(&rsp->ttt, conn->id); 1220 to_be32(&rsp->stat_sn, conn->StatSN); 1221 to_be32(&rsp->exp_cmd_sn, conn->sess->ExpCmdSN); 1222 to_be32(&rsp->max_cmd_sn, conn->sess->MaxCmdSN); 1223 iscsi_conn_write_pdu(conn, rsp_pdu, iscsi_conn_pdu_generic_complete, NULL); 1224 conn->last_nopin = spdk_get_ticks(); 1225 conn->nop_outstanding = true; 1226 } 1227 1228 void 1229 iscsi_conn_handle_nop(struct spdk_iscsi_conn *conn) 1230 { 1231 uint64_t tsc; 1232 1233 /** 1234 * This function will be executed by nop_poller of iSCSI polling group, so 1235 * we need to check the connection state first, then do the nop interval 1236 * expiration check work. 1237 */ 1238 if ((conn->state == ISCSI_CONN_STATE_EXITED) || 1239 (conn->state == ISCSI_CONN_STATE_EXITING)) { 1240 return; 1241 } 1242 1243 /* Check for nop interval expiration */ 1244 tsc = spdk_get_ticks(); 1245 if (conn->nop_outstanding) { 1246 if ((tsc - conn->last_nopin) > conn->timeout) { 1247 SPDK_ERRLOG("Timed out waiting for NOP-Out response from initiator\n"); 1248 SPDK_ERRLOG(" tsc=0x%" PRIx64 ", last_nopin=0x%" PRIx64 "\n", tsc, conn->last_nopin); 1249 SPDK_ERRLOG(" initiator=%s, target=%s\n", conn->initiator_name, 1250 conn->target_short_name); 1251 conn->state = ISCSI_CONN_STATE_EXITING; 1252 } 1253 } else if (tsc - conn->last_nopin > conn->nopininterval) { 1254 iscsi_conn_send_nopin(conn); 1255 } 1256 } 1257 1258 /** 1259 * \brief Reads data for the specified iSCSI connection from its TCP socket. 1260 * 1261 * The TCP socket is marked as non-blocking, so this function may not read 1262 * all data requested. 1263 * 1264 * Returns SPDK_ISCSI_CONNECTION_FATAL if the recv() operation indicates a fatal 1265 * error with the TCP connection (including if the TCP connection was closed 1266 * unexpectedly. 1267 * 1268 * Otherwise returns the number of bytes successfully read. 1269 */ 1270 int 1271 iscsi_conn_read_data(struct spdk_iscsi_conn *conn, int bytes, 1272 void *buf) 1273 { 1274 int ret; 1275 1276 if (bytes == 0) { 1277 return 0; 1278 } 1279 1280 ret = spdk_sock_recv(conn->sock, buf, bytes); 1281 1282 if (ret > 0) { 1283 spdk_trace_record(TRACE_ISCSI_READ_FROM_SOCKET_DONE, conn->id, ret, 0); 1284 return ret; 1285 } 1286 1287 if (ret < 0) { 1288 if (errno == EAGAIN || errno == EWOULDBLOCK) { 1289 return 0; 1290 } 1291 1292 /* For connect reset issue, do not output error log */ 1293 if (errno == ECONNRESET) { 1294 SPDK_DEBUGLOG(iscsi, "spdk_sock_recv() failed, errno %d: %s\n", 1295 errno, spdk_strerror(errno)); 1296 } else { 1297 SPDK_ERRLOG("spdk_sock_recv() failed, errno %d: %s\n", 1298 errno, spdk_strerror(errno)); 1299 } 1300 } 1301 1302 /* connection closed */ 1303 return SPDK_ISCSI_CONNECTION_FATAL; 1304 } 1305 1306 int 1307 iscsi_conn_readv_data(struct spdk_iscsi_conn *conn, 1308 struct iovec *iov, int iovcnt) 1309 { 1310 int ret; 1311 1312 if (iov == NULL || iovcnt == 0) { 1313 return 0; 1314 } 1315 1316 if (iovcnt == 1) { 1317 return iscsi_conn_read_data(conn, iov[0].iov_len, 1318 iov[0].iov_base); 1319 } 1320 1321 ret = spdk_sock_readv(conn->sock, iov, iovcnt); 1322 1323 if (ret > 0) { 1324 spdk_trace_record(TRACE_ISCSI_READ_FROM_SOCKET_DONE, conn->id, ret, 0); 1325 return ret; 1326 } 1327 1328 if (ret < 0) { 1329 if (errno == EAGAIN || errno == EWOULDBLOCK) { 1330 return 0; 1331 } 1332 1333 /* For connect reset issue, do not output error log */ 1334 if (errno == ECONNRESET) { 1335 SPDK_DEBUGLOG(iscsi, "spdk_sock_readv() failed, errno %d: %s\n", 1336 errno, spdk_strerror(errno)); 1337 } else { 1338 SPDK_ERRLOG("spdk_sock_readv() failed, errno %d: %s\n", 1339 errno, spdk_strerror(errno)); 1340 } 1341 } 1342 1343 /* connection closed */ 1344 return SPDK_ISCSI_CONNECTION_FATAL; 1345 } 1346 1347 static bool 1348 iscsi_is_free_pdu_deferred(struct spdk_iscsi_pdu *pdu) 1349 { 1350 if (pdu == NULL) { 1351 return false; 1352 } 1353 1354 if (pdu->bhs.opcode == ISCSI_OP_R2T || 1355 pdu->bhs.opcode == ISCSI_OP_SCSI_DATAIN) { 1356 return true; 1357 } 1358 1359 return false; 1360 } 1361 1362 static int 1363 iscsi_dif_verify(struct spdk_iscsi_pdu *pdu, struct spdk_dif_ctx *dif_ctx) 1364 { 1365 struct iovec iov; 1366 struct spdk_dif_error err_blk = {}; 1367 uint32_t num_blocks; 1368 int rc; 1369 1370 iov.iov_base = pdu->data; 1371 iov.iov_len = pdu->data_buf_len; 1372 num_blocks = pdu->data_buf_len / dif_ctx->block_size; 1373 1374 rc = spdk_dif_verify(&iov, 1, num_blocks, dif_ctx, &err_blk); 1375 if (rc != 0) { 1376 SPDK_ERRLOG("DIF error detected. type=%d, offset=%" PRIu32 "\n", 1377 err_blk.err_type, err_blk.err_offset); 1378 } 1379 1380 return rc; 1381 } 1382 1383 static void 1384 _iscsi_conn_pdu_write_done(void *cb_arg, int err) 1385 { 1386 struct spdk_iscsi_pdu *pdu = cb_arg; 1387 struct spdk_iscsi_conn *conn = pdu->conn; 1388 1389 assert(conn != NULL); 1390 1391 if (spdk_unlikely(conn->state >= ISCSI_CONN_STATE_EXITING)) { 1392 /* The other policy will recycle the resource */ 1393 return; 1394 } 1395 1396 TAILQ_REMOVE(&conn->write_pdu_list, pdu, tailq); 1397 1398 if (err != 0) { 1399 conn->state = ISCSI_CONN_STATE_EXITING; 1400 } else { 1401 spdk_trace_record(TRACE_ISCSI_FLUSH_WRITEBUF_DONE, conn->id, pdu->mapped_length, (uintptr_t)pdu); 1402 } 1403 1404 if ((conn->full_feature) && 1405 (conn->sess->ErrorRecoveryLevel >= 1) && 1406 iscsi_is_free_pdu_deferred(pdu)) { 1407 SPDK_DEBUGLOG(iscsi, "stat_sn=%d\n", 1408 from_be32(&pdu->bhs.stat_sn)); 1409 TAILQ_INSERT_TAIL(&conn->snack_pdu_list, pdu, 1410 tailq); 1411 } else { 1412 iscsi_conn_free_pdu(conn, pdu); 1413 } 1414 } 1415 1416 void 1417 iscsi_conn_pdu_generic_complete(void *cb_arg) 1418 { 1419 } 1420 1421 void 1422 iscsi_conn_write_pdu(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu, 1423 iscsi_conn_xfer_complete_cb cb_fn, 1424 void *cb_arg) 1425 { 1426 uint32_t crc32c; 1427 ssize_t rc; 1428 1429 if (spdk_unlikely(pdu->dif_insert_or_strip)) { 1430 rc = iscsi_dif_verify(pdu, &pdu->dif_ctx); 1431 if (rc != 0) { 1432 iscsi_conn_free_pdu(conn, pdu); 1433 conn->state = ISCSI_CONN_STATE_EXITING; 1434 return; 1435 } 1436 } 1437 1438 if (pdu->bhs.opcode != ISCSI_OP_LOGIN_RSP) { 1439 /* Header Digest */ 1440 if (conn->header_digest) { 1441 crc32c = iscsi_pdu_calc_header_digest(pdu); 1442 MAKE_DIGEST_WORD(pdu->header_digest, crc32c); 1443 } 1444 1445 /* Data Digest */ 1446 if (conn->data_digest && DGET24(pdu->bhs.data_segment_len) != 0) { 1447 crc32c = iscsi_pdu_calc_data_digest(pdu); 1448 MAKE_DIGEST_WORD(pdu->data_digest, crc32c); 1449 } 1450 } 1451 1452 pdu->cb_fn = cb_fn; 1453 pdu->cb_arg = cb_arg; 1454 TAILQ_INSERT_TAIL(&conn->write_pdu_list, pdu, tailq); 1455 1456 if (spdk_unlikely(conn->state >= ISCSI_CONN_STATE_EXITING)) { 1457 return; 1458 } 1459 pdu->sock_req.iovcnt = iscsi_build_iovs(conn, pdu->iov, SPDK_COUNTOF(pdu->iov), pdu, 1460 &pdu->mapped_length); 1461 pdu->sock_req.cb_fn = _iscsi_conn_pdu_write_done; 1462 pdu->sock_req.cb_arg = pdu; 1463 1464 spdk_trace_record(TRACE_ISCSI_FLUSH_WRITEBUF_START, conn->id, pdu->mapped_length, (uintptr_t)pdu, 1465 pdu->sock_req.iovcnt); 1466 spdk_sock_writev_async(conn->sock, &pdu->sock_req); 1467 } 1468 1469 static void 1470 iscsi_conn_sock_cb(void *arg, struct spdk_sock_group *group, struct spdk_sock *sock) 1471 { 1472 struct spdk_iscsi_conn *conn = arg; 1473 int rc; 1474 1475 assert(conn != NULL); 1476 1477 if ((conn->state == ISCSI_CONN_STATE_EXITED) || 1478 (conn->state == ISCSI_CONN_STATE_EXITING)) { 1479 return; 1480 } 1481 1482 /* Handle incoming PDUs */ 1483 rc = iscsi_handle_incoming_pdus(conn); 1484 if (rc < 0) { 1485 conn->state = ISCSI_CONN_STATE_EXITING; 1486 } 1487 } 1488 1489 static void 1490 iscsi_conn_full_feature_migrate(void *arg) 1491 { 1492 struct spdk_iscsi_conn *conn = arg; 1493 1494 if (conn->state >= ISCSI_CONN_STATE_EXITING) { 1495 /* Connection is being exited before this callback is executed. */ 1496 SPDK_DEBUGLOG(iscsi, "Connection is already exited.\n"); 1497 return; 1498 } 1499 1500 if (conn->sess->session_type == SESSION_TYPE_NORMAL) { 1501 iscsi_conn_open_luns(conn); 1502 } 1503 1504 /* Add this connection to the assigned poll group. */ 1505 iscsi_poll_group_add_conn(conn->pg, conn); 1506 } 1507 1508 static struct spdk_iscsi_poll_group *g_next_pg = NULL; 1509 1510 void 1511 iscsi_conn_schedule(struct spdk_iscsi_conn *conn) 1512 { 1513 struct spdk_iscsi_poll_group *pg; 1514 struct spdk_iscsi_tgt_node *target; 1515 1516 if (conn->sess->session_type != SESSION_TYPE_NORMAL) { 1517 /* Leave all non-normal sessions on the acceptor 1518 * thread. */ 1519 return; 1520 } 1521 pthread_mutex_lock(&g_iscsi.mutex); 1522 1523 target = conn->sess->target; 1524 pthread_mutex_lock(&target->mutex); 1525 target->num_active_conns++; 1526 if (target->num_active_conns == 1) { 1527 /** 1528 * This is the only active connection for this target node. 1529 * Pick a poll group using round-robin. 1530 */ 1531 if (g_next_pg == NULL) { 1532 g_next_pg = TAILQ_FIRST(&g_iscsi.poll_group_head); 1533 assert(g_next_pg != NULL); 1534 } 1535 1536 pg = g_next_pg; 1537 g_next_pg = TAILQ_NEXT(g_next_pg, link); 1538 1539 /* Save the pg in the target node so it can be used for any other connections to this target node. */ 1540 target->pg = pg; 1541 } else { 1542 /** 1543 * There are other active connections for this target node. 1544 */ 1545 pg = target->pg; 1546 } 1547 1548 pthread_mutex_unlock(&target->mutex); 1549 pthread_mutex_unlock(&g_iscsi.mutex); 1550 1551 assert(spdk_io_channel_get_thread(spdk_io_channel_from_ctx(conn->pg)) == 1552 spdk_get_thread()); 1553 1554 /* Remove this connection from the previous poll group */ 1555 iscsi_poll_group_remove_conn(conn->pg, conn); 1556 1557 conn->pg = pg; 1558 1559 spdk_thread_send_msg(spdk_io_channel_get_thread(spdk_io_channel_from_ctx(pg)), 1560 iscsi_conn_full_feature_migrate, conn); 1561 } 1562 1563 static int 1564 logout_timeout(void *arg) 1565 { 1566 struct spdk_iscsi_conn *conn = arg; 1567 1568 if (conn->state < ISCSI_CONN_STATE_EXITING) { 1569 conn->state = ISCSI_CONN_STATE_EXITING; 1570 } 1571 1572 return SPDK_POLLER_BUSY; 1573 } 1574 1575 void 1576 iscsi_conn_logout(struct spdk_iscsi_conn *conn) 1577 { 1578 conn->is_logged_out = true; 1579 conn->logout_timer = SPDK_POLLER_REGISTER(logout_timeout, conn, ISCSI_LOGOUT_TIMEOUT * 1000000); 1580 } 1581 1582 static const char * 1583 iscsi_conn_get_state(struct spdk_iscsi_conn *conn) 1584 { 1585 switch (conn->state) { 1586 SPDK_ISCSI_CONNECTION_STATUS(ISCSI_CONN_STATE_INVALID, "invalid"); 1587 SPDK_ISCSI_CONNECTION_STATUS(ISCSI_CONN_STATE_RUNNING, "running"); 1588 SPDK_ISCSI_CONNECTION_STATUS(ISCSI_CONN_STATE_EXITING, "exiting"); 1589 SPDK_ISCSI_CONNECTION_STATUS(ISCSI_CONN_STATE_EXITED, "exited"); 1590 } 1591 return "unknown"; 1592 } 1593 1594 static const char * 1595 iscsi_conn_get_login_phase(struct spdk_iscsi_conn *conn) 1596 { 1597 switch (conn->login_phase) { 1598 SPDK_ISCSI_CONNECTION_STATUS(ISCSI_SECURITY_NEGOTIATION_PHASE, "security_negotiation_phase"); 1599 SPDK_ISCSI_CONNECTION_STATUS(ISCSI_OPERATIONAL_NEGOTIATION_PHASE, "operational_negotiation_phase"); 1600 SPDK_ISCSI_CONNECTION_STATUS(ISCSI_FULL_FEATURE_PHASE, "full_feature_phase"); 1601 } 1602 return "not_started"; 1603 } 1604 1605 SPDK_TRACE_REGISTER_FN(iscsi_conn_trace, "iscsi_conn", TRACE_GROUP_ISCSI) 1606 { 1607 spdk_trace_register_owner(OWNER_ISCSI_CONN, 'c'); 1608 spdk_trace_register_object(OBJECT_ISCSI_PDU, 'p'); 1609 spdk_trace_register_description("ISCSI_READ_DONE", TRACE_ISCSI_READ_FROM_SOCKET_DONE, 1610 OWNER_ISCSI_CONN, OBJECT_NONE, 0, 1611 SPDK_TRACE_ARG_TYPE_INT, ""); 1612 spdk_trace_register_description("ISCSI_WRITE_START", TRACE_ISCSI_FLUSH_WRITEBUF_START, 1613 OWNER_ISCSI_CONN, OBJECT_ISCSI_PDU, 1, 1614 SPDK_TRACE_ARG_TYPE_INT, "iovec"); 1615 spdk_trace_register_description("ISCSI_WRITE_DONE", TRACE_ISCSI_FLUSH_WRITEBUF_DONE, 1616 OWNER_ISCSI_CONN, OBJECT_ISCSI_PDU, 0, 1617 SPDK_TRACE_ARG_TYPE_INT, ""); 1618 spdk_trace_register_description("ISCSI_READ_PDU", TRACE_ISCSI_READ_PDU, 1619 OWNER_ISCSI_CONN, OBJECT_ISCSI_PDU, 1, 1620 SPDK_TRACE_ARG_TYPE_INT, "opc"); 1621 spdk_trace_register_description("ISCSI_TASK_DONE", TRACE_ISCSI_TASK_DONE, 1622 OWNER_ISCSI_CONN, OBJECT_SCSI_TASK, 0, 1623 SPDK_TRACE_ARG_TYPE_INT, ""); 1624 spdk_trace_register_description("ISCSI_TASK_QUEUE", TRACE_ISCSI_TASK_QUEUE, 1625 OWNER_ISCSI_CONN, OBJECT_SCSI_TASK, 1, 1626 SPDK_TRACE_ARG_TYPE_PTR, "pdu"); 1627 spdk_trace_register_description("ISCSI_TASK_EXECUTED", TRACE_ISCSI_TASK_EXECUTED, 1628 OWNER_ISCSI_CONN, OBJECT_ISCSI_PDU, 0, 1629 SPDK_TRACE_ARG_TYPE_INT, ""); 1630 spdk_trace_register_description("ISCSI_PDU_COMPLETED", TRACE_ISCSI_PDU_COMPLETED, 1631 OWNER_ISCSI_CONN, OBJECT_ISCSI_PDU, 0, 1632 SPDK_TRACE_ARG_TYPE_INT, ""); 1633 } 1634 1635 void 1636 iscsi_conn_info_json(struct spdk_json_write_ctx *w, struct spdk_iscsi_conn *conn) 1637 { 1638 uint16_t tsih; 1639 1640 if (!conn->is_valid) { 1641 return; 1642 } 1643 1644 spdk_json_write_object_begin(w); 1645 1646 spdk_json_write_named_int32(w, "id", conn->id); 1647 1648 spdk_json_write_named_int32(w, "cid", conn->cid); 1649 1650 /* 1651 * If we try to return data for a connection that has not 1652 * logged in yet, the session will not be set. So in this 1653 * case, return -1 for the tsih rather than segfaulting 1654 * on the null conn->sess. 1655 */ 1656 if (conn->sess == NULL) { 1657 tsih = -1; 1658 } else { 1659 tsih = conn->sess->tsih; 1660 } 1661 spdk_json_write_named_int32(w, "tsih", tsih); 1662 1663 spdk_json_write_named_string(w, "state", iscsi_conn_get_state(conn)); 1664 1665 spdk_json_write_named_string(w, "login_phase", iscsi_conn_get_login_phase(conn)); 1666 1667 spdk_json_write_named_string(w, "initiator_addr", conn->initiator_addr); 1668 1669 spdk_json_write_named_string(w, "target_addr", conn->target_addr); 1670 1671 spdk_json_write_named_string(w, "target_node_name", conn->target_short_name); 1672 1673 spdk_json_write_named_string(w, "thread_name", 1674 spdk_thread_get_name(spdk_get_thread())); 1675 1676 spdk_json_write_object_end(w); 1677 } 1678