1 /*- 2 * BSD LICENSE 3 * 4 * Copyright (c) Intel Corporation. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * * Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * * Neither the name of Intel Corporation nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include "spdk/stdinc.h" 35 #include "spdk/crc32.h" 36 #include "spdk/endian.h" 37 #include "spdk/assert.h" 38 #include "spdk/thread.h" 39 #include "spdk/nvmf.h" 40 #include "spdk/nvmf_spec.h" 41 #include "spdk/sock.h" 42 #include "spdk/string.h" 43 #include "spdk/trace.h" 44 #include "spdk/util.h" 45 46 #include "nvmf_internal.h" 47 #include "transport.h" 48 49 #include "spdk_internal/log.h" 50 #include "spdk_internal/nvme_tcp.h" 51 52 #define NVMF_TCP_MAX_ACCEPT_SOCK_ONE_TIME 16 53 54 #define NVMF_TCP_PDU_MAX_H2C_DATA_SIZE 131072 55 #define NVMF_TCP_PDU_MAX_C2H_DATA_SIZE 131072 56 #define NVMF_TCP_QPAIR_MAX_C2H_PDU_NUM 64 /* Maximal c2h_data pdu number for ecah tqpair */ 57 58 /* This is used to support the Linux kernel NVMe-oF initiator */ 59 #define LINUX_KERNEL_SUPPORT_NOT_SENDING_RESP_FOR_C2H 0 60 61 /* spdk nvmf related structure */ 62 enum spdk_nvmf_tcp_req_state { 63 64 /* The request is not currently in use */ 65 TCP_REQUEST_STATE_FREE = 0, 66 67 /* Initial state when request first received */ 68 TCP_REQUEST_STATE_NEW, 69 70 /* The request is queued until a data buffer is available. */ 71 TCP_REQUEST_STATE_NEED_BUFFER, 72 73 /* The request is pending on r2t slots */ 74 TCP_REQUEST_STATE_DATA_PENDING_FOR_R2T, 75 76 /* The request is currently transferring data from the host to the controller. */ 77 TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER, 78 79 /* The request is ready to execute at the block device */ 80 TCP_REQUEST_STATE_READY_TO_EXECUTE, 81 82 /* The request is currently executing at the block device */ 83 TCP_REQUEST_STATE_EXECUTING, 84 85 /* The request finished executing at the block device */ 86 TCP_REQUEST_STATE_EXECUTED, 87 88 /* The request is ready to send a completion */ 89 TCP_REQUEST_STATE_READY_TO_COMPLETE, 90 91 /* The request is currently transferring final pdus from the controller to the host. */ 92 TCP_REQUEST_STATE_TRANSFERRING_CONTROLLER_TO_HOST, 93 94 /* The request completed and can be marked free. */ 95 TCP_REQUEST_STATE_COMPLETED, 96 97 /* Terminator */ 98 TCP_REQUEST_NUM_STATES, 99 }; 100 101 static const char *spdk_nvmf_tcp_term_req_fes_str[] = { 102 "Invalid PDU Header Field", 103 "PDU Sequence Error", 104 "Header Digiest Error", 105 "Data Transfer Out of Range", 106 "R2T Limit Exceeded", 107 "Unsupported parameter", 108 }; 109 110 #define OBJECT_NVMF_TCP_IO 0x80 111 112 #define TRACE_GROUP_NVMF_TCP 0x5 113 #define TRACE_TCP_REQUEST_STATE_NEW SPDK_TPOINT_ID(TRACE_GROUP_NVMF_TCP, 0x0) 114 #define TRACE_TCP_REQUEST_STATE_NEED_BUFFER SPDK_TPOINT_ID(TRACE_GROUP_NVMF_TCP, 0x1) 115 #define TRACE_TCP_REQUEST_STATE_DATA_PENDING_FOR_R2T SPDK_TPOINT_ID(TRACE_GROUP_NVMF_TCP, 0x2) 116 #define TRACE_TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER SPDK_TPOINT_ID(TRACE_GROUP_NVMF_TCP, 0x3) 117 #define TRACE_TCP_REQUEST_STATE_READY_TO_EXECUTE SPDK_TPOINT_ID(TRACE_GROUP_NVMF_TCP, 0x4) 118 #define TRACE_TCP_REQUEST_STATE_EXECUTING SPDK_TPOINT_ID(TRACE_GROUP_NVMF_TCP, 0x5) 119 #define TRACE_TCP_REQUEST_STATE_EXECUTED SPDK_TPOINT_ID(TRACE_GROUP_NVMF_TCP, 0x6) 120 #define TRACE_TCP_REQUEST_STATE_READY_TO_COMPLETE SPDK_TPOINT_ID(TRACE_GROUP_NVMF_TCP, 0x7) 121 #define TRACE_TCP_REQUEST_STATE_TRANSFERRING_CONTROLLER_TO_HOST SPDK_TPOINT_ID(TRACE_GROUP_NVMF_TCP, 0x8) 122 #define TRACE_TCP_REQUEST_STATE_COMPLETED SPDK_TPOINT_ID(TRACE_GROUP_NVMF_TCP, 0x9) 123 #define TRACE_TCP_FLUSH_WRITEBUF_START SPDK_TPOINT_ID(TRACE_GROUP_NVMF_TCP, 0xA) 124 #define TRACE_TCP_FLUSH_WRITEBUF_DONE SPDK_TPOINT_ID(TRACE_GROUP_NVMF_TCP, 0xB) 125 #define TRACE_TCP_READ_FROM_SOCKET_DONE SPDK_TPOINT_ID(TRACE_GROUP_NVMF_TCP, 0xC) 126 127 SPDK_TRACE_REGISTER_FN(nvmf_tcp_trace, "nvmf_tcp", TRACE_GROUP_NVMF_TCP) 128 { 129 spdk_trace_register_object(OBJECT_NVMF_TCP_IO, 'r'); 130 spdk_trace_register_description("TCP_REQ_NEW", 131 TRACE_TCP_REQUEST_STATE_NEW, 132 OWNER_NONE, OBJECT_NVMF_TCP_IO, 1, 1, ""); 133 spdk_trace_register_description("TCP_REQ_NEED_BUFFER", 134 TRACE_TCP_REQUEST_STATE_NEED_BUFFER, 135 OWNER_NONE, OBJECT_NVMF_TCP_IO, 0, 1, ""); 136 spdk_trace_register_description("TCP_REQ_TX_PENDING_R2T", 137 TRACE_TCP_REQUEST_STATE_DATA_PENDING_FOR_R2T, 138 OWNER_NONE, OBJECT_NVMF_TCP_IO, 0, 1, ""); 139 spdk_trace_register_description("TCP_REQ_TX_H_TO_C", 140 TRACE_TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER, 141 OWNER_NONE, OBJECT_NVMF_TCP_IO, 0, 1, ""); 142 spdk_trace_register_description("TCP_REQ_RDY_TO_EXECUTE", 143 TRACE_TCP_REQUEST_STATE_READY_TO_EXECUTE, 144 OWNER_NONE, OBJECT_NVMF_TCP_IO, 0, 1, ""); 145 spdk_trace_register_description("TCP_REQ_EXECUTING", 146 TRACE_TCP_REQUEST_STATE_EXECUTING, 147 OWNER_NONE, OBJECT_NVMF_TCP_IO, 0, 1, ""); 148 spdk_trace_register_description("TCP_REQ_EXECUTED", 149 TRACE_TCP_REQUEST_STATE_EXECUTED, 150 OWNER_NONE, OBJECT_NVMF_TCP_IO, 0, 1, ""); 151 spdk_trace_register_description("TCP_REQ_RDY_TO_COMPLETE", 152 TRACE_TCP_REQUEST_STATE_READY_TO_COMPLETE, 153 OWNER_NONE, OBJECT_NVMF_TCP_IO, 0, 1, ""); 154 spdk_trace_register_description("TCP_REQ_TRANSFER_C2H", 155 TRACE_TCP_REQUEST_STATE_TRANSFERRING_CONTROLLER_TO_HOST, 156 OWNER_NONE, OBJECT_NVMF_TCP_IO, 0, 1, ""); 157 spdk_trace_register_description("TCP_REQ_COMPLETED", 158 TRACE_TCP_REQUEST_STATE_COMPLETED, 159 OWNER_NONE, OBJECT_NVMF_TCP_IO, 0, 1, ""); 160 spdk_trace_register_description("TCP_WRITE_START", 161 TRACE_TCP_FLUSH_WRITEBUF_START, 162 OWNER_NONE, OBJECT_NONE, 0, 0, ""); 163 spdk_trace_register_description("TCP_WRITE_DONE", 164 TRACE_TCP_FLUSH_WRITEBUF_DONE, 165 OWNER_NONE, OBJECT_NONE, 0, 0, ""); 166 spdk_trace_register_description("TCP_READ_DONE", 167 TRACE_TCP_READ_FROM_SOCKET_DONE, 168 OWNER_NONE, OBJECT_NONE, 0, 0, ""); 169 } 170 171 struct spdk_nvmf_tcp_req { 172 struct spdk_nvmf_request req; 173 struct spdk_nvme_cpl rsp; 174 struct spdk_nvme_cmd cmd; 175 176 /* In-capsule data buffer */ 177 uint8_t *buf; 178 179 bool data_from_pool; 180 void *buffers[SPDK_NVMF_MAX_SGL_ENTRIES]; 181 182 /* transfer_tag */ 183 uint16_t ttag; 184 185 /* 186 * next_expected_r2t_offset is used when we receive the h2c_data PDU. 187 */ 188 uint32_t next_expected_r2t_offset; 189 uint32_t r2tl_remain; 190 191 /* 192 * c2h_data_offset is used when we send the c2h_data PDU. 193 */ 194 uint32_t c2h_data_offset; 195 uint32_t c2h_data_pdu_num; 196 197 enum spdk_nvmf_tcp_req_state state; 198 bool has_incapsule_data; 199 200 TAILQ_ENTRY(spdk_nvmf_tcp_req) link; 201 TAILQ_ENTRY(spdk_nvmf_tcp_req) state_link; 202 }; 203 204 struct spdk_nvmf_tcp_qpair { 205 struct spdk_nvmf_qpair qpair; 206 struct spdk_nvmf_tcp_poll_group *group; 207 struct spdk_nvmf_tcp_port *port; 208 struct spdk_sock *sock; 209 struct spdk_poller *flush_poller; 210 211 enum nvme_tcp_pdu_recv_state recv_state; 212 enum nvme_tcp_qpair_state state; 213 214 struct nvme_tcp_pdu pdu_in_progress; 215 216 TAILQ_HEAD(, nvme_tcp_pdu) send_queue; 217 TAILQ_HEAD(, nvme_tcp_pdu) free_queue; 218 219 struct nvme_tcp_pdu *pdu; 220 struct nvme_tcp_pdu *pdu_pool; 221 uint16_t free_pdu_num; 222 223 /* Queues to track the requests in all states */ 224 TAILQ_HEAD(, spdk_nvmf_tcp_req) state_queue[TCP_REQUEST_NUM_STATES]; 225 /* Number of requests in each state */ 226 int32_t state_cntr[TCP_REQUEST_NUM_STATES]; 227 228 uint32_t maxr2t; 229 uint32_t pending_r2t; 230 TAILQ_HEAD(, spdk_nvmf_tcp_req) queued_c2h_data_tcp_req; 231 232 uint8_t cpda; 233 234 /* Array of size "max_queue_depth * InCapsuleDataSize" containing 235 * buffers to be used for in capsule data. 236 */ 237 void *buf; 238 void *bufs; 239 struct spdk_nvmf_tcp_req *req; 240 struct spdk_nvmf_tcp_req *reqs; 241 242 bool host_hdgst_enable; 243 bool host_ddgst_enable; 244 245 246 /* The maximum number of I/O outstanding on this connection at one time */ 247 uint16_t max_queue_depth; 248 249 250 /** Specifies the maximum number of PDU-Data bytes per H2C Data Transfer PDU */ 251 uint32_t maxh2cdata; 252 253 uint32_t c2h_data_pdu_cnt; 254 255 /* IP address */ 256 char initiator_addr[SPDK_NVMF_TRADDR_MAX_LEN]; 257 char target_addr[SPDK_NVMF_TRADDR_MAX_LEN]; 258 259 /* IP port */ 260 uint16_t initiator_port; 261 uint16_t target_port; 262 263 /* Timer used to destroy qpair after detecting transport error issue if initiator does 264 * not close the connection. 265 */ 266 struct spdk_poller *timeout_poller; 267 268 TAILQ_ENTRY(spdk_nvmf_tcp_qpair) link; 269 }; 270 271 struct spdk_nvmf_tcp_poll_group { 272 struct spdk_nvmf_transport_poll_group group; 273 struct spdk_sock_group *sock_group; 274 275 /* Requests that are waiting to obtain a data buffer */ 276 TAILQ_HEAD(, spdk_nvmf_tcp_req) pending_data_buf_queue; 277 278 TAILQ_HEAD(, spdk_nvmf_tcp_qpair) qpairs; 279 }; 280 281 struct spdk_nvmf_tcp_port { 282 struct spdk_nvme_transport_id trid; 283 struct spdk_sock *listen_sock; 284 uint32_t ref; 285 TAILQ_ENTRY(spdk_nvmf_tcp_port) link; 286 }; 287 288 struct spdk_nvmf_tcp_transport { 289 struct spdk_nvmf_transport transport; 290 291 pthread_mutex_t lock; 292 293 TAILQ_HEAD(, spdk_nvmf_tcp_port) ports; 294 }; 295 296 static void spdk_nvmf_tcp_qpair_process_pending(struct spdk_nvmf_tcp_transport *ttransport, 297 struct spdk_nvmf_tcp_qpair *tqpair); 298 static bool spdk_nvmf_tcp_req_process(struct spdk_nvmf_tcp_transport *ttransport, 299 struct spdk_nvmf_tcp_req *tcp_req); 300 static void spdk_nvmf_tcp_handle_pending_c2h_data_queue(struct spdk_nvmf_tcp_qpair *tqpair); 301 302 static void 303 spdk_nvmf_tcp_req_set_state(struct spdk_nvmf_tcp_req *tcp_req, 304 enum spdk_nvmf_tcp_req_state state) 305 { 306 struct spdk_nvmf_qpair *qpair; 307 struct spdk_nvmf_tcp_qpair *tqpair; 308 309 qpair = tcp_req->req.qpair; 310 tqpair = SPDK_CONTAINEROF(qpair, struct spdk_nvmf_tcp_qpair, qpair); 311 312 TAILQ_REMOVE(&tqpair->state_queue[tcp_req->state], tcp_req, state_link); 313 tqpair->state_cntr[tcp_req->state]--; 314 assert(tqpair->state_cntr[tcp_req->state] >= 0); 315 316 TAILQ_INSERT_TAIL(&tqpair->state_queue[state], tcp_req, state_link); 317 tqpair->state_cntr[state]++; 318 319 tcp_req->state = state; 320 } 321 322 static struct nvme_tcp_pdu * 323 spdk_nvmf_tcp_pdu_get(struct spdk_nvmf_tcp_qpair *tqpair) 324 { 325 struct nvme_tcp_pdu *pdu; 326 327 pdu = TAILQ_FIRST(&tqpair->free_queue); 328 if (!pdu) { 329 SPDK_ERRLOG("Unable to get PDU for tqpair=%p\n", tqpair); 330 abort(); 331 return NULL; 332 } 333 334 tqpair->free_pdu_num--; 335 TAILQ_REMOVE(&tqpair->free_queue, pdu, tailq); 336 memset(pdu, 0, sizeof(*pdu)); 337 pdu->ref = 1; 338 339 return pdu; 340 } 341 342 static void 343 spdk_nvmf_tcp_pdu_put(struct spdk_nvmf_tcp_qpair *tqpair, struct nvme_tcp_pdu *pdu) 344 { 345 if (!pdu) { 346 return; 347 } 348 349 assert(pdu->ref > 0); 350 351 pdu->ref--; 352 if (pdu->ref == 0) { 353 tqpair->free_pdu_num++; 354 TAILQ_INSERT_HEAD(&tqpair->free_queue, pdu, tailq); 355 } 356 } 357 358 static struct spdk_nvmf_tcp_req * 359 spdk_nvmf_tcp_req_get(struct spdk_nvmf_tcp_qpair *tqpair) 360 { 361 struct spdk_nvmf_tcp_req *tcp_req; 362 363 tcp_req = TAILQ_FIRST(&tqpair->state_queue[TCP_REQUEST_STATE_FREE]); 364 if (!tcp_req) { 365 SPDK_ERRLOG("Cannot allocate tcp_req on tqpair=%p\n", tqpair); 366 return NULL; 367 } 368 369 memset(&tcp_req->cmd, 0, sizeof(tcp_req->cmd)); 370 memset(&tcp_req->rsp, 0, sizeof(tcp_req->rsp)); 371 tcp_req->next_expected_r2t_offset = 0; 372 tcp_req->r2tl_remain = 0; 373 tcp_req->c2h_data_offset = 0; 374 tcp_req->has_incapsule_data = false; 375 376 spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_NEW); 377 return tcp_req; 378 } 379 380 static void 381 nvmf_tcp_request_free(struct spdk_nvmf_tcp_req *tcp_req) 382 { 383 struct spdk_nvmf_tcp_transport *ttransport; 384 385 if (!tcp_req) { 386 return; 387 } 388 389 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "tcp_req=%p will be freed\n", tcp_req); 390 ttransport = SPDK_CONTAINEROF(tcp_req->req.qpair->transport, 391 struct spdk_nvmf_tcp_transport, transport); 392 spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_COMPLETED); 393 spdk_nvmf_tcp_req_process(ttransport, tcp_req); 394 } 395 396 static int 397 spdk_nvmf_tcp_req_free(struct spdk_nvmf_request *req) 398 { 399 struct spdk_nvmf_tcp_req *tcp_req = SPDK_CONTAINEROF(req, struct spdk_nvmf_tcp_req, req); 400 401 nvmf_tcp_request_free(tcp_req); 402 403 return 0; 404 } 405 406 static void 407 spdk_nvmf_tcp_drain_state_queue(struct spdk_nvmf_tcp_qpair *tqpair, 408 enum spdk_nvmf_tcp_req_state state) 409 { 410 struct spdk_nvmf_tcp_req *tcp_req, *req_tmp; 411 412 TAILQ_FOREACH_SAFE(tcp_req, &tqpair->state_queue[state], state_link, req_tmp) { 413 nvmf_tcp_request_free(tcp_req); 414 } 415 } 416 417 static void 418 spdk_nvmf_tcp_cleanup_all_states(struct spdk_nvmf_tcp_qpair *tqpair) 419 { 420 struct spdk_nvmf_tcp_req *tcp_req, *req_tmp; 421 struct nvme_tcp_pdu *pdu, *tmp_pdu; 422 423 /* Free the pdus in the send_queue */ 424 TAILQ_FOREACH_SAFE(pdu, &tqpair->send_queue, tailq, tmp_pdu) { 425 TAILQ_REMOVE(&tqpair->send_queue, pdu, tailq); 426 /* Also check the pdu type, we need to calculte the c2h_data_pdu_cnt later */ 427 if (pdu->hdr.common.pdu_type == SPDK_NVME_TCP_PDU_TYPE_C2H_DATA) { 428 assert(tqpair->c2h_data_pdu_cnt > 0); 429 tqpair->c2h_data_pdu_cnt--; 430 } 431 spdk_nvmf_tcp_pdu_put(tqpair, pdu); 432 } 433 434 TAILQ_FOREACH_SAFE(tcp_req, &tqpair->queued_c2h_data_tcp_req, link, req_tmp) { 435 TAILQ_REMOVE(&tqpair->queued_c2h_data_tcp_req, tcp_req, link); 436 } 437 spdk_nvmf_tcp_drain_state_queue(tqpair, TCP_REQUEST_STATE_TRANSFERRING_CONTROLLER_TO_HOST); 438 439 spdk_nvmf_tcp_drain_state_queue(tqpair, TCP_REQUEST_STATE_NEW); 440 441 spdk_nvmf_tcp_drain_state_queue(tqpair, TCP_REQUEST_STATE_DATA_PENDING_FOR_R2T); 442 443 /* Wipe the requests waiting for buffer from the global list */ 444 TAILQ_FOREACH_SAFE(tcp_req, &tqpair->state_queue[TCP_REQUEST_STATE_NEED_BUFFER], state_link, 445 req_tmp) { 446 TAILQ_REMOVE(&tqpair->group->pending_data_buf_queue, tcp_req, link); 447 } 448 449 spdk_nvmf_tcp_drain_state_queue(tqpair, TCP_REQUEST_STATE_NEED_BUFFER); 450 spdk_nvmf_tcp_drain_state_queue(tqpair, TCP_REQUEST_STATE_EXECUTING); 451 spdk_nvmf_tcp_drain_state_queue(tqpair, TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER); 452 } 453 454 static void 455 nvmf_tcp_dump_qpair_req_contents(struct spdk_nvmf_tcp_qpair *tqpair) 456 { 457 int i; 458 struct spdk_nvmf_tcp_req *tcp_req; 459 460 SPDK_ERRLOG("Dumping contents of queue pair (QID %d)\n", tqpair->qpair.qid); 461 for (i = 1; i < TCP_REQUEST_NUM_STATES; i++) { 462 SPDK_ERRLOG("\tNum of requests in state[%d] = %d\n", i, tqpair->state_cntr[i]); 463 TAILQ_FOREACH(tcp_req, &tqpair->state_queue[i], state_link) { 464 SPDK_ERRLOG("\t\tRequest Data From Pool: %d\n", tcp_req->data_from_pool); 465 SPDK_ERRLOG("\t\tRequest opcode: %d\n", tcp_req->req.cmd->nvmf_cmd.opcode); 466 } 467 } 468 } 469 470 static void 471 spdk_nvmf_tcp_qpair_destroy(struct spdk_nvmf_tcp_qpair *tqpair) 472 { 473 int err = 0; 474 475 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "enter\n"); 476 477 spdk_poller_unregister(&tqpair->flush_poller); 478 spdk_sock_close(&tqpair->sock); 479 spdk_nvmf_tcp_cleanup_all_states(tqpair); 480 481 if (tqpair->free_pdu_num != (tqpair->max_queue_depth + NVMF_TCP_QPAIR_MAX_C2H_PDU_NUM)) { 482 SPDK_ERRLOG("tqpair(%p) free pdu pool num is %u but should be %u\n", tqpair, 483 tqpair->free_pdu_num, 484 (tqpair->max_queue_depth + NVMF_TCP_QPAIR_MAX_C2H_PDU_NUM)); 485 err++; 486 } 487 488 if (tqpair->state_cntr[TCP_REQUEST_STATE_FREE] != tqpair->max_queue_depth) { 489 SPDK_ERRLOG("tqpair(%p) free tcp request num is %u but should be %u\n", tqpair, 490 tqpair->state_cntr[TCP_REQUEST_STATE_FREE], 491 tqpair->max_queue_depth); 492 err++; 493 } 494 495 if (tqpair->c2h_data_pdu_cnt != 0) { 496 SPDK_ERRLOG("tqpair(%p) free c2h_data_pdu cnt is %u but should be 0\n", tqpair, 497 tqpair->c2h_data_pdu_cnt); 498 err++; 499 } 500 501 if (err > 0) { 502 nvmf_tcp_dump_qpair_req_contents(tqpair); 503 } 504 free(tqpair->pdu); 505 free(tqpair->pdu_pool); 506 free(tqpair->req); 507 free(tqpair->reqs); 508 spdk_dma_free(tqpair->buf); 509 spdk_dma_free(tqpair->bufs); 510 free(tqpair); 511 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Leave\n"); 512 } 513 514 static int 515 spdk_nvmf_tcp_destroy(struct spdk_nvmf_transport *transport) 516 { 517 struct spdk_nvmf_tcp_transport *ttransport; 518 519 assert(transport != NULL); 520 ttransport = SPDK_CONTAINEROF(transport, struct spdk_nvmf_tcp_transport, transport); 521 522 pthread_mutex_destroy(&ttransport->lock); 523 free(ttransport); 524 return 0; 525 } 526 527 static struct spdk_nvmf_transport * 528 spdk_nvmf_tcp_create(struct spdk_nvmf_transport_opts *opts) 529 { 530 struct spdk_nvmf_tcp_transport *ttransport; 531 uint32_t sge_count; 532 uint32_t min_shared_buffers; 533 534 ttransport = calloc(1, sizeof(*ttransport)); 535 if (!ttransport) { 536 return NULL; 537 } 538 539 TAILQ_INIT(&ttransport->ports); 540 541 ttransport->transport.ops = &spdk_nvmf_transport_tcp; 542 543 SPDK_NOTICELOG("*** TCP Transport Init ***\n"); 544 545 SPDK_INFOLOG(SPDK_LOG_NVMF_TCP, "*** TCP Transport Init ***\n" 546 " Transport opts: max_ioq_depth=%d, max_io_size=%d,\n" 547 " max_qpairs_per_ctrlr=%d, io_unit_size=%d,\n" 548 " in_capsule_data_size=%d, max_aq_depth=%d\n" 549 " num_shared_buffers=%d\n", 550 opts->max_queue_depth, 551 opts->max_io_size, 552 opts->max_qpairs_per_ctrlr, 553 opts->io_unit_size, 554 opts->in_capsule_data_size, 555 opts->max_aq_depth, 556 opts->num_shared_buffers); 557 558 /* I/O unit size cannot be larger than max I/O size */ 559 if (opts->io_unit_size > opts->max_io_size) { 560 opts->io_unit_size = opts->max_io_size; 561 } 562 563 sge_count = opts->max_io_size / opts->io_unit_size; 564 if (sge_count > SPDK_NVMF_MAX_SGL_ENTRIES) { 565 SPDK_ERRLOG("Unsupported IO Unit size specified, %d bytes\n", opts->io_unit_size); 566 free(ttransport); 567 return NULL; 568 } 569 570 min_shared_buffers = spdk_thread_get_count() * opts->buf_cache_size; 571 if (min_shared_buffers > opts->num_shared_buffers) { 572 SPDK_ERRLOG("There are not enough buffers to satisfy" 573 "per-poll group caches for each thread. (%" PRIu32 ")" 574 "supplied. (%" PRIu32 ") required\n", opts->num_shared_buffers, min_shared_buffers); 575 SPDK_ERRLOG("Please specify a larger number of shared buffers\n"); 576 spdk_nvmf_tcp_destroy(&ttransport->transport); 577 return NULL; 578 } 579 580 pthread_mutex_init(&ttransport->lock, NULL); 581 582 return &ttransport->transport; 583 } 584 585 static int 586 _spdk_nvmf_tcp_trsvcid_to_int(const char *trsvcid) 587 { 588 unsigned long long ull; 589 char *end = NULL; 590 591 ull = strtoull(trsvcid, &end, 10); 592 if (end == NULL || end == trsvcid || *end != '\0') { 593 return -1; 594 } 595 596 /* Valid TCP/IP port numbers are in [0, 65535] */ 597 if (ull > 65535) { 598 return -1; 599 } 600 601 return (int)ull; 602 } 603 604 /** 605 * Canonicalize a listen address trid. 606 */ 607 static int 608 _spdk_nvmf_tcp_canon_listen_trid(struct spdk_nvme_transport_id *canon_trid, 609 const struct spdk_nvme_transport_id *trid) 610 { 611 int trsvcid_int; 612 613 trsvcid_int = _spdk_nvmf_tcp_trsvcid_to_int(trid->trsvcid); 614 if (trsvcid_int < 0) { 615 return -EINVAL; 616 } 617 618 memset(canon_trid, 0, sizeof(*canon_trid)); 619 canon_trid->trtype = SPDK_NVME_TRANSPORT_TCP; 620 canon_trid->adrfam = trid->adrfam; 621 snprintf(canon_trid->traddr, sizeof(canon_trid->traddr), "%s", trid->traddr); 622 snprintf(canon_trid->trsvcid, sizeof(canon_trid->trsvcid), "%d", trsvcid_int); 623 624 return 0; 625 } 626 627 /** 628 * Find an existing listening port. 629 * 630 * Caller must hold ttransport->lock. 631 */ 632 static struct spdk_nvmf_tcp_port * 633 _spdk_nvmf_tcp_find_port(struct spdk_nvmf_tcp_transport *ttransport, 634 const struct spdk_nvme_transport_id *trid) 635 { 636 struct spdk_nvme_transport_id canon_trid; 637 struct spdk_nvmf_tcp_port *port; 638 639 if (_spdk_nvmf_tcp_canon_listen_trid(&canon_trid, trid) != 0) { 640 return NULL; 641 } 642 643 TAILQ_FOREACH(port, &ttransport->ports, link) { 644 if (spdk_nvme_transport_id_compare(&canon_trid, &port->trid) == 0) { 645 return port; 646 } 647 } 648 649 return NULL; 650 } 651 652 static int 653 spdk_nvmf_tcp_listen(struct spdk_nvmf_transport *transport, 654 const struct spdk_nvme_transport_id *trid) 655 { 656 struct spdk_nvmf_tcp_transport *ttransport; 657 struct spdk_nvmf_tcp_port *port; 658 int trsvcid_int; 659 uint8_t adrfam; 660 661 ttransport = SPDK_CONTAINEROF(transport, struct spdk_nvmf_tcp_transport, transport); 662 663 trsvcid_int = _spdk_nvmf_tcp_trsvcid_to_int(trid->trsvcid); 664 if (trsvcid_int < 0) { 665 SPDK_ERRLOG("Invalid trsvcid '%s'\n", trid->trsvcid); 666 return -EINVAL; 667 } 668 669 pthread_mutex_lock(&ttransport->lock); 670 671 port = _spdk_nvmf_tcp_find_port(ttransport, trid); 672 if (port) { 673 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Already listening on %s port %s\n", 674 trid->traddr, trid->trsvcid); 675 port->ref++; 676 pthread_mutex_unlock(&ttransport->lock); 677 return 0; 678 } 679 680 port = calloc(1, sizeof(*port)); 681 if (!port) { 682 SPDK_ERRLOG("Port allocation failed\n"); 683 free(port); 684 pthread_mutex_unlock(&ttransport->lock); 685 return -ENOMEM; 686 } 687 688 port->ref = 1; 689 690 if (_spdk_nvmf_tcp_canon_listen_trid(&port->trid, trid) != 0) { 691 SPDK_ERRLOG("Invalid traddr %s / trsvcid %s\n", 692 trid->traddr, trid->trsvcid); 693 free(port); 694 pthread_mutex_unlock(&ttransport->lock); 695 return -ENOMEM; 696 } 697 698 port->listen_sock = spdk_sock_listen(trid->traddr, trsvcid_int); 699 if (port->listen_sock == NULL) { 700 SPDK_ERRLOG("spdk_sock_listen(%s, %d) failed: %s (%d)\n", 701 trid->traddr, trsvcid_int, 702 spdk_strerror(errno), errno); 703 free(port); 704 pthread_mutex_unlock(&ttransport->lock); 705 return -errno; 706 } 707 708 if (spdk_sock_is_ipv4(port->listen_sock)) { 709 adrfam = SPDK_NVMF_ADRFAM_IPV4; 710 } else if (spdk_sock_is_ipv6(port->listen_sock)) { 711 adrfam = SPDK_NVMF_ADRFAM_IPV6; 712 } else { 713 SPDK_ERRLOG("Unhandled socket type\n"); 714 adrfam = 0; 715 } 716 717 if (adrfam != trid->adrfam) { 718 SPDK_ERRLOG("Socket address family mismatch\n"); 719 spdk_sock_close(&port->listen_sock); 720 free(port); 721 pthread_mutex_unlock(&ttransport->lock); 722 return -EINVAL; 723 } 724 725 SPDK_NOTICELOG("*** NVMe/TCP Target Listening on %s port %d ***\n", 726 trid->traddr, trsvcid_int); 727 728 TAILQ_INSERT_TAIL(&ttransport->ports, port, link); 729 pthread_mutex_unlock(&ttransport->lock); 730 731 return 0; 732 } 733 734 static int 735 spdk_nvmf_tcp_stop_listen(struct spdk_nvmf_transport *transport, 736 const struct spdk_nvme_transport_id *trid) 737 { 738 struct spdk_nvmf_tcp_transport *ttransport; 739 struct spdk_nvmf_tcp_port *port; 740 int rc; 741 742 ttransport = SPDK_CONTAINEROF(transport, struct spdk_nvmf_tcp_transport, transport); 743 744 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Removing listen address %s port %s\n", 745 trid->traddr, trid->trsvcid); 746 747 pthread_mutex_lock(&ttransport->lock); 748 port = _spdk_nvmf_tcp_find_port(ttransport, trid); 749 if (port) { 750 assert(port->ref > 0); 751 port->ref--; 752 if (port->ref == 0) { 753 TAILQ_REMOVE(&ttransport->ports, port, link); 754 spdk_sock_close(&port->listen_sock); 755 free(port); 756 } 757 rc = 0; 758 } else { 759 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Port not found\n"); 760 rc = -ENOENT; 761 } 762 pthread_mutex_unlock(&ttransport->lock); 763 764 return rc; 765 } 766 767 static int 768 spdk_nvmf_tcp_qpair_flush_pdus_internal(struct spdk_nvmf_tcp_qpair *tqpair) 769 { 770 const int array_size = 32; 771 struct iovec iovec_array[array_size]; 772 struct iovec *iov = iovec_array; 773 int iovec_cnt = 0; 774 int bytes = 0; 775 int total_length = 0; 776 uint32_t mapped_length; 777 struct nvme_tcp_pdu *pdu; 778 int pdu_length; 779 TAILQ_HEAD(, nvme_tcp_pdu) completed_pdus_list; 780 struct spdk_nvmf_tcp_transport *ttransport; 781 782 pdu = TAILQ_FIRST(&tqpair->send_queue); 783 784 if (pdu == NULL) { 785 return 0; 786 } 787 788 /* 789 * Build up a list of iovecs for the first few PDUs in the 790 * tqpair 's send_queue. 791 */ 792 while (pdu != NULL && ((array_size - iovec_cnt) >= 3)) { 793 iovec_cnt += nvme_tcp_build_iovecs(&iovec_array[iovec_cnt], 794 array_size - iovec_cnt, 795 pdu, 796 tqpair->host_hdgst_enable, 797 tqpair->host_ddgst_enable, 798 &mapped_length); 799 total_length += mapped_length; 800 pdu = TAILQ_NEXT(pdu, tailq); 801 } 802 803 spdk_trace_record(TRACE_TCP_FLUSH_WRITEBUF_START, 0, total_length, 0, iovec_cnt); 804 805 bytes = spdk_sock_writev(tqpair->sock, iov, iovec_cnt); 806 if (bytes == -1) { 807 if (errno == EWOULDBLOCK || errno == EAGAIN) { 808 return 1; 809 } else { 810 SPDK_ERRLOG("spdk_sock_writev() failed, errno %d: %s\n", 811 errno, spdk_strerror(errno)); 812 return -1; 813 } 814 } 815 816 spdk_trace_record(TRACE_TCP_FLUSH_WRITEBUF_DONE, 0, bytes, 0, 0); 817 818 pdu = TAILQ_FIRST(&tqpair->send_queue); 819 820 /* 821 * Free any PDUs that were fully written. If a PDU was only 822 * partially written, update its writev_offset so that next 823 * time only the unwritten portion will be sent to writev(). 824 */ 825 TAILQ_INIT(&completed_pdus_list); 826 while (bytes > 0) { 827 pdu_length = pdu->hdr.common.plen - pdu->writev_offset; 828 if (bytes >= pdu_length) { 829 bytes -= pdu_length; 830 TAILQ_REMOVE(&tqpair->send_queue, pdu, tailq); 831 TAILQ_INSERT_TAIL(&completed_pdus_list, pdu, tailq); 832 pdu = TAILQ_FIRST(&tqpair->send_queue); 833 834 } else { 835 pdu->writev_offset += bytes; 836 bytes = 0; 837 } 838 } 839 840 while (!TAILQ_EMPTY(&completed_pdus_list)) { 841 pdu = TAILQ_FIRST(&completed_pdus_list); 842 TAILQ_REMOVE(&completed_pdus_list, pdu, tailq); 843 assert(pdu->cb_fn != NULL); 844 pdu->cb_fn(pdu->cb_arg); 845 spdk_nvmf_tcp_pdu_put(tqpair, pdu); 846 } 847 848 ttransport = SPDK_CONTAINEROF(tqpair->qpair.transport, struct spdk_nvmf_tcp_transport, transport); 849 spdk_nvmf_tcp_qpair_process_pending(ttransport, tqpair); 850 851 return TAILQ_EMPTY(&tqpair->send_queue) ? 0 : 1; 852 } 853 854 static int 855 spdk_nvmf_tcp_qpair_flush_pdus(void *_tqpair) 856 { 857 struct spdk_nvmf_tcp_qpair *tqpair = _tqpair; 858 int rc; 859 860 if (tqpair->state == NVME_TCP_QPAIR_STATE_RUNNING) { 861 rc = spdk_nvmf_tcp_qpair_flush_pdus_internal(tqpair); 862 if (rc == 0 && tqpair->flush_poller != NULL) { 863 spdk_poller_unregister(&tqpair->flush_poller); 864 } else if (rc == 1 && tqpair->flush_poller == NULL) { 865 tqpair->flush_poller = spdk_poller_register(spdk_nvmf_tcp_qpair_flush_pdus, 866 tqpair, 50); 867 } 868 } else { 869 /* 870 * If the tqpair state is not RUNNING, then 871 * keep trying to flush PDUs until our list is 872 * empty - to make sure all data is sent before 873 * closing the connection. 874 */ 875 do { 876 rc = spdk_nvmf_tcp_qpair_flush_pdus_internal(tqpair); 877 } while (rc == 1); 878 } 879 880 if (rc < 0 && tqpair->state < NVME_TCP_QPAIR_STATE_EXITING) { 881 /* 882 * If the poller has already started destruction of the tqpair, 883 * i.e. the socket read failed, then the connection state may already 884 * be EXITED. We don't want to set it back to EXITING in that case. 885 */ 886 tqpair->state = NVME_TCP_QPAIR_STATE_EXITING; 887 } 888 889 return -1; 890 } 891 892 static void 893 spdk_nvmf_tcp_qpair_write_pdu(struct spdk_nvmf_tcp_qpair *tqpair, 894 struct nvme_tcp_pdu *pdu, 895 nvme_tcp_qpair_xfer_complete_cb cb_fn, 896 void *cb_arg) 897 { 898 int enable_digest; 899 int hlen; 900 uint32_t crc32c; 901 902 hlen = pdu->hdr.common.hlen; 903 enable_digest = 1; 904 if (pdu->hdr.common.pdu_type == SPDK_NVME_TCP_PDU_TYPE_IC_RESP || 905 pdu->hdr.common.pdu_type == SPDK_NVME_TCP_PDU_TYPE_C2H_TERM_REQ) { 906 /* this PDU should be sent without digest */ 907 enable_digest = 0; 908 } 909 910 /* Header Digest */ 911 if (enable_digest && tqpair->host_hdgst_enable) { 912 crc32c = nvme_tcp_pdu_calc_header_digest(pdu); 913 MAKE_DIGEST_WORD((uint8_t *)pdu->hdr.raw + hlen, crc32c); 914 } 915 916 /* Data Digest */ 917 if (pdu->data_len > 0 && enable_digest && tqpair->host_ddgst_enable) { 918 crc32c = nvme_tcp_pdu_calc_data_digest(pdu); 919 MAKE_DIGEST_WORD(pdu->data_digest, crc32c); 920 } 921 922 pdu->cb_fn = cb_fn; 923 pdu->cb_arg = cb_arg; 924 TAILQ_INSERT_TAIL(&tqpair->send_queue, pdu, tailq); 925 spdk_nvmf_tcp_qpair_flush_pdus(tqpair); 926 } 927 928 static int 929 spdk_nvmf_tcp_qpair_init_mem_resource(struct spdk_nvmf_tcp_qpair *tqpair, uint16_t size) 930 { 931 int i; 932 struct spdk_nvmf_tcp_req *tcp_req; 933 struct spdk_nvmf_transport *transport = tqpair->qpair.transport; 934 struct spdk_nvmf_tcp_transport *ttransport; 935 ttransport = SPDK_CONTAINEROF(transport, struct spdk_nvmf_tcp_transport, transport); 936 937 if (!tqpair->qpair.sq_head_max) { 938 tqpair->req = calloc(1, sizeof(*tqpair->req)); 939 if (!tqpair->req) { 940 SPDK_ERRLOG("Unable to allocate req on tqpair=%p.\n", tqpair); 941 return -1; 942 } 943 944 if (transport->opts.in_capsule_data_size) { 945 tqpair->buf = spdk_dma_zmalloc(ttransport->transport.opts.in_capsule_data_size, 0x1000, NULL); 946 if (!tqpair->buf) { 947 SPDK_ERRLOG("Unable to allocate buf on tqpair=%p.\n", tqpair); 948 return -1; 949 } 950 } 951 952 tcp_req = tqpair->req; 953 tcp_req->ttag = 0; 954 tcp_req->req.qpair = &tqpair->qpair; 955 956 /* Set up memory to receive commands */ 957 if (tqpair->buf) { 958 tcp_req->buf = tqpair->buf; 959 } 960 961 /* Set the cmdn and rsp */ 962 tcp_req->req.rsp = (union nvmf_c2h_msg *)&tcp_req->rsp; 963 tcp_req->req.cmd = (union nvmf_h2c_msg *)&tcp_req->cmd; 964 965 /* Initialize request state to FREE */ 966 tcp_req->state = TCP_REQUEST_STATE_FREE; 967 TAILQ_INSERT_TAIL(&tqpair->state_queue[tcp_req->state], tcp_req, state_link); 968 969 tqpair->pdu = calloc(NVMF_TCP_QPAIR_MAX_C2H_PDU_NUM + 1, sizeof(*tqpair->pdu)); 970 if (!tqpair->pdu) { 971 SPDK_ERRLOG("Unable to allocate pdu on tqpair=%p.\n", tqpair); 972 return -1; 973 } 974 975 for (i = 0; i < 1 + NVMF_TCP_QPAIR_MAX_C2H_PDU_NUM; i++) { 976 TAILQ_INSERT_TAIL(&tqpair->free_queue, &tqpair->pdu[i], tailq); 977 } 978 979 } else { 980 tqpair->reqs = calloc(size, sizeof(*tqpair->reqs)); 981 if (!tqpair->reqs) { 982 SPDK_ERRLOG("Unable to allocate reqs on tqpair=%p\n", tqpair); 983 return -1; 984 } 985 986 if (transport->opts.in_capsule_data_size) { 987 tqpair->bufs = spdk_dma_zmalloc(size * transport->opts.in_capsule_data_size, 988 0x1000, NULL); 989 if (!tqpair->bufs) { 990 SPDK_ERRLOG("Unable to allocate bufs on tqpair=%p.\n", tqpair); 991 return -1; 992 } 993 } 994 995 for (i = 0; i < size; i++) { 996 struct spdk_nvmf_tcp_req *tcp_req = &tqpair->reqs[i]; 997 998 tcp_req->ttag = i + 1; 999 tcp_req->req.qpair = &tqpair->qpair; 1000 1001 /* Set up memory to receive commands */ 1002 if (tqpair->bufs) { 1003 tcp_req->buf = (void *)((uintptr_t)tqpair->bufs + (i * transport->opts.in_capsule_data_size)); 1004 } 1005 1006 /* Set the cmdn and rsp */ 1007 tcp_req->req.rsp = (union nvmf_c2h_msg *)&tcp_req->rsp; 1008 tcp_req->req.cmd = (union nvmf_h2c_msg *)&tcp_req->cmd; 1009 1010 /* Initialize request state to FREE */ 1011 tcp_req->state = TCP_REQUEST_STATE_FREE; 1012 TAILQ_INSERT_TAIL(&tqpair->state_queue[tcp_req->state], tcp_req, state_link); 1013 } 1014 1015 tqpair->pdu_pool = calloc(size, sizeof(*tqpair->pdu_pool)); 1016 if (!tqpair->pdu_pool) { 1017 SPDK_ERRLOG("Unable to allocate pdu pool on tqpair =%p.\n", tqpair); 1018 return -1; 1019 } 1020 1021 for (i = 0; i < size; i++) { 1022 TAILQ_INSERT_TAIL(&tqpair->free_queue, &tqpair->pdu_pool[i], tailq); 1023 } 1024 } 1025 1026 return 0; 1027 } 1028 1029 static int 1030 spdk_nvmf_tcp_qpair_init(struct spdk_nvmf_qpair *qpair) 1031 { 1032 struct spdk_nvmf_tcp_qpair *tqpair; 1033 int i; 1034 1035 tqpair = SPDK_CONTAINEROF(qpair, struct spdk_nvmf_tcp_qpair, qpair); 1036 1037 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "New TCP Connection: %p\n", qpair); 1038 1039 TAILQ_INIT(&tqpair->send_queue); 1040 TAILQ_INIT(&tqpair->free_queue); 1041 TAILQ_INIT(&tqpair->queued_c2h_data_tcp_req); 1042 1043 /* Initialise request state queues of the qpair */ 1044 for (i = TCP_REQUEST_STATE_FREE; i < TCP_REQUEST_NUM_STATES; i++) { 1045 TAILQ_INIT(&tqpair->state_queue[i]); 1046 } 1047 1048 tqpair->host_hdgst_enable = true; 1049 tqpair->host_ddgst_enable = true; 1050 1051 return 0; 1052 } 1053 1054 static int 1055 spdk_nvmf_tcp_qpair_sock_init(struct spdk_nvmf_tcp_qpair *tqpair) 1056 { 1057 1058 int rc; 1059 int buf_size; 1060 1061 /* set recv buffer size */ 1062 buf_size = 2 * 1024 * 1024; 1063 rc = spdk_sock_set_recvbuf(tqpair->sock, buf_size); 1064 if (rc != 0) { 1065 SPDK_ERRLOG("spdk_sock_set_recvbuf failed\n"); 1066 return rc; 1067 } 1068 1069 /* set send buffer size */ 1070 rc = spdk_sock_set_sendbuf(tqpair->sock, buf_size); 1071 if (rc != 0) { 1072 SPDK_ERRLOG("spdk_sock_set_sendbuf failed\n"); 1073 return rc; 1074 } 1075 1076 /* set low water mark */ 1077 rc = spdk_sock_set_recvlowat(tqpair->sock, sizeof(struct spdk_nvme_tcp_c2h_data_hdr)); 1078 if (rc != 0) { 1079 SPDK_ERRLOG("spdk_sock_set_recvlowat() failed\n"); 1080 return rc; 1081 } 1082 1083 return 0; 1084 } 1085 1086 static void 1087 _spdk_nvmf_tcp_handle_connect(struct spdk_nvmf_transport *transport, 1088 struct spdk_nvmf_tcp_port *port, 1089 struct spdk_sock *sock, new_qpair_fn cb_fn) 1090 { 1091 struct spdk_nvmf_tcp_qpair *tqpair; 1092 int rc; 1093 1094 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "New connection accepted on %s port %s\n", 1095 port->trid.traddr, port->trid.trsvcid); 1096 1097 tqpair = calloc(1, sizeof(struct spdk_nvmf_tcp_qpair)); 1098 if (tqpair == NULL) { 1099 SPDK_ERRLOG("Could not allocate new connection.\n"); 1100 spdk_sock_close(&sock); 1101 return; 1102 } 1103 1104 tqpair->sock = sock; 1105 tqpair->max_queue_depth = 1; 1106 tqpair->free_pdu_num = tqpair->max_queue_depth + NVMF_TCP_QPAIR_MAX_C2H_PDU_NUM; 1107 tqpair->state_cntr[TCP_REQUEST_STATE_FREE] = tqpair->max_queue_depth; 1108 tqpair->port = port; 1109 tqpair->qpair.transport = transport; 1110 1111 rc = spdk_sock_getaddr(tqpair->sock, tqpair->target_addr, 1112 sizeof(tqpair->target_addr), &tqpair->target_port, 1113 tqpair->initiator_addr, sizeof(tqpair->initiator_addr), 1114 &tqpair->initiator_port); 1115 if (rc < 0) { 1116 SPDK_ERRLOG("spdk_sock_getaddr() failed of tqpair=%p\n", tqpair); 1117 spdk_nvmf_tcp_qpair_destroy(tqpair); 1118 return; 1119 } 1120 1121 cb_fn(&tqpair->qpair); 1122 } 1123 1124 static void 1125 spdk_nvmf_tcp_port_accept(struct spdk_nvmf_transport *transport, struct spdk_nvmf_tcp_port *port, 1126 new_qpair_fn cb_fn) 1127 { 1128 struct spdk_sock *sock; 1129 int i; 1130 1131 for (i = 0; i < NVMF_TCP_MAX_ACCEPT_SOCK_ONE_TIME; i++) { 1132 sock = spdk_sock_accept(port->listen_sock); 1133 if (sock) { 1134 _spdk_nvmf_tcp_handle_connect(transport, port, sock, cb_fn); 1135 } 1136 } 1137 } 1138 1139 static void 1140 spdk_nvmf_tcp_accept(struct spdk_nvmf_transport *transport, new_qpair_fn cb_fn) 1141 { 1142 struct spdk_nvmf_tcp_transport *ttransport; 1143 struct spdk_nvmf_tcp_port *port; 1144 1145 ttransport = SPDK_CONTAINEROF(transport, struct spdk_nvmf_tcp_transport, transport); 1146 1147 TAILQ_FOREACH(port, &ttransport->ports, link) { 1148 spdk_nvmf_tcp_port_accept(transport, port, cb_fn); 1149 } 1150 } 1151 1152 static void 1153 spdk_nvmf_tcp_discover(struct spdk_nvmf_transport *transport, 1154 struct spdk_nvme_transport_id *trid, 1155 struct spdk_nvmf_discovery_log_page_entry *entry) 1156 { 1157 entry->trtype = SPDK_NVMF_TRTYPE_TCP; 1158 entry->adrfam = trid->adrfam; 1159 entry->treq.secure_channel = SPDK_NVMF_TREQ_SECURE_CHANNEL_NOT_SPECIFIED; 1160 1161 spdk_strcpy_pad(entry->trsvcid, trid->trsvcid, sizeof(entry->trsvcid), ' '); 1162 spdk_strcpy_pad(entry->traddr, trid->traddr, sizeof(entry->traddr), ' '); 1163 1164 entry->tsas.tcp.sectype = SPDK_NVME_TCP_SECURITY_NONE; 1165 } 1166 1167 static struct spdk_nvmf_transport_poll_group * 1168 spdk_nvmf_tcp_poll_group_create(struct spdk_nvmf_transport *transport) 1169 { 1170 struct spdk_nvmf_tcp_poll_group *tgroup; 1171 1172 tgroup = calloc(1, sizeof(*tgroup)); 1173 if (!tgroup) { 1174 return NULL; 1175 } 1176 1177 tgroup->sock_group = spdk_sock_group_create(); 1178 if (!tgroup->sock_group) { 1179 goto cleanup; 1180 } 1181 1182 TAILQ_INIT(&tgroup->qpairs); 1183 TAILQ_INIT(&tgroup->pending_data_buf_queue); 1184 1185 return &tgroup->group; 1186 1187 cleanup: 1188 free(tgroup); 1189 return NULL; 1190 } 1191 1192 static void 1193 spdk_nvmf_tcp_poll_group_destroy(struct spdk_nvmf_transport_poll_group *group) 1194 { 1195 struct spdk_nvmf_tcp_poll_group *tgroup; 1196 1197 tgroup = SPDK_CONTAINEROF(group, struct spdk_nvmf_tcp_poll_group, group); 1198 spdk_sock_group_close(&tgroup->sock_group); 1199 1200 if (!TAILQ_EMPTY(&tgroup->pending_data_buf_queue)) { 1201 SPDK_ERRLOG("Pending I/O list wasn't empty on poll group destruction\n"); 1202 } 1203 1204 free(tgroup); 1205 } 1206 1207 static void 1208 spdk_nvmf_tcp_qpair_set_recv_state(struct spdk_nvmf_tcp_qpair *tqpair, 1209 enum nvme_tcp_pdu_recv_state state) 1210 { 1211 if (tqpair->recv_state == state) { 1212 SPDK_ERRLOG("The recv state of tqpair=%p is same with the state(%d) to be set\n", 1213 tqpair, state); 1214 return; 1215 } 1216 1217 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "tqpair(%p) recv state=%d\n", tqpair, state); 1218 tqpair->recv_state = state; 1219 1220 switch (state) { 1221 case NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_CH: 1222 case NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PSH: 1223 case NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD: 1224 break; 1225 case NVME_TCP_PDU_RECV_STATE_ERROR: 1226 case NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY: 1227 memset(&tqpair->pdu_in_progress, 0, sizeof(tqpair->pdu_in_progress)); 1228 break; 1229 default: 1230 SPDK_ERRLOG("The state(%d) is invalid\n", state); 1231 abort(); 1232 break; 1233 } 1234 } 1235 1236 static int 1237 spdk_nvmf_tcp_qpair_handle_timeout(void *ctx) 1238 { 1239 struct spdk_nvmf_tcp_qpair *tqpair = ctx; 1240 1241 assert(tqpair->recv_state == NVME_TCP_PDU_RECV_STATE_ERROR); 1242 1243 SPDK_ERRLOG("No pdu coming for tqpair=%p within %d seconds\n", tqpair, 1244 SPDK_NVME_TCP_QPAIR_EXIT_TIMEOUT); 1245 tqpair->state = NVME_TCP_QPAIR_STATE_EXITED; 1246 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "will disconect the tqpair=%p\n", tqpair); 1247 spdk_poller_unregister(&tqpair->timeout_poller); 1248 spdk_nvmf_qpair_disconnect(&tqpair->qpair, NULL, NULL); 1249 1250 return 0; 1251 } 1252 1253 static void 1254 spdk_nvmf_tcp_send_c2h_term_req_complete(void *cb_arg) 1255 { 1256 struct spdk_nvmf_tcp_qpair *tqpair = (struct spdk_nvmf_tcp_qpair *)cb_arg; 1257 1258 if (!tqpair->timeout_poller) { 1259 tqpair->timeout_poller = spdk_poller_register(spdk_nvmf_tcp_qpair_handle_timeout, tqpair, 1260 SPDK_NVME_TCP_QPAIR_EXIT_TIMEOUT * 1000000); 1261 } 1262 } 1263 1264 static void 1265 spdk_nvmf_tcp_send_c2h_term_req(struct spdk_nvmf_tcp_qpair *tqpair, struct nvme_tcp_pdu *pdu, 1266 enum spdk_nvme_tcp_term_req_fes fes, uint32_t error_offset) 1267 { 1268 struct nvme_tcp_pdu *rsp_pdu; 1269 struct spdk_nvme_tcp_term_req_hdr *c2h_term_req; 1270 uint32_t c2h_term_req_hdr_len = sizeof(*c2h_term_req); 1271 uint32_t copy_len; 1272 1273 rsp_pdu = spdk_nvmf_tcp_pdu_get(tqpair); 1274 if (!rsp_pdu) { 1275 tqpair->state = NVME_TCP_QPAIR_STATE_EXITING; 1276 spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_ERROR); 1277 return; 1278 } 1279 1280 c2h_term_req = &rsp_pdu->hdr.term_req; 1281 c2h_term_req->common.pdu_type = SPDK_NVME_TCP_PDU_TYPE_C2H_TERM_REQ; 1282 c2h_term_req->common.hlen = c2h_term_req_hdr_len; 1283 1284 if ((fes == SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD) || 1285 (fes == SPDK_NVME_TCP_TERM_REQ_FES_INVALID_DATA_UNSUPPORTED_PARAMETER)) { 1286 DSET32(&c2h_term_req->fei, error_offset); 1287 } 1288 1289 copy_len = pdu->hdr.common.hlen; 1290 if (copy_len > SPDK_NVME_TCP_TERM_REQ_ERROR_DATA_MAX_SIZE) { 1291 copy_len = SPDK_NVME_TCP_TERM_REQ_ERROR_DATA_MAX_SIZE; 1292 } 1293 1294 /* Copy the error info into the buffer */ 1295 memcpy((uint8_t *)rsp_pdu->hdr.raw + c2h_term_req_hdr_len, pdu->hdr.raw, copy_len); 1296 nvme_tcp_pdu_set_data(rsp_pdu, (uint8_t *)rsp_pdu->hdr.raw + c2h_term_req_hdr_len, copy_len); 1297 1298 /* Contain the header of the wrong received pdu */ 1299 c2h_term_req->common.plen = c2h_term_req->common.hlen + copy_len; 1300 spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_ERROR); 1301 spdk_nvmf_tcp_qpair_write_pdu(tqpair, rsp_pdu, spdk_nvmf_tcp_send_c2h_term_req_complete, tqpair); 1302 } 1303 1304 static void 1305 spdk_nvmf_tcp_capsule_cmd_hdr_handle(struct spdk_nvmf_tcp_transport *ttransport, 1306 struct spdk_nvmf_tcp_qpair *tqpair, 1307 struct nvme_tcp_pdu *pdu) 1308 { 1309 struct spdk_nvmf_tcp_req *tcp_req; 1310 1311 tcp_req = spdk_nvmf_tcp_req_get(tqpair); 1312 if (!tcp_req) { 1313 SPDK_ERRLOG("Cannot allocate tcp_req\n"); 1314 tqpair->state = NVME_TCP_QPAIR_STATE_EXITING; 1315 spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_ERROR); 1316 return; 1317 } 1318 1319 pdu->ctx = tcp_req; 1320 spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_NEW); 1321 spdk_nvmf_tcp_req_process(ttransport, tcp_req); 1322 return; 1323 } 1324 1325 static void 1326 spdk_nvmf_tcp_capsule_cmd_payload_handle(struct spdk_nvmf_tcp_transport *ttransport, 1327 struct spdk_nvmf_tcp_qpair *tqpair, 1328 struct nvme_tcp_pdu *pdu) 1329 { 1330 struct spdk_nvmf_tcp_req *tcp_req; 1331 struct spdk_nvme_tcp_cmd *capsule_cmd; 1332 uint32_t error_offset = 0; 1333 enum spdk_nvme_tcp_term_req_fes fes; 1334 1335 capsule_cmd = &pdu->hdr.capsule_cmd; 1336 tcp_req = pdu->ctx; 1337 assert(tcp_req != NULL); 1338 if (capsule_cmd->common.pdo > SPDK_NVME_TCP_PDU_PDO_MAX_OFFSET) { 1339 SPDK_ERRLOG("Expected ICReq capsule_cmd pdu offset <= %d, got %c\n", 1340 SPDK_NVME_TCP_PDU_PDO_MAX_OFFSET, capsule_cmd->common.pdo); 1341 fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD; 1342 error_offset = offsetof(struct spdk_nvme_tcp_common_pdu_hdr, pdo); 1343 goto err; 1344 } 1345 1346 spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY); 1347 spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_READY_TO_EXECUTE); 1348 spdk_nvmf_tcp_req_process(ttransport, tcp_req); 1349 1350 return; 1351 err: 1352 spdk_nvmf_tcp_send_c2h_term_req(tqpair, pdu, fes, error_offset); 1353 } 1354 1355 static void 1356 spdk_nvmf_tcp_h2c_data_hdr_handle(struct spdk_nvmf_tcp_transport *ttransport, 1357 struct spdk_nvmf_tcp_qpair *tqpair, 1358 struct nvme_tcp_pdu *pdu) 1359 { 1360 struct spdk_nvmf_tcp_req *tcp_req; 1361 uint32_t error_offset = 0; 1362 enum spdk_nvme_tcp_term_req_fes fes = 0; 1363 struct spdk_nvme_tcp_h2c_data_hdr *h2c_data; 1364 uint32_t iov_index; 1365 bool ttag_offset_error = false; 1366 1367 h2c_data = &pdu->hdr.h2c_data; 1368 1369 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "tqpair=%p, r2t_info: datao=%u, datal=%u, cccid=%u, ttag=%u\n", 1370 tqpair, h2c_data->datao, h2c_data->datal, h2c_data->cccid, h2c_data->ttag); 1371 1372 /* According to the information in the pdu to find the req */ 1373 TAILQ_FOREACH(tcp_req, &tqpair->state_queue[TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER], 1374 state_link) { 1375 if ((tcp_req->req.cmd->nvme_cmd.cid == h2c_data->cccid) && (tcp_req->ttag == h2c_data->ttag)) { 1376 break; 1377 } 1378 1379 if (!ttag_offset_error && (tcp_req->req.cmd->nvme_cmd.cid == h2c_data->cccid)) { 1380 ttag_offset_error = true; 1381 } 1382 } 1383 1384 if (!tcp_req) { 1385 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "tcp_req is not found for tqpair=%p\n", tqpair); 1386 fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_DATA_UNSUPPORTED_PARAMETER; 1387 if (!ttag_offset_error) { 1388 error_offset = offsetof(struct spdk_nvme_tcp_h2c_data_hdr, cccid); 1389 } else { 1390 error_offset = offsetof(struct spdk_nvme_tcp_h2c_data_hdr, ttag); 1391 } 1392 goto err; 1393 } 1394 1395 if (tcp_req->next_expected_r2t_offset != h2c_data->datao) { 1396 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, 1397 "tcp_req(%p), tqpair=%p, expected_r2t_offset=%u, but data offset =%u\n", 1398 tcp_req, tqpair, tcp_req->next_expected_r2t_offset, h2c_data->datao); 1399 fes = SPDK_NVME_TCP_TERM_REQ_FES_DATA_TRANSFER_OUT_OF_RANGE; 1400 goto err; 1401 } 1402 1403 if (h2c_data->datal > tqpair->maxh2cdata) { 1404 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "tcp_req(%p), tqpair=%p, datao=%u execeeds maxh2cdata size=%u\n", 1405 tcp_req, tqpair, h2c_data->datao, tqpair->maxh2cdata); 1406 fes = SPDK_NVME_TCP_TERM_REQ_FES_DATA_TRANSFER_OUT_OF_RANGE; 1407 goto err; 1408 } 1409 1410 if ((h2c_data->datao + h2c_data->datal) > tcp_req->req.length) { 1411 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, 1412 "tcp_req(%p), tqpair=%p, (datao=%u + datal=%u) execeeds requested length=%u\n", 1413 tcp_req, tqpair, h2c_data->datao, h2c_data->datal, tcp_req->req.length); 1414 fes = SPDK_NVME_TCP_TERM_REQ_FES_R2T_LIMIT_EXCEEDED; 1415 goto err; 1416 } 1417 1418 pdu->ctx = tcp_req; 1419 iov_index = pdu->hdr.h2c_data.datao / ttransport->transport.opts.io_unit_size; 1420 nvme_tcp_pdu_set_data(pdu, tcp_req->req.iov[iov_index].iov_base + (pdu->hdr.h2c_data.datao % 1421 ttransport->transport.opts.io_unit_size), h2c_data->datal); 1422 spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD); 1423 return; 1424 1425 err: 1426 spdk_nvmf_tcp_send_c2h_term_req(tqpair, pdu, fes, error_offset); 1427 } 1428 1429 static void 1430 spdk_nvmf_tcp_pdu_cmd_complete(void *cb_arg) 1431 { 1432 struct spdk_nvmf_tcp_req *tcp_req = cb_arg; 1433 nvmf_tcp_request_free(tcp_req); 1434 } 1435 1436 static void 1437 spdk_nvmf_tcp_send_capsule_resp_pdu(struct spdk_nvmf_tcp_req *tcp_req, 1438 struct spdk_nvmf_tcp_qpair *tqpair) 1439 { 1440 struct nvme_tcp_pdu *rsp_pdu; 1441 struct spdk_nvme_tcp_rsp *capsule_resp; 1442 1443 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "enter, tqpair=%p\n", tqpair); 1444 rsp_pdu = spdk_nvmf_tcp_pdu_get(tqpair); 1445 if (!rsp_pdu) { 1446 spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_ERROR); 1447 tqpair->state = NVME_TCP_QPAIR_STATE_EXITING; 1448 return; 1449 } 1450 1451 capsule_resp = &rsp_pdu->hdr.capsule_resp; 1452 capsule_resp->common.pdu_type = SPDK_NVME_TCP_PDU_TYPE_CAPSULE_RESP; 1453 capsule_resp->common.plen = capsule_resp->common.hlen = sizeof(*capsule_resp); 1454 capsule_resp->rccqe = tcp_req->req.rsp->nvme_cpl; 1455 if (tqpair->host_hdgst_enable) { 1456 capsule_resp->common.flags |= SPDK_NVME_TCP_CH_FLAGS_HDGSTF; 1457 capsule_resp->common.plen += SPDK_NVME_TCP_DIGEST_LEN; 1458 } 1459 1460 spdk_nvmf_tcp_qpair_write_pdu(tqpair, rsp_pdu, spdk_nvmf_tcp_pdu_cmd_complete, tcp_req); 1461 } 1462 1463 static void 1464 spdk_nvmf_tcp_pdu_c2h_data_complete(void *cb_arg) 1465 { 1466 struct spdk_nvmf_tcp_req *tcp_req = cb_arg; 1467 struct spdk_nvmf_tcp_qpair *tqpair = SPDK_CONTAINEROF(tcp_req->req.qpair, 1468 struct spdk_nvmf_tcp_qpair, qpair); 1469 1470 assert(tqpair != NULL); 1471 assert(tcp_req->c2h_data_pdu_num > 0); 1472 tcp_req->c2h_data_pdu_num--; 1473 if (!tcp_req->c2h_data_pdu_num) { 1474 #if LINUX_KERNEL_SUPPORT_NOT_SENDING_RESP_FOR_C2H 1475 nvmf_tcp_request_free(tcp_req); 1476 #else 1477 spdk_nvmf_tcp_send_capsule_resp_pdu(tcp_req, tqpair); 1478 #endif 1479 } 1480 1481 tqpair->c2h_data_pdu_cnt--; 1482 spdk_nvmf_tcp_handle_pending_c2h_data_queue(tqpair); 1483 } 1484 1485 static void 1486 spdk_nvmf_tcp_send_r2t_pdu(struct spdk_nvmf_tcp_qpair *tqpair, 1487 struct spdk_nvmf_tcp_req *tcp_req) 1488 { 1489 struct nvme_tcp_pdu *rsp_pdu; 1490 struct spdk_nvme_tcp_r2t_hdr *r2t; 1491 1492 rsp_pdu = spdk_nvmf_tcp_pdu_get(tqpair); 1493 if (!rsp_pdu) { 1494 tqpair->state = NVME_TCP_QPAIR_STATE_EXITING; 1495 spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_ERROR); 1496 return; 1497 } 1498 1499 r2t = &rsp_pdu->hdr.r2t; 1500 r2t->common.pdu_type = SPDK_NVME_TCP_PDU_TYPE_R2T; 1501 r2t->common.plen = r2t->common.hlen = sizeof(*r2t); 1502 1503 if (tqpair->host_hdgst_enable) { 1504 r2t->common.flags |= SPDK_NVME_TCP_CH_FLAGS_HDGSTF; 1505 r2t->common.plen += SPDK_NVME_TCP_DIGEST_LEN; 1506 } 1507 1508 r2t->cccid = tcp_req->req.cmd->nvme_cmd.cid; 1509 r2t->ttag = tcp_req->ttag; 1510 r2t->r2to = tcp_req->next_expected_r2t_offset; 1511 r2t->r2tl = spdk_min(tcp_req->req.length - tcp_req->next_expected_r2t_offset, tqpair->maxh2cdata); 1512 tcp_req->r2tl_remain = r2t->r2tl; 1513 1514 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, 1515 "tcp_req(%p) on tqpair(%p), r2t_info: cccid=%u, ttag=%u, r2to=%u, r2tl=%u\n", 1516 tcp_req, tqpair, r2t->cccid, r2t->ttag, r2t->r2to, r2t->r2tl); 1517 spdk_nvmf_tcp_qpair_write_pdu(tqpair, rsp_pdu, spdk_nvmf_tcp_pdu_cmd_complete, NULL); 1518 } 1519 1520 static void 1521 spdk_nvmf_tcp_handle_queued_r2t_req(struct spdk_nvmf_tcp_qpair *tqpair) 1522 { 1523 struct spdk_nvmf_tcp_req *tcp_req, *req_tmp; 1524 1525 TAILQ_FOREACH_SAFE(tcp_req, &tqpair->state_queue[TCP_REQUEST_STATE_DATA_PENDING_FOR_R2T], 1526 state_link, req_tmp) { 1527 if (tqpair->pending_r2t < tqpair->maxr2t) { 1528 tqpair->pending_r2t++; 1529 spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER); 1530 spdk_nvmf_tcp_send_r2t_pdu(tqpair, tcp_req); 1531 } else { 1532 break; 1533 } 1534 } 1535 } 1536 1537 static void 1538 spdk_nvmf_tcp_h2c_data_payload_handle(struct spdk_nvmf_tcp_transport *ttransport, 1539 struct spdk_nvmf_tcp_qpair *tqpair, 1540 struct nvme_tcp_pdu *pdu) 1541 { 1542 struct spdk_nvmf_tcp_req *tcp_req; 1543 1544 tcp_req = pdu->ctx; 1545 assert(tcp_req != NULL); 1546 1547 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "enter\n"); 1548 1549 tcp_req->next_expected_r2t_offset += pdu->data_len; 1550 tcp_req->r2tl_remain -= pdu->data_len; 1551 spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY); 1552 1553 if (!tcp_req->r2tl_remain) { 1554 if (tcp_req->next_expected_r2t_offset == tcp_req->req.length) { 1555 assert(tqpair->pending_r2t > 0); 1556 tqpair->pending_r2t--; 1557 assert(tqpair->pending_r2t < tqpair->maxr2t); 1558 spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_READY_TO_EXECUTE); 1559 spdk_nvmf_tcp_req_process(ttransport, tcp_req); 1560 1561 spdk_nvmf_tcp_handle_queued_r2t_req(tqpair); 1562 } else { 1563 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Send r2t pdu for tcp_req=%p on tqpair=%p\n", tcp_req, tqpair); 1564 spdk_nvmf_tcp_send_r2t_pdu(tqpair, tcp_req); 1565 } 1566 } 1567 } 1568 1569 static void 1570 spdk_nvmf_tcp_h2c_term_req_dump(struct spdk_nvme_tcp_term_req_hdr *h2c_term_req) 1571 { 1572 SPDK_ERRLOG("Error info of pdu(%p): %s\n", h2c_term_req, 1573 spdk_nvmf_tcp_term_req_fes_str[h2c_term_req->fes]); 1574 if ((h2c_term_req->fes == SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD) || 1575 (h2c_term_req->fes == SPDK_NVME_TCP_TERM_REQ_FES_INVALID_DATA_UNSUPPORTED_PARAMETER)) { 1576 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "The offset from the start of the PDU header is %u\n", 1577 DGET32(h2c_term_req->fei)); 1578 } 1579 } 1580 1581 static void 1582 spdk_nvmf_tcp_h2c_term_req_hdr_handle(struct spdk_nvmf_tcp_qpair *tqpair, 1583 struct nvme_tcp_pdu *pdu) 1584 { 1585 struct spdk_nvme_tcp_term_req_hdr *h2c_term_req = &pdu->hdr.term_req; 1586 uint32_t error_offset = 0; 1587 enum spdk_nvme_tcp_term_req_fes fes; 1588 1589 1590 if (h2c_term_req->fes > SPDK_NVME_TCP_TERM_REQ_FES_INVALID_DATA_UNSUPPORTED_PARAMETER) { 1591 SPDK_ERRLOG("Fatal Error Stauts(FES) is unknown for h2c_term_req pdu=%p\n", pdu); 1592 fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD; 1593 error_offset = offsetof(struct spdk_nvme_tcp_term_req_hdr, fes); 1594 goto end; 1595 } 1596 1597 /* set the data buffer */ 1598 nvme_tcp_pdu_set_data(pdu, (uint8_t *)pdu->hdr.raw + h2c_term_req->common.hlen, 1599 h2c_term_req->common.plen - h2c_term_req->common.hlen); 1600 spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD); 1601 return; 1602 end: 1603 spdk_nvmf_tcp_send_c2h_term_req(tqpair, pdu, fes, error_offset); 1604 return; 1605 } 1606 1607 static void 1608 spdk_nvmf_tcp_h2c_term_req_payload_handle(struct spdk_nvmf_tcp_qpair *tqpair, 1609 struct nvme_tcp_pdu *pdu) 1610 { 1611 struct spdk_nvme_tcp_term_req_hdr *h2c_term_req = &pdu->hdr.term_req; 1612 1613 spdk_nvmf_tcp_h2c_term_req_dump(h2c_term_req); 1614 spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_ERROR); 1615 return; 1616 } 1617 1618 static void 1619 spdk_nvmf_tcp_pdu_payload_handle(struct spdk_nvmf_tcp_qpair *tqpair) 1620 { 1621 int rc = 0; 1622 struct nvme_tcp_pdu *pdu; 1623 uint32_t crc32c, error_offset = 0; 1624 enum spdk_nvme_tcp_term_req_fes fes; 1625 struct spdk_nvmf_tcp_transport *ttransport; 1626 1627 assert(tqpair->recv_state == NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD); 1628 pdu = &tqpair->pdu_in_progress; 1629 1630 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "enter\n"); 1631 /* check data digest if need */ 1632 if (pdu->ddgst_enable) { 1633 crc32c = nvme_tcp_pdu_calc_data_digest(pdu); 1634 rc = MATCH_DIGEST_WORD(pdu->data_digest, crc32c); 1635 if (rc == 0) { 1636 SPDK_ERRLOG("Data digest error on tqpair=(%p) with pdu=%p\n", tqpair, pdu); 1637 fes = SPDK_NVME_TCP_TERM_REQ_FES_HDGST_ERROR; 1638 spdk_nvmf_tcp_send_c2h_term_req(tqpair, pdu, fes, error_offset); 1639 return; 1640 1641 } 1642 } 1643 1644 ttransport = SPDK_CONTAINEROF(tqpair->qpair.transport, struct spdk_nvmf_tcp_transport, transport); 1645 switch (pdu->hdr.common.pdu_type) { 1646 case SPDK_NVME_TCP_PDU_TYPE_CAPSULE_CMD: 1647 spdk_nvmf_tcp_capsule_cmd_payload_handle(ttransport, tqpair, pdu); 1648 break; 1649 case SPDK_NVME_TCP_PDU_TYPE_H2C_DATA: 1650 spdk_nvmf_tcp_h2c_data_payload_handle(ttransport, tqpair, pdu); 1651 break; 1652 1653 case SPDK_NVME_TCP_PDU_TYPE_H2C_TERM_REQ: 1654 spdk_nvmf_tcp_h2c_term_req_payload_handle(tqpair, pdu); 1655 break; 1656 1657 default: 1658 /* The code should not go to here */ 1659 SPDK_ERRLOG("The code should not go to here\n"); 1660 break; 1661 } 1662 } 1663 1664 static void 1665 spdk_nvmf_tcp_send_icresp_complete(void *cb_arg) 1666 { 1667 struct spdk_nvmf_tcp_qpair *tqpair = cb_arg; 1668 1669 tqpair->state = NVME_TCP_QPAIR_STATE_RUNNING; 1670 } 1671 1672 static void 1673 spdk_nvmf_tcp_icreq_handle(struct spdk_nvmf_tcp_transport *ttransport, 1674 struct spdk_nvmf_tcp_qpair *tqpair, 1675 struct nvme_tcp_pdu *pdu) 1676 { 1677 struct spdk_nvme_tcp_ic_req *ic_req = &pdu->hdr.ic_req; 1678 struct nvme_tcp_pdu *rsp_pdu; 1679 struct spdk_nvme_tcp_ic_resp *ic_resp; 1680 uint32_t error_offset = 0; 1681 enum spdk_nvme_tcp_term_req_fes fes; 1682 1683 /* Only PFV 0 is defined currently */ 1684 if (ic_req->pfv != 0) { 1685 SPDK_ERRLOG("Expected ICReq PFV %u, got %u\n", 0u, ic_req->pfv); 1686 fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD; 1687 error_offset = offsetof(struct spdk_nvme_tcp_ic_req, pfv); 1688 goto end; 1689 } 1690 1691 /* MAXR2T is 0's based */ 1692 tqpair->maxr2t = ic_req->maxr2t + 1ull; 1693 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "maxr2t =%u\n", tqpair->maxr2t); 1694 1695 tqpair->host_hdgst_enable = ic_req->dgst.bits.hdgst_enable ? true : false; 1696 tqpair->host_ddgst_enable = ic_req->dgst.bits.ddgst_enable ? true : false; 1697 1698 tqpair->cpda = spdk_min(ic_req->hpda, SPDK_NVME_TCP_CPDA_MAX); 1699 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "cpda of tqpair=(%p) is : %u\n", tqpair, tqpair->cpda); 1700 1701 rsp_pdu = spdk_nvmf_tcp_pdu_get(tqpair); 1702 if (!rsp_pdu) { 1703 tqpair->state = NVME_TCP_QPAIR_STATE_EXITING; 1704 spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_ERROR); 1705 return; 1706 } 1707 1708 ic_resp = &rsp_pdu->hdr.ic_resp; 1709 ic_resp->common.pdu_type = SPDK_NVME_TCP_PDU_TYPE_IC_RESP; 1710 ic_resp->common.hlen = ic_resp->common.plen = sizeof(*ic_resp); 1711 ic_resp->pfv = 0; 1712 ic_resp->cpda = tqpair->cpda; 1713 tqpair->maxh2cdata = spdk_min(NVMF_TCP_PDU_MAX_H2C_DATA_SIZE, 1714 ttransport->transport.opts.io_unit_size); 1715 ic_resp->maxh2cdata = tqpair->maxh2cdata; 1716 ic_resp->dgst.bits.hdgst_enable = tqpair->host_hdgst_enable ? 1 : 0; 1717 ic_resp->dgst.bits.ddgst_enable = tqpair->host_ddgst_enable ? 1 : 0; 1718 1719 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "host_hdgst_enable: %u\n", tqpair->host_hdgst_enable); 1720 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "host_ddgst_enable: %u\n", tqpair->host_ddgst_enable); 1721 1722 spdk_nvmf_tcp_qpair_write_pdu(tqpair, rsp_pdu, spdk_nvmf_tcp_send_icresp_complete, tqpair); 1723 spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY); 1724 return; 1725 end: 1726 spdk_nvmf_tcp_send_c2h_term_req(tqpair, pdu, fes, error_offset); 1727 return; 1728 } 1729 1730 static void 1731 spdk_nvmf_tcp_pdu_psh_handle(struct spdk_nvmf_tcp_qpair *tqpair) 1732 { 1733 struct nvme_tcp_pdu *pdu; 1734 int rc; 1735 uint32_t crc32c, error_offset = 0; 1736 enum spdk_nvme_tcp_term_req_fes fes; 1737 struct spdk_nvmf_tcp_transport *ttransport; 1738 1739 assert(tqpair->recv_state == NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PSH); 1740 pdu = &tqpair->pdu_in_progress; 1741 1742 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "pdu type of tqpair(%p) is %d\n", tqpair, 1743 pdu->hdr.common.pdu_type); 1744 /* check header digest if needed */ 1745 if (pdu->has_hdgst) { 1746 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Compare the header of pdu=%p on tqpair=%p\n", pdu, tqpair); 1747 crc32c = nvme_tcp_pdu_calc_header_digest(pdu); 1748 rc = MATCH_DIGEST_WORD((uint8_t *)pdu->hdr.raw + pdu->hdr.common.hlen, crc32c); 1749 if (rc == 0) { 1750 SPDK_ERRLOG("Header digest error on tqpair=(%p) with pdu=%p\n", tqpair, pdu); 1751 fes = SPDK_NVME_TCP_TERM_REQ_FES_HDGST_ERROR; 1752 spdk_nvmf_tcp_send_c2h_term_req(tqpair, pdu, fes, error_offset); 1753 return; 1754 1755 } 1756 } 1757 1758 ttransport = SPDK_CONTAINEROF(tqpair->qpair.transport, struct spdk_nvmf_tcp_transport, transport); 1759 switch (pdu->hdr.common.pdu_type) { 1760 case SPDK_NVME_TCP_PDU_TYPE_IC_REQ: 1761 spdk_nvmf_tcp_icreq_handle(ttransport, tqpair, pdu); 1762 break; 1763 case SPDK_NVME_TCP_PDU_TYPE_CAPSULE_CMD: 1764 spdk_nvmf_tcp_capsule_cmd_hdr_handle(ttransport, tqpair, pdu); 1765 break; 1766 case SPDK_NVME_TCP_PDU_TYPE_H2C_DATA: 1767 spdk_nvmf_tcp_h2c_data_hdr_handle(ttransport, tqpair, pdu); 1768 break; 1769 1770 case SPDK_NVME_TCP_PDU_TYPE_H2C_TERM_REQ: 1771 spdk_nvmf_tcp_h2c_term_req_hdr_handle(tqpair, pdu); 1772 break; 1773 1774 default: 1775 SPDK_ERRLOG("Unexpected PDU type 0x%02x\n", tqpair->pdu_in_progress.hdr.common.pdu_type); 1776 fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD; 1777 error_offset = 1; 1778 spdk_nvmf_tcp_send_c2h_term_req(tqpair, pdu, fes, error_offset); 1779 break; 1780 } 1781 } 1782 1783 static void 1784 spdk_nvmf_tcp_pdu_ch_handle(struct spdk_nvmf_tcp_qpair *tqpair) 1785 { 1786 struct nvme_tcp_pdu *pdu; 1787 uint32_t error_offset = 0; 1788 enum spdk_nvme_tcp_term_req_fes fes; 1789 uint8_t expected_hlen, pdo; 1790 bool plen_error = false, pdo_error = false; 1791 1792 assert(tqpair->recv_state == NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_CH); 1793 pdu = &tqpair->pdu_in_progress; 1794 1795 if (pdu->hdr.common.pdu_type == SPDK_NVME_TCP_PDU_TYPE_IC_REQ) { 1796 if (tqpair->state != NVME_TCP_QPAIR_STATE_INVALID) { 1797 SPDK_ERRLOG("Already received ICreq PDU, and reject this pdu=%p\n", pdu); 1798 fes = SPDK_NVME_TCP_TERM_REQ_FES_PDU_SEQUENCE_ERROR; 1799 goto err; 1800 } 1801 expected_hlen = sizeof(struct spdk_nvme_tcp_ic_req); 1802 if (pdu->hdr.common.plen != expected_hlen) { 1803 plen_error = true; 1804 } 1805 } else { 1806 if (tqpair->state != NVME_TCP_QPAIR_STATE_RUNNING) { 1807 SPDK_ERRLOG("The TCP/IP connection is not negotitated\n"); 1808 fes = SPDK_NVME_TCP_TERM_REQ_FES_PDU_SEQUENCE_ERROR; 1809 goto err; 1810 } 1811 1812 switch (pdu->hdr.common.pdu_type) { 1813 case SPDK_NVME_TCP_PDU_TYPE_CAPSULE_CMD: 1814 expected_hlen = sizeof(struct spdk_nvme_tcp_cmd); 1815 pdo = pdu->hdr.common.pdo; 1816 if ((tqpair->cpda != 0) && (pdo != ((tqpair->cpda + 1) << 2))) { 1817 pdo_error = true; 1818 break; 1819 } 1820 1821 if (pdu->hdr.common.plen < expected_hlen) { 1822 plen_error = true; 1823 } 1824 break; 1825 case SPDK_NVME_TCP_PDU_TYPE_H2C_DATA: 1826 expected_hlen = sizeof(struct spdk_nvme_tcp_h2c_data_hdr); 1827 pdo = pdu->hdr.common.pdo; 1828 if ((tqpair->cpda != 0) && (pdo != ((tqpair->cpda + 1) << 2))) { 1829 pdo_error = true; 1830 break; 1831 } 1832 if (pdu->hdr.common.plen < expected_hlen) { 1833 plen_error = true; 1834 } 1835 break; 1836 1837 case SPDK_NVME_TCP_PDU_TYPE_H2C_TERM_REQ: 1838 expected_hlen = sizeof(struct spdk_nvme_tcp_term_req_hdr); 1839 if ((pdu->hdr.common.plen <= expected_hlen) || 1840 (pdu->hdr.common.plen > SPDK_NVME_TCP_TERM_REQ_PDU_MAX_SIZE)) { 1841 plen_error = true; 1842 } 1843 break; 1844 1845 default: 1846 SPDK_ERRLOG("Unexpected PDU type 0x%02x\n", pdu->hdr.common.pdu_type); 1847 fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD; 1848 error_offset = offsetof(struct spdk_nvme_tcp_common_pdu_hdr, pdu_type); 1849 goto err; 1850 } 1851 } 1852 1853 if (pdu->hdr.common.hlen != expected_hlen) { 1854 SPDK_ERRLOG("PDU type=0x%02x, Expected ICReq header length %u, got %u on tqpair=%p\n", 1855 pdu->hdr.common.pdu_type, 1856 expected_hlen, pdu->hdr.common.hlen, tqpair); 1857 fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD; 1858 error_offset = offsetof(struct spdk_nvme_tcp_common_pdu_hdr, hlen); 1859 goto err; 1860 } else if (pdo_error) { 1861 fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD; 1862 error_offset = offsetof(struct spdk_nvme_tcp_common_pdu_hdr, pdo); 1863 } else if (plen_error) { 1864 fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD; 1865 error_offset = offsetof(struct spdk_nvme_tcp_common_pdu_hdr, plen); 1866 goto err; 1867 } else { 1868 spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PSH); 1869 return; 1870 } 1871 err: 1872 spdk_nvmf_tcp_send_c2h_term_req(tqpair, pdu, fes, error_offset); 1873 } 1874 1875 static int 1876 spdk_nvmf_tcp_sock_process(struct spdk_nvmf_tcp_qpair *tqpair) 1877 { 1878 int rc = 0; 1879 struct nvme_tcp_pdu *pdu; 1880 enum nvme_tcp_pdu_recv_state prev_state; 1881 uint32_t data_len; 1882 uint8_t psh_len, pdo, hlen; 1883 int8_t padding_len; 1884 1885 /* The loop here is to allow for several back-to-back state changes. */ 1886 do { 1887 prev_state = tqpair->recv_state; 1888 1889 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "tqpair(%p) recv pdu entering state %d\n", tqpair, prev_state); 1890 1891 switch (tqpair->recv_state) { 1892 /* Wait for the common header */ 1893 case NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY: 1894 case NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_CH: 1895 pdu = &tqpair->pdu_in_progress; 1896 1897 rc = nvme_tcp_read_data(tqpair->sock, 1898 sizeof(struct spdk_nvme_tcp_common_pdu_hdr) - pdu->ch_valid_bytes, 1899 (void *)&pdu->hdr.common + pdu->ch_valid_bytes); 1900 if (rc < 0) { 1901 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "will disconnect tqpair=%p\n", tqpair); 1902 return NVME_TCP_PDU_FATAL; 1903 } else if (rc > 0) { 1904 pdu->ch_valid_bytes += rc; 1905 spdk_trace_record(TRACE_TCP_READ_FROM_SOCKET_DONE, 0, rc, 0, 0); 1906 if (spdk_likely(tqpair->recv_state == NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY)) { 1907 spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_CH); 1908 } 1909 } 1910 1911 if (pdu->ch_valid_bytes < sizeof(struct spdk_nvme_tcp_common_pdu_hdr)) { 1912 return NVME_TCP_PDU_IN_PROGRESS; 1913 } 1914 1915 /* The command header of this PDU has now been read from the socket. */ 1916 spdk_nvmf_tcp_pdu_ch_handle(tqpair); 1917 break; 1918 /* Wait for the pdu specific header */ 1919 case NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PSH: 1920 pdu = &tqpair->pdu_in_progress; 1921 psh_len = hlen = pdu->hdr.common.hlen; 1922 /* Only capsule_cmd and h2c_data has header digest */ 1923 if (((pdu->hdr.common.pdu_type == SPDK_NVME_TCP_PDU_TYPE_CAPSULE_CMD) || 1924 (pdu->hdr.common.pdu_type == SPDK_NVME_TCP_PDU_TYPE_H2C_DATA)) && 1925 tqpair->host_hdgst_enable) { 1926 pdu->has_hdgst = true; 1927 psh_len += SPDK_NVME_TCP_DIGEST_LEN; 1928 if (pdu->hdr.common.plen > psh_len) { 1929 pdo = pdu->hdr.common.pdo; 1930 padding_len = pdo - psh_len; 1931 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "padding length is =%d for pdu=%p on tqpair=%p\n", padding_len, 1932 pdu, tqpair); 1933 if (padding_len > 0) { 1934 psh_len = pdo; 1935 } 1936 } 1937 } 1938 1939 psh_len -= sizeof(struct spdk_nvme_tcp_common_pdu_hdr); 1940 /* The following will read psh + hdgest (if possbile) + padding (if posssible) */ 1941 if (pdu->psh_valid_bytes < psh_len) { 1942 rc = nvme_tcp_read_data(tqpair->sock, 1943 psh_len - pdu->psh_valid_bytes, 1944 (void *)&pdu->hdr.raw + sizeof(struct spdk_nvme_tcp_common_pdu_hdr) + pdu->psh_valid_bytes); 1945 if (rc < 0) { 1946 return NVME_TCP_PDU_FATAL; 1947 } else if (rc > 0) { 1948 spdk_trace_record(TRACE_TCP_READ_FROM_SOCKET_DONE, 1949 0, rc, 0, 0); 1950 pdu->psh_valid_bytes += rc; 1951 } 1952 if (pdu->psh_valid_bytes < psh_len) { 1953 return NVME_TCP_PDU_IN_PROGRESS; 1954 } 1955 } 1956 1957 /* All header(ch, psh, head digist) of this PDU has now been read from the socket. */ 1958 spdk_nvmf_tcp_pdu_psh_handle(tqpair); 1959 break; 1960 case NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD: 1961 pdu = &tqpair->pdu_in_progress; 1962 1963 /* check whether the data is valid, if not we just return */ 1964 if (!pdu->data_len) { 1965 return NVME_TCP_PDU_IN_PROGRESS; 1966 } 1967 1968 data_len = pdu->data_len; 1969 /* data digest */ 1970 if (spdk_unlikely((pdu->hdr.common.pdu_type != SPDK_NVME_TCP_PDU_TYPE_H2C_TERM_REQ) && 1971 tqpair->host_ddgst_enable)) { 1972 data_len += SPDK_NVME_TCP_DIGEST_LEN; 1973 pdu->ddgst_enable = true; 1974 } 1975 1976 rc = nvme_tcp_read_payload_data(tqpair->sock, pdu); 1977 if (rc < 0) { 1978 return NVME_TCP_PDU_IN_PROGRESS; 1979 } 1980 1981 pdu->readv_offset += rc; 1982 if (pdu->readv_offset < data_len) { 1983 return NVME_TCP_PDU_IN_PROGRESS; 1984 } 1985 1986 /* All of this PDU has now been read from the socket. */ 1987 spdk_nvmf_tcp_pdu_payload_handle(tqpair); 1988 break; 1989 case NVME_TCP_PDU_RECV_STATE_ERROR: 1990 pdu = &tqpair->pdu_in_progress; 1991 /* Check whether the connection is closed. Each time, we only read 1 byte every time */ 1992 rc = nvme_tcp_read_data(tqpair->sock, 1, (void *)&pdu->hdr.common); 1993 if (rc < 0) { 1994 return NVME_TCP_PDU_FATAL; 1995 } 1996 break; 1997 default: 1998 assert(0); 1999 SPDK_ERRLOG("code should not come to here"); 2000 break; 2001 } 2002 } while (tqpair->recv_state != prev_state); 2003 2004 return rc; 2005 } 2006 2007 static enum spdk_nvme_data_transfer 2008 spdk_nvmf_tcp_req_get_xfer(struct spdk_nvmf_tcp_req *tcp_req) { 2009 enum spdk_nvme_data_transfer xfer; 2010 struct spdk_nvme_cmd *cmd = &tcp_req->req.cmd->nvme_cmd; 2011 struct spdk_nvme_sgl_descriptor *sgl = &cmd->dptr.sgl1; 2012 2013 /* Figure out data transfer direction */ 2014 if (cmd->opc == SPDK_NVME_OPC_FABRIC) 2015 { 2016 xfer = spdk_nvme_opc_get_data_transfer(tcp_req->req.cmd->nvmf_cmd.fctype); 2017 } else 2018 { 2019 xfer = spdk_nvme_opc_get_data_transfer(cmd->opc); 2020 2021 /* Some admin commands are special cases */ 2022 if ((tcp_req->req.qpair->qid == 0) && 2023 ((cmd->opc == SPDK_NVME_OPC_GET_FEATURES) || 2024 (cmd->opc == SPDK_NVME_OPC_SET_FEATURES))) { 2025 switch (cmd->cdw10 & 0xff) { 2026 case SPDK_NVME_FEAT_LBA_RANGE_TYPE: 2027 case SPDK_NVME_FEAT_AUTONOMOUS_POWER_STATE_TRANSITION: 2028 case SPDK_NVME_FEAT_HOST_IDENTIFIER: 2029 break; 2030 default: 2031 xfer = SPDK_NVME_DATA_NONE; 2032 } 2033 } 2034 } 2035 2036 if (xfer == SPDK_NVME_DATA_NONE) 2037 { 2038 return xfer; 2039 } 2040 2041 /* Even for commands that may transfer data, they could have specified 0 length. 2042 * We want those to show up with xfer SPDK_NVME_DATA_NONE. 2043 */ 2044 switch (sgl->generic.type) 2045 { 2046 case SPDK_NVME_SGL_TYPE_DATA_BLOCK: 2047 case SPDK_NVME_SGL_TYPE_BIT_BUCKET: 2048 case SPDK_NVME_SGL_TYPE_SEGMENT: 2049 case SPDK_NVME_SGL_TYPE_LAST_SEGMENT: 2050 case SPDK_NVME_SGL_TYPE_TRANSPORT_DATA_BLOCK: 2051 if (sgl->unkeyed.length == 0) { 2052 xfer = SPDK_NVME_DATA_NONE; 2053 } 2054 break; 2055 case SPDK_NVME_SGL_TYPE_KEYED_DATA_BLOCK: 2056 if (sgl->keyed.length == 0) { 2057 xfer = SPDK_NVME_DATA_NONE; 2058 } 2059 break; 2060 } 2061 2062 return xfer; 2063 } 2064 2065 static void 2066 spdk_nvmf_tcp_request_free_buffers(struct spdk_nvmf_tcp_req *tcp_req, 2067 struct spdk_nvmf_transport_poll_group *group, struct spdk_nvmf_transport *transport) 2068 { 2069 for (uint32_t i = 0; i < tcp_req->req.iovcnt; i++) { 2070 assert(tcp_req->buffers[i] != NULL); 2071 if (group->buf_cache_count < group->buf_cache_size) { 2072 STAILQ_INSERT_HEAD(&group->buf_cache, 2073 (struct spdk_nvmf_transport_pg_cache_buf *)tcp_req->buffers[i], link); 2074 group->buf_cache_count++; 2075 } else { 2076 spdk_mempool_put(transport->data_buf_pool, tcp_req->buffers[i]); 2077 } 2078 tcp_req->req.iov[i].iov_base = NULL; 2079 tcp_req->buffers[i] = NULL; 2080 tcp_req->req.iov[i].iov_len = 0; 2081 } 2082 tcp_req->data_from_pool = false; 2083 } 2084 2085 static int 2086 spdk_nvmf_tcp_req_fill_iovs(struct spdk_nvmf_tcp_transport *ttransport, 2087 struct spdk_nvmf_tcp_req *tcp_req) 2088 { 2089 void *buf = NULL; 2090 uint32_t length = tcp_req->req.length; 2091 uint32_t i = 0; 2092 struct spdk_nvmf_tcp_qpair *tqpair; 2093 struct spdk_nvmf_transport_poll_group *group; 2094 2095 tqpair = SPDK_CONTAINEROF(tcp_req->req.qpair, struct spdk_nvmf_tcp_qpair, qpair); 2096 group = &tqpair->group->group; 2097 2098 tcp_req->req.iovcnt = 0; 2099 while (length) { 2100 if (!(STAILQ_EMPTY(&group->buf_cache))) { 2101 group->buf_cache_count--; 2102 buf = STAILQ_FIRST(&group->buf_cache); 2103 STAILQ_REMOVE_HEAD(&group->buf_cache, link); 2104 } else { 2105 buf = spdk_mempool_get(ttransport->transport.data_buf_pool); 2106 if (!buf) { 2107 goto nomem; 2108 } 2109 } 2110 2111 tcp_req->req.iov[i].iov_base = (void *)((uintptr_t)(buf + NVMF_DATA_BUFFER_MASK) & 2112 ~NVMF_DATA_BUFFER_MASK); 2113 tcp_req->req.iov[i].iov_len = spdk_min(length, ttransport->transport.opts.io_unit_size); 2114 tcp_req->req.iovcnt++; 2115 tcp_req->buffers[i] = buf; 2116 length -= tcp_req->req.iov[i].iov_len; 2117 i++; 2118 } 2119 2120 assert(tcp_req->req.iovcnt < SPDK_NVMF_MAX_SGL_ENTRIES); 2121 tcp_req->data_from_pool = true; 2122 return 0; 2123 2124 nomem: 2125 spdk_nvmf_tcp_request_free_buffers(tcp_req, group, &ttransport->transport); 2126 tcp_req->req.iovcnt = 0; 2127 return -ENOMEM; 2128 } 2129 2130 static int 2131 spdk_nvmf_tcp_req_parse_sgl(struct spdk_nvmf_tcp_transport *ttransport, 2132 struct spdk_nvmf_tcp_req *tcp_req) 2133 { 2134 struct spdk_nvme_cmd *cmd; 2135 struct spdk_nvme_cpl *rsp; 2136 struct spdk_nvme_sgl_descriptor *sgl; 2137 2138 cmd = &tcp_req->req.cmd->nvme_cmd; 2139 rsp = &tcp_req->req.rsp->nvme_cpl; 2140 sgl = &cmd->dptr.sgl1; 2141 2142 if (sgl->generic.type == SPDK_NVME_SGL_TYPE_TRANSPORT_DATA_BLOCK && 2143 sgl->unkeyed.subtype == SPDK_NVME_SGL_SUBTYPE_TRANSPORT) { 2144 if (sgl->unkeyed.length > ttransport->transport.opts.max_io_size) { 2145 SPDK_ERRLOG("SGL length 0x%x exceeds max io size 0x%x\n", 2146 sgl->unkeyed.length, ttransport->transport.opts.max_io_size); 2147 rsp->status.sc = SPDK_NVME_SC_DATA_SGL_LENGTH_INVALID; 2148 return -1; 2149 } 2150 2151 /* fill request length and populate iovs */ 2152 tcp_req->req.length = sgl->unkeyed.length; 2153 2154 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Data requested length= 0x%x\n", 2155 sgl->unkeyed.length); 2156 2157 if (spdk_nvmf_tcp_req_fill_iovs(ttransport, tcp_req) < 0) { 2158 /* No available buffers. Queue this request up. */ 2159 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "No available large data buffers. Queueing request %p\n", tcp_req); 2160 return 0; 2161 } 2162 2163 /* backward compatible */ 2164 tcp_req->req.data = tcp_req->req.iov[0].iov_base; 2165 2166 2167 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Request %p took %d buffer/s from central pool, and data=%p\n", 2168 tcp_req, 2169 tcp_req->req.iovcnt, tcp_req->req.data); 2170 2171 return 0; 2172 } else if (sgl->generic.type == SPDK_NVME_SGL_TYPE_DATA_BLOCK && 2173 sgl->unkeyed.subtype == SPDK_NVME_SGL_SUBTYPE_OFFSET) { 2174 uint64_t offset = sgl->address; 2175 uint32_t max_len = ttransport->transport.opts.in_capsule_data_size; 2176 2177 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "In-capsule data: offset 0x%" PRIx64 ", length 0x%x\n", 2178 offset, sgl->unkeyed.length); 2179 2180 if (offset > max_len) { 2181 SPDK_ERRLOG("In-capsule offset 0x%" PRIx64 " exceeds capsule length 0x%x\n", 2182 offset, max_len); 2183 rsp->status.sc = SPDK_NVME_SC_INVALID_SGL_OFFSET; 2184 return -1; 2185 } 2186 max_len -= (uint32_t)offset; 2187 2188 if (sgl->unkeyed.length > max_len) { 2189 SPDK_ERRLOG("In-capsule data length 0x%x exceeds capsule length 0x%x\n", 2190 sgl->unkeyed.length, max_len); 2191 rsp->status.sc = SPDK_NVME_SC_DATA_SGL_LENGTH_INVALID; 2192 return -1; 2193 } 2194 2195 tcp_req->req.data = tcp_req->buf + offset; 2196 tcp_req->data_from_pool = false; 2197 tcp_req->req.length = sgl->unkeyed.length; 2198 2199 tcp_req->req.iov[0].iov_base = tcp_req->req.data; 2200 tcp_req->req.iov[0].iov_len = tcp_req->req.length; 2201 tcp_req->req.iovcnt = 1; 2202 2203 return 0; 2204 } 2205 2206 SPDK_ERRLOG("Invalid NVMf I/O Command SGL: Type 0x%x, Subtype 0x%x\n", 2207 sgl->generic.type, sgl->generic.subtype); 2208 rsp->status.sc = SPDK_NVME_SC_SGL_DESCRIPTOR_TYPE_INVALID; 2209 return -1; 2210 } 2211 2212 static void 2213 spdk_nvmf_tcp_send_c2h_data(struct spdk_nvmf_tcp_qpair *tqpair, 2214 struct spdk_nvmf_tcp_req *tcp_req) 2215 { 2216 struct nvme_tcp_pdu *rsp_pdu; 2217 struct spdk_nvme_tcp_c2h_data_hdr *c2h_data; 2218 uint32_t plen, pdo, alignment, offset, iov_index; 2219 2220 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "enter\n"); 2221 2222 /* always use the first iov_len, which is correct */ 2223 iov_index = tcp_req->c2h_data_offset / tcp_req->req.iov[0].iov_len; 2224 offset = tcp_req->c2h_data_offset % tcp_req->req.iov[0].iov_len; 2225 2226 rsp_pdu = spdk_nvmf_tcp_pdu_get(tqpair); 2227 assert(rsp_pdu != NULL); 2228 2229 c2h_data = &rsp_pdu->hdr.c2h_data; 2230 c2h_data->common.pdu_type = SPDK_NVME_TCP_PDU_TYPE_C2H_DATA; 2231 plen = c2h_data->common.hlen = sizeof(*c2h_data); 2232 2233 if (tqpair->host_hdgst_enable) { 2234 plen += SPDK_NVME_TCP_DIGEST_LEN; 2235 c2h_data->common.flags |= SPDK_NVME_TCP_CH_FLAGS_HDGSTF; 2236 } 2237 2238 /* set the psh */ 2239 c2h_data->cccid = tcp_req->req.cmd->nvme_cmd.cid; 2240 c2h_data->datal = spdk_min(NVMF_TCP_PDU_MAX_C2H_DATA_SIZE, 2241 (tcp_req->req.iov[iov_index].iov_len - offset)); 2242 c2h_data->datao = tcp_req->c2h_data_offset; 2243 2244 /* set the padding */ 2245 rsp_pdu->padding_len = 0; 2246 pdo = plen; 2247 if (tqpair->cpda) { 2248 alignment = (tqpair->cpda + 1) << 2; 2249 if (alignment > plen) { 2250 rsp_pdu->padding_len = alignment - plen; 2251 pdo = plen = alignment; 2252 } 2253 } 2254 2255 c2h_data->common.pdo = pdo; 2256 plen += c2h_data->datal; 2257 if (tqpair->host_ddgst_enable) { 2258 c2h_data->common.flags |= SPDK_NVME_TCP_CH_FLAGS_DDGSTF; 2259 plen += SPDK_NVME_TCP_DIGEST_LEN; 2260 } 2261 2262 c2h_data->common.plen = plen; 2263 2264 nvme_tcp_pdu_set_data(rsp_pdu, tcp_req->req.iov[iov_index].iov_base + offset, c2h_data->datal); 2265 2266 tcp_req->c2h_data_offset += c2h_data->datal; 2267 if (iov_index == (tcp_req->req.iovcnt - 1) && (tcp_req->c2h_data_offset == tcp_req->req.length)) { 2268 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Last pdu for tcp_req=%p on tqpair=%p\n", tcp_req, tqpair); 2269 c2h_data->common.flags |= SPDK_NVME_TCP_C2H_DATA_FLAGS_LAST_PDU; 2270 /* The linux kernel does not support this yet */ 2271 #if LINUX_KERNEL_SUPPORT_NOT_SENDING_RESP_FOR_C2H 2272 c2h_data->common.flags |= SPDK_NVME_TCP_C2H_DATA_FLAGS_SUCCESS; 2273 #endif 2274 TAILQ_REMOVE(&tqpair->queued_c2h_data_tcp_req, tcp_req, link); 2275 } 2276 2277 tqpair->c2h_data_pdu_cnt += 1; 2278 spdk_nvmf_tcp_qpair_write_pdu(tqpair, rsp_pdu, spdk_nvmf_tcp_pdu_c2h_data_complete, tcp_req); 2279 } 2280 2281 static int 2282 spdk_nvmf_tcp_calc_c2h_data_pdu_num(struct spdk_nvmf_tcp_req *tcp_req) 2283 { 2284 uint32_t i, iov_cnt, pdu_num = 0; 2285 2286 iov_cnt = tcp_req->req.iovcnt; 2287 for (i = 0; i < iov_cnt; i++) { 2288 pdu_num += (tcp_req->req.iov[i].iov_len + NVMF_TCP_PDU_MAX_C2H_DATA_SIZE - 1) / 2289 NVMF_TCP_PDU_MAX_C2H_DATA_SIZE; 2290 } 2291 2292 return pdu_num; 2293 } 2294 2295 static void 2296 spdk_nvmf_tcp_handle_pending_c2h_data_queue(struct spdk_nvmf_tcp_qpair *tqpair) 2297 { 2298 struct spdk_nvmf_tcp_req *tcp_req; 2299 2300 while (!TAILQ_EMPTY(&tqpair->queued_c2h_data_tcp_req) && 2301 (tqpair->c2h_data_pdu_cnt < NVMF_TCP_QPAIR_MAX_C2H_PDU_NUM)) { 2302 tcp_req = TAILQ_FIRST(&tqpair->queued_c2h_data_tcp_req); 2303 spdk_nvmf_tcp_send_c2h_data(tqpair, tcp_req); 2304 } 2305 } 2306 2307 static void 2308 spdk_nvmf_tcp_queue_c2h_data(struct spdk_nvmf_tcp_req *tcp_req, 2309 struct spdk_nvmf_tcp_qpair *tqpair) 2310 { 2311 tcp_req->c2h_data_pdu_num = spdk_nvmf_tcp_calc_c2h_data_pdu_num(tcp_req); 2312 2313 assert(tcp_req->c2h_data_pdu_num < NVMF_TCP_QPAIR_MAX_C2H_PDU_NUM); 2314 2315 TAILQ_INSERT_TAIL(&tqpair->queued_c2h_data_tcp_req, tcp_req, link); 2316 spdk_nvmf_tcp_handle_pending_c2h_data_queue(tqpair); 2317 } 2318 2319 static int 2320 request_transfer_out(struct spdk_nvmf_request *req) 2321 { 2322 struct spdk_nvmf_tcp_req *tcp_req; 2323 struct spdk_nvmf_qpair *qpair; 2324 struct spdk_nvmf_tcp_qpair *tqpair; 2325 struct spdk_nvme_cpl *rsp; 2326 2327 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "enter\n"); 2328 2329 qpair = req->qpair; 2330 rsp = &req->rsp->nvme_cpl; 2331 tcp_req = SPDK_CONTAINEROF(req, struct spdk_nvmf_tcp_req, req); 2332 2333 /* Advance our sq_head pointer */ 2334 if (qpair->sq_head == qpair->sq_head_max) { 2335 qpair->sq_head = 0; 2336 } else { 2337 qpair->sq_head++; 2338 } 2339 rsp->sqhd = qpair->sq_head; 2340 2341 tqpair = SPDK_CONTAINEROF(tcp_req->req.qpair, struct spdk_nvmf_tcp_qpair, qpair); 2342 spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_TRANSFERRING_CONTROLLER_TO_HOST); 2343 if (rsp->status.sc == SPDK_NVME_SC_SUCCESS && 2344 req->xfer == SPDK_NVME_DATA_CONTROLLER_TO_HOST) { 2345 spdk_nvmf_tcp_queue_c2h_data(tcp_req, tqpair); 2346 } else { 2347 spdk_nvmf_tcp_send_capsule_resp_pdu(tcp_req, tqpair); 2348 } 2349 2350 return 0; 2351 } 2352 2353 static void 2354 spdk_nvmf_tcp_pdu_set_buf_from_req(struct spdk_nvmf_tcp_qpair *tqpair, 2355 struct spdk_nvmf_tcp_req *tcp_req) 2356 { 2357 struct nvme_tcp_pdu *pdu; 2358 2359 if (tcp_req->data_from_pool) { 2360 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Will send r2t for tcp_req(%p) on tqpair=%p\n", tcp_req, tqpair); 2361 tcp_req->next_expected_r2t_offset = 0; 2362 spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_DATA_PENDING_FOR_R2T); 2363 spdk_nvmf_tcp_handle_queued_r2t_req(tqpair); 2364 } else { 2365 pdu = &tqpair->pdu_in_progress; 2366 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Not need to send r2t for tcp_req(%p) on tqpair=%p\n", tcp_req, 2367 tqpair); 2368 /* No need to send r2t, contained in the capsuled data */ 2369 nvme_tcp_pdu_set_data(pdu, tcp_req->req.data, tcp_req->req.length); 2370 spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD); 2371 spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER); 2372 } 2373 } 2374 2375 static void 2376 spdk_nvmf_tcp_set_incapsule_data(struct spdk_nvmf_tcp_qpair *tqpair, 2377 struct spdk_nvmf_tcp_req *tcp_req) 2378 { 2379 struct nvme_tcp_pdu *pdu; 2380 uint32_t plen = 0; 2381 2382 pdu = &tqpair->pdu_in_progress; 2383 plen = pdu->hdr.common.hlen; 2384 2385 if (tqpair->host_hdgst_enable) { 2386 plen += SPDK_NVME_TCP_DIGEST_LEN; 2387 } 2388 2389 if (pdu->hdr.common.plen != plen) { 2390 tcp_req->has_incapsule_data = true; 2391 } 2392 } 2393 2394 static bool 2395 spdk_nvmf_tcp_req_process(struct spdk_nvmf_tcp_transport *ttransport, 2396 struct spdk_nvmf_tcp_req *tcp_req) 2397 { 2398 struct spdk_nvmf_tcp_qpair *tqpair; 2399 struct spdk_nvme_cpl *rsp = &tcp_req->req.rsp->nvme_cpl; 2400 int rc; 2401 enum spdk_nvmf_tcp_req_state prev_state; 2402 bool progress = false; 2403 struct spdk_nvmf_transport_poll_group *group; 2404 2405 tqpair = SPDK_CONTAINEROF(tcp_req->req.qpair, struct spdk_nvmf_tcp_qpair, qpair); 2406 group = &tqpair->group->group; 2407 assert(tcp_req->state != TCP_REQUEST_STATE_FREE); 2408 2409 /* The loop here is to allow for several back-to-back state changes. */ 2410 do { 2411 prev_state = tcp_req->state; 2412 2413 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Request %p entering state %d on tqpair=%p\n", tcp_req, prev_state, 2414 tqpair); 2415 2416 switch (tcp_req->state) { 2417 case TCP_REQUEST_STATE_FREE: 2418 /* Some external code must kick a request into TCP_REQUEST_STATE_NEW 2419 * to escape this state. */ 2420 break; 2421 case TCP_REQUEST_STATE_NEW: 2422 spdk_trace_record(TRACE_TCP_REQUEST_STATE_NEW, 0, 0, (uintptr_t)tcp_req, 0); 2423 2424 /* copy the cmd from the receive pdu */ 2425 tcp_req->cmd = tqpair->pdu_in_progress.hdr.capsule_cmd.ccsqe; 2426 2427 /* The next state transition depends on the data transfer needs of this request. */ 2428 tcp_req->req.xfer = spdk_nvmf_tcp_req_get_xfer(tcp_req); 2429 2430 /* If no data to transfer, ready to execute. */ 2431 if (tcp_req->req.xfer == SPDK_NVME_DATA_NONE) { 2432 /* Reset the tqpair receving pdu state */ 2433 spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY); 2434 spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_READY_TO_EXECUTE); 2435 break; 2436 } 2437 2438 spdk_nvmf_tcp_set_incapsule_data(tqpair, tcp_req); 2439 2440 if (!tcp_req->has_incapsule_data) { 2441 spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY); 2442 } 2443 2444 spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_NEED_BUFFER); 2445 TAILQ_INSERT_TAIL(&tqpair->group->pending_data_buf_queue, tcp_req, link); 2446 break; 2447 case TCP_REQUEST_STATE_NEED_BUFFER: 2448 spdk_trace_record(TRACE_TCP_REQUEST_STATE_NEED_BUFFER, 0, 0, (uintptr_t)tcp_req, 0); 2449 2450 assert(tcp_req->req.xfer != SPDK_NVME_DATA_NONE); 2451 2452 if (!tcp_req->has_incapsule_data && 2453 (tcp_req != TAILQ_FIRST(&tqpair->group->pending_data_buf_queue))) { 2454 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, 2455 "Not the first element to wait for the buf for tcp_req(%p) on tqpair=%p\n", 2456 tcp_req, tqpair); 2457 /* This request needs to wait in line to obtain a buffer */ 2458 break; 2459 } 2460 2461 /* Try to get a data buffer */ 2462 rc = spdk_nvmf_tcp_req_parse_sgl(ttransport, tcp_req); 2463 if (rc < 0) { 2464 TAILQ_REMOVE(&tqpair->group->pending_data_buf_queue, tcp_req, link); 2465 rsp->status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR; 2466 /* Reset the tqpair receving pdu state */ 2467 spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_ERROR); 2468 spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_READY_TO_COMPLETE); 2469 break; 2470 } 2471 2472 if (!tcp_req->req.data) { 2473 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "No buffer allocated for tcp_req(%p) on tqpair(%p\n)", 2474 tcp_req, tqpair); 2475 /* No buffers available. */ 2476 break; 2477 } 2478 2479 TAILQ_REMOVE(&tqpair->group->pending_data_buf_queue, tcp_req, link); 2480 2481 /* If data is transferring from host to controller, we need to do a transfer from the host. */ 2482 if (tcp_req->req.xfer == SPDK_NVME_DATA_HOST_TO_CONTROLLER) { 2483 spdk_nvmf_tcp_pdu_set_buf_from_req(tqpair, tcp_req); 2484 break; 2485 } 2486 2487 spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_READY_TO_EXECUTE); 2488 break; 2489 case TCP_REQUEST_STATE_DATA_PENDING_FOR_R2T: 2490 spdk_trace_record(TCP_REQUEST_STATE_DATA_PENDING_FOR_R2T, 0, 0, 2491 (uintptr_t)tcp_req, 0); 2492 /* Some external code must kick a request into TCP_REQUEST_STATE_DATA_PENDING_R2T 2493 * to escape this state. */ 2494 break; 2495 2496 case TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER: 2497 spdk_trace_record(TRACE_TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER, 0, 0, 2498 (uintptr_t)tcp_req, 0); 2499 /* Some external code must kick a request into TCP_REQUEST_STATE_READY_TO_EXECUTE 2500 * to escape this state. */ 2501 break; 2502 case TCP_REQUEST_STATE_READY_TO_EXECUTE: 2503 spdk_trace_record(TRACE_TCP_REQUEST_STATE_READY_TO_EXECUTE, 0, 0, (uintptr_t)tcp_req, 0); 2504 spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_EXECUTING); 2505 spdk_nvmf_request_exec(&tcp_req->req); 2506 break; 2507 case TCP_REQUEST_STATE_EXECUTING: 2508 spdk_trace_record(TRACE_TCP_REQUEST_STATE_EXECUTING, 0, 0, (uintptr_t)tcp_req, 0); 2509 /* Some external code must kick a request into TCP_REQUEST_STATE_EXECUTED 2510 * to escape this state. */ 2511 break; 2512 case TCP_REQUEST_STATE_EXECUTED: 2513 spdk_trace_record(TRACE_TCP_REQUEST_STATE_EXECUTED, 0, 0, (uintptr_t)tcp_req, 0); 2514 spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_READY_TO_COMPLETE); 2515 break; 2516 case TCP_REQUEST_STATE_READY_TO_COMPLETE: 2517 spdk_trace_record(TRACE_TCP_REQUEST_STATE_READY_TO_COMPLETE, 0, 0, (uintptr_t)tcp_req, 0); 2518 rc = request_transfer_out(&tcp_req->req); 2519 assert(rc == 0); /* No good way to handle this currently */ 2520 break; 2521 case TCP_REQUEST_STATE_TRANSFERRING_CONTROLLER_TO_HOST: 2522 spdk_trace_record(TRACE_TCP_REQUEST_STATE_TRANSFERRING_CONTROLLER_TO_HOST, 0, 0, 2523 (uintptr_t)tcp_req, 2524 0); 2525 /* Some external code must kick a request into TCP_REQUEST_STATE_COMPLETED 2526 * to escape this state. */ 2527 break; 2528 case TCP_REQUEST_STATE_COMPLETED: 2529 spdk_trace_record(TRACE_TCP_REQUEST_STATE_COMPLETED, 0, 0, (uintptr_t)tcp_req, 0); 2530 if (tcp_req->data_from_pool) { 2531 spdk_nvmf_tcp_request_free_buffers(tcp_req, group, &ttransport->transport); 2532 } 2533 tcp_req->req.length = 0; 2534 tcp_req->req.iovcnt = 0; 2535 tcp_req->req.data = NULL; 2536 spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_FREE); 2537 break; 2538 case TCP_REQUEST_NUM_STATES: 2539 default: 2540 assert(0); 2541 break; 2542 } 2543 2544 if (tcp_req->state != prev_state) { 2545 progress = true; 2546 } 2547 } while (tcp_req->state != prev_state); 2548 2549 return progress; 2550 } 2551 2552 static void 2553 spdk_nvmf_tcp_qpair_process_pending(struct spdk_nvmf_tcp_transport *ttransport, 2554 struct spdk_nvmf_tcp_qpair *tqpair) 2555 { 2556 struct spdk_nvmf_tcp_req *tcp_req, *req_tmp; 2557 2558 /* Tqpair is not in a good state, so return it */ 2559 if (spdk_unlikely(tqpair->recv_state == NVME_TCP_PDU_RECV_STATE_ERROR)) { 2560 return; 2561 } 2562 2563 spdk_nvmf_tcp_handle_queued_r2t_req(tqpair); 2564 2565 TAILQ_FOREACH_SAFE(tcp_req, &tqpair->group->pending_data_buf_queue, link, req_tmp) { 2566 if (spdk_nvmf_tcp_req_process(ttransport, tcp_req) == false) { 2567 break; 2568 } 2569 } 2570 } 2571 2572 static void 2573 spdk_nvmf_tcp_sock_cb(void *arg, struct spdk_sock_group *group, struct spdk_sock *sock) 2574 { 2575 struct spdk_nvmf_tcp_qpair *tqpair = arg; 2576 struct spdk_nvmf_tcp_transport *ttransport; 2577 int rc; 2578 2579 assert(tqpair != NULL); 2580 2581 ttransport = SPDK_CONTAINEROF(tqpair->qpair.transport, struct spdk_nvmf_tcp_transport, transport); 2582 spdk_nvmf_tcp_qpair_process_pending(ttransport, tqpair); 2583 rc = spdk_nvmf_tcp_sock_process(tqpair); 2584 2585 /* check the following two factors: 2586 * rc: The socket is closed 2587 * State of tqpair: The tqpair is in EXITING state due to internal error 2588 */ 2589 if ((rc < 0) || (tqpair->state == NVME_TCP_QPAIR_STATE_EXITING)) { 2590 tqpair->state = NVME_TCP_QPAIR_STATE_EXITED; 2591 spdk_nvmf_tcp_qpair_flush_pdus(tqpair); 2592 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "will disconect the tqpair=%p\n", tqpair); 2593 spdk_poller_unregister(&tqpair->timeout_poller); 2594 spdk_nvmf_qpair_disconnect(&tqpair->qpair, NULL, NULL); 2595 } 2596 } 2597 2598 static int 2599 spdk_nvmf_tcp_poll_group_add(struct spdk_nvmf_transport_poll_group *group, 2600 struct spdk_nvmf_qpair *qpair) 2601 { 2602 struct spdk_nvmf_tcp_poll_group *tgroup; 2603 struct spdk_nvmf_tcp_qpair *tqpair; 2604 int rc; 2605 2606 tgroup = SPDK_CONTAINEROF(group, struct spdk_nvmf_tcp_poll_group, group); 2607 tqpair = SPDK_CONTAINEROF(qpair, struct spdk_nvmf_tcp_qpair, qpair); 2608 2609 rc = spdk_sock_group_add_sock(tgroup->sock_group, tqpair->sock, 2610 spdk_nvmf_tcp_sock_cb, tqpair); 2611 if (rc != 0) { 2612 SPDK_ERRLOG("Could not add sock to sock_group: %s (%d)\n", 2613 spdk_strerror(errno), errno); 2614 spdk_nvmf_tcp_qpair_destroy(tqpair); 2615 return -1; 2616 } 2617 2618 rc = spdk_nvmf_tcp_qpair_sock_init(tqpair); 2619 if (rc != 0) { 2620 SPDK_ERRLOG("Cannot set sock opt for tqpair=%p\n", tqpair); 2621 spdk_nvmf_tcp_qpair_destroy(tqpair); 2622 return -1; 2623 } 2624 2625 rc = spdk_nvmf_tcp_qpair_init(&tqpair->qpair); 2626 if (rc < 0) { 2627 SPDK_ERRLOG("Cannot init tqpair=%p\n", tqpair); 2628 spdk_nvmf_tcp_qpair_destroy(tqpair); 2629 return -1; 2630 } 2631 2632 rc = spdk_nvmf_tcp_qpair_init_mem_resource(tqpair, 1); 2633 if (rc < 0) { 2634 SPDK_ERRLOG("Cannot init memory resource info for tqpair=%p\n", tqpair); 2635 spdk_nvmf_tcp_qpair_destroy(tqpair); 2636 return -1; 2637 } 2638 2639 tqpair->group = tgroup; 2640 tqpair->state = NVME_TCP_QPAIR_STATE_INVALID; 2641 TAILQ_INSERT_TAIL(&tgroup->qpairs, tqpair, link); 2642 2643 return 0; 2644 } 2645 2646 static int 2647 spdk_nvmf_tcp_poll_group_remove(struct spdk_nvmf_transport_poll_group *group, 2648 struct spdk_nvmf_qpair *qpair) 2649 { 2650 struct spdk_nvmf_tcp_poll_group *tgroup; 2651 struct spdk_nvmf_tcp_qpair *tqpair; 2652 int rc; 2653 2654 tgroup = SPDK_CONTAINEROF(group, struct spdk_nvmf_tcp_poll_group, group); 2655 tqpair = SPDK_CONTAINEROF(qpair, struct spdk_nvmf_tcp_qpair, qpair); 2656 2657 assert(tqpair->group == tgroup); 2658 2659 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "remove tqpair=%p from the tgroup=%p\n", tqpair, tgroup); 2660 TAILQ_REMOVE(&tgroup->qpairs, tqpair, link); 2661 rc = spdk_sock_group_remove_sock(tgroup->sock_group, tqpair->sock); 2662 if (rc != 0) { 2663 SPDK_ERRLOG("Could not remove sock from sock_group: %s (%d)\n", 2664 spdk_strerror(errno), errno); 2665 } 2666 2667 return rc; 2668 } 2669 2670 static int 2671 spdk_nvmf_tcp_req_complete(struct spdk_nvmf_request *req) 2672 { 2673 struct spdk_nvmf_tcp_transport *ttransport; 2674 struct spdk_nvmf_tcp_req *tcp_req; 2675 2676 ttransport = SPDK_CONTAINEROF(req->qpair->transport, struct spdk_nvmf_tcp_transport, transport); 2677 tcp_req = SPDK_CONTAINEROF(req, struct spdk_nvmf_tcp_req, req); 2678 2679 spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_EXECUTED); 2680 spdk_nvmf_tcp_req_process(ttransport, tcp_req); 2681 2682 return 0; 2683 } 2684 2685 static void 2686 spdk_nvmf_tcp_close_qpair(struct spdk_nvmf_qpair *qpair) 2687 { 2688 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "enter\n"); 2689 2690 spdk_nvmf_tcp_qpair_destroy(SPDK_CONTAINEROF(qpair, struct spdk_nvmf_tcp_qpair, qpair)); 2691 } 2692 2693 static int 2694 spdk_nvmf_tcp_poll_group_poll(struct spdk_nvmf_transport_poll_group *group) 2695 { 2696 struct spdk_nvmf_tcp_poll_group *tgroup; 2697 int rc; 2698 2699 tgroup = SPDK_CONTAINEROF(group, struct spdk_nvmf_tcp_poll_group, group); 2700 2701 if (spdk_unlikely(TAILQ_EMPTY(&tgroup->qpairs))) { 2702 return 0; 2703 } 2704 2705 rc = spdk_sock_group_poll(tgroup->sock_group); 2706 if (rc < 0) { 2707 SPDK_ERRLOG("Failed to poll sock_group=%p\n", tgroup->sock_group); 2708 return rc; 2709 } 2710 2711 return 0; 2712 } 2713 2714 static int 2715 spdk_nvmf_tcp_qpair_get_trid(struct spdk_nvmf_qpair *qpair, 2716 struct spdk_nvme_transport_id *trid, bool peer) 2717 { 2718 struct spdk_nvmf_tcp_qpair *tqpair; 2719 uint16_t port; 2720 2721 tqpair = SPDK_CONTAINEROF(qpair, struct spdk_nvmf_tcp_qpair, qpair); 2722 trid->trtype = SPDK_NVME_TRANSPORT_TCP; 2723 2724 if (peer) { 2725 snprintf(trid->traddr, sizeof(trid->traddr), "%s", tqpair->initiator_addr); 2726 port = tqpair->initiator_port; 2727 } else { 2728 snprintf(trid->traddr, sizeof(trid->traddr), "%s", tqpair->target_addr); 2729 port = tqpair->target_port; 2730 } 2731 2732 if (spdk_sock_is_ipv4(tqpair->sock)) { 2733 trid->adrfam = SPDK_NVMF_ADRFAM_IPV4; 2734 } else if (spdk_sock_is_ipv4(tqpair->sock)) { 2735 trid->adrfam = SPDK_NVMF_ADRFAM_IPV6; 2736 } else { 2737 return -1; 2738 } 2739 2740 snprintf(trid->trsvcid, sizeof(trid->trsvcid), "%d", port); 2741 return 0; 2742 } 2743 2744 static int 2745 spdk_nvmf_tcp_qpair_get_local_trid(struct spdk_nvmf_qpair *qpair, 2746 struct spdk_nvme_transport_id *trid) 2747 { 2748 return spdk_nvmf_tcp_qpair_get_trid(qpair, trid, 0); 2749 } 2750 2751 static int 2752 spdk_nvmf_tcp_qpair_get_peer_trid(struct spdk_nvmf_qpair *qpair, 2753 struct spdk_nvme_transport_id *trid) 2754 { 2755 return spdk_nvmf_tcp_qpair_get_trid(qpair, trid, 1); 2756 } 2757 2758 static int 2759 spdk_nvmf_tcp_qpair_get_listen_trid(struct spdk_nvmf_qpair *qpair, 2760 struct spdk_nvme_transport_id *trid) 2761 { 2762 return spdk_nvmf_tcp_qpair_get_trid(qpair, trid, 0); 2763 } 2764 2765 static int 2766 spdk_nvmf_tcp_qpair_set_sq_size(struct spdk_nvmf_qpair *qpair) 2767 { 2768 struct spdk_nvmf_tcp_qpair *tqpair; 2769 int rc; 2770 tqpair = SPDK_CONTAINEROF(qpair, struct spdk_nvmf_tcp_qpair, qpair); 2771 2772 rc = spdk_nvmf_tcp_qpair_init_mem_resource(tqpair, tqpair->qpair.sq_head_max); 2773 if (!rc) { 2774 tqpair->max_queue_depth += tqpair->qpair.sq_head_max; 2775 tqpair->free_pdu_num += tqpair->qpair.sq_head_max; 2776 tqpair->state_cntr[TCP_REQUEST_STATE_FREE] += tqpair->qpair.sq_head_max; 2777 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "The queue depth=%u for tqpair=%p\n", 2778 tqpair->max_queue_depth, tqpair); 2779 } 2780 2781 return rc; 2782 2783 } 2784 2785 #define SPDK_NVMF_TCP_DEFAULT_MAX_QUEUE_DEPTH 128 2786 #define SPDK_NVMF_TCP_DEFAULT_AQ_DEPTH 128 2787 #define SPDK_NVMF_TCP_DEFAULT_MAX_QPAIRS_PER_CTRLR 64 2788 #define SPDK_NVMF_TCP_DEFAULT_IN_CAPSULE_DATA_SIZE 4096 2789 #define SPDK_NVMF_TCP_DEFAULT_MAX_IO_SIZE 131072 2790 #define SPDK_NVMF_TCP_DEFAULT_IO_UNIT_SIZE 131072 2791 #define SPDK_NVMF_TCP_DEFAULT_NUM_SHARED_BUFFERS 511 2792 #define SPDK_NVMF_TCP_DEFAULT_BUFFER_CACHE_SIZE 32 2793 2794 static void 2795 spdk_nvmf_tcp_opts_init(struct spdk_nvmf_transport_opts *opts) 2796 { 2797 opts->max_queue_depth = SPDK_NVMF_TCP_DEFAULT_MAX_QUEUE_DEPTH; 2798 opts->max_qpairs_per_ctrlr = SPDK_NVMF_TCP_DEFAULT_MAX_QPAIRS_PER_CTRLR; 2799 opts->in_capsule_data_size = SPDK_NVMF_TCP_DEFAULT_IN_CAPSULE_DATA_SIZE; 2800 opts->max_io_size = SPDK_NVMF_TCP_DEFAULT_MAX_IO_SIZE; 2801 opts->io_unit_size = SPDK_NVMF_TCP_DEFAULT_IO_UNIT_SIZE; 2802 opts->max_aq_depth = SPDK_NVMF_TCP_DEFAULT_AQ_DEPTH; 2803 opts->num_shared_buffers = SPDK_NVMF_TCP_DEFAULT_NUM_SHARED_BUFFERS; 2804 opts->buf_cache_size = SPDK_NVMF_TCP_DEFAULT_BUFFER_CACHE_SIZE; 2805 } 2806 2807 const struct spdk_nvmf_transport_ops spdk_nvmf_transport_tcp = { 2808 .type = SPDK_NVME_TRANSPORT_TCP, 2809 .opts_init = spdk_nvmf_tcp_opts_init, 2810 .create = spdk_nvmf_tcp_create, 2811 .destroy = spdk_nvmf_tcp_destroy, 2812 2813 .listen = spdk_nvmf_tcp_listen, 2814 .stop_listen = spdk_nvmf_tcp_stop_listen, 2815 .accept = spdk_nvmf_tcp_accept, 2816 2817 .listener_discover = spdk_nvmf_tcp_discover, 2818 2819 .poll_group_create = spdk_nvmf_tcp_poll_group_create, 2820 .poll_group_destroy = spdk_nvmf_tcp_poll_group_destroy, 2821 .poll_group_add = spdk_nvmf_tcp_poll_group_add, 2822 .poll_group_remove = spdk_nvmf_tcp_poll_group_remove, 2823 .poll_group_poll = spdk_nvmf_tcp_poll_group_poll, 2824 2825 .req_free = spdk_nvmf_tcp_req_free, 2826 .req_complete = spdk_nvmf_tcp_req_complete, 2827 2828 .qpair_fini = spdk_nvmf_tcp_close_qpair, 2829 .qpair_get_local_trid = spdk_nvmf_tcp_qpair_get_local_trid, 2830 .qpair_get_peer_trid = spdk_nvmf_tcp_qpair_get_peer_trid, 2831 .qpair_get_listen_trid = spdk_nvmf_tcp_qpair_get_listen_trid, 2832 .qpair_set_sqsize = spdk_nvmf_tcp_qpair_set_sq_size, 2833 }; 2834 2835 SPDK_LOG_REGISTER_COMPONENT("nvmf_tcp", SPDK_LOG_NVMF_TCP) 2836