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