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