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_FLUSH_WRITEBUF_PDU_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_COMPLETING_INCAPSULE", "", 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_FLUSH_WRITEBUF_START", "", 161 TRACE_TCP_FLUSH_WRITEBUF_START, 162 OWNER_NONE, OBJECT_NONE, 0, 0, ""); 163 spdk_trace_register_description("TCP_FLUSH_WRITEBUF_DONE", "", 164 TRACE_TCP_FLUSH_WRITEBUF_DONE, 165 OWNER_NONE, OBJECT_NONE, 0, 0, ""); 166 spdk_trace_register_description("TCP_FLUSH_WRITEBUF_PDU_DONE", "", 167 TRACE_TCP_FLUSH_WRITEBUF_PDU_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 writev_offset; 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 pdu, 795 tqpair->host_hdgst_enable, 796 tqpair->host_ddgst_enable); 797 total_length += pdu->hdr.common.plen; 798 pdu = TAILQ_NEXT(pdu, tailq); 799 } 800 801 /* 802 * Check if the first PDU was partially written out the last time 803 * this function was called, and if so adjust the iovec array 804 * accordingly. 805 */ 806 writev_offset = TAILQ_FIRST(&tqpair->send_queue)->writev_offset; 807 total_length -= writev_offset; 808 while ((writev_offset > 0) && (iovec_cnt > 0)) { 809 if (writev_offset >= iov->iov_len) { 810 writev_offset -= iov->iov_len; 811 iov++; 812 iovec_cnt--; 813 } else { 814 iov->iov_len -= writev_offset; 815 iov->iov_base = (char *)iov->iov_base + writev_offset; 816 writev_offset = 0; 817 } 818 } 819 820 spdk_trace_record(TRACE_TCP_FLUSH_WRITEBUF_START, 0, total_length, 0, iovec_cnt); 821 822 bytes = spdk_sock_writev(tqpair->sock, iov, iovec_cnt); 823 if (bytes == -1) { 824 if (errno == EWOULDBLOCK || errno == EAGAIN) { 825 return 1; 826 } else { 827 SPDK_ERRLOG("spdk_sock_writev() failed, errno %d: %s\n", 828 errno, spdk_strerror(errno)); 829 return -1; 830 } 831 } 832 833 spdk_trace_record(TRACE_TCP_FLUSH_WRITEBUF_DONE, 0, bytes, 0, 0); 834 835 pdu = TAILQ_FIRST(&tqpair->send_queue); 836 837 /* 838 * Free any PDUs that were fully written. If a PDU was only 839 * partially written, update its writev_offset so that next 840 * time only the unwritten portion will be sent to writev(). 841 */ 842 TAILQ_INIT(&completed_pdus_list); 843 while (bytes > 0) { 844 pdu_length = pdu->hdr.common.plen - pdu->writev_offset; 845 if (bytes >= pdu_length) { 846 bytes -= pdu_length; 847 TAILQ_REMOVE(&tqpair->send_queue, pdu, tailq); 848 TAILQ_INSERT_TAIL(&completed_pdus_list, pdu, tailq); 849 pdu = TAILQ_FIRST(&tqpair->send_queue); 850 851 } else { 852 pdu->writev_offset += bytes; 853 bytes = 0; 854 } 855 } 856 857 while (!TAILQ_EMPTY(&completed_pdus_list)) { 858 pdu = TAILQ_FIRST(&completed_pdus_list); 859 TAILQ_REMOVE(&completed_pdus_list, pdu, tailq); 860 assert(pdu->cb_fn != NULL); 861 pdu->cb_fn(pdu->cb_arg); 862 spdk_nvmf_tcp_pdu_put(tqpair, pdu); 863 } 864 865 ttransport = SPDK_CONTAINEROF(tqpair->qpair.transport, struct spdk_nvmf_tcp_transport, transport); 866 spdk_nvmf_tcp_qpair_process_pending(ttransport, tqpair); 867 868 return TAILQ_EMPTY(&tqpair->send_queue) ? 0 : 1; 869 } 870 871 static int 872 spdk_nvmf_tcp_qpair_flush_pdus(void *_tqpair) 873 { 874 struct spdk_nvmf_tcp_qpair *tqpair = _tqpair; 875 int rc; 876 877 if (tqpair->state == NVME_TCP_QPAIR_STATE_RUNNING) { 878 rc = spdk_nvmf_tcp_qpair_flush_pdus_internal(tqpair); 879 if (rc == 0 && tqpair->flush_poller != NULL) { 880 spdk_poller_unregister(&tqpair->flush_poller); 881 } else if (rc == 1 && tqpair->flush_poller == NULL) { 882 tqpair->flush_poller = spdk_poller_register(spdk_nvmf_tcp_qpair_flush_pdus, 883 tqpair, 50); 884 } 885 } else { 886 /* 887 * If the tqpair state is not RUNNING, then 888 * keep trying to flush PDUs until our list is 889 * empty - to make sure all data is sent before 890 * closing the connection. 891 */ 892 do { 893 rc = spdk_nvmf_tcp_qpair_flush_pdus_internal(tqpair); 894 } while (rc == 1); 895 } 896 897 if (rc < 0 && tqpair->state < NVME_TCP_QPAIR_STATE_EXITING) { 898 /* 899 * If the poller has already started destruction of the tqpair, 900 * i.e. the socket read failed, then the connection state may already 901 * be EXITED. We don't want to set it back to EXITING in that case. 902 */ 903 tqpair->state = NVME_TCP_QPAIR_STATE_EXITING; 904 } 905 906 return -1; 907 } 908 909 static void 910 spdk_nvmf_tcp_qpair_write_pdu(struct spdk_nvmf_tcp_qpair *tqpair, 911 struct nvme_tcp_pdu *pdu, 912 nvme_tcp_qpair_xfer_complete_cb cb_fn, 913 void *cb_arg) 914 { 915 int enable_digest; 916 int hlen; 917 uint32_t crc32c; 918 919 hlen = pdu->hdr.common.hlen; 920 enable_digest = 1; 921 if (pdu->hdr.common.pdu_type == SPDK_NVME_TCP_PDU_TYPE_IC_RESP || 922 pdu->hdr.common.pdu_type == SPDK_NVME_TCP_PDU_TYPE_C2H_TERM_REQ) { 923 /* this PDU should be sent without digest */ 924 enable_digest = 0; 925 } 926 927 /* Header Digest */ 928 if (enable_digest && tqpair->host_hdgst_enable) { 929 crc32c = nvme_tcp_pdu_calc_header_digest(pdu); 930 MAKE_DIGEST_WORD((uint8_t *)pdu->hdr.raw + hlen, crc32c); 931 } 932 933 /* Data Digest */ 934 if (pdu->data_len > 0 && enable_digest && tqpair->host_ddgst_enable) { 935 crc32c = nvme_tcp_pdu_calc_data_digest(pdu); 936 MAKE_DIGEST_WORD(pdu->data_digest, crc32c); 937 } 938 939 pdu->cb_fn = cb_fn; 940 pdu->cb_arg = cb_arg; 941 TAILQ_INSERT_TAIL(&tqpair->send_queue, pdu, tailq); 942 spdk_nvmf_tcp_qpair_flush_pdus(tqpair); 943 } 944 945 static int 946 spdk_nvmf_tcp_qpair_init_mem_resource(struct spdk_nvmf_tcp_qpair *tqpair, uint16_t size) 947 { 948 int i; 949 struct spdk_nvmf_tcp_req *tcp_req; 950 struct spdk_nvmf_transport *transport = tqpair->qpair.transport; 951 struct spdk_nvmf_tcp_transport *ttransport; 952 ttransport = SPDK_CONTAINEROF(transport, struct spdk_nvmf_tcp_transport, transport); 953 954 if (!tqpair->qpair.sq_head_max) { 955 tqpair->req = calloc(1, sizeof(*tqpair->req)); 956 if (!tqpair->req) { 957 SPDK_ERRLOG("Unable to allocate req on tqpair=%p.\n", tqpair); 958 return -1; 959 } 960 961 if (transport->opts.in_capsule_data_size) { 962 tqpair->buf = spdk_dma_zmalloc(ttransport->transport.opts.in_capsule_data_size, 0x1000, NULL); 963 if (!tqpair->buf) { 964 SPDK_ERRLOG("Unable to allocate buf on tqpair=%p.\n", tqpair); 965 return -1; 966 } 967 } 968 969 tcp_req = tqpair->req; 970 tcp_req->ttag = 0; 971 tcp_req->req.qpair = &tqpair->qpair; 972 973 /* Set up memory to receive commands */ 974 if (tqpair->buf) { 975 tcp_req->buf = tqpair->buf; 976 } 977 978 /* Set the cmdn and rsp */ 979 tcp_req->req.rsp = (union nvmf_c2h_msg *)&tcp_req->rsp; 980 tcp_req->req.cmd = (union nvmf_h2c_msg *)&tcp_req->cmd; 981 982 /* Initialize request state to FREE */ 983 tcp_req->state = TCP_REQUEST_STATE_FREE; 984 TAILQ_INSERT_TAIL(&tqpair->state_queue[tcp_req->state], tcp_req, state_link); 985 986 tqpair->pdu = calloc(NVMF_TCP_QPAIR_MAX_C2H_PDU_NUM + 1, sizeof(*tqpair->pdu)); 987 if (!tqpair->pdu) { 988 SPDK_ERRLOG("Unable to allocate pdu on tqpair=%p.\n", tqpair); 989 return -1; 990 } 991 992 for (i = 0; i < 1 + NVMF_TCP_QPAIR_MAX_C2H_PDU_NUM; i++) { 993 TAILQ_INSERT_TAIL(&tqpair->free_queue, &tqpair->pdu[i], tailq); 994 } 995 996 } else { 997 tqpair->reqs = calloc(size, sizeof(*tqpair->reqs)); 998 if (!tqpair->reqs) { 999 SPDK_ERRLOG("Unable to allocate reqs on tqpair=%p\n", tqpair); 1000 return -1; 1001 } 1002 1003 if (transport->opts.in_capsule_data_size) { 1004 tqpair->bufs = spdk_dma_zmalloc(size * transport->opts.in_capsule_data_size, 1005 0x1000, NULL); 1006 if (!tqpair->bufs) { 1007 SPDK_ERRLOG("Unable to allocate bufs on tqpair=%p.\n", tqpair); 1008 return -1; 1009 } 1010 } 1011 1012 for (i = 0; i < size; i++) { 1013 struct spdk_nvmf_tcp_req *tcp_req = &tqpair->reqs[i]; 1014 1015 tcp_req->ttag = i + 1; 1016 tcp_req->req.qpair = &tqpair->qpair; 1017 1018 /* Set up memory to receive commands */ 1019 if (tqpair->bufs) { 1020 tcp_req->buf = (void *)((uintptr_t)tqpair->bufs + (i * transport->opts.in_capsule_data_size)); 1021 } 1022 1023 /* Set the cmdn and rsp */ 1024 tcp_req->req.rsp = (union nvmf_c2h_msg *)&tcp_req->rsp; 1025 tcp_req->req.cmd = (union nvmf_h2c_msg *)&tcp_req->cmd; 1026 1027 /* Initialize request state to FREE */ 1028 tcp_req->state = TCP_REQUEST_STATE_FREE; 1029 TAILQ_INSERT_TAIL(&tqpair->state_queue[tcp_req->state], tcp_req, state_link); 1030 } 1031 1032 tqpair->pdu_pool = calloc(size, sizeof(*tqpair->pdu_pool)); 1033 if (!tqpair->pdu_pool) { 1034 SPDK_ERRLOG("Unable to allocate pdu pool on tqpair =%p.\n", tqpair); 1035 return -1; 1036 } 1037 1038 for (i = 0; i < size; i++) { 1039 TAILQ_INSERT_TAIL(&tqpair->free_queue, &tqpair->pdu_pool[i], tailq); 1040 } 1041 } 1042 1043 return 0; 1044 } 1045 1046 static int 1047 spdk_nvmf_tcp_qpair_init(struct spdk_nvmf_qpair *qpair) 1048 { 1049 struct spdk_nvmf_tcp_qpair *tqpair; 1050 int i; 1051 1052 tqpair = SPDK_CONTAINEROF(qpair, struct spdk_nvmf_tcp_qpair, qpair); 1053 1054 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "New TCP Connection: %p\n", qpair); 1055 1056 TAILQ_INIT(&tqpair->send_queue); 1057 TAILQ_INIT(&tqpair->free_queue); 1058 TAILQ_INIT(&tqpair->queued_c2h_data_tcp_req); 1059 1060 /* Initialise request state queues of the qpair */ 1061 for (i = TCP_REQUEST_STATE_FREE; i < TCP_REQUEST_NUM_STATES; i++) { 1062 TAILQ_INIT(&tqpair->state_queue[i]); 1063 } 1064 1065 tqpair->host_hdgst_enable = true; 1066 tqpair->host_ddgst_enable = true; 1067 1068 return 0; 1069 } 1070 1071 static int 1072 spdk_nvmf_tcp_qpair_sock_init(struct spdk_nvmf_tcp_qpair *tqpair) 1073 { 1074 1075 int rc; 1076 int buf_size; 1077 1078 /* set recv buffer size */ 1079 buf_size = 2 * 1024 * 1024; 1080 rc = spdk_sock_set_recvbuf(tqpair->sock, buf_size); 1081 if (rc != 0) { 1082 SPDK_ERRLOG("spdk_sock_set_recvbuf failed\n"); 1083 return rc; 1084 } 1085 1086 /* set send buffer size */ 1087 rc = spdk_sock_set_sendbuf(tqpair->sock, buf_size); 1088 if (rc != 0) { 1089 SPDK_ERRLOG("spdk_sock_set_sendbuf failed\n"); 1090 return rc; 1091 } 1092 1093 /* set low water mark */ 1094 rc = spdk_sock_set_recvlowat(tqpair->sock, sizeof(struct spdk_nvme_tcp_c2h_data_hdr)); 1095 if (rc != 0) { 1096 SPDK_ERRLOG("spdk_sock_set_recvlowat() failed\n"); 1097 return rc; 1098 } 1099 1100 return 0; 1101 } 1102 1103 static void 1104 _spdk_nvmf_tcp_handle_connect(struct spdk_nvmf_transport *transport, 1105 struct spdk_nvmf_tcp_port *port, 1106 struct spdk_sock *sock, new_qpair_fn cb_fn) 1107 { 1108 struct spdk_nvmf_tcp_qpair *tqpair; 1109 int rc; 1110 1111 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "New connection accepted on %s port %s\n", 1112 port->trid.traddr, port->trid.trsvcid); 1113 1114 tqpair = calloc(1, sizeof(struct spdk_nvmf_tcp_qpair)); 1115 if (tqpair == NULL) { 1116 SPDK_ERRLOG("Could not allocate new connection.\n"); 1117 spdk_sock_close(&sock); 1118 return; 1119 } 1120 1121 tqpair->sock = sock; 1122 tqpair->max_queue_depth = 1; 1123 tqpair->free_pdu_num = tqpair->max_queue_depth + NVMF_TCP_QPAIR_MAX_C2H_PDU_NUM; 1124 tqpair->state_cntr[TCP_REQUEST_STATE_FREE] = tqpair->max_queue_depth; 1125 tqpair->port = port; 1126 tqpair->qpair.transport = transport; 1127 1128 rc = spdk_sock_getaddr(tqpair->sock, tqpair->target_addr, 1129 sizeof(tqpair->target_addr), &tqpair->target_port, 1130 tqpair->initiator_addr, sizeof(tqpair->initiator_addr), 1131 &tqpair->initiator_port); 1132 if (rc < 0) { 1133 SPDK_ERRLOG("spdk_sock_getaddr() failed of tqpair=%p\n", tqpair); 1134 spdk_nvmf_tcp_qpair_destroy(tqpair); 1135 return; 1136 } 1137 1138 cb_fn(&tqpair->qpair); 1139 } 1140 1141 static void 1142 spdk_nvmf_tcp_port_accept(struct spdk_nvmf_transport *transport, struct spdk_nvmf_tcp_port *port, 1143 new_qpair_fn cb_fn) 1144 { 1145 struct spdk_sock *sock; 1146 int i; 1147 1148 for (i = 0; i < NVMF_TCP_MAX_ACCEPT_SOCK_ONE_TIME; i++) { 1149 sock = spdk_sock_accept(port->listen_sock); 1150 if (sock) { 1151 _spdk_nvmf_tcp_handle_connect(transport, port, sock, cb_fn); 1152 } 1153 } 1154 } 1155 1156 static void 1157 spdk_nvmf_tcp_accept(struct spdk_nvmf_transport *transport, new_qpair_fn cb_fn) 1158 { 1159 struct spdk_nvmf_tcp_transport *ttransport; 1160 struct spdk_nvmf_tcp_port *port; 1161 1162 ttransport = SPDK_CONTAINEROF(transport, struct spdk_nvmf_tcp_transport, transport); 1163 1164 TAILQ_FOREACH(port, &ttransport->ports, link) { 1165 spdk_nvmf_tcp_port_accept(transport, port, cb_fn); 1166 } 1167 } 1168 1169 static void 1170 spdk_nvmf_tcp_discover(struct spdk_nvmf_transport *transport, 1171 struct spdk_nvme_transport_id *trid, 1172 struct spdk_nvmf_discovery_log_page_entry *entry) 1173 { 1174 entry->trtype = SPDK_NVMF_TRTYPE_TCP; 1175 entry->adrfam = trid->adrfam; 1176 entry->treq.secure_channel = SPDK_NVMF_TREQ_SECURE_CHANNEL_NOT_SPECIFIED; 1177 1178 spdk_strcpy_pad(entry->trsvcid, trid->trsvcid, sizeof(entry->trsvcid), ' '); 1179 spdk_strcpy_pad(entry->traddr, trid->traddr, sizeof(entry->traddr), ' '); 1180 1181 entry->tsas.tcp.sectype = SPDK_NVME_TCP_SECURITY_NONE; 1182 } 1183 1184 static struct spdk_nvmf_transport_poll_group * 1185 spdk_nvmf_tcp_poll_group_create(struct spdk_nvmf_transport *transport) 1186 { 1187 struct spdk_nvmf_tcp_poll_group *tgroup; 1188 1189 tgroup = calloc(1, sizeof(*tgroup)); 1190 if (!tgroup) { 1191 return NULL; 1192 } 1193 1194 tgroup->sock_group = spdk_sock_group_create(); 1195 if (!tgroup->sock_group) { 1196 goto cleanup; 1197 } 1198 1199 TAILQ_INIT(&tgroup->qpairs); 1200 TAILQ_INIT(&tgroup->pending_data_buf_queue); 1201 1202 return &tgroup->group; 1203 1204 cleanup: 1205 free(tgroup); 1206 return NULL; 1207 } 1208 1209 static void 1210 spdk_nvmf_tcp_poll_group_destroy(struct spdk_nvmf_transport_poll_group *group) 1211 { 1212 struct spdk_nvmf_tcp_poll_group *tgroup; 1213 1214 tgroup = SPDK_CONTAINEROF(group, struct spdk_nvmf_tcp_poll_group, group); 1215 spdk_sock_group_close(&tgroup->sock_group); 1216 1217 if (!TAILQ_EMPTY(&tgroup->pending_data_buf_queue)) { 1218 SPDK_ERRLOG("Pending I/O list wasn't empty on poll group destruction\n"); 1219 } 1220 1221 free(tgroup); 1222 } 1223 1224 static void 1225 spdk_nvmf_tcp_qpair_set_recv_state(struct spdk_nvmf_tcp_qpair *tqpair, 1226 enum nvme_tcp_pdu_recv_state state) 1227 { 1228 if (tqpair->recv_state == state) { 1229 SPDK_ERRLOG("The recv state of tqpair=%p is same with the state(%d) to be set\n", 1230 tqpair, state); 1231 return; 1232 } 1233 1234 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "tqpair(%p) recv state=%d\n", tqpair, state); 1235 tqpair->recv_state = state; 1236 1237 switch (state) { 1238 case NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_CH: 1239 case NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PSH: 1240 case NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD: 1241 break; 1242 case NVME_TCP_PDU_RECV_STATE_ERROR: 1243 case NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY: 1244 memset(&tqpair->pdu_in_progress, 0, sizeof(tqpair->pdu_in_progress)); 1245 break; 1246 default: 1247 SPDK_ERRLOG("The state(%d) is invalid\n", state); 1248 abort(); 1249 break; 1250 } 1251 } 1252 1253 static int 1254 spdk_nvmf_tcp_qpair_handle_timeout(void *ctx) 1255 { 1256 struct spdk_nvmf_tcp_qpair *tqpair = ctx; 1257 1258 assert(tqpair->recv_state == NVME_TCP_PDU_RECV_STATE_ERROR); 1259 1260 SPDK_ERRLOG("No pdu coming for tqpair=%p within %d seconds\n", tqpair, 1261 SPDK_NVME_TCP_QPAIR_EXIT_TIMEOUT); 1262 tqpair->state = NVME_TCP_QPAIR_STATE_EXITED; 1263 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "will disconect the tqpair=%p\n", tqpair); 1264 spdk_poller_unregister(&tqpair->timeout_poller); 1265 spdk_nvmf_qpair_disconnect(&tqpair->qpair, NULL, NULL); 1266 1267 return 0; 1268 } 1269 1270 static void 1271 spdk_nvmf_tcp_send_c2h_term_req_complete(void *cb_arg) 1272 { 1273 struct spdk_nvmf_tcp_qpair *tqpair = (struct spdk_nvmf_tcp_qpair *)cb_arg; 1274 1275 if (!tqpair->timeout_poller) { 1276 tqpair->timeout_poller = spdk_poller_register(spdk_nvmf_tcp_qpair_handle_timeout, tqpair, 1277 SPDK_NVME_TCP_QPAIR_EXIT_TIMEOUT * 1000000); 1278 } 1279 } 1280 1281 static void 1282 spdk_nvmf_tcp_send_c2h_term_req(struct spdk_nvmf_tcp_qpair *tqpair, struct nvme_tcp_pdu *pdu, 1283 enum spdk_nvme_tcp_term_req_fes fes, uint32_t error_offset) 1284 { 1285 struct nvme_tcp_pdu *rsp_pdu; 1286 struct spdk_nvme_tcp_term_req_hdr *c2h_term_req; 1287 uint32_t c2h_term_req_hdr_len = sizeof(*c2h_term_req); 1288 uint32_t copy_len; 1289 1290 rsp_pdu = spdk_nvmf_tcp_pdu_get(tqpair); 1291 if (!rsp_pdu) { 1292 tqpair->state = NVME_TCP_QPAIR_STATE_EXITING; 1293 spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_ERROR); 1294 return; 1295 } 1296 1297 c2h_term_req = &rsp_pdu->hdr.term_req; 1298 c2h_term_req->common.pdu_type = SPDK_NVME_TCP_PDU_TYPE_C2H_TERM_REQ; 1299 c2h_term_req->common.hlen = c2h_term_req_hdr_len; 1300 1301 if ((fes == SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD) || 1302 (fes == SPDK_NVME_TCP_TERM_REQ_FES_INVALID_DATA_UNSUPPORTED_PARAMETER)) { 1303 DSET32(&c2h_term_req->fei, error_offset); 1304 } 1305 1306 rsp_pdu->data = (uint8_t *)rsp_pdu->hdr.raw + c2h_term_req_hdr_len; 1307 copy_len = pdu->hdr.common.hlen; 1308 if (copy_len > SPDK_NVME_TCP_TERM_REQ_ERROR_DATA_MAX_SIZE) { 1309 copy_len = SPDK_NVME_TCP_TERM_REQ_ERROR_DATA_MAX_SIZE; 1310 } 1311 1312 /* Copy the error info into the buffer */ 1313 memcpy((uint8_t *)rsp_pdu->data, pdu->hdr.raw, copy_len); 1314 rsp_pdu->data_len = copy_len; 1315 1316 /* Contain the header of the wrong received pdu */ 1317 c2h_term_req->common.plen = c2h_term_req->common.hlen + copy_len; 1318 spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_ERROR); 1319 spdk_nvmf_tcp_qpair_write_pdu(tqpair, rsp_pdu, spdk_nvmf_tcp_send_c2h_term_req_complete, tqpair); 1320 } 1321 1322 static void 1323 spdk_nvmf_tcp_capsule_cmd_hdr_handle(struct spdk_nvmf_tcp_transport *ttransport, 1324 struct spdk_nvmf_tcp_qpair *tqpair, 1325 struct nvme_tcp_pdu *pdu) 1326 { 1327 struct spdk_nvmf_tcp_req *tcp_req; 1328 1329 tcp_req = spdk_nvmf_tcp_req_get(tqpair); 1330 if (!tcp_req) { 1331 SPDK_ERRLOG("Cannot allocate tcp_req\n"); 1332 tqpair->state = NVME_TCP_QPAIR_STATE_EXITING; 1333 spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_ERROR); 1334 return; 1335 } 1336 1337 pdu->ctx = tcp_req; 1338 spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_NEW); 1339 spdk_nvmf_tcp_req_process(ttransport, tcp_req); 1340 return; 1341 } 1342 1343 static void 1344 spdk_nvmf_tcp_capsule_cmd_payload_handle(struct spdk_nvmf_tcp_transport *ttransport, 1345 struct spdk_nvmf_tcp_qpair *tqpair, 1346 struct nvme_tcp_pdu *pdu) 1347 { 1348 struct spdk_nvmf_tcp_req *tcp_req; 1349 struct spdk_nvme_tcp_cmd *capsule_cmd; 1350 uint32_t error_offset = 0; 1351 enum spdk_nvme_tcp_term_req_fes fes; 1352 1353 capsule_cmd = &pdu->hdr.capsule_cmd; 1354 tcp_req = pdu->ctx; 1355 assert(tcp_req != NULL); 1356 if (capsule_cmd->common.pdo > SPDK_NVME_TCP_PDU_PDO_MAX_OFFSET) { 1357 SPDK_ERRLOG("Expected ICReq capsule_cmd pdu offset <= %d, got %c\n", 1358 SPDK_NVME_TCP_PDU_PDO_MAX_OFFSET, capsule_cmd->common.pdo); 1359 fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD; 1360 error_offset = offsetof(struct spdk_nvme_tcp_common_pdu_hdr, pdo); 1361 goto err; 1362 } 1363 1364 spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY); 1365 spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_READY_TO_EXECUTE); 1366 spdk_nvmf_tcp_req_process(ttransport, tcp_req); 1367 1368 return; 1369 err: 1370 spdk_nvmf_tcp_send_c2h_term_req(tqpair, pdu, fes, error_offset); 1371 } 1372 1373 static void 1374 spdk_nvmf_tcp_h2c_data_hdr_handle(struct spdk_nvmf_tcp_transport *ttransport, 1375 struct spdk_nvmf_tcp_qpair *tqpair, 1376 struct nvme_tcp_pdu *pdu) 1377 { 1378 struct spdk_nvmf_tcp_req *tcp_req; 1379 uint32_t error_offset = 0; 1380 enum spdk_nvme_tcp_term_req_fes fes = 0; 1381 struct spdk_nvme_tcp_h2c_data_hdr *h2c_data; 1382 uint32_t iov_index; 1383 bool ttag_offset_error = false; 1384 1385 h2c_data = &pdu->hdr.h2c_data; 1386 1387 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "tqpair=%p, r2t_info: datao=%u, datal=%u, cccid=%u, ttag=%u\n", 1388 tqpair, h2c_data->datao, h2c_data->datal, h2c_data->cccid, h2c_data->ttag); 1389 1390 /* According to the information in the pdu to find the req */ 1391 TAILQ_FOREACH(tcp_req, &tqpair->state_queue[TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER], 1392 state_link) { 1393 if ((tcp_req->req.cmd->nvme_cmd.cid == h2c_data->cccid) && (tcp_req->ttag == h2c_data->ttag)) { 1394 break; 1395 } 1396 1397 if (!ttag_offset_error && (tcp_req->req.cmd->nvme_cmd.cid == h2c_data->cccid)) { 1398 ttag_offset_error = true; 1399 } 1400 } 1401 1402 if (!tcp_req) { 1403 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "tcp_req is not found for tqpair=%p\n", tqpair); 1404 fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_DATA_UNSUPPORTED_PARAMETER; 1405 if (!ttag_offset_error) { 1406 error_offset = offsetof(struct spdk_nvme_tcp_h2c_data_hdr, cccid); 1407 } else { 1408 error_offset = offsetof(struct spdk_nvme_tcp_h2c_data_hdr, ttag); 1409 } 1410 goto err; 1411 } 1412 1413 if (tcp_req->next_expected_r2t_offset != h2c_data->datao) { 1414 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, 1415 "tcp_req(%p), tqpair=%p, expected_r2t_offset=%u, but data offset =%u\n", 1416 tcp_req, tqpair, tcp_req->next_expected_r2t_offset, h2c_data->datao); 1417 fes = SPDK_NVME_TCP_TERM_REQ_FES_DATA_TRANSFER_OUT_OF_RANGE; 1418 goto err; 1419 } 1420 1421 if (h2c_data->datal > tqpair->maxh2cdata) { 1422 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "tcp_req(%p), tqpair=%p, datao=%u execeeds maxh2cdata size=%u\n", 1423 tcp_req, tqpair, h2c_data->datao, tqpair->maxh2cdata); 1424 fes = SPDK_NVME_TCP_TERM_REQ_FES_DATA_TRANSFER_OUT_OF_RANGE; 1425 goto err; 1426 } 1427 1428 if ((h2c_data->datao + h2c_data->datal) > tcp_req->req.length) { 1429 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, 1430 "tcp_req(%p), tqpair=%p, (datao=%u + datal=%u) execeeds requested length=%u\n", 1431 tcp_req, tqpair, h2c_data->datao, h2c_data->datal, tcp_req->req.length); 1432 fes = SPDK_NVME_TCP_TERM_REQ_FES_R2T_LIMIT_EXCEEDED; 1433 goto err; 1434 } 1435 1436 pdu->ctx = tcp_req; 1437 pdu->data_len = h2c_data->datal; 1438 iov_index = pdu->hdr.h2c_data.datao / ttransport->transport.opts.io_unit_size; 1439 pdu->data = tcp_req->req.iov[iov_index].iov_base + (pdu->hdr.h2c_data.datao % 1440 ttransport->transport.opts.io_unit_size); 1441 spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD); 1442 return; 1443 1444 err: 1445 spdk_nvmf_tcp_send_c2h_term_req(tqpair, pdu, fes, error_offset); 1446 } 1447 1448 static void 1449 spdk_nvmf_tcp_pdu_cmd_complete(void *cb_arg) 1450 { 1451 struct spdk_nvmf_tcp_req *tcp_req = cb_arg; 1452 nvmf_tcp_request_free(tcp_req); 1453 } 1454 1455 static void 1456 spdk_nvmf_tcp_send_capsule_resp_pdu(struct spdk_nvmf_tcp_req *tcp_req, 1457 struct spdk_nvmf_tcp_qpair *tqpair) 1458 { 1459 struct nvme_tcp_pdu *rsp_pdu; 1460 struct spdk_nvme_tcp_rsp *capsule_resp; 1461 1462 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "enter, tqpair=%p\n", tqpair); 1463 rsp_pdu = spdk_nvmf_tcp_pdu_get(tqpair); 1464 if (!rsp_pdu) { 1465 spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_ERROR); 1466 tqpair->state = NVME_TCP_QPAIR_STATE_EXITING; 1467 return; 1468 } 1469 1470 capsule_resp = &rsp_pdu->hdr.capsule_resp; 1471 capsule_resp->common.pdu_type = SPDK_NVME_TCP_PDU_TYPE_CAPSULE_RESP; 1472 capsule_resp->common.plen = capsule_resp->common.hlen = sizeof(*capsule_resp); 1473 capsule_resp->rccqe = tcp_req->req.rsp->nvme_cpl; 1474 if (tqpair->host_hdgst_enable) { 1475 capsule_resp->common.flags |= SPDK_NVME_TCP_CH_FLAGS_HDGSTF; 1476 capsule_resp->common.plen += SPDK_NVME_TCP_DIGEST_LEN; 1477 } 1478 1479 spdk_nvmf_tcp_qpair_write_pdu(tqpair, rsp_pdu, spdk_nvmf_tcp_pdu_cmd_complete, tcp_req); 1480 } 1481 1482 static void 1483 spdk_nvmf_tcp_pdu_c2h_data_complete(void *cb_arg) 1484 { 1485 struct spdk_nvmf_tcp_req *tcp_req = cb_arg; 1486 struct spdk_nvmf_tcp_qpair *tqpair = SPDK_CONTAINEROF(tcp_req->req.qpair, 1487 struct spdk_nvmf_tcp_qpair, qpair); 1488 1489 assert(tqpair != NULL); 1490 assert(tcp_req->c2h_data_pdu_num > 0); 1491 tcp_req->c2h_data_pdu_num--; 1492 if (!tcp_req->c2h_data_pdu_num) { 1493 #if LINUX_KERNEL_SUPPORT_NOT_SENDING_RESP_FOR_C2H 1494 nvmf_tcp_request_free(tcp_req); 1495 #else 1496 spdk_nvmf_tcp_send_capsule_resp_pdu(tcp_req, tqpair); 1497 #endif 1498 } 1499 1500 tqpair->c2h_data_pdu_cnt--; 1501 spdk_nvmf_tcp_handle_pending_c2h_data_queue(tqpair); 1502 } 1503 1504 static void 1505 spdk_nvmf_tcp_send_r2t_pdu(struct spdk_nvmf_tcp_qpair *tqpair, 1506 struct spdk_nvmf_tcp_req *tcp_req) 1507 { 1508 struct nvme_tcp_pdu *rsp_pdu; 1509 struct spdk_nvme_tcp_r2t_hdr *r2t; 1510 1511 rsp_pdu = spdk_nvmf_tcp_pdu_get(tqpair); 1512 if (!rsp_pdu) { 1513 tqpair->state = NVME_TCP_QPAIR_STATE_EXITING; 1514 spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_ERROR); 1515 return; 1516 } 1517 1518 r2t = &rsp_pdu->hdr.r2t; 1519 r2t->common.pdu_type = SPDK_NVME_TCP_PDU_TYPE_R2T; 1520 r2t->common.plen = r2t->common.hlen = sizeof(*r2t); 1521 1522 if (tqpair->host_hdgst_enable) { 1523 r2t->common.flags |= SPDK_NVME_TCP_CH_FLAGS_HDGSTF; 1524 r2t->common.plen += SPDK_NVME_TCP_DIGEST_LEN; 1525 } 1526 1527 r2t->cccid = tcp_req->req.cmd->nvme_cmd.cid; 1528 r2t->ttag = tcp_req->ttag; 1529 r2t->r2to = tcp_req->next_expected_r2t_offset; 1530 r2t->r2tl = spdk_min(tcp_req->req.length - tcp_req->next_expected_r2t_offset, tqpair->maxh2cdata); 1531 tcp_req->r2tl_remain = r2t->r2tl; 1532 1533 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, 1534 "tcp_req(%p) on tqpair(%p), r2t_info: cccid=%u, ttag=%u, r2to=%u, r2tl=%u\n", 1535 tcp_req, tqpair, r2t->cccid, r2t->ttag, r2t->r2to, r2t->r2tl); 1536 spdk_nvmf_tcp_qpair_write_pdu(tqpair, rsp_pdu, spdk_nvmf_tcp_pdu_cmd_complete, NULL); 1537 } 1538 1539 static void 1540 spdk_nvmf_tcp_handle_queued_r2t_req(struct spdk_nvmf_tcp_qpair *tqpair) 1541 { 1542 struct spdk_nvmf_tcp_req *tcp_req, *req_tmp; 1543 1544 TAILQ_FOREACH_SAFE(tcp_req, &tqpair->state_queue[TCP_REQUEST_STATE_DATA_PENDING_FOR_R2T], 1545 state_link, req_tmp) { 1546 if (tqpair->pending_r2t < tqpair->maxr2t) { 1547 tqpair->pending_r2t++; 1548 spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER); 1549 spdk_nvmf_tcp_send_r2t_pdu(tqpair, tcp_req); 1550 } else { 1551 break; 1552 } 1553 } 1554 } 1555 1556 static void 1557 spdk_nvmf_tcp_h2c_data_payload_handle(struct spdk_nvmf_tcp_transport *ttransport, 1558 struct spdk_nvmf_tcp_qpair *tqpair, 1559 struct nvme_tcp_pdu *pdu) 1560 { 1561 struct spdk_nvmf_tcp_req *tcp_req; 1562 1563 tcp_req = pdu->ctx; 1564 assert(tcp_req != NULL); 1565 1566 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "enter\n"); 1567 1568 tcp_req->next_expected_r2t_offset += pdu->data_len; 1569 tcp_req->r2tl_remain -= pdu->data_len; 1570 spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY); 1571 1572 if (!tcp_req->r2tl_remain) { 1573 if (tcp_req->next_expected_r2t_offset == tcp_req->req.length) { 1574 assert(tqpair->pending_r2t > 0); 1575 tqpair->pending_r2t--; 1576 assert(tqpair->pending_r2t < tqpair->maxr2t); 1577 spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_READY_TO_EXECUTE); 1578 spdk_nvmf_tcp_req_process(ttransport, tcp_req); 1579 1580 spdk_nvmf_tcp_handle_queued_r2t_req(tqpair); 1581 } else { 1582 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Send r2t pdu for tcp_req=%p on tqpair=%p\n", tcp_req, tqpair); 1583 spdk_nvmf_tcp_send_r2t_pdu(tqpair, tcp_req); 1584 } 1585 } 1586 } 1587 1588 static void 1589 spdk_nvmf_tcp_h2c_term_req_dump(struct spdk_nvme_tcp_term_req_hdr *h2c_term_req) 1590 { 1591 SPDK_ERRLOG("Error info of pdu(%p): %s\n", h2c_term_req, 1592 spdk_nvmf_tcp_term_req_fes_str[h2c_term_req->fes]); 1593 if ((h2c_term_req->fes == SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD) || 1594 (h2c_term_req->fes == SPDK_NVME_TCP_TERM_REQ_FES_INVALID_DATA_UNSUPPORTED_PARAMETER)) { 1595 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "The offset from the start of the PDU header is %u\n", 1596 DGET32(h2c_term_req->fei)); 1597 } 1598 } 1599 1600 static void 1601 spdk_nvmf_tcp_h2c_term_req_hdr_handle(struct spdk_nvmf_tcp_qpair *tqpair, 1602 struct nvme_tcp_pdu *pdu) 1603 { 1604 struct spdk_nvme_tcp_term_req_hdr *h2c_term_req = &pdu->hdr.term_req; 1605 uint32_t error_offset = 0; 1606 enum spdk_nvme_tcp_term_req_fes fes; 1607 1608 1609 if (h2c_term_req->fes > SPDK_NVME_TCP_TERM_REQ_FES_INVALID_DATA_UNSUPPORTED_PARAMETER) { 1610 SPDK_ERRLOG("Fatal Error Stauts(FES) is unknown for h2c_term_req pdu=%p\n", pdu); 1611 fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD; 1612 error_offset = offsetof(struct spdk_nvme_tcp_term_req_hdr, fes); 1613 goto end; 1614 } 1615 1616 /* set the data buffer */ 1617 pdu->data = (uint8_t *)pdu->hdr.raw + h2c_term_req->common.hlen; 1618 pdu->data_len = h2c_term_req->common.plen - h2c_term_req->common.hlen; 1619 spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD); 1620 return; 1621 end: 1622 spdk_nvmf_tcp_send_c2h_term_req(tqpair, pdu, fes, error_offset); 1623 return; 1624 } 1625 1626 static void 1627 spdk_nvmf_tcp_h2c_term_req_payload_handle(struct spdk_nvmf_tcp_qpair *tqpair, 1628 struct nvme_tcp_pdu *pdu) 1629 { 1630 struct spdk_nvme_tcp_term_req_hdr *h2c_term_req = &pdu->hdr.term_req; 1631 1632 spdk_nvmf_tcp_h2c_term_req_dump(h2c_term_req); 1633 spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_ERROR); 1634 return; 1635 } 1636 1637 static void 1638 spdk_nvmf_tcp_pdu_payload_handle(struct spdk_nvmf_tcp_qpair *tqpair) 1639 { 1640 int rc = 0; 1641 struct nvme_tcp_pdu *pdu; 1642 uint32_t crc32c, error_offset = 0; 1643 enum spdk_nvme_tcp_term_req_fes fes; 1644 struct spdk_nvmf_tcp_transport *ttransport; 1645 1646 assert(tqpair->recv_state == NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD); 1647 pdu = &tqpair->pdu_in_progress; 1648 1649 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "enter\n"); 1650 /* check data digest if need */ 1651 if (pdu->ddigest_valid_bytes) { 1652 crc32c = nvme_tcp_pdu_calc_data_digest(pdu); 1653 rc = MATCH_DIGEST_WORD(pdu->data_digest, crc32c); 1654 if (rc == 0) { 1655 SPDK_ERRLOG("Data digest error on tqpair=(%p) with pdu=%p\n", tqpair, pdu); 1656 fes = SPDK_NVME_TCP_TERM_REQ_FES_HDGST_ERROR; 1657 spdk_nvmf_tcp_send_c2h_term_req(tqpair, pdu, fes, error_offset); 1658 return; 1659 1660 } 1661 } 1662 1663 ttransport = SPDK_CONTAINEROF(tqpair->qpair.transport, struct spdk_nvmf_tcp_transport, transport); 1664 switch (pdu->hdr.common.pdu_type) { 1665 case SPDK_NVME_TCP_PDU_TYPE_CAPSULE_CMD: 1666 spdk_nvmf_tcp_capsule_cmd_payload_handle(ttransport, tqpair, pdu); 1667 break; 1668 case SPDK_NVME_TCP_PDU_TYPE_H2C_DATA: 1669 spdk_nvmf_tcp_h2c_data_payload_handle(ttransport, tqpair, pdu); 1670 break; 1671 1672 case SPDK_NVME_TCP_PDU_TYPE_H2C_TERM_REQ: 1673 spdk_nvmf_tcp_h2c_term_req_payload_handle(tqpair, pdu); 1674 break; 1675 1676 default: 1677 /* The code should not go to here */ 1678 SPDK_ERRLOG("The code should not go to here\n"); 1679 break; 1680 } 1681 } 1682 1683 static void 1684 spdk_nvmf_tcp_send_icresp_complete(void *cb_arg) 1685 { 1686 struct spdk_nvmf_tcp_qpair *tqpair = cb_arg; 1687 1688 tqpair->state = NVME_TCP_QPAIR_STATE_RUNNING; 1689 } 1690 1691 static void 1692 spdk_nvmf_tcp_icreq_handle(struct spdk_nvmf_tcp_transport *ttransport, 1693 struct spdk_nvmf_tcp_qpair *tqpair, 1694 struct nvme_tcp_pdu *pdu) 1695 { 1696 struct spdk_nvme_tcp_ic_req *ic_req = &pdu->hdr.ic_req; 1697 struct nvme_tcp_pdu *rsp_pdu; 1698 struct spdk_nvme_tcp_ic_resp *ic_resp; 1699 uint32_t error_offset = 0; 1700 enum spdk_nvme_tcp_term_req_fes fes; 1701 1702 /* Only PFV 0 is defined currently */ 1703 if (ic_req->pfv != 0) { 1704 SPDK_ERRLOG("Expected ICReq PFV %u, got %u\n", 0u, ic_req->pfv); 1705 fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD; 1706 error_offset = offsetof(struct spdk_nvme_tcp_ic_req, pfv); 1707 goto end; 1708 } 1709 1710 /* MAXR2T is 0's based */ 1711 tqpair->maxr2t = ic_req->maxr2t + 1ull; 1712 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "maxr2t =%u\n", tqpair->maxr2t); 1713 1714 tqpair->host_hdgst_enable = ic_req->dgst.bits.hdgst_enable ? true : false; 1715 tqpair->host_ddgst_enable = ic_req->dgst.bits.ddgst_enable ? true : false; 1716 1717 tqpair->cpda = spdk_min(ic_req->hpda, SPDK_NVME_TCP_CPDA_MAX); 1718 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "cpda of tqpair=(%p) is : %u\n", tqpair, tqpair->cpda); 1719 1720 rsp_pdu = spdk_nvmf_tcp_pdu_get(tqpair); 1721 if (!rsp_pdu) { 1722 tqpair->state = NVME_TCP_QPAIR_STATE_EXITING; 1723 spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_ERROR); 1724 return; 1725 } 1726 1727 ic_resp = &rsp_pdu->hdr.ic_resp; 1728 ic_resp->common.pdu_type = SPDK_NVME_TCP_PDU_TYPE_IC_RESP; 1729 ic_resp->common.hlen = ic_resp->common.plen = sizeof(*ic_resp); 1730 ic_resp->pfv = 0; 1731 ic_resp->cpda = tqpair->cpda; 1732 tqpair->maxh2cdata = spdk_min(NVMF_TCP_PDU_MAX_H2C_DATA_SIZE, 1733 ttransport->transport.opts.io_unit_size); 1734 ic_resp->maxh2cdata = tqpair->maxh2cdata; 1735 ic_resp->dgst.bits.hdgst_enable = tqpair->host_hdgst_enable ? 1 : 0; 1736 ic_resp->dgst.bits.ddgst_enable = tqpair->host_ddgst_enable ? 1 : 0; 1737 1738 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "host_hdgst_enable: %u\n", tqpair->host_hdgst_enable); 1739 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "host_ddgst_enable: %u\n", tqpair->host_ddgst_enable); 1740 1741 spdk_nvmf_tcp_qpair_write_pdu(tqpair, rsp_pdu, spdk_nvmf_tcp_send_icresp_complete, tqpair); 1742 spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY); 1743 return; 1744 end: 1745 spdk_nvmf_tcp_send_c2h_term_req(tqpair, pdu, fes, error_offset); 1746 return; 1747 } 1748 1749 static void 1750 spdk_nvmf_tcp_pdu_psh_handle(struct spdk_nvmf_tcp_qpair *tqpair) 1751 { 1752 struct nvme_tcp_pdu *pdu; 1753 int rc; 1754 uint32_t crc32c, error_offset = 0; 1755 enum spdk_nvme_tcp_term_req_fes fes; 1756 struct spdk_nvmf_tcp_transport *ttransport; 1757 1758 assert(tqpair->recv_state == NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PSH); 1759 pdu = &tqpair->pdu_in_progress; 1760 1761 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "pdu type of tqpair(%p) is %d\n", tqpair, 1762 pdu->hdr.common.pdu_type); 1763 /* check header digest if needed */ 1764 if (pdu->has_hdgst) { 1765 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Compare the header of pdu=%p on tqpair=%p\n", pdu, tqpair); 1766 crc32c = nvme_tcp_pdu_calc_header_digest(pdu); 1767 rc = MATCH_DIGEST_WORD((uint8_t *)pdu->hdr.raw + pdu->hdr.common.hlen, crc32c); 1768 if (rc == 0) { 1769 SPDK_ERRLOG("Header digest error on tqpair=(%p) with pdu=%p\n", tqpair, pdu); 1770 fes = SPDK_NVME_TCP_TERM_REQ_FES_HDGST_ERROR; 1771 spdk_nvmf_tcp_send_c2h_term_req(tqpair, pdu, fes, error_offset); 1772 return; 1773 1774 } 1775 } 1776 1777 ttransport = SPDK_CONTAINEROF(tqpair->qpair.transport, struct spdk_nvmf_tcp_transport, transport); 1778 switch (pdu->hdr.common.pdu_type) { 1779 case SPDK_NVME_TCP_PDU_TYPE_IC_REQ: 1780 spdk_nvmf_tcp_icreq_handle(ttransport, tqpair, pdu); 1781 break; 1782 case SPDK_NVME_TCP_PDU_TYPE_CAPSULE_CMD: 1783 spdk_nvmf_tcp_capsule_cmd_hdr_handle(ttransport, tqpair, pdu); 1784 break; 1785 case SPDK_NVME_TCP_PDU_TYPE_H2C_DATA: 1786 spdk_nvmf_tcp_h2c_data_hdr_handle(ttransport, tqpair, pdu); 1787 break; 1788 1789 case SPDK_NVME_TCP_PDU_TYPE_H2C_TERM_REQ: 1790 spdk_nvmf_tcp_h2c_term_req_hdr_handle(tqpair, pdu); 1791 break; 1792 1793 default: 1794 SPDK_ERRLOG("Unexpected PDU type 0x%02x\n", tqpair->pdu_in_progress.hdr.common.pdu_type); 1795 fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD; 1796 error_offset = 1; 1797 spdk_nvmf_tcp_send_c2h_term_req(tqpair, pdu, fes, error_offset); 1798 break; 1799 } 1800 } 1801 1802 static void 1803 spdk_nvmf_tcp_pdu_ch_handle(struct spdk_nvmf_tcp_qpair *tqpair) 1804 { 1805 struct nvme_tcp_pdu *pdu; 1806 uint32_t error_offset = 0; 1807 enum spdk_nvme_tcp_term_req_fes fes; 1808 uint8_t expected_hlen, pdo; 1809 bool plen_error = false, pdo_error = false; 1810 1811 assert(tqpair->recv_state == NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_CH); 1812 pdu = &tqpair->pdu_in_progress; 1813 1814 if (pdu->hdr.common.pdu_type == SPDK_NVME_TCP_PDU_TYPE_IC_REQ) { 1815 if (tqpair->state != NVME_TCP_QPAIR_STATE_INVALID) { 1816 SPDK_ERRLOG("Already received ICreq PDU, and reject this pdu=%p\n", pdu); 1817 fes = SPDK_NVME_TCP_TERM_REQ_FES_PDU_SEQUENCE_ERROR; 1818 goto err; 1819 } 1820 expected_hlen = sizeof(struct spdk_nvme_tcp_ic_req); 1821 if (pdu->hdr.common.plen != expected_hlen) { 1822 plen_error = true; 1823 } 1824 } else { 1825 if (tqpair->state != NVME_TCP_QPAIR_STATE_RUNNING) { 1826 SPDK_ERRLOG("The TCP/IP connection is not negotitated\n"); 1827 fes = SPDK_NVME_TCP_TERM_REQ_FES_PDU_SEQUENCE_ERROR; 1828 goto err; 1829 } 1830 1831 switch (pdu->hdr.common.pdu_type) { 1832 case SPDK_NVME_TCP_PDU_TYPE_CAPSULE_CMD: 1833 expected_hlen = sizeof(struct spdk_nvme_tcp_cmd); 1834 pdo = pdu->hdr.common.pdo; 1835 if ((tqpair->cpda != 0) && (pdo != ((tqpair->cpda + 1) << 2))) { 1836 pdo_error = true; 1837 break; 1838 } 1839 1840 if (pdu->hdr.common.plen < expected_hlen) { 1841 plen_error = true; 1842 } 1843 break; 1844 case SPDK_NVME_TCP_PDU_TYPE_H2C_DATA: 1845 expected_hlen = sizeof(struct spdk_nvme_tcp_h2c_data_hdr); 1846 pdo = pdu->hdr.common.pdo; 1847 if ((tqpair->cpda != 0) && (pdo != ((tqpair->cpda + 1) << 2))) { 1848 pdo_error = true; 1849 break; 1850 } 1851 if (pdu->hdr.common.plen < expected_hlen) { 1852 plen_error = true; 1853 } 1854 break; 1855 1856 case SPDK_NVME_TCP_PDU_TYPE_H2C_TERM_REQ: 1857 expected_hlen = sizeof(struct spdk_nvme_tcp_term_req_hdr); 1858 if ((pdu->hdr.common.plen <= expected_hlen) || 1859 (pdu->hdr.common.plen > SPDK_NVME_TCP_TERM_REQ_PDU_MAX_SIZE)) { 1860 plen_error = true; 1861 } 1862 break; 1863 1864 default: 1865 SPDK_ERRLOG("Unexpected PDU type 0x%02x\n", pdu->hdr.common.pdu_type); 1866 fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD; 1867 error_offset = offsetof(struct spdk_nvme_tcp_common_pdu_hdr, pdu_type); 1868 goto err; 1869 } 1870 } 1871 1872 if (pdu->hdr.common.hlen != expected_hlen) { 1873 SPDK_ERRLOG("PDU type=0x%02x, Expected ICReq header length %u, got %u on tqpair=%p\n", 1874 pdu->hdr.common.pdu_type, 1875 expected_hlen, pdu->hdr.common.hlen, tqpair); 1876 fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD; 1877 error_offset = offsetof(struct spdk_nvme_tcp_common_pdu_hdr, hlen); 1878 goto err; 1879 } else if (pdo_error) { 1880 fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD; 1881 error_offset = offsetof(struct spdk_nvme_tcp_common_pdu_hdr, pdo); 1882 } else if (plen_error) { 1883 fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD; 1884 error_offset = offsetof(struct spdk_nvme_tcp_common_pdu_hdr, plen); 1885 goto err; 1886 } else { 1887 spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PSH); 1888 return; 1889 } 1890 err: 1891 spdk_nvmf_tcp_send_c2h_term_req(tqpair, pdu, fes, error_offset); 1892 } 1893 1894 static int 1895 spdk_nvmf_tcp_sock_process(struct spdk_nvmf_tcp_qpair *tqpair) 1896 { 1897 int rc = 0; 1898 struct nvme_tcp_pdu *pdu; 1899 enum nvme_tcp_pdu_recv_state prev_state; 1900 uint32_t data_len; 1901 uint8_t psh_len, pdo, hlen; 1902 int8_t padding_len; 1903 1904 /* The loop here is to allow for several back-to-back state changes. */ 1905 do { 1906 prev_state = tqpair->recv_state; 1907 1908 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "tqpair(%p) recv pdu entering state %d\n", tqpair, prev_state); 1909 1910 switch (tqpair->recv_state) { 1911 /* Wait for the common header */ 1912 case NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY: 1913 case NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_CH: 1914 pdu = &tqpair->pdu_in_progress; 1915 1916 rc = nvme_tcp_read_data(tqpair->sock, 1917 sizeof(struct spdk_nvme_tcp_common_pdu_hdr) - pdu->ch_valid_bytes, 1918 (void *)&pdu->hdr.common + pdu->ch_valid_bytes); 1919 if (rc < 0) { 1920 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "will disconnect tqpair=%p\n", tqpair); 1921 return NVME_TCP_PDU_FATAL; 1922 } else if (rc > 0) { 1923 pdu->ch_valid_bytes += rc; 1924 spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_CH); 1925 } 1926 1927 if (pdu->ch_valid_bytes < sizeof(struct spdk_nvme_tcp_common_pdu_hdr)) { 1928 return NVME_TCP_PDU_IN_PROGRESS; 1929 } 1930 1931 /* The command header of this PDU has now been read from the socket. */ 1932 spdk_nvmf_tcp_pdu_ch_handle(tqpair); 1933 break; 1934 /* Wait for the pdu specific header */ 1935 case NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PSH: 1936 pdu = &tqpair->pdu_in_progress; 1937 psh_len = hlen = pdu->hdr.common.hlen; 1938 /* Only capsule_cmd and h2c_data has header digest */ 1939 if (((pdu->hdr.common.pdu_type == SPDK_NVME_TCP_PDU_TYPE_CAPSULE_CMD) || 1940 (pdu->hdr.common.pdu_type == SPDK_NVME_TCP_PDU_TYPE_H2C_DATA)) && 1941 tqpair->host_hdgst_enable) { 1942 pdu->has_hdgst = true; 1943 psh_len += SPDK_NVME_TCP_DIGEST_LEN; 1944 if (pdu->hdr.common.plen > psh_len) { 1945 pdo = pdu->hdr.common.pdo; 1946 padding_len = pdo - psh_len; 1947 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "padding length is =%d for pdu=%p on tqpair=%p\n", padding_len, 1948 pdu, tqpair); 1949 if (padding_len > 0) { 1950 psh_len = pdo; 1951 } 1952 } 1953 } 1954 1955 psh_len -= sizeof(struct spdk_nvme_tcp_common_pdu_hdr); 1956 /* The following will read psh + hdgest (if possbile) + padding (if posssible) */ 1957 if (pdu->psh_valid_bytes < psh_len) { 1958 rc = nvme_tcp_read_data(tqpair->sock, 1959 psh_len - pdu->psh_valid_bytes, 1960 (void *)&pdu->hdr.raw + sizeof(struct spdk_nvme_tcp_common_pdu_hdr) + pdu->psh_valid_bytes); 1961 if (rc < 0) { 1962 return NVME_TCP_PDU_FATAL; 1963 } 1964 1965 pdu->psh_valid_bytes += rc; 1966 if (pdu->psh_valid_bytes < psh_len) { 1967 return NVME_TCP_PDU_IN_PROGRESS; 1968 } 1969 } 1970 1971 /* All header(ch, psh, head digist) of this PDU has now been read from the socket. */ 1972 spdk_nvmf_tcp_pdu_psh_handle(tqpair); 1973 break; 1974 case NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD: 1975 pdu = &tqpair->pdu_in_progress; 1976 1977 /* check whether the data is valid, if not we just return */ 1978 if (!pdu->data) { 1979 return NVME_TCP_PDU_IN_PROGRESS; 1980 } 1981 1982 data_len = pdu->data_len; 1983 /* data len */ 1984 if (pdu->data_valid_bytes < data_len) { 1985 rc = nvme_tcp_read_data(tqpair->sock, data_len - pdu->data_valid_bytes, 1986 (void *)pdu->data + pdu->data_valid_bytes); 1987 if (rc < 0) { 1988 return NVME_TCP_PDU_FATAL; 1989 } 1990 1991 pdu->data_valid_bytes += rc; 1992 if (pdu->data_valid_bytes < data_len) { 1993 return NVME_TCP_PDU_IN_PROGRESS; 1994 } 1995 } 1996 1997 /* data digest */ 1998 if ((pdu->hdr.common.pdu_type != SPDK_NVME_TCP_PDU_TYPE_H2C_TERM_REQ) && 1999 tqpair->host_ddgst_enable && (pdu->ddigest_valid_bytes < SPDK_NVME_TCP_DIGEST_LEN)) { 2000 rc = nvme_tcp_read_data(tqpair->sock, 2001 SPDK_NVME_TCP_DIGEST_LEN - pdu->ddigest_valid_bytes, 2002 pdu->data_digest + pdu->ddigest_valid_bytes); 2003 if (rc < 0) { 2004 return NVME_TCP_PDU_FATAL; 2005 } 2006 2007 pdu->ddigest_valid_bytes += rc; 2008 if (pdu->ddigest_valid_bytes < SPDK_NVME_TCP_DIGEST_LEN) { 2009 return NVME_TCP_PDU_IN_PROGRESS; 2010 } 2011 } 2012 2013 /* All of this PDU has now been read from the socket. */ 2014 spdk_nvmf_tcp_pdu_payload_handle(tqpair); 2015 break; 2016 case NVME_TCP_PDU_RECV_STATE_ERROR: 2017 pdu = &tqpair->pdu_in_progress; 2018 /* Check whether the connection is closed. Each time, we only read 1 byte every time */ 2019 rc = nvme_tcp_read_data(tqpair->sock, 1, (void *)&pdu->hdr.common); 2020 if (rc < 0) { 2021 return NVME_TCP_PDU_FATAL; 2022 } 2023 break; 2024 default: 2025 assert(0); 2026 SPDK_ERRLOG("code should not come to here"); 2027 break; 2028 } 2029 } while (tqpair->recv_state != prev_state); 2030 2031 return rc; 2032 } 2033 2034 static enum spdk_nvme_data_transfer 2035 spdk_nvmf_tcp_req_get_xfer(struct spdk_nvmf_tcp_req *tcp_req) { 2036 enum spdk_nvme_data_transfer xfer; 2037 struct spdk_nvme_cmd *cmd = &tcp_req->req.cmd->nvme_cmd; 2038 struct spdk_nvme_sgl_descriptor *sgl = &cmd->dptr.sgl1; 2039 2040 /* Figure out data transfer direction */ 2041 if (cmd->opc == SPDK_NVME_OPC_FABRIC) 2042 { 2043 xfer = spdk_nvme_opc_get_data_transfer(tcp_req->req.cmd->nvmf_cmd.fctype); 2044 } else 2045 { 2046 xfer = spdk_nvme_opc_get_data_transfer(cmd->opc); 2047 2048 /* Some admin commands are special cases */ 2049 if ((tcp_req->req.qpair->qid == 0) && 2050 ((cmd->opc == SPDK_NVME_OPC_GET_FEATURES) || 2051 (cmd->opc == SPDK_NVME_OPC_SET_FEATURES))) { 2052 switch (cmd->cdw10 & 0xff) { 2053 case SPDK_NVME_FEAT_LBA_RANGE_TYPE: 2054 case SPDK_NVME_FEAT_AUTONOMOUS_POWER_STATE_TRANSITION: 2055 case SPDK_NVME_FEAT_HOST_IDENTIFIER: 2056 break; 2057 default: 2058 xfer = SPDK_NVME_DATA_NONE; 2059 } 2060 } 2061 } 2062 2063 if (xfer == SPDK_NVME_DATA_NONE) 2064 { 2065 return xfer; 2066 } 2067 2068 /* Even for commands that may transfer data, they could have specified 0 length. 2069 * We want those to show up with xfer SPDK_NVME_DATA_NONE. 2070 */ 2071 switch (sgl->generic.type) 2072 { 2073 case SPDK_NVME_SGL_TYPE_DATA_BLOCK: 2074 case SPDK_NVME_SGL_TYPE_BIT_BUCKET: 2075 case SPDK_NVME_SGL_TYPE_SEGMENT: 2076 case SPDK_NVME_SGL_TYPE_LAST_SEGMENT: 2077 case SPDK_NVME_SGL_TYPE_TRANSPORT_DATA_BLOCK: 2078 if (sgl->unkeyed.length == 0) { 2079 xfer = SPDK_NVME_DATA_NONE; 2080 } 2081 break; 2082 case SPDK_NVME_SGL_TYPE_KEYED_DATA_BLOCK: 2083 if (sgl->keyed.length == 0) { 2084 xfer = SPDK_NVME_DATA_NONE; 2085 } 2086 break; 2087 } 2088 2089 return xfer; 2090 } 2091 2092 static void 2093 spdk_nvmf_tcp_request_free_buffers(struct spdk_nvmf_tcp_req *tcp_req, 2094 struct spdk_nvmf_transport_poll_group *group, struct spdk_nvmf_transport *transport) 2095 { 2096 for (uint32_t i = 0; i < tcp_req->req.iovcnt; i++) { 2097 assert(tcp_req->buffers[i] != NULL); 2098 if (group->buf_cache_count < group->buf_cache_size) { 2099 STAILQ_INSERT_HEAD(&group->buf_cache, 2100 (struct spdk_nvmf_transport_pg_cache_buf *)tcp_req->buffers[i], link); 2101 group->buf_cache_count++; 2102 } else { 2103 spdk_mempool_put(transport->data_buf_pool, tcp_req->buffers[i]); 2104 } 2105 tcp_req->req.iov[i].iov_base = NULL; 2106 tcp_req->buffers[i] = NULL; 2107 tcp_req->req.iov[i].iov_len = 0; 2108 } 2109 tcp_req->data_from_pool = false; 2110 } 2111 2112 static int 2113 spdk_nvmf_tcp_req_fill_iovs(struct spdk_nvmf_tcp_transport *ttransport, 2114 struct spdk_nvmf_tcp_req *tcp_req) 2115 { 2116 void *buf = NULL; 2117 uint32_t length = tcp_req->req.length; 2118 uint32_t i = 0; 2119 struct spdk_nvmf_tcp_qpair *tqpair; 2120 struct spdk_nvmf_transport_poll_group *group; 2121 2122 tqpair = SPDK_CONTAINEROF(tcp_req->req.qpair, struct spdk_nvmf_tcp_qpair, qpair); 2123 group = &tqpair->group->group; 2124 2125 tcp_req->req.iovcnt = 0; 2126 while (length) { 2127 if (!(STAILQ_EMPTY(&group->buf_cache))) { 2128 group->buf_cache_count--; 2129 buf = STAILQ_FIRST(&group->buf_cache); 2130 STAILQ_REMOVE_HEAD(&group->buf_cache, link); 2131 } else { 2132 buf = spdk_mempool_get(ttransport->transport.data_buf_pool); 2133 if (!buf) { 2134 goto nomem; 2135 } 2136 } 2137 2138 tcp_req->req.iov[i].iov_base = (void *)((uintptr_t)(buf + NVMF_DATA_BUFFER_MASK) & 2139 ~NVMF_DATA_BUFFER_MASK); 2140 tcp_req->req.iov[i].iov_len = spdk_min(length, ttransport->transport.opts.io_unit_size); 2141 tcp_req->req.iovcnt++; 2142 tcp_req->buffers[i] = buf; 2143 length -= tcp_req->req.iov[i].iov_len; 2144 i++; 2145 } 2146 2147 assert(tcp_req->req.iovcnt < SPDK_NVMF_MAX_SGL_ENTRIES); 2148 tcp_req->data_from_pool = true; 2149 return 0; 2150 2151 nomem: 2152 spdk_nvmf_tcp_request_free_buffers(tcp_req, group, &ttransport->transport); 2153 tcp_req->req.iovcnt = 0; 2154 return -ENOMEM; 2155 } 2156 2157 static int 2158 spdk_nvmf_tcp_req_parse_sgl(struct spdk_nvmf_tcp_transport *ttransport, 2159 struct spdk_nvmf_tcp_req *tcp_req) 2160 { 2161 struct spdk_nvme_cmd *cmd; 2162 struct spdk_nvme_cpl *rsp; 2163 struct spdk_nvme_sgl_descriptor *sgl; 2164 2165 cmd = &tcp_req->req.cmd->nvme_cmd; 2166 rsp = &tcp_req->req.rsp->nvme_cpl; 2167 sgl = &cmd->dptr.sgl1; 2168 2169 if (sgl->generic.type == SPDK_NVME_SGL_TYPE_TRANSPORT_DATA_BLOCK && 2170 sgl->unkeyed.subtype == SPDK_NVME_SGL_SUBTYPE_TRANSPORT) { 2171 if (sgl->unkeyed.length > ttransport->transport.opts.max_io_size) { 2172 SPDK_ERRLOG("SGL length 0x%x exceeds max io size 0x%x\n", 2173 sgl->unkeyed.length, ttransport->transport.opts.max_io_size); 2174 rsp->status.sc = SPDK_NVME_SC_DATA_SGL_LENGTH_INVALID; 2175 return -1; 2176 } 2177 2178 /* fill request length and populate iovs */ 2179 tcp_req->req.length = sgl->unkeyed.length; 2180 2181 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Data requested length= 0x%x\n", 2182 sgl->unkeyed.length); 2183 2184 if (spdk_nvmf_tcp_req_fill_iovs(ttransport, tcp_req) < 0) { 2185 /* No available buffers. Queue this request up. */ 2186 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "No available large data buffers. Queueing request %p\n", tcp_req); 2187 return 0; 2188 } 2189 2190 /* backward compatible */ 2191 tcp_req->req.data = tcp_req->req.iov[0].iov_base; 2192 2193 2194 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Request %p took %d buffer/s from central pool, and data=%p\n", 2195 tcp_req, 2196 tcp_req->req.iovcnt, tcp_req->req.data); 2197 2198 return 0; 2199 } else if (sgl->generic.type == SPDK_NVME_SGL_TYPE_DATA_BLOCK && 2200 sgl->unkeyed.subtype == SPDK_NVME_SGL_SUBTYPE_OFFSET) { 2201 uint64_t offset = sgl->address; 2202 uint32_t max_len = ttransport->transport.opts.in_capsule_data_size; 2203 2204 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "In-capsule data: offset 0x%" PRIx64 ", length 0x%x\n", 2205 offset, sgl->unkeyed.length); 2206 2207 if (offset > max_len) { 2208 SPDK_ERRLOG("In-capsule offset 0x%" PRIx64 " exceeds capsule length 0x%x\n", 2209 offset, max_len); 2210 rsp->status.sc = SPDK_NVME_SC_INVALID_SGL_OFFSET; 2211 return -1; 2212 } 2213 max_len -= (uint32_t)offset; 2214 2215 if (sgl->unkeyed.length > max_len) { 2216 SPDK_ERRLOG("In-capsule data length 0x%x exceeds capsule length 0x%x\n", 2217 sgl->unkeyed.length, max_len); 2218 rsp->status.sc = SPDK_NVME_SC_DATA_SGL_LENGTH_INVALID; 2219 return -1; 2220 } 2221 2222 tcp_req->req.data = tcp_req->buf + offset; 2223 tcp_req->data_from_pool = false; 2224 tcp_req->req.length = sgl->unkeyed.length; 2225 2226 tcp_req->req.iov[0].iov_base = tcp_req->req.data; 2227 tcp_req->req.iov[0].iov_len = tcp_req->req.length; 2228 tcp_req->req.iovcnt = 1; 2229 2230 return 0; 2231 } 2232 2233 SPDK_ERRLOG("Invalid NVMf I/O Command SGL: Type 0x%x, Subtype 0x%x\n", 2234 sgl->generic.type, sgl->generic.subtype); 2235 rsp->status.sc = SPDK_NVME_SC_SGL_DESCRIPTOR_TYPE_INVALID; 2236 return -1; 2237 } 2238 2239 static void 2240 spdk_nvmf_tcp_send_c2h_data(struct spdk_nvmf_tcp_qpair *tqpair, 2241 struct spdk_nvmf_tcp_req *tcp_req) 2242 { 2243 struct nvme_tcp_pdu *rsp_pdu; 2244 struct spdk_nvme_tcp_c2h_data_hdr *c2h_data; 2245 uint32_t plen, pdo, alignment, offset, iov_index; 2246 2247 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "enter\n"); 2248 2249 /* always use the first iov_len, which is correct */ 2250 iov_index = tcp_req->c2h_data_offset / tcp_req->req.iov[0].iov_len; 2251 offset = tcp_req->c2h_data_offset % tcp_req->req.iov[0].iov_len; 2252 2253 rsp_pdu = spdk_nvmf_tcp_pdu_get(tqpair); 2254 assert(rsp_pdu != NULL); 2255 2256 c2h_data = &rsp_pdu->hdr.c2h_data; 2257 c2h_data->common.pdu_type = SPDK_NVME_TCP_PDU_TYPE_C2H_DATA; 2258 plen = c2h_data->common.hlen = sizeof(*c2h_data); 2259 2260 if (tqpair->host_hdgst_enable) { 2261 plen += SPDK_NVME_TCP_DIGEST_LEN; 2262 c2h_data->common.flags |= SPDK_NVME_TCP_CH_FLAGS_HDGSTF; 2263 } 2264 2265 /* set the psh */ 2266 c2h_data->cccid = tcp_req->req.cmd->nvme_cmd.cid; 2267 c2h_data->datal = spdk_min(NVMF_TCP_PDU_MAX_C2H_DATA_SIZE, 2268 (tcp_req->req.iov[iov_index].iov_len - offset)); 2269 c2h_data->datao = tcp_req->c2h_data_offset; 2270 2271 /* set the padding */ 2272 rsp_pdu->padding_len = 0; 2273 pdo = plen; 2274 if (tqpair->cpda) { 2275 alignment = (tqpair->cpda + 1) << 2; 2276 if (alignment > plen) { 2277 rsp_pdu->padding_len = alignment - plen; 2278 pdo = plen = alignment; 2279 } 2280 } 2281 2282 c2h_data->common.pdo = pdo; 2283 plen += c2h_data->datal; 2284 if (tqpair->host_ddgst_enable) { 2285 c2h_data->common.flags |= SPDK_NVME_TCP_CH_FLAGS_DDGSTF; 2286 plen += SPDK_NVME_TCP_DIGEST_LEN; 2287 } 2288 2289 c2h_data->common.plen = plen; 2290 2291 rsp_pdu->data = tcp_req->req.iov[iov_index].iov_base + offset; 2292 rsp_pdu->data_len = c2h_data->datal; 2293 2294 tcp_req->c2h_data_offset += c2h_data->datal; 2295 if (iov_index == (tcp_req->req.iovcnt - 1) && (tcp_req->c2h_data_offset == tcp_req->req.length)) { 2296 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Last pdu for tcp_req=%p on tqpair=%p\n", tcp_req, tqpair); 2297 c2h_data->common.flags |= SPDK_NVME_TCP_C2H_DATA_FLAGS_LAST_PDU; 2298 /* The linux kernel does not support this yet */ 2299 #if LINUX_KERNEL_SUPPORT_NOT_SENDING_RESP_FOR_C2H 2300 c2h_data->common.flags |= SPDK_NVME_TCP_C2H_DATA_FLAGS_SUCCESS; 2301 #endif 2302 TAILQ_REMOVE(&tqpair->queued_c2h_data_tcp_req, tcp_req, link); 2303 } 2304 2305 tqpair->c2h_data_pdu_cnt += 1; 2306 spdk_nvmf_tcp_qpair_write_pdu(tqpair, rsp_pdu, spdk_nvmf_tcp_pdu_c2h_data_complete, tcp_req); 2307 } 2308 2309 static int 2310 spdk_nvmf_tcp_calc_c2h_data_pdu_num(struct spdk_nvmf_tcp_req *tcp_req) 2311 { 2312 uint32_t i, iov_cnt, pdu_num = 0; 2313 2314 iov_cnt = tcp_req->req.iovcnt; 2315 for (i = 0; i < iov_cnt; i++) { 2316 pdu_num += (tcp_req->req.iov[i].iov_len + NVMF_TCP_PDU_MAX_C2H_DATA_SIZE - 1) / 2317 NVMF_TCP_PDU_MAX_C2H_DATA_SIZE; 2318 } 2319 2320 return pdu_num; 2321 } 2322 2323 static void 2324 spdk_nvmf_tcp_handle_pending_c2h_data_queue(struct spdk_nvmf_tcp_qpair *tqpair) 2325 { 2326 struct spdk_nvmf_tcp_req *tcp_req; 2327 2328 while (!TAILQ_EMPTY(&tqpair->queued_c2h_data_tcp_req) && 2329 (tqpair->c2h_data_pdu_cnt < NVMF_TCP_QPAIR_MAX_C2H_PDU_NUM)) { 2330 tcp_req = TAILQ_FIRST(&tqpair->queued_c2h_data_tcp_req); 2331 spdk_nvmf_tcp_send_c2h_data(tqpair, tcp_req); 2332 } 2333 } 2334 2335 static void 2336 spdk_nvmf_tcp_queue_c2h_data(struct spdk_nvmf_tcp_req *tcp_req, 2337 struct spdk_nvmf_tcp_qpair *tqpair) 2338 { 2339 tcp_req->c2h_data_pdu_num = spdk_nvmf_tcp_calc_c2h_data_pdu_num(tcp_req); 2340 2341 assert(tcp_req->c2h_data_pdu_num < NVMF_TCP_QPAIR_MAX_C2H_PDU_NUM); 2342 2343 TAILQ_INSERT_TAIL(&tqpair->queued_c2h_data_tcp_req, tcp_req, link); 2344 spdk_nvmf_tcp_handle_pending_c2h_data_queue(tqpair); 2345 } 2346 2347 static int 2348 request_transfer_out(struct spdk_nvmf_request *req) 2349 { 2350 struct spdk_nvmf_tcp_req *tcp_req; 2351 struct spdk_nvmf_qpair *qpair; 2352 struct spdk_nvmf_tcp_qpair *tqpair; 2353 struct spdk_nvme_cpl *rsp; 2354 2355 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "enter\n"); 2356 2357 qpair = req->qpair; 2358 rsp = &req->rsp->nvme_cpl; 2359 tcp_req = SPDK_CONTAINEROF(req, struct spdk_nvmf_tcp_req, req); 2360 2361 /* Advance our sq_head pointer */ 2362 if (qpair->sq_head == qpair->sq_head_max) { 2363 qpair->sq_head = 0; 2364 } else { 2365 qpair->sq_head++; 2366 } 2367 rsp->sqhd = qpair->sq_head; 2368 2369 tqpair = SPDK_CONTAINEROF(tcp_req->req.qpair, struct spdk_nvmf_tcp_qpair, qpair); 2370 spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_TRANSFERRING_CONTROLLER_TO_HOST); 2371 if (rsp->status.sc == SPDK_NVME_SC_SUCCESS && 2372 req->xfer == SPDK_NVME_DATA_CONTROLLER_TO_HOST) { 2373 spdk_nvmf_tcp_queue_c2h_data(tcp_req, tqpair); 2374 } else { 2375 spdk_nvmf_tcp_send_capsule_resp_pdu(tcp_req, tqpair); 2376 } 2377 2378 return 0; 2379 } 2380 2381 static void 2382 spdk_nvmf_tcp_pdu_set_buf_from_req(struct spdk_nvmf_tcp_qpair *tqpair, 2383 struct spdk_nvmf_tcp_req *tcp_req) 2384 { 2385 struct nvme_tcp_pdu *pdu; 2386 2387 if (tcp_req->data_from_pool) { 2388 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Will send r2t for tcp_req(%p) on tqpair=%p\n", tcp_req, tqpair); 2389 tcp_req->next_expected_r2t_offset = 0; 2390 spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_DATA_PENDING_FOR_R2T); 2391 spdk_nvmf_tcp_handle_queued_r2t_req(tqpair); 2392 } else { 2393 pdu = &tqpair->pdu_in_progress; 2394 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Not need to send r2t for tcp_req(%p) on tqpair=%p\n", tcp_req, 2395 tqpair); 2396 /* No need to send r2t, contained in the capsuled data */ 2397 pdu->data = tcp_req->req.data; 2398 pdu->data_len = tcp_req->req.length; 2399 spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD); 2400 spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER); 2401 } 2402 } 2403 2404 static void 2405 spdk_nvmf_tcp_set_incapsule_data(struct spdk_nvmf_tcp_qpair *tqpair, 2406 struct spdk_nvmf_tcp_req *tcp_req) 2407 { 2408 struct nvme_tcp_pdu *pdu; 2409 uint32_t plen = 0; 2410 2411 pdu = &tqpair->pdu_in_progress; 2412 plen = pdu->hdr.common.hlen; 2413 2414 if (tqpair->host_hdgst_enable) { 2415 plen += SPDK_NVME_TCP_DIGEST_LEN; 2416 } 2417 2418 if (pdu->hdr.common.plen != plen) { 2419 tcp_req->has_incapsule_data = true; 2420 } 2421 } 2422 2423 static bool 2424 spdk_nvmf_tcp_req_process(struct spdk_nvmf_tcp_transport *ttransport, 2425 struct spdk_nvmf_tcp_req *tcp_req) 2426 { 2427 struct spdk_nvmf_tcp_qpair *tqpair; 2428 struct spdk_nvme_cpl *rsp = &tcp_req->req.rsp->nvme_cpl; 2429 int rc; 2430 enum spdk_nvmf_tcp_req_state prev_state; 2431 bool progress = false; 2432 struct spdk_nvmf_transport_poll_group *group; 2433 2434 tqpair = SPDK_CONTAINEROF(tcp_req->req.qpair, struct spdk_nvmf_tcp_qpair, qpair); 2435 group = &tqpair->group->group; 2436 assert(tcp_req->state != TCP_REQUEST_STATE_FREE); 2437 2438 /* The loop here is to allow for several back-to-back state changes. */ 2439 do { 2440 prev_state = tcp_req->state; 2441 2442 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Request %p entering state %d on tqpair=%p\n", tcp_req, prev_state, 2443 tqpair); 2444 2445 switch (tcp_req->state) { 2446 case TCP_REQUEST_STATE_FREE: 2447 /* Some external code must kick a request into TCP_REQUEST_STATE_NEW 2448 * to escape this state. */ 2449 break; 2450 case TCP_REQUEST_STATE_NEW: 2451 spdk_trace_record(TRACE_TCP_REQUEST_STATE_NEW, 0, 0, (uintptr_t)tcp_req, 0); 2452 2453 /* copy the cmd from the receive pdu */ 2454 tcp_req->cmd = tqpair->pdu_in_progress.hdr.capsule_cmd.ccsqe; 2455 2456 /* The next state transition depends on the data transfer needs of this request. */ 2457 tcp_req->req.xfer = spdk_nvmf_tcp_req_get_xfer(tcp_req); 2458 2459 /* If no data to transfer, ready to execute. */ 2460 if (tcp_req->req.xfer == SPDK_NVME_DATA_NONE) { 2461 /* Reset the tqpair receving pdu state */ 2462 spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY); 2463 spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_READY_TO_EXECUTE); 2464 break; 2465 } 2466 2467 spdk_nvmf_tcp_set_incapsule_data(tqpair, tcp_req); 2468 2469 if (!tcp_req->has_incapsule_data) { 2470 spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY); 2471 } 2472 2473 spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_NEED_BUFFER); 2474 TAILQ_INSERT_TAIL(&tqpair->group->pending_data_buf_queue, tcp_req, link); 2475 break; 2476 case TCP_REQUEST_STATE_NEED_BUFFER: 2477 spdk_trace_record(TRACE_TCP_REQUEST_STATE_NEED_BUFFER, 0, 0, (uintptr_t)tcp_req, 0); 2478 2479 assert(tcp_req->req.xfer != SPDK_NVME_DATA_NONE); 2480 2481 if (!tcp_req->has_incapsule_data && 2482 (tcp_req != TAILQ_FIRST(&tqpair->group->pending_data_buf_queue))) { 2483 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, 2484 "Not the first element to wait for the buf for tcp_req(%p) on tqpair=%p\n", 2485 tcp_req, tqpair); 2486 /* This request needs to wait in line to obtain a buffer */ 2487 break; 2488 } 2489 2490 /* Try to get a data buffer */ 2491 rc = spdk_nvmf_tcp_req_parse_sgl(ttransport, tcp_req); 2492 if (rc < 0) { 2493 TAILQ_REMOVE(&tqpair->group->pending_data_buf_queue, tcp_req, link); 2494 rsp->status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR; 2495 /* Reset the tqpair receving pdu state */ 2496 spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_ERROR); 2497 spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_READY_TO_COMPLETE); 2498 break; 2499 } 2500 2501 if (!tcp_req->req.data) { 2502 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "No buffer allocated for tcp_req(%p) on tqpair(%p\n)", 2503 tcp_req, tqpair); 2504 /* No buffers available. */ 2505 break; 2506 } 2507 2508 TAILQ_REMOVE(&tqpair->group->pending_data_buf_queue, tcp_req, link); 2509 2510 /* If data is transferring from host to controller, we need to do a transfer from the host. */ 2511 if (tcp_req->req.xfer == SPDK_NVME_DATA_HOST_TO_CONTROLLER) { 2512 spdk_nvmf_tcp_pdu_set_buf_from_req(tqpair, tcp_req); 2513 break; 2514 } 2515 2516 spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_READY_TO_EXECUTE); 2517 break; 2518 case TCP_REQUEST_STATE_DATA_PENDING_FOR_R2T: 2519 spdk_trace_record(TCP_REQUEST_STATE_DATA_PENDING_FOR_R2T, 0, 0, 2520 (uintptr_t)tcp_req, 0); 2521 /* Some external code must kick a request into TCP_REQUEST_STATE_DATA_PENDING_R2T 2522 * to escape this state. */ 2523 break; 2524 2525 case TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER: 2526 spdk_trace_record(TRACE_TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER, 0, 0, 2527 (uintptr_t)tcp_req, 0); 2528 /* Some external code must kick a request into TCP_REQUEST_STATE_READY_TO_EXECUTE 2529 * to escape this state. */ 2530 break; 2531 case TCP_REQUEST_STATE_READY_TO_EXECUTE: 2532 spdk_trace_record(TRACE_TCP_REQUEST_STATE_READY_TO_EXECUTE, 0, 0, (uintptr_t)tcp_req, 0); 2533 spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_EXECUTING); 2534 spdk_nvmf_request_exec(&tcp_req->req); 2535 break; 2536 case TCP_REQUEST_STATE_EXECUTING: 2537 spdk_trace_record(TRACE_TCP_REQUEST_STATE_EXECUTING, 0, 0, (uintptr_t)tcp_req, 0); 2538 /* Some external code must kick a request into TCP_REQUEST_STATE_EXECUTED 2539 * to escape this state. */ 2540 break; 2541 case TCP_REQUEST_STATE_EXECUTED: 2542 spdk_trace_record(TRACE_TCP_REQUEST_STATE_EXECUTED, 0, 0, (uintptr_t)tcp_req, 0); 2543 spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_READY_TO_COMPLETE); 2544 break; 2545 case TCP_REQUEST_STATE_READY_TO_COMPLETE: 2546 spdk_trace_record(TRACE_TCP_REQUEST_STATE_READY_TO_COMPLETE, 0, 0, (uintptr_t)tcp_req, 0); 2547 rc = request_transfer_out(&tcp_req->req); 2548 assert(rc == 0); /* No good way to handle this currently */ 2549 break; 2550 case TCP_REQUEST_STATE_TRANSFERRING_CONTROLLER_TO_HOST: 2551 spdk_trace_record(TRACE_TCP_REQUEST_STATE_TRANSFERRING_CONTROLLER_TO_HOST, 0, 0, 2552 (uintptr_t)tcp_req, 2553 0); 2554 /* Some external code must kick a request into TCP_REQUEST_STATE_COMPLETED 2555 * to escape this state. */ 2556 break; 2557 case TCP_REQUEST_STATE_COMPLETED: 2558 spdk_trace_record(TRACE_TCP_REQUEST_STATE_COMPLETED, 0, 0, (uintptr_t)tcp_req, 0); 2559 if (tcp_req->data_from_pool) { 2560 spdk_nvmf_tcp_request_free_buffers(tcp_req, group, &ttransport->transport); 2561 } 2562 tcp_req->req.length = 0; 2563 tcp_req->req.iovcnt = 0; 2564 tcp_req->req.data = NULL; 2565 spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_FREE); 2566 break; 2567 case TCP_REQUEST_NUM_STATES: 2568 default: 2569 assert(0); 2570 break; 2571 } 2572 2573 if (tcp_req->state != prev_state) { 2574 progress = true; 2575 } 2576 } while (tcp_req->state != prev_state); 2577 2578 return progress; 2579 } 2580 2581 static void 2582 spdk_nvmf_tcp_qpair_process_pending(struct spdk_nvmf_tcp_transport *ttransport, 2583 struct spdk_nvmf_tcp_qpair *tqpair) 2584 { 2585 struct spdk_nvmf_tcp_req *tcp_req, *req_tmp; 2586 2587 /* Tqpair is not in a good state, so return it */ 2588 if (spdk_unlikely(tqpair->recv_state == NVME_TCP_PDU_RECV_STATE_ERROR)) { 2589 return; 2590 } 2591 2592 spdk_nvmf_tcp_handle_queued_r2t_req(tqpair); 2593 2594 TAILQ_FOREACH_SAFE(tcp_req, &tqpair->group->pending_data_buf_queue, link, req_tmp) { 2595 if (spdk_nvmf_tcp_req_process(ttransport, tcp_req) == false) { 2596 break; 2597 } 2598 } 2599 } 2600 2601 static void 2602 spdk_nvmf_tcp_sock_cb(void *arg, struct spdk_sock_group *group, struct spdk_sock *sock) 2603 { 2604 struct spdk_nvmf_tcp_qpair *tqpair = arg; 2605 struct spdk_nvmf_tcp_transport *ttransport; 2606 int rc; 2607 2608 assert(tqpair != NULL); 2609 2610 ttransport = SPDK_CONTAINEROF(tqpair->qpair.transport, struct spdk_nvmf_tcp_transport, transport); 2611 spdk_nvmf_tcp_qpair_process_pending(ttransport, tqpair); 2612 rc = spdk_nvmf_tcp_sock_process(tqpair); 2613 2614 /* check the following two factors: 2615 * rc: The socket is closed 2616 * State of tqpair: The tqpair is in EXITING state due to internal error 2617 */ 2618 if ((rc < 0) || (tqpair->state == NVME_TCP_QPAIR_STATE_EXITING)) { 2619 tqpair->state = NVME_TCP_QPAIR_STATE_EXITED; 2620 spdk_nvmf_tcp_qpair_flush_pdus(tqpair); 2621 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "will disconect the tqpair=%p\n", tqpair); 2622 spdk_poller_unregister(&tqpair->timeout_poller); 2623 spdk_nvmf_qpair_disconnect(&tqpair->qpair, NULL, NULL); 2624 } 2625 } 2626 2627 static int 2628 spdk_nvmf_tcp_poll_group_add(struct spdk_nvmf_transport_poll_group *group, 2629 struct spdk_nvmf_qpair *qpair) 2630 { 2631 struct spdk_nvmf_tcp_poll_group *tgroup; 2632 struct spdk_nvmf_tcp_qpair *tqpair; 2633 int rc; 2634 2635 tgroup = SPDK_CONTAINEROF(group, struct spdk_nvmf_tcp_poll_group, group); 2636 tqpair = SPDK_CONTAINEROF(qpair, struct spdk_nvmf_tcp_qpair, qpair); 2637 2638 rc = spdk_sock_group_add_sock(tgroup->sock_group, tqpair->sock, 2639 spdk_nvmf_tcp_sock_cb, tqpair); 2640 if (rc != 0) { 2641 SPDK_ERRLOG("Could not add sock to sock_group: %s (%d)\n", 2642 spdk_strerror(errno), errno); 2643 spdk_nvmf_tcp_qpair_destroy(tqpair); 2644 return -1; 2645 } 2646 2647 rc = spdk_nvmf_tcp_qpair_sock_init(tqpair); 2648 if (rc != 0) { 2649 SPDK_ERRLOG("Cannot set sock opt for tqpair=%p\n", tqpair); 2650 spdk_nvmf_tcp_qpair_destroy(tqpair); 2651 return -1; 2652 } 2653 2654 rc = spdk_nvmf_tcp_qpair_init(&tqpair->qpair); 2655 if (rc < 0) { 2656 SPDK_ERRLOG("Cannot init tqpair=%p\n", tqpair); 2657 spdk_nvmf_tcp_qpair_destroy(tqpair); 2658 return -1; 2659 } 2660 2661 rc = spdk_nvmf_tcp_qpair_init_mem_resource(tqpair, 1); 2662 if (rc < 0) { 2663 SPDK_ERRLOG("Cannot init memory resource info for tqpair=%p\n", tqpair); 2664 spdk_nvmf_tcp_qpair_destroy(tqpair); 2665 return -1; 2666 } 2667 2668 tqpair->group = tgroup; 2669 tqpair->state = NVME_TCP_QPAIR_STATE_INVALID; 2670 TAILQ_INSERT_TAIL(&tgroup->qpairs, tqpair, link); 2671 2672 return 0; 2673 } 2674 2675 static int 2676 spdk_nvmf_tcp_poll_group_remove(struct spdk_nvmf_transport_poll_group *group, 2677 struct spdk_nvmf_qpair *qpair) 2678 { 2679 struct spdk_nvmf_tcp_poll_group *tgroup; 2680 struct spdk_nvmf_tcp_qpair *tqpair; 2681 int rc; 2682 2683 tgroup = SPDK_CONTAINEROF(group, struct spdk_nvmf_tcp_poll_group, group); 2684 tqpair = SPDK_CONTAINEROF(qpair, struct spdk_nvmf_tcp_qpair, qpair); 2685 2686 assert(tqpair->group == tgroup); 2687 tqpair->group = NULL; 2688 2689 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "remove tqpair=%p from the tgroup=%p\n", tqpair, tgroup); 2690 TAILQ_REMOVE(&tgroup->qpairs, tqpair, link); 2691 rc = spdk_sock_group_remove_sock(tgroup->sock_group, tqpair->sock); 2692 if (rc != 0) { 2693 SPDK_ERRLOG("Could not remove sock from sock_group: %s (%d)\n", 2694 spdk_strerror(errno), errno); 2695 } 2696 2697 return rc; 2698 } 2699 2700 static int 2701 spdk_nvmf_tcp_req_complete(struct spdk_nvmf_request *req) 2702 { 2703 struct spdk_nvmf_tcp_transport *ttransport; 2704 struct spdk_nvmf_tcp_req *tcp_req; 2705 2706 ttransport = SPDK_CONTAINEROF(req->qpair->transport, struct spdk_nvmf_tcp_transport, transport); 2707 tcp_req = SPDK_CONTAINEROF(req, struct spdk_nvmf_tcp_req, req); 2708 2709 spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_EXECUTED); 2710 spdk_nvmf_tcp_req_process(ttransport, tcp_req); 2711 2712 return 0; 2713 } 2714 2715 static void 2716 spdk_nvmf_tcp_close_qpair(struct spdk_nvmf_qpair *qpair) 2717 { 2718 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "enter\n"); 2719 2720 spdk_nvmf_tcp_qpair_destroy(SPDK_CONTAINEROF(qpair, struct spdk_nvmf_tcp_qpair, qpair)); 2721 } 2722 2723 static int 2724 spdk_nvmf_tcp_poll_group_poll(struct spdk_nvmf_transport_poll_group *group) 2725 { 2726 struct spdk_nvmf_tcp_poll_group *tgroup; 2727 int rc; 2728 2729 tgroup = SPDK_CONTAINEROF(group, struct spdk_nvmf_tcp_poll_group, group); 2730 2731 if (spdk_unlikely(TAILQ_EMPTY(&tgroup->qpairs))) { 2732 return 0; 2733 } 2734 2735 rc = spdk_sock_group_poll(tgroup->sock_group); 2736 if (rc < 0) { 2737 SPDK_ERRLOG("Failed to poll sock_group=%p\n", tgroup->sock_group); 2738 return rc; 2739 } 2740 2741 return 0; 2742 } 2743 2744 static bool 2745 spdk_nvmf_tcp_qpair_is_idle(struct spdk_nvmf_qpair *qpair) 2746 { 2747 struct spdk_nvmf_tcp_qpair *tqpair; 2748 2749 tqpair = SPDK_CONTAINEROF(qpair, struct spdk_nvmf_tcp_qpair, qpair); 2750 if (tqpair->state_cntr[TCP_REQUEST_STATE_FREE] == tqpair->max_queue_depth) { 2751 return true; 2752 } 2753 2754 return false; 2755 } 2756 2757 static int 2758 spdk_nvmf_tcp_qpair_get_trid(struct spdk_nvmf_qpair *qpair, 2759 struct spdk_nvme_transport_id *trid, bool peer) 2760 { 2761 struct spdk_nvmf_tcp_qpair *tqpair; 2762 uint16_t port; 2763 2764 tqpair = SPDK_CONTAINEROF(qpair, struct spdk_nvmf_tcp_qpair, qpair); 2765 trid->trtype = SPDK_NVME_TRANSPORT_TCP; 2766 2767 if (peer) { 2768 snprintf(trid->traddr, sizeof(trid->traddr), "%s", tqpair->initiator_addr); 2769 port = tqpair->initiator_port; 2770 } else { 2771 snprintf(trid->traddr, sizeof(trid->traddr), "%s", tqpair->target_addr); 2772 port = tqpair->target_port; 2773 } 2774 2775 if (spdk_sock_is_ipv4(tqpair->sock)) { 2776 trid->adrfam = SPDK_NVMF_ADRFAM_IPV4; 2777 } else if (spdk_sock_is_ipv4(tqpair->sock)) { 2778 trid->adrfam = SPDK_NVMF_ADRFAM_IPV6; 2779 } else { 2780 return -1; 2781 } 2782 2783 snprintf(trid->trsvcid, sizeof(trid->trsvcid), "%d", port); 2784 return 0; 2785 } 2786 2787 static int 2788 spdk_nvmf_tcp_qpair_get_local_trid(struct spdk_nvmf_qpair *qpair, 2789 struct spdk_nvme_transport_id *trid) 2790 { 2791 return spdk_nvmf_tcp_qpair_get_trid(qpair, trid, 0); 2792 } 2793 2794 static int 2795 spdk_nvmf_tcp_qpair_get_peer_trid(struct spdk_nvmf_qpair *qpair, 2796 struct spdk_nvme_transport_id *trid) 2797 { 2798 return spdk_nvmf_tcp_qpair_get_trid(qpair, trid, 1); 2799 } 2800 2801 static int 2802 spdk_nvmf_tcp_qpair_get_listen_trid(struct spdk_nvmf_qpair *qpair, 2803 struct spdk_nvme_transport_id *trid) 2804 { 2805 return spdk_nvmf_tcp_qpair_get_trid(qpair, trid, 0); 2806 } 2807 2808 static int 2809 spdk_nvmf_tcp_qpair_set_sq_size(struct spdk_nvmf_qpair *qpair) 2810 { 2811 struct spdk_nvmf_tcp_qpair *tqpair; 2812 int rc; 2813 tqpair = SPDK_CONTAINEROF(qpair, struct spdk_nvmf_tcp_qpair, qpair); 2814 2815 rc = spdk_nvmf_tcp_qpair_init_mem_resource(tqpair, tqpair->qpair.sq_head_max); 2816 if (!rc) { 2817 tqpair->max_queue_depth += tqpair->qpair.sq_head_max; 2818 tqpair->free_pdu_num += tqpair->qpair.sq_head_max; 2819 tqpair->state_cntr[TCP_REQUEST_STATE_FREE] += tqpair->qpair.sq_head_max; 2820 SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "The queue depth=%u for tqpair=%p\n", 2821 tqpair->max_queue_depth, tqpair); 2822 } 2823 2824 return rc; 2825 2826 } 2827 2828 #define SPDK_NVMF_TCP_DEFAULT_MAX_QUEUE_DEPTH 128 2829 #define SPDK_NVMF_TCP_DEFAULT_AQ_DEPTH 128 2830 #define SPDK_NVMF_TCP_DEFAULT_MAX_QPAIRS_PER_CTRLR 64 2831 #define SPDK_NVMF_TCP_DEFAULT_IN_CAPSULE_DATA_SIZE 4096 2832 #define SPDK_NVMF_TCP_DEFAULT_MAX_IO_SIZE 131072 2833 #define SPDK_NVMF_TCP_DEFAULT_IO_UNIT_SIZE 131072 2834 #define SPDK_NVMF_TCP_DEFAULT_NUM_SHARED_BUFFERS 512 2835 #define SPDK_NVMF_TCP_DEFAULT_BUFFER_CACHE_SIZE 32 2836 2837 static void 2838 spdk_nvmf_tcp_opts_init(struct spdk_nvmf_transport_opts *opts) 2839 { 2840 opts->max_queue_depth = SPDK_NVMF_TCP_DEFAULT_MAX_QUEUE_DEPTH; 2841 opts->max_qpairs_per_ctrlr = SPDK_NVMF_TCP_DEFAULT_MAX_QPAIRS_PER_CTRLR; 2842 opts->in_capsule_data_size = SPDK_NVMF_TCP_DEFAULT_IN_CAPSULE_DATA_SIZE; 2843 opts->max_io_size = SPDK_NVMF_TCP_DEFAULT_MAX_IO_SIZE; 2844 opts->io_unit_size = SPDK_NVMF_TCP_DEFAULT_IO_UNIT_SIZE; 2845 opts->max_aq_depth = SPDK_NVMF_TCP_DEFAULT_AQ_DEPTH; 2846 opts->num_shared_buffers = SPDK_NVMF_TCP_DEFAULT_NUM_SHARED_BUFFERS; 2847 opts->buf_cache_size = SPDK_NVMF_TCP_DEFAULT_BUFFER_CACHE_SIZE; 2848 } 2849 2850 const struct spdk_nvmf_transport_ops spdk_nvmf_transport_tcp = { 2851 .type = SPDK_NVME_TRANSPORT_TCP, 2852 .opts_init = spdk_nvmf_tcp_opts_init, 2853 .create = spdk_nvmf_tcp_create, 2854 .destroy = spdk_nvmf_tcp_destroy, 2855 2856 .listen = spdk_nvmf_tcp_listen, 2857 .stop_listen = spdk_nvmf_tcp_stop_listen, 2858 .accept = spdk_nvmf_tcp_accept, 2859 2860 .listener_discover = spdk_nvmf_tcp_discover, 2861 2862 .poll_group_create = spdk_nvmf_tcp_poll_group_create, 2863 .poll_group_destroy = spdk_nvmf_tcp_poll_group_destroy, 2864 .poll_group_add = spdk_nvmf_tcp_poll_group_add, 2865 .poll_group_remove = spdk_nvmf_tcp_poll_group_remove, 2866 .poll_group_poll = spdk_nvmf_tcp_poll_group_poll, 2867 2868 .req_free = spdk_nvmf_tcp_req_free, 2869 .req_complete = spdk_nvmf_tcp_req_complete, 2870 2871 .qpair_fini = spdk_nvmf_tcp_close_qpair, 2872 .qpair_is_idle = spdk_nvmf_tcp_qpair_is_idle, 2873 .qpair_get_local_trid = spdk_nvmf_tcp_qpair_get_local_trid, 2874 .qpair_get_peer_trid = spdk_nvmf_tcp_qpair_get_peer_trid, 2875 .qpair_get_listen_trid = spdk_nvmf_tcp_qpair_get_listen_trid, 2876 .qpair_set_sqsize = spdk_nvmf_tcp_qpair_set_sq_size, 2877 }; 2878 2879 SPDK_LOG_REGISTER_COMPONENT("nvmf_tcp", SPDK_LOG_NVMF_TCP) 2880