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