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 36 #include <infiniband/verbs.h> 37 #include <rdma/rdma_cma.h> 38 #include <rdma/rdma_verbs.h> 39 40 #include "spdk/config.h" 41 #include "spdk/thread.h" 42 #include "spdk/likely.h" 43 #include "spdk/nvmf_transport.h" 44 #include "spdk/string.h" 45 #include "spdk/trace.h" 46 #include "spdk/util.h" 47 48 #include "spdk_internal/assert.h" 49 #include "spdk_internal/log.h" 50 51 struct spdk_nvme_rdma_hooks g_nvmf_hooks = {}; 52 const struct spdk_nvmf_transport_ops spdk_nvmf_transport_rdma; 53 54 /* 55 RDMA Connection Resource Defaults 56 */ 57 #define NVMF_DEFAULT_TX_SGE SPDK_NVMF_MAX_SGL_ENTRIES 58 #define NVMF_DEFAULT_RSP_SGE 1 59 #define NVMF_DEFAULT_RX_SGE 2 60 61 /* The RDMA completion queue size */ 62 #define DEFAULT_NVMF_RDMA_CQ_SIZE 4096 63 #define MAX_WR_PER_QP(queue_depth) (queue_depth * 3 + 2) 64 65 /* Timeout for destroying defunct rqpairs */ 66 #define NVMF_RDMA_QPAIR_DESTROY_TIMEOUT_US 4000000 67 68 static int g_spdk_nvmf_ibv_query_mask = 69 IBV_QP_STATE | 70 IBV_QP_PKEY_INDEX | 71 IBV_QP_PORT | 72 IBV_QP_ACCESS_FLAGS | 73 IBV_QP_AV | 74 IBV_QP_PATH_MTU | 75 IBV_QP_DEST_QPN | 76 IBV_QP_RQ_PSN | 77 IBV_QP_MAX_DEST_RD_ATOMIC | 78 IBV_QP_MIN_RNR_TIMER | 79 IBV_QP_SQ_PSN | 80 IBV_QP_TIMEOUT | 81 IBV_QP_RETRY_CNT | 82 IBV_QP_RNR_RETRY | 83 IBV_QP_MAX_QP_RD_ATOMIC; 84 85 enum spdk_nvmf_rdma_request_state { 86 /* The request is not currently in use */ 87 RDMA_REQUEST_STATE_FREE = 0, 88 89 /* Initial state when request first received */ 90 RDMA_REQUEST_STATE_NEW, 91 92 /* The request is queued until a data buffer is available. */ 93 RDMA_REQUEST_STATE_NEED_BUFFER, 94 95 /* The request is waiting on RDMA queue depth availability 96 * to transfer data from the host to the controller. 97 */ 98 RDMA_REQUEST_STATE_DATA_TRANSFER_TO_CONTROLLER_PENDING, 99 100 /* The request is currently transferring data from the host to the controller. */ 101 RDMA_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER, 102 103 /* The request is ready to execute at the block device */ 104 RDMA_REQUEST_STATE_READY_TO_EXECUTE, 105 106 /* The request is currently executing at the block device */ 107 RDMA_REQUEST_STATE_EXECUTING, 108 109 /* The request finished executing at the block device */ 110 RDMA_REQUEST_STATE_EXECUTED, 111 112 /* The request is waiting on RDMA queue depth availability 113 * to transfer data from the controller to the host. 114 */ 115 RDMA_REQUEST_STATE_DATA_TRANSFER_TO_HOST_PENDING, 116 117 /* The request is ready to send a completion */ 118 RDMA_REQUEST_STATE_READY_TO_COMPLETE, 119 120 /* The request is currently transferring data from the controller to the host. */ 121 RDMA_REQUEST_STATE_TRANSFERRING_CONTROLLER_TO_HOST, 122 123 /* The request currently has an outstanding completion without an 124 * associated data transfer. 125 */ 126 RDMA_REQUEST_STATE_COMPLETING, 127 128 /* The request completed and can be marked free. */ 129 RDMA_REQUEST_STATE_COMPLETED, 130 131 /* Terminator */ 132 RDMA_REQUEST_NUM_STATES, 133 }; 134 135 #define OBJECT_NVMF_RDMA_IO 0x40 136 137 #define TRACE_GROUP_NVMF_RDMA 0x4 138 #define TRACE_RDMA_REQUEST_STATE_NEW SPDK_TPOINT_ID(TRACE_GROUP_NVMF_RDMA, 0x0) 139 #define TRACE_RDMA_REQUEST_STATE_NEED_BUFFER SPDK_TPOINT_ID(TRACE_GROUP_NVMF_RDMA, 0x1) 140 #define TRACE_RDMA_REQUEST_STATE_DATA_TRANSFER_TO_CONTROLLER_PENDING SPDK_TPOINT_ID(TRACE_GROUP_NVMF_RDMA, 0x2) 141 #define TRACE_RDMA_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER SPDK_TPOINT_ID(TRACE_GROUP_NVMF_RDMA, 0x3) 142 #define TRACE_RDMA_REQUEST_STATE_READY_TO_EXECUTE SPDK_TPOINT_ID(TRACE_GROUP_NVMF_RDMA, 0x4) 143 #define TRACE_RDMA_REQUEST_STATE_EXECUTING SPDK_TPOINT_ID(TRACE_GROUP_NVMF_RDMA, 0x5) 144 #define TRACE_RDMA_REQUEST_STATE_EXECUTED SPDK_TPOINT_ID(TRACE_GROUP_NVMF_RDMA, 0x6) 145 #define TRACE_RDMA_REQUEST_STATE_DATA_TRANSFER_TO_HOST_PENDING SPDK_TPOINT_ID(TRACE_GROUP_NVMF_RDMA, 0x7) 146 #define TRACE_RDMA_REQUEST_STATE_READY_TO_COMPLETE SPDK_TPOINT_ID(TRACE_GROUP_NVMF_RDMA, 0x8) 147 #define TRACE_RDMA_REQUEST_STATE_TRANSFERRING_CONTROLLER_TO_HOST SPDK_TPOINT_ID(TRACE_GROUP_NVMF_RDMA, 0x9) 148 #define TRACE_RDMA_REQUEST_STATE_COMPLETING SPDK_TPOINT_ID(TRACE_GROUP_NVMF_RDMA, 0xA) 149 #define TRACE_RDMA_REQUEST_STATE_COMPLETED SPDK_TPOINT_ID(TRACE_GROUP_NVMF_RDMA, 0xB) 150 #define TRACE_RDMA_QP_CREATE SPDK_TPOINT_ID(TRACE_GROUP_NVMF_RDMA, 0xC) 151 #define TRACE_RDMA_IBV_ASYNC_EVENT SPDK_TPOINT_ID(TRACE_GROUP_NVMF_RDMA, 0xD) 152 #define TRACE_RDMA_CM_ASYNC_EVENT SPDK_TPOINT_ID(TRACE_GROUP_NVMF_RDMA, 0xE) 153 #define TRACE_RDMA_QP_STATE_CHANGE SPDK_TPOINT_ID(TRACE_GROUP_NVMF_RDMA, 0xF) 154 #define TRACE_RDMA_QP_DISCONNECT SPDK_TPOINT_ID(TRACE_GROUP_NVMF_RDMA, 0x10) 155 #define TRACE_RDMA_QP_DESTROY SPDK_TPOINT_ID(TRACE_GROUP_NVMF_RDMA, 0x11) 156 157 SPDK_TRACE_REGISTER_FN(nvmf_trace, "nvmf_rdma", TRACE_GROUP_NVMF_RDMA) 158 { 159 spdk_trace_register_object(OBJECT_NVMF_RDMA_IO, 'r'); 160 spdk_trace_register_description("RDMA_REQ_NEW", TRACE_RDMA_REQUEST_STATE_NEW, 161 OWNER_NONE, OBJECT_NVMF_RDMA_IO, 1, 1, "cmid: "); 162 spdk_trace_register_description("RDMA_REQ_NEED_BUFFER", TRACE_RDMA_REQUEST_STATE_NEED_BUFFER, 163 OWNER_NONE, OBJECT_NVMF_RDMA_IO, 0, 1, "cmid: "); 164 spdk_trace_register_description("RDMA_REQ_TX_PENDING_C2H", 165 TRACE_RDMA_REQUEST_STATE_DATA_TRANSFER_TO_HOST_PENDING, 166 OWNER_NONE, OBJECT_NVMF_RDMA_IO, 0, 1, "cmid: "); 167 spdk_trace_register_description("RDMA_REQ_TX_PENDING_H2C", 168 TRACE_RDMA_REQUEST_STATE_DATA_TRANSFER_TO_CONTROLLER_PENDING, 169 OWNER_NONE, OBJECT_NVMF_RDMA_IO, 0, 1, "cmid: "); 170 spdk_trace_register_description("RDMA_REQ_TX_H2C", 171 TRACE_RDMA_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER, 172 OWNER_NONE, OBJECT_NVMF_RDMA_IO, 0, 1, "cmid: "); 173 spdk_trace_register_description("RDMA_REQ_RDY_TO_EXECUTE", 174 TRACE_RDMA_REQUEST_STATE_READY_TO_EXECUTE, 175 OWNER_NONE, OBJECT_NVMF_RDMA_IO, 0, 1, "cmid: "); 176 spdk_trace_register_description("RDMA_REQ_EXECUTING", 177 TRACE_RDMA_REQUEST_STATE_EXECUTING, 178 OWNER_NONE, OBJECT_NVMF_RDMA_IO, 0, 1, "cmid: "); 179 spdk_trace_register_description("RDMA_REQ_EXECUTED", 180 TRACE_RDMA_REQUEST_STATE_EXECUTED, 181 OWNER_NONE, OBJECT_NVMF_RDMA_IO, 0, 1, "cmid: "); 182 spdk_trace_register_description("RDMA_REQ_RDY_TO_COMPL", 183 TRACE_RDMA_REQUEST_STATE_READY_TO_COMPLETE, 184 OWNER_NONE, OBJECT_NVMF_RDMA_IO, 0, 1, "cmid: "); 185 spdk_trace_register_description("RDMA_REQ_COMPLETING_C2H", 186 TRACE_RDMA_REQUEST_STATE_TRANSFERRING_CONTROLLER_TO_HOST, 187 OWNER_NONE, OBJECT_NVMF_RDMA_IO, 0, 1, "cmid: "); 188 spdk_trace_register_description("RDMA_REQ_COMPLETING", 189 TRACE_RDMA_REQUEST_STATE_COMPLETING, 190 OWNER_NONE, OBJECT_NVMF_RDMA_IO, 0, 1, "cmid: "); 191 spdk_trace_register_description("RDMA_REQ_COMPLETED", 192 TRACE_RDMA_REQUEST_STATE_COMPLETED, 193 OWNER_NONE, OBJECT_NVMF_RDMA_IO, 0, 1, "cmid: "); 194 195 spdk_trace_register_description("RDMA_QP_CREATE", TRACE_RDMA_QP_CREATE, 196 OWNER_NONE, OBJECT_NONE, 0, 0, ""); 197 spdk_trace_register_description("RDMA_IBV_ASYNC_EVENT", TRACE_RDMA_IBV_ASYNC_EVENT, 198 OWNER_NONE, OBJECT_NONE, 0, 0, "type: "); 199 spdk_trace_register_description("RDMA_CM_ASYNC_EVENT", TRACE_RDMA_CM_ASYNC_EVENT, 200 OWNER_NONE, OBJECT_NONE, 0, 0, "type: "); 201 spdk_trace_register_description("RDMA_QP_STATE_CHANGE", TRACE_RDMA_QP_STATE_CHANGE, 202 OWNER_NONE, OBJECT_NONE, 0, 1, "state: "); 203 spdk_trace_register_description("RDMA_QP_DISCONNECT", TRACE_RDMA_QP_DISCONNECT, 204 OWNER_NONE, OBJECT_NONE, 0, 0, ""); 205 spdk_trace_register_description("RDMA_QP_DESTROY", TRACE_RDMA_QP_DESTROY, 206 OWNER_NONE, OBJECT_NONE, 0, 0, ""); 207 } 208 209 enum spdk_nvmf_rdma_wr_type { 210 RDMA_WR_TYPE_RECV, 211 RDMA_WR_TYPE_SEND, 212 RDMA_WR_TYPE_DATA, 213 }; 214 215 struct spdk_nvmf_rdma_wr { 216 enum spdk_nvmf_rdma_wr_type type; 217 }; 218 219 /* This structure holds commands as they are received off the wire. 220 * It must be dynamically paired with a full request object 221 * (spdk_nvmf_rdma_request) to service a request. It is separate 222 * from the request because RDMA does not appear to order 223 * completions, so occasionally we'll get a new incoming 224 * command when there aren't any free request objects. 225 */ 226 struct spdk_nvmf_rdma_recv { 227 struct ibv_recv_wr wr; 228 struct ibv_sge sgl[NVMF_DEFAULT_RX_SGE]; 229 230 struct spdk_nvmf_rdma_qpair *qpair; 231 232 /* In-capsule data buffer */ 233 uint8_t *buf; 234 235 struct spdk_nvmf_rdma_wr rdma_wr; 236 uint64_t receive_tsc; 237 238 STAILQ_ENTRY(spdk_nvmf_rdma_recv) link; 239 }; 240 241 struct spdk_nvmf_rdma_request_data { 242 struct spdk_nvmf_rdma_wr rdma_wr; 243 struct ibv_send_wr wr; 244 struct ibv_sge sgl[SPDK_NVMF_MAX_SGL_ENTRIES]; 245 }; 246 247 struct spdk_nvmf_rdma_request { 248 struct spdk_nvmf_request req; 249 250 enum spdk_nvmf_rdma_request_state state; 251 252 struct spdk_nvmf_rdma_recv *recv; 253 254 struct { 255 struct spdk_nvmf_rdma_wr rdma_wr; 256 struct ibv_send_wr wr; 257 struct ibv_sge sgl[NVMF_DEFAULT_RSP_SGE]; 258 } rsp; 259 260 struct spdk_nvmf_rdma_request_data data; 261 262 uint32_t iovpos; 263 264 uint32_t num_outstanding_data_wr; 265 uint64_t receive_tsc; 266 267 STAILQ_ENTRY(spdk_nvmf_rdma_request) state_link; 268 }; 269 270 enum spdk_nvmf_rdma_qpair_disconnect_flags { 271 RDMA_QP_DISCONNECTING = 1, 272 RDMA_QP_RECV_DRAINED = 1 << 1, 273 RDMA_QP_SEND_DRAINED = 1 << 2 274 }; 275 276 struct spdk_nvmf_rdma_resource_opts { 277 struct spdk_nvmf_rdma_qpair *qpair; 278 /* qp points either to an ibv_qp object or an ibv_srq object depending on the value of shared. */ 279 void *qp; 280 struct ibv_pd *pd; 281 uint32_t max_queue_depth; 282 uint32_t in_capsule_data_size; 283 bool shared; 284 }; 285 286 struct spdk_nvmf_send_wr_list { 287 struct ibv_send_wr *first; 288 struct ibv_send_wr *last; 289 }; 290 291 struct spdk_nvmf_recv_wr_list { 292 struct ibv_recv_wr *first; 293 struct ibv_recv_wr *last; 294 }; 295 296 struct spdk_nvmf_rdma_resources { 297 /* Array of size "max_queue_depth" containing RDMA requests. */ 298 struct spdk_nvmf_rdma_request *reqs; 299 300 /* Array of size "max_queue_depth" containing RDMA recvs. */ 301 struct spdk_nvmf_rdma_recv *recvs; 302 303 /* Array of size "max_queue_depth" containing 64 byte capsules 304 * used for receive. 305 */ 306 union nvmf_h2c_msg *cmds; 307 struct ibv_mr *cmds_mr; 308 309 /* Array of size "max_queue_depth" containing 16 byte completions 310 * to be sent back to the user. 311 */ 312 union nvmf_c2h_msg *cpls; 313 struct ibv_mr *cpls_mr; 314 315 /* Array of size "max_queue_depth * InCapsuleDataSize" containing 316 * buffers to be used for in capsule data. 317 */ 318 void *bufs; 319 struct ibv_mr *bufs_mr; 320 321 /* The list of pending recvs to transfer */ 322 struct spdk_nvmf_recv_wr_list recvs_to_post; 323 324 /* Receives that are waiting for a request object */ 325 STAILQ_HEAD(, spdk_nvmf_rdma_recv) incoming_queue; 326 327 /* Queue to track free requests */ 328 STAILQ_HEAD(, spdk_nvmf_rdma_request) free_queue; 329 }; 330 331 typedef void (*spdk_nvmf_rdma_qpair_ibv_event)(struct spdk_nvmf_rdma_qpair *rqpair); 332 333 struct spdk_nvmf_rdma_ibv_event_ctx { 334 struct spdk_nvmf_rdma_qpair *rqpair; 335 spdk_nvmf_rdma_qpair_ibv_event cb_fn; 336 /* Link to other ibv events associated with this qpair */ 337 STAILQ_ENTRY(spdk_nvmf_rdma_ibv_event_ctx) link; 338 }; 339 340 struct spdk_nvmf_rdma_qpair { 341 struct spdk_nvmf_qpair qpair; 342 343 struct spdk_nvmf_rdma_device *device; 344 struct spdk_nvmf_rdma_poller *poller; 345 346 struct rdma_cm_id *cm_id; 347 struct ibv_srq *srq; 348 struct rdma_cm_id *listen_id; 349 350 /* The maximum number of I/O outstanding on this connection at one time */ 351 uint16_t max_queue_depth; 352 353 /* The maximum number of active RDMA READ and ATOMIC operations at one time */ 354 uint16_t max_read_depth; 355 356 /* The maximum number of RDMA SEND operations at one time */ 357 uint32_t max_send_depth; 358 359 /* The current number of outstanding WRs from this qpair's 360 * recv queue. Should not exceed device->attr.max_queue_depth. 361 */ 362 uint16_t current_recv_depth; 363 364 /* The current number of active RDMA READ operations */ 365 uint16_t current_read_depth; 366 367 /* The current number of posted WRs from this qpair's 368 * send queue. Should not exceed max_send_depth. 369 */ 370 uint32_t current_send_depth; 371 372 /* The maximum number of SGEs per WR on the send queue */ 373 uint32_t max_send_sge; 374 375 /* The maximum number of SGEs per WR on the recv queue */ 376 uint32_t max_recv_sge; 377 378 /* The list of pending send requests for a transfer */ 379 struct spdk_nvmf_send_wr_list sends_to_post; 380 381 struct spdk_nvmf_rdma_resources *resources; 382 383 STAILQ_HEAD(, spdk_nvmf_rdma_request) pending_rdma_read_queue; 384 385 STAILQ_HEAD(, spdk_nvmf_rdma_request) pending_rdma_write_queue; 386 387 /* Number of requests not in the free state */ 388 uint32_t qd; 389 390 TAILQ_ENTRY(spdk_nvmf_rdma_qpair) link; 391 392 STAILQ_ENTRY(spdk_nvmf_rdma_qpair) recv_link; 393 394 STAILQ_ENTRY(spdk_nvmf_rdma_qpair) send_link; 395 396 /* IBV queue pair attributes: they are used to manage 397 * qp state and recover from errors. 398 */ 399 enum ibv_qp_state ibv_state; 400 401 uint32_t disconnect_flags; 402 403 /* Poller registered in case the qpair doesn't properly 404 * complete the qpair destruct process and becomes defunct. 405 */ 406 407 struct spdk_poller *destruct_poller; 408 409 /* List of ibv async events */ 410 STAILQ_HEAD(, spdk_nvmf_rdma_ibv_event_ctx) ibv_events; 411 412 /* There are several ways a disconnect can start on a qpair 413 * and they are not all mutually exclusive. It is important 414 * that we only initialize one of these paths. 415 */ 416 bool disconnect_started; 417 /* Lets us know that we have received the last_wqe event. */ 418 bool last_wqe_reached; 419 }; 420 421 struct spdk_nvmf_rdma_poller_stat { 422 uint64_t completions; 423 uint64_t polls; 424 uint64_t requests; 425 uint64_t request_latency; 426 uint64_t pending_free_request; 427 uint64_t pending_rdma_read; 428 uint64_t pending_rdma_write; 429 }; 430 431 struct spdk_nvmf_rdma_poller { 432 struct spdk_nvmf_rdma_device *device; 433 struct spdk_nvmf_rdma_poll_group *group; 434 435 int num_cqe; 436 int required_num_wr; 437 struct ibv_cq *cq; 438 439 /* The maximum number of I/O outstanding on the shared receive queue at one time */ 440 uint16_t max_srq_depth; 441 442 /* Shared receive queue */ 443 struct ibv_srq *srq; 444 445 struct spdk_nvmf_rdma_resources *resources; 446 struct spdk_nvmf_rdma_poller_stat stat; 447 448 TAILQ_HEAD(, spdk_nvmf_rdma_qpair) qpairs; 449 450 STAILQ_HEAD(, spdk_nvmf_rdma_qpair) qpairs_pending_recv; 451 452 STAILQ_HEAD(, spdk_nvmf_rdma_qpair) qpairs_pending_send; 453 454 TAILQ_ENTRY(spdk_nvmf_rdma_poller) link; 455 }; 456 457 struct spdk_nvmf_rdma_poll_group_stat { 458 uint64_t pending_data_buffer; 459 }; 460 461 struct spdk_nvmf_rdma_poll_group { 462 struct spdk_nvmf_transport_poll_group group; 463 struct spdk_nvmf_rdma_poll_group_stat stat; 464 TAILQ_HEAD(, spdk_nvmf_rdma_poller) pollers; 465 TAILQ_ENTRY(spdk_nvmf_rdma_poll_group) link; 466 /* 467 * buffers which are split across multiple RDMA 468 * memory regions cannot be used by this transport. 469 */ 470 STAILQ_HEAD(, spdk_nvmf_transport_pg_cache_buf) retired_bufs; 471 }; 472 473 struct spdk_nvmf_rdma_conn_sched { 474 struct spdk_nvmf_rdma_poll_group *next_admin_pg; 475 struct spdk_nvmf_rdma_poll_group *next_io_pg; 476 }; 477 478 /* Assuming rdma_cm uses just one protection domain per ibv_context. */ 479 struct spdk_nvmf_rdma_device { 480 struct ibv_device_attr attr; 481 struct ibv_context *context; 482 483 struct spdk_mem_map *map; 484 struct ibv_pd *pd; 485 486 int num_srq; 487 488 TAILQ_ENTRY(spdk_nvmf_rdma_device) link; 489 }; 490 491 struct spdk_nvmf_rdma_port { 492 const struct spdk_nvme_transport_id *trid; 493 struct rdma_cm_id *id; 494 struct spdk_nvmf_rdma_device *device; 495 TAILQ_ENTRY(spdk_nvmf_rdma_port) link; 496 }; 497 498 struct spdk_nvmf_rdma_transport { 499 struct spdk_nvmf_transport transport; 500 501 struct spdk_nvmf_rdma_conn_sched conn_sched; 502 503 struct rdma_event_channel *event_channel; 504 505 struct spdk_mempool *data_wr_pool; 506 507 pthread_mutex_t lock; 508 509 /* fields used to poll RDMA/IB events */ 510 nfds_t npoll_fds; 511 struct pollfd *poll_fds; 512 513 TAILQ_HEAD(, spdk_nvmf_rdma_device) devices; 514 TAILQ_HEAD(, spdk_nvmf_rdma_port) ports; 515 TAILQ_HEAD(, spdk_nvmf_rdma_poll_group) poll_groups; 516 }; 517 518 static inline void 519 spdk_nvmf_rdma_start_disconnect(struct spdk_nvmf_rdma_qpair *rqpair); 520 521 static bool 522 spdk_nvmf_rdma_request_process(struct spdk_nvmf_rdma_transport *rtransport, 523 struct spdk_nvmf_rdma_request *rdma_req); 524 525 static inline int 526 spdk_nvmf_rdma_check_ibv_state(enum ibv_qp_state state) 527 { 528 switch (state) { 529 case IBV_QPS_RESET: 530 case IBV_QPS_INIT: 531 case IBV_QPS_RTR: 532 case IBV_QPS_RTS: 533 case IBV_QPS_SQD: 534 case IBV_QPS_SQE: 535 case IBV_QPS_ERR: 536 return 0; 537 default: 538 return -1; 539 } 540 } 541 542 static inline enum spdk_nvme_media_error_status_code 543 spdk_nvmf_rdma_dif_error_to_compl_status(uint8_t err_type) { 544 enum spdk_nvme_media_error_status_code result; 545 switch (err_type) 546 { 547 case SPDK_DIF_REFTAG_ERROR: 548 result = SPDK_NVME_SC_REFERENCE_TAG_CHECK_ERROR; 549 break; 550 case SPDK_DIF_APPTAG_ERROR: 551 result = SPDK_NVME_SC_APPLICATION_TAG_CHECK_ERROR; 552 break; 553 case SPDK_DIF_GUARD_ERROR: 554 result = SPDK_NVME_SC_GUARD_CHECK_ERROR; 555 break; 556 default: 557 SPDK_UNREACHABLE(); 558 } 559 560 return result; 561 } 562 563 static enum ibv_qp_state 564 spdk_nvmf_rdma_update_ibv_state(struct spdk_nvmf_rdma_qpair *rqpair) { 565 enum ibv_qp_state old_state, new_state; 566 struct ibv_qp_attr qp_attr; 567 struct ibv_qp_init_attr init_attr; 568 int rc; 569 570 old_state = rqpair->ibv_state; 571 rc = ibv_query_qp(rqpair->cm_id->qp, &qp_attr, 572 g_spdk_nvmf_ibv_query_mask, &init_attr); 573 574 if (rc) 575 { 576 SPDK_ERRLOG("Failed to get updated RDMA queue pair state!\n"); 577 return IBV_QPS_ERR + 1; 578 } 579 580 new_state = qp_attr.qp_state; 581 rqpair->ibv_state = new_state; 582 qp_attr.ah_attr.port_num = qp_attr.port_num; 583 584 rc = spdk_nvmf_rdma_check_ibv_state(new_state); 585 if (rc) 586 { 587 SPDK_ERRLOG("QP#%d: bad state updated: %u, maybe hardware issue\n", rqpair->qpair.qid, new_state); 588 /* 589 * IBV_QPS_UNKNOWN undefined if lib version smaller than libibverbs-1.1.8 590 * IBV_QPS_UNKNOWN is the enum element after IBV_QPS_ERR 591 */ 592 return IBV_QPS_ERR + 1; 593 } 594 595 if (old_state != new_state) 596 { 597 spdk_trace_record(TRACE_RDMA_QP_STATE_CHANGE, 0, 0, 598 (uintptr_t)rqpair->cm_id, new_state); 599 } 600 return new_state; 601 } 602 603 static const char *str_ibv_qp_state[] = { 604 "IBV_QPS_RESET", 605 "IBV_QPS_INIT", 606 "IBV_QPS_RTR", 607 "IBV_QPS_RTS", 608 "IBV_QPS_SQD", 609 "IBV_QPS_SQE", 610 "IBV_QPS_ERR", 611 "IBV_QPS_UNKNOWN" 612 }; 613 614 static int 615 spdk_nvmf_rdma_set_ibv_state(struct spdk_nvmf_rdma_qpair *rqpair, 616 enum ibv_qp_state new_state) 617 { 618 struct ibv_qp_attr qp_attr; 619 struct ibv_qp_init_attr init_attr; 620 int rc; 621 enum ibv_qp_state state; 622 static int attr_mask_rc[] = { 623 [IBV_QPS_RESET] = IBV_QP_STATE, 624 [IBV_QPS_INIT] = (IBV_QP_STATE | 625 IBV_QP_PKEY_INDEX | 626 IBV_QP_PORT | 627 IBV_QP_ACCESS_FLAGS), 628 [IBV_QPS_RTR] = (IBV_QP_STATE | 629 IBV_QP_AV | 630 IBV_QP_PATH_MTU | 631 IBV_QP_DEST_QPN | 632 IBV_QP_RQ_PSN | 633 IBV_QP_MAX_DEST_RD_ATOMIC | 634 IBV_QP_MIN_RNR_TIMER), 635 [IBV_QPS_RTS] = (IBV_QP_STATE | 636 IBV_QP_SQ_PSN | 637 IBV_QP_TIMEOUT | 638 IBV_QP_RETRY_CNT | 639 IBV_QP_RNR_RETRY | 640 IBV_QP_MAX_QP_RD_ATOMIC), 641 [IBV_QPS_SQD] = IBV_QP_STATE, 642 [IBV_QPS_SQE] = IBV_QP_STATE, 643 [IBV_QPS_ERR] = IBV_QP_STATE, 644 }; 645 646 rc = spdk_nvmf_rdma_check_ibv_state(new_state); 647 if (rc) { 648 SPDK_ERRLOG("QP#%d: bad state requested: %u\n", 649 rqpair->qpair.qid, new_state); 650 return rc; 651 } 652 653 rc = ibv_query_qp(rqpair->cm_id->qp, &qp_attr, 654 g_spdk_nvmf_ibv_query_mask, &init_attr); 655 656 if (rc) { 657 SPDK_ERRLOG("Failed to get updated RDMA queue pair state!\n"); 658 } 659 660 qp_attr.cur_qp_state = rqpair->ibv_state; 661 qp_attr.qp_state = new_state; 662 663 rc = ibv_modify_qp(rqpair->cm_id->qp, &qp_attr, 664 attr_mask_rc[new_state]); 665 666 if (rc) { 667 SPDK_ERRLOG("QP#%d: failed to set state to: %s, %d (%s)\n", 668 rqpair->qpair.qid, str_ibv_qp_state[new_state], errno, strerror(errno)); 669 return rc; 670 } 671 672 state = spdk_nvmf_rdma_update_ibv_state(rqpair); 673 674 if (state != new_state) { 675 SPDK_ERRLOG("QP#%d: expected state: %s, actual state: %s\n", 676 rqpair->qpair.qid, str_ibv_qp_state[new_state], 677 str_ibv_qp_state[state]); 678 return -1; 679 } 680 SPDK_DEBUGLOG(SPDK_LOG_RDMA, "IBV QP#%u changed to: %s\n", rqpair->qpair.qid, 681 str_ibv_qp_state[state]); 682 return 0; 683 } 684 685 static void 686 nvmf_rdma_request_free_data(struct spdk_nvmf_rdma_request *rdma_req, 687 struct spdk_nvmf_rdma_transport *rtransport) 688 { 689 struct spdk_nvmf_rdma_request_data *data_wr; 690 struct ibv_send_wr *next_send_wr; 691 uint64_t req_wrid; 692 693 rdma_req->num_outstanding_data_wr = 0; 694 data_wr = &rdma_req->data; 695 req_wrid = data_wr->wr.wr_id; 696 while (data_wr && data_wr->wr.wr_id == req_wrid) { 697 memset(data_wr->sgl, 0, sizeof(data_wr->wr.sg_list[0]) * data_wr->wr.num_sge); 698 data_wr->wr.num_sge = 0; 699 next_send_wr = data_wr->wr.next; 700 if (data_wr != &rdma_req->data) { 701 spdk_mempool_put(rtransport->data_wr_pool, data_wr); 702 } 703 data_wr = (!next_send_wr || next_send_wr == &rdma_req->rsp.wr) ? NULL : 704 SPDK_CONTAINEROF(next_send_wr, struct spdk_nvmf_rdma_request_data, wr); 705 } 706 } 707 708 static void 709 nvmf_rdma_dump_request(struct spdk_nvmf_rdma_request *req) 710 { 711 SPDK_ERRLOG("\t\tRequest Data From Pool: %d\n", req->req.data_from_pool); 712 if (req->req.cmd) { 713 SPDK_ERRLOG("\t\tRequest opcode: %d\n", req->req.cmd->nvmf_cmd.opcode); 714 } 715 if (req->recv) { 716 SPDK_ERRLOG("\t\tRequest recv wr_id%lu\n", req->recv->wr.wr_id); 717 } 718 } 719 720 static void 721 nvmf_rdma_dump_qpair_contents(struct spdk_nvmf_rdma_qpair *rqpair) 722 { 723 int i; 724 725 SPDK_ERRLOG("Dumping contents of queue pair (QID %d)\n", rqpair->qpair.qid); 726 for (i = 0; i < rqpair->max_queue_depth; i++) { 727 if (rqpair->resources->reqs[i].state != RDMA_REQUEST_STATE_FREE) { 728 nvmf_rdma_dump_request(&rqpair->resources->reqs[i]); 729 } 730 } 731 } 732 733 static void 734 nvmf_rdma_resources_destroy(struct spdk_nvmf_rdma_resources *resources) 735 { 736 if (resources->cmds_mr) { 737 ibv_dereg_mr(resources->cmds_mr); 738 } 739 740 if (resources->cpls_mr) { 741 ibv_dereg_mr(resources->cpls_mr); 742 } 743 744 if (resources->bufs_mr) { 745 ibv_dereg_mr(resources->bufs_mr); 746 } 747 748 spdk_free(resources->cmds); 749 spdk_free(resources->cpls); 750 spdk_free(resources->bufs); 751 free(resources->reqs); 752 free(resources->recvs); 753 free(resources); 754 } 755 756 757 static struct spdk_nvmf_rdma_resources * 758 nvmf_rdma_resources_create(struct spdk_nvmf_rdma_resource_opts *opts) 759 { 760 struct spdk_nvmf_rdma_resources *resources; 761 struct spdk_nvmf_rdma_request *rdma_req; 762 struct spdk_nvmf_rdma_recv *rdma_recv; 763 struct ibv_qp *qp; 764 struct ibv_srq *srq; 765 uint32_t i; 766 int rc; 767 768 resources = calloc(1, sizeof(struct spdk_nvmf_rdma_resources)); 769 if (!resources) { 770 SPDK_ERRLOG("Unable to allocate resources for receive queue.\n"); 771 return NULL; 772 } 773 774 resources->reqs = calloc(opts->max_queue_depth, sizeof(*resources->reqs)); 775 resources->recvs = calloc(opts->max_queue_depth, sizeof(*resources->recvs)); 776 resources->cmds = spdk_zmalloc(opts->max_queue_depth * sizeof(*resources->cmds), 777 0x1000, NULL, SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA); 778 resources->cpls = spdk_zmalloc(opts->max_queue_depth * sizeof(*resources->cpls), 779 0x1000, NULL, SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA); 780 781 if (opts->in_capsule_data_size > 0) { 782 resources->bufs = spdk_zmalloc(opts->max_queue_depth * opts->in_capsule_data_size, 783 0x1000, NULL, SPDK_ENV_LCORE_ID_ANY, 784 SPDK_MALLOC_DMA); 785 } 786 787 if (!resources->reqs || !resources->recvs || !resources->cmds || 788 !resources->cpls || (opts->in_capsule_data_size && !resources->bufs)) { 789 SPDK_ERRLOG("Unable to allocate sufficient memory for RDMA queue.\n"); 790 goto cleanup; 791 } 792 793 resources->cmds_mr = ibv_reg_mr(opts->pd, resources->cmds, 794 opts->max_queue_depth * sizeof(*resources->cmds), 795 IBV_ACCESS_LOCAL_WRITE); 796 resources->cpls_mr = ibv_reg_mr(opts->pd, resources->cpls, 797 opts->max_queue_depth * sizeof(*resources->cpls), 798 0); 799 800 if (opts->in_capsule_data_size) { 801 resources->bufs_mr = ibv_reg_mr(opts->pd, resources->bufs, 802 opts->max_queue_depth * 803 opts->in_capsule_data_size, 804 IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE); 805 } 806 807 if (!resources->cmds_mr || !resources->cpls_mr || 808 (opts->in_capsule_data_size && 809 !resources->bufs_mr)) { 810 goto cleanup; 811 } 812 SPDK_DEBUGLOG(SPDK_LOG_RDMA, "Command Array: %p Length: %lx LKey: %x\n", 813 resources->cmds, opts->max_queue_depth * sizeof(*resources->cmds), 814 resources->cmds_mr->lkey); 815 SPDK_DEBUGLOG(SPDK_LOG_RDMA, "Completion Array: %p Length: %lx LKey: %x\n", 816 resources->cpls, opts->max_queue_depth * sizeof(*resources->cpls), 817 resources->cpls_mr->lkey); 818 if (resources->bufs && resources->bufs_mr) { 819 SPDK_DEBUGLOG(SPDK_LOG_RDMA, "In Capsule Data Array: %p Length: %x LKey: %x\n", 820 resources->bufs, opts->max_queue_depth * 821 opts->in_capsule_data_size, resources->bufs_mr->lkey); 822 } 823 824 /* Initialize queues */ 825 STAILQ_INIT(&resources->incoming_queue); 826 STAILQ_INIT(&resources->free_queue); 827 828 for (i = 0; i < opts->max_queue_depth; i++) { 829 struct ibv_recv_wr *bad_wr = NULL; 830 831 rdma_recv = &resources->recvs[i]; 832 rdma_recv->qpair = opts->qpair; 833 834 /* Set up memory to receive commands */ 835 if (resources->bufs) { 836 rdma_recv->buf = (void *)((uintptr_t)resources->bufs + (i * 837 opts->in_capsule_data_size)); 838 } 839 840 rdma_recv->rdma_wr.type = RDMA_WR_TYPE_RECV; 841 842 rdma_recv->sgl[0].addr = (uintptr_t)&resources->cmds[i]; 843 rdma_recv->sgl[0].length = sizeof(resources->cmds[i]); 844 rdma_recv->sgl[0].lkey = resources->cmds_mr->lkey; 845 rdma_recv->wr.num_sge = 1; 846 847 if (rdma_recv->buf && resources->bufs_mr) { 848 rdma_recv->sgl[1].addr = (uintptr_t)rdma_recv->buf; 849 rdma_recv->sgl[1].length = opts->in_capsule_data_size; 850 rdma_recv->sgl[1].lkey = resources->bufs_mr->lkey; 851 rdma_recv->wr.num_sge++; 852 } 853 854 rdma_recv->wr.wr_id = (uintptr_t)&rdma_recv->rdma_wr; 855 rdma_recv->wr.sg_list = rdma_recv->sgl; 856 if (opts->shared) { 857 srq = (struct ibv_srq *)opts->qp; 858 rc = ibv_post_srq_recv(srq, &rdma_recv->wr, &bad_wr); 859 } else { 860 qp = (struct ibv_qp *)opts->qp; 861 rc = ibv_post_recv(qp, &rdma_recv->wr, &bad_wr); 862 } 863 if (rc) { 864 goto cleanup; 865 } 866 } 867 868 for (i = 0; i < opts->max_queue_depth; i++) { 869 rdma_req = &resources->reqs[i]; 870 871 if (opts->qpair != NULL) { 872 rdma_req->req.qpair = &opts->qpair->qpair; 873 } else { 874 rdma_req->req.qpair = NULL; 875 } 876 rdma_req->req.cmd = NULL; 877 878 /* Set up memory to send responses */ 879 rdma_req->req.rsp = &resources->cpls[i]; 880 881 rdma_req->rsp.sgl[0].addr = (uintptr_t)&resources->cpls[i]; 882 rdma_req->rsp.sgl[0].length = sizeof(resources->cpls[i]); 883 rdma_req->rsp.sgl[0].lkey = resources->cpls_mr->lkey; 884 885 rdma_req->rsp.rdma_wr.type = RDMA_WR_TYPE_SEND; 886 rdma_req->rsp.wr.wr_id = (uintptr_t)&rdma_req->rsp.rdma_wr; 887 rdma_req->rsp.wr.next = NULL; 888 rdma_req->rsp.wr.opcode = IBV_WR_SEND; 889 rdma_req->rsp.wr.send_flags = IBV_SEND_SIGNALED; 890 rdma_req->rsp.wr.sg_list = rdma_req->rsp.sgl; 891 rdma_req->rsp.wr.num_sge = SPDK_COUNTOF(rdma_req->rsp.sgl); 892 893 /* Set up memory for data buffers */ 894 rdma_req->data.rdma_wr.type = RDMA_WR_TYPE_DATA; 895 rdma_req->data.wr.wr_id = (uintptr_t)&rdma_req->data.rdma_wr; 896 rdma_req->data.wr.next = NULL; 897 rdma_req->data.wr.send_flags = IBV_SEND_SIGNALED; 898 rdma_req->data.wr.sg_list = rdma_req->data.sgl; 899 rdma_req->data.wr.num_sge = SPDK_COUNTOF(rdma_req->data.sgl); 900 901 /* Initialize request state to FREE */ 902 rdma_req->state = RDMA_REQUEST_STATE_FREE; 903 STAILQ_INSERT_TAIL(&resources->free_queue, rdma_req, state_link); 904 } 905 906 return resources; 907 908 cleanup: 909 nvmf_rdma_resources_destroy(resources); 910 return NULL; 911 } 912 913 static void 914 spdk_nvmf_rdma_qpair_clean_ibv_events(struct spdk_nvmf_rdma_qpair *rqpair) 915 { 916 struct spdk_nvmf_rdma_ibv_event_ctx *ctx, *tctx; 917 STAILQ_FOREACH_SAFE(ctx, &rqpair->ibv_events, link, tctx) { 918 ctx->rqpair = NULL; 919 /* Memory allocated for ctx is freed in spdk_nvmf_rdma_qpair_process_ibv_event */ 920 STAILQ_REMOVE(&rqpair->ibv_events, ctx, spdk_nvmf_rdma_ibv_event_ctx, link); 921 } 922 } 923 924 static void 925 spdk_nvmf_rdma_qpair_destroy(struct spdk_nvmf_rdma_qpair *rqpair) 926 { 927 struct spdk_nvmf_rdma_recv *rdma_recv, *recv_tmp; 928 struct ibv_recv_wr *bad_recv_wr = NULL; 929 int rc; 930 931 spdk_trace_record(TRACE_RDMA_QP_DESTROY, 0, 0, (uintptr_t)rqpair->cm_id, 0); 932 933 spdk_poller_unregister(&rqpair->destruct_poller); 934 935 if (rqpair->qd != 0) { 936 struct spdk_nvmf_qpair *qpair = &rqpair->qpair; 937 struct spdk_nvmf_rdma_transport *rtransport = SPDK_CONTAINEROF(qpair->transport, 938 struct spdk_nvmf_rdma_transport, transport); 939 struct spdk_nvmf_rdma_request *req; 940 uint32_t i, max_req_count = 0; 941 942 SPDK_WARNLOG("Destroying qpair when queue depth is %d\n", rqpair->qd); 943 944 if (rqpair->srq == NULL) { 945 nvmf_rdma_dump_qpair_contents(rqpair); 946 max_req_count = rqpair->max_queue_depth; 947 } else if (rqpair->poller && rqpair->resources) { 948 max_req_count = rqpair->poller->max_srq_depth; 949 } 950 951 SPDK_DEBUGLOG(SPDK_LOG_RDMA, "Release incomplete requests\n"); 952 for (i = 0; i < max_req_count; i++) { 953 req = &rqpair->resources->reqs[i]; 954 if (req->req.qpair == qpair && req->state != RDMA_REQUEST_STATE_FREE) { 955 /* spdk_nvmf_rdma_request_process checks qpair ibv and internal state 956 * and completes a request */ 957 spdk_nvmf_rdma_request_process(rtransport, req); 958 } 959 } 960 assert(rqpair->qd == 0); 961 } 962 963 if (rqpair->poller) { 964 TAILQ_REMOVE(&rqpair->poller->qpairs, rqpair, link); 965 966 if (rqpair->srq != NULL && rqpair->resources != NULL) { 967 /* Drop all received but unprocessed commands for this queue and return them to SRQ */ 968 STAILQ_FOREACH_SAFE(rdma_recv, &rqpair->resources->incoming_queue, link, recv_tmp) { 969 if (rqpair == rdma_recv->qpair) { 970 STAILQ_REMOVE(&rqpair->resources->incoming_queue, rdma_recv, spdk_nvmf_rdma_recv, link); 971 rc = ibv_post_srq_recv(rqpair->srq, &rdma_recv->wr, &bad_recv_wr); 972 if (rc) { 973 SPDK_ERRLOG("Unable to re-post rx descriptor\n"); 974 } 975 } 976 } 977 } 978 } 979 980 if (rqpair->cm_id) { 981 if (rqpair->cm_id->qp != NULL) { 982 rdma_destroy_qp(rqpair->cm_id); 983 } 984 rdma_destroy_id(rqpair->cm_id); 985 986 if (rqpair->poller != NULL && rqpair->srq == NULL) { 987 rqpair->poller->required_num_wr -= MAX_WR_PER_QP(rqpair->max_queue_depth); 988 } 989 } 990 991 if (rqpair->srq == NULL && rqpair->resources != NULL) { 992 nvmf_rdma_resources_destroy(rqpair->resources); 993 } 994 995 spdk_nvmf_rdma_qpair_clean_ibv_events(rqpair); 996 997 free(rqpair); 998 } 999 1000 static int 1001 nvmf_rdma_resize_cq(struct spdk_nvmf_rdma_qpair *rqpair, struct spdk_nvmf_rdma_device *device) 1002 { 1003 struct spdk_nvmf_rdma_poller *rpoller; 1004 int rc, num_cqe, required_num_wr; 1005 1006 /* Enlarge CQ size dynamically */ 1007 rpoller = rqpair->poller; 1008 required_num_wr = rpoller->required_num_wr + MAX_WR_PER_QP(rqpair->max_queue_depth); 1009 num_cqe = rpoller->num_cqe; 1010 if (num_cqe < required_num_wr) { 1011 num_cqe = spdk_max(num_cqe * 2, required_num_wr); 1012 num_cqe = spdk_min(num_cqe, device->attr.max_cqe); 1013 } 1014 1015 if (rpoller->num_cqe != num_cqe) { 1016 if (required_num_wr > device->attr.max_cqe) { 1017 SPDK_ERRLOG("RDMA CQE requirement (%d) exceeds device max_cqe limitation (%d)\n", 1018 required_num_wr, device->attr.max_cqe); 1019 return -1; 1020 } 1021 1022 SPDK_DEBUGLOG(SPDK_LOG_RDMA, "Resize RDMA CQ from %d to %d\n", rpoller->num_cqe, num_cqe); 1023 rc = ibv_resize_cq(rpoller->cq, num_cqe); 1024 if (rc) { 1025 SPDK_ERRLOG("RDMA CQ resize failed: errno %d: %s\n", errno, spdk_strerror(errno)); 1026 return -1; 1027 } 1028 1029 rpoller->num_cqe = num_cqe; 1030 } 1031 1032 rpoller->required_num_wr = required_num_wr; 1033 return 0; 1034 } 1035 1036 static int 1037 spdk_nvmf_rdma_qpair_initialize(struct spdk_nvmf_qpair *qpair) 1038 { 1039 struct spdk_nvmf_rdma_qpair *rqpair; 1040 int rc; 1041 struct spdk_nvmf_rdma_transport *rtransport; 1042 struct spdk_nvmf_transport *transport; 1043 struct spdk_nvmf_rdma_resource_opts opts; 1044 struct spdk_nvmf_rdma_device *device; 1045 struct ibv_qp_init_attr ibv_init_attr; 1046 1047 rqpair = SPDK_CONTAINEROF(qpair, struct spdk_nvmf_rdma_qpair, qpair); 1048 device = rqpair->device; 1049 1050 memset(&ibv_init_attr, 0, sizeof(struct ibv_qp_init_attr)); 1051 ibv_init_attr.qp_context = rqpair; 1052 ibv_init_attr.qp_type = IBV_QPT_RC; 1053 ibv_init_attr.send_cq = rqpair->poller->cq; 1054 ibv_init_attr.recv_cq = rqpair->poller->cq; 1055 1056 if (rqpair->srq) { 1057 ibv_init_attr.srq = rqpair->srq; 1058 } else { 1059 ibv_init_attr.cap.max_recv_wr = rqpair->max_queue_depth; 1060 } 1061 1062 ibv_init_attr.cap.max_send_wr = (uint32_t)rqpair->max_queue_depth * 1063 2; /* SEND, READ or WRITE operations */ 1064 ibv_init_attr.cap.max_send_sge = spdk_min((uint32_t)device->attr.max_sge, NVMF_DEFAULT_TX_SGE); 1065 ibv_init_attr.cap.max_recv_sge = spdk_min((uint32_t)device->attr.max_sge, NVMF_DEFAULT_RX_SGE); 1066 1067 if (rqpair->srq == NULL && nvmf_rdma_resize_cq(rqpair, device) < 0) { 1068 SPDK_ERRLOG("Failed to resize the completion queue. Cannot initialize qpair.\n"); 1069 goto error; 1070 } 1071 1072 rc = rdma_create_qp(rqpair->cm_id, device->pd, &ibv_init_attr); 1073 if (rc) { 1074 SPDK_ERRLOG("rdma_create_qp failed: errno %d: %s\n", errno, spdk_strerror(errno)); 1075 goto error; 1076 } 1077 1078 rqpair->max_send_depth = spdk_min((uint32_t)(rqpair->max_queue_depth * 2), 1079 ibv_init_attr.cap.max_send_wr); 1080 rqpair->max_send_sge = spdk_min(NVMF_DEFAULT_TX_SGE, ibv_init_attr.cap.max_send_sge); 1081 rqpair->max_recv_sge = spdk_min(NVMF_DEFAULT_RX_SGE, ibv_init_attr.cap.max_recv_sge); 1082 spdk_trace_record(TRACE_RDMA_QP_CREATE, 0, 0, (uintptr_t)rqpair->cm_id, 0); 1083 SPDK_DEBUGLOG(SPDK_LOG_RDMA, "New RDMA Connection: %p\n", qpair); 1084 1085 rqpair->sends_to_post.first = NULL; 1086 rqpair->sends_to_post.last = NULL; 1087 1088 if (rqpair->poller->srq == NULL) { 1089 rtransport = SPDK_CONTAINEROF(qpair->transport, struct spdk_nvmf_rdma_transport, transport); 1090 transport = &rtransport->transport; 1091 1092 opts.qp = rqpair->cm_id->qp; 1093 opts.pd = rqpair->cm_id->pd; 1094 opts.qpair = rqpair; 1095 opts.shared = false; 1096 opts.max_queue_depth = rqpair->max_queue_depth; 1097 opts.in_capsule_data_size = transport->opts.in_capsule_data_size; 1098 1099 rqpair->resources = nvmf_rdma_resources_create(&opts); 1100 1101 if (!rqpair->resources) { 1102 SPDK_ERRLOG("Unable to allocate resources for receive queue.\n"); 1103 rdma_destroy_qp(rqpair->cm_id); 1104 goto error; 1105 } 1106 } else { 1107 rqpair->resources = rqpair->poller->resources; 1108 } 1109 1110 rqpair->current_recv_depth = 0; 1111 STAILQ_INIT(&rqpair->pending_rdma_read_queue); 1112 STAILQ_INIT(&rqpair->pending_rdma_write_queue); 1113 1114 return 0; 1115 1116 error: 1117 rdma_destroy_id(rqpair->cm_id); 1118 rqpair->cm_id = NULL; 1119 return -1; 1120 } 1121 1122 /* Append the given recv wr structure to the resource structs outstanding recvs list. */ 1123 /* This function accepts either a single wr or the first wr in a linked list. */ 1124 static void 1125 nvmf_rdma_qpair_queue_recv_wrs(struct spdk_nvmf_rdma_qpair *rqpair, struct ibv_recv_wr *first) 1126 { 1127 struct ibv_recv_wr *last; 1128 1129 last = first; 1130 while (last->next != NULL) { 1131 last = last->next; 1132 } 1133 1134 if (rqpair->resources->recvs_to_post.first == NULL) { 1135 rqpair->resources->recvs_to_post.first = first; 1136 rqpair->resources->recvs_to_post.last = last; 1137 if (rqpair->srq == NULL) { 1138 STAILQ_INSERT_TAIL(&rqpair->poller->qpairs_pending_recv, rqpair, recv_link); 1139 } 1140 } else { 1141 rqpair->resources->recvs_to_post.last->next = first; 1142 rqpair->resources->recvs_to_post.last = last; 1143 } 1144 } 1145 1146 /* Append the given send wr structure to the qpair's outstanding sends list. */ 1147 /* This function accepts either a single wr or the first wr in a linked list. */ 1148 static void 1149 nvmf_rdma_qpair_queue_send_wrs(struct spdk_nvmf_rdma_qpair *rqpair, struct ibv_send_wr *first) 1150 { 1151 struct ibv_send_wr *last; 1152 1153 last = first; 1154 while (last->next != NULL) { 1155 last = last->next; 1156 } 1157 1158 if (rqpair->sends_to_post.first == NULL) { 1159 rqpair->sends_to_post.first = first; 1160 rqpair->sends_to_post.last = last; 1161 STAILQ_INSERT_TAIL(&rqpair->poller->qpairs_pending_send, rqpair, send_link); 1162 } else { 1163 rqpair->sends_to_post.last->next = first; 1164 rqpair->sends_to_post.last = last; 1165 } 1166 } 1167 1168 static int 1169 request_transfer_in(struct spdk_nvmf_request *req) 1170 { 1171 struct spdk_nvmf_rdma_request *rdma_req; 1172 struct spdk_nvmf_qpair *qpair; 1173 struct spdk_nvmf_rdma_qpair *rqpair; 1174 1175 qpair = req->qpair; 1176 rdma_req = SPDK_CONTAINEROF(req, struct spdk_nvmf_rdma_request, req); 1177 rqpair = SPDK_CONTAINEROF(qpair, struct spdk_nvmf_rdma_qpair, qpair); 1178 1179 assert(req->xfer == SPDK_NVME_DATA_HOST_TO_CONTROLLER); 1180 assert(rdma_req != NULL); 1181 1182 nvmf_rdma_qpair_queue_send_wrs(rqpair, &rdma_req->data.wr); 1183 rqpair->current_read_depth += rdma_req->num_outstanding_data_wr; 1184 rqpair->current_send_depth += rdma_req->num_outstanding_data_wr; 1185 return 0; 1186 } 1187 1188 static int 1189 request_transfer_out(struct spdk_nvmf_request *req, int *data_posted) 1190 { 1191 int num_outstanding_data_wr = 0; 1192 struct spdk_nvmf_rdma_request *rdma_req; 1193 struct spdk_nvmf_qpair *qpair; 1194 struct spdk_nvmf_rdma_qpair *rqpair; 1195 struct spdk_nvme_cpl *rsp; 1196 struct ibv_send_wr *first = NULL; 1197 1198 *data_posted = 0; 1199 qpair = req->qpair; 1200 rsp = &req->rsp->nvme_cpl; 1201 rdma_req = SPDK_CONTAINEROF(req, struct spdk_nvmf_rdma_request, req); 1202 rqpair = SPDK_CONTAINEROF(qpair, struct spdk_nvmf_rdma_qpair, qpair); 1203 1204 /* Advance our sq_head pointer */ 1205 if (qpair->sq_head == qpair->sq_head_max) { 1206 qpair->sq_head = 0; 1207 } else { 1208 qpair->sq_head++; 1209 } 1210 rsp->sqhd = qpair->sq_head; 1211 1212 /* queue the capsule for the recv buffer */ 1213 assert(rdma_req->recv != NULL); 1214 1215 nvmf_rdma_qpair_queue_recv_wrs(rqpair, &rdma_req->recv->wr); 1216 1217 rdma_req->recv = NULL; 1218 assert(rqpair->current_recv_depth > 0); 1219 rqpair->current_recv_depth--; 1220 1221 /* Build the response which consists of optional 1222 * RDMA WRITEs to transfer data, plus an RDMA SEND 1223 * containing the response. 1224 */ 1225 first = &rdma_req->rsp.wr; 1226 1227 if (rsp->status.sc == SPDK_NVME_SC_SUCCESS && 1228 req->xfer == SPDK_NVME_DATA_CONTROLLER_TO_HOST) { 1229 first = &rdma_req->data.wr; 1230 *data_posted = 1; 1231 num_outstanding_data_wr = rdma_req->num_outstanding_data_wr; 1232 } 1233 nvmf_rdma_qpair_queue_send_wrs(rqpair, first); 1234 /* +1 for the rsp wr */ 1235 rqpair->current_send_depth += num_outstanding_data_wr + 1; 1236 1237 return 0; 1238 } 1239 1240 static int 1241 spdk_nvmf_rdma_event_accept(struct rdma_cm_id *id, struct spdk_nvmf_rdma_qpair *rqpair) 1242 { 1243 struct spdk_nvmf_rdma_accept_private_data accept_data; 1244 struct rdma_conn_param ctrlr_event_data = {}; 1245 int rc; 1246 1247 accept_data.recfmt = 0; 1248 accept_data.crqsize = rqpair->max_queue_depth; 1249 1250 ctrlr_event_data.private_data = &accept_data; 1251 ctrlr_event_data.private_data_len = sizeof(accept_data); 1252 if (id->ps == RDMA_PS_TCP) { 1253 ctrlr_event_data.responder_resources = 0; /* We accept 0 reads from the host */ 1254 ctrlr_event_data.initiator_depth = rqpair->max_read_depth; 1255 } 1256 1257 /* Configure infinite retries for the initiator side qpair. 1258 * When using a shared receive queue on the target side, 1259 * we need to pass this value to the initiator to prevent the 1260 * initiator side NIC from completing SEND requests back to the 1261 * initiator with status rnr_retry_count_exceeded. */ 1262 if (rqpair->srq != NULL) { 1263 ctrlr_event_data.rnr_retry_count = 0x7; 1264 } 1265 1266 rc = rdma_accept(id, &ctrlr_event_data); 1267 if (rc) { 1268 SPDK_ERRLOG("Error %d on rdma_accept\n", errno); 1269 } else { 1270 SPDK_DEBUGLOG(SPDK_LOG_RDMA, "Sent back the accept\n"); 1271 } 1272 1273 return rc; 1274 } 1275 1276 static void 1277 spdk_nvmf_rdma_event_reject(struct rdma_cm_id *id, enum spdk_nvmf_rdma_transport_error error) 1278 { 1279 struct spdk_nvmf_rdma_reject_private_data rej_data; 1280 1281 rej_data.recfmt = 0; 1282 rej_data.sts = error; 1283 1284 rdma_reject(id, &rej_data, sizeof(rej_data)); 1285 } 1286 1287 static int 1288 nvmf_rdma_connect(struct spdk_nvmf_transport *transport, struct rdma_cm_event *event, 1289 new_qpair_fn cb_fn, void *cb_arg) 1290 { 1291 struct spdk_nvmf_rdma_transport *rtransport; 1292 struct spdk_nvmf_rdma_qpair *rqpair = NULL; 1293 struct spdk_nvmf_rdma_port *port; 1294 struct rdma_conn_param *rdma_param = NULL; 1295 const struct spdk_nvmf_rdma_request_private_data *private_data = NULL; 1296 uint16_t max_queue_depth; 1297 uint16_t max_read_depth; 1298 1299 rtransport = SPDK_CONTAINEROF(transport, struct spdk_nvmf_rdma_transport, transport); 1300 1301 assert(event->id != NULL); /* Impossible. Can't even reject the connection. */ 1302 assert(event->id->verbs != NULL); /* Impossible. No way to handle this. */ 1303 1304 rdma_param = &event->param.conn; 1305 if (rdma_param->private_data == NULL || 1306 rdma_param->private_data_len < sizeof(struct spdk_nvmf_rdma_request_private_data)) { 1307 SPDK_ERRLOG("connect request: no private data provided\n"); 1308 spdk_nvmf_rdma_event_reject(event->id, SPDK_NVMF_RDMA_ERROR_INVALID_PRIVATE_DATA_LENGTH); 1309 return -1; 1310 } 1311 1312 private_data = rdma_param->private_data; 1313 if (private_data->recfmt != 0) { 1314 SPDK_ERRLOG("Received RDMA private data with RECFMT != 0\n"); 1315 spdk_nvmf_rdma_event_reject(event->id, SPDK_NVMF_RDMA_ERROR_INVALID_RECFMT); 1316 return -1; 1317 } 1318 1319 SPDK_DEBUGLOG(SPDK_LOG_RDMA, "Connect Recv on fabric intf name %s, dev_name %s\n", 1320 event->id->verbs->device->name, event->id->verbs->device->dev_name); 1321 1322 port = event->listen_id->context; 1323 SPDK_DEBUGLOG(SPDK_LOG_RDMA, "Listen Id was %p with verbs %p. ListenAddr: %p\n", 1324 event->listen_id, event->listen_id->verbs, port); 1325 1326 /* Figure out the supported queue depth. This is a multi-step process 1327 * that takes into account hardware maximums, host provided values, 1328 * and our target's internal memory limits */ 1329 1330 SPDK_DEBUGLOG(SPDK_LOG_RDMA, "Calculating Queue Depth\n"); 1331 1332 /* Start with the maximum queue depth allowed by the target */ 1333 max_queue_depth = rtransport->transport.opts.max_queue_depth; 1334 max_read_depth = rtransport->transport.opts.max_queue_depth; 1335 SPDK_DEBUGLOG(SPDK_LOG_RDMA, "Target Max Queue Depth: %d\n", 1336 rtransport->transport.opts.max_queue_depth); 1337 1338 /* Next check the local NIC's hardware limitations */ 1339 SPDK_DEBUGLOG(SPDK_LOG_RDMA, 1340 "Local NIC Max Send/Recv Queue Depth: %d Max Read/Write Queue Depth: %d\n", 1341 port->device->attr.max_qp_wr, port->device->attr.max_qp_rd_atom); 1342 max_queue_depth = spdk_min(max_queue_depth, port->device->attr.max_qp_wr); 1343 max_read_depth = spdk_min(max_read_depth, port->device->attr.max_qp_init_rd_atom); 1344 1345 /* Next check the remote NIC's hardware limitations */ 1346 SPDK_DEBUGLOG(SPDK_LOG_RDMA, 1347 "Host (Initiator) NIC Max Incoming RDMA R/W operations: %d Max Outgoing RDMA R/W operations: %d\n", 1348 rdma_param->initiator_depth, rdma_param->responder_resources); 1349 if (rdma_param->initiator_depth > 0) { 1350 max_read_depth = spdk_min(max_read_depth, rdma_param->initiator_depth); 1351 } 1352 1353 /* Finally check for the host software requested values, which are 1354 * optional. */ 1355 if (rdma_param->private_data != NULL && 1356 rdma_param->private_data_len >= sizeof(struct spdk_nvmf_rdma_request_private_data)) { 1357 SPDK_DEBUGLOG(SPDK_LOG_RDMA, "Host Receive Queue Size: %d\n", private_data->hrqsize); 1358 SPDK_DEBUGLOG(SPDK_LOG_RDMA, "Host Send Queue Size: %d\n", private_data->hsqsize); 1359 max_queue_depth = spdk_min(max_queue_depth, private_data->hrqsize); 1360 max_queue_depth = spdk_min(max_queue_depth, private_data->hsqsize + 1); 1361 } 1362 1363 SPDK_DEBUGLOG(SPDK_LOG_RDMA, "Final Negotiated Queue Depth: %d R/W Depth: %d\n", 1364 max_queue_depth, max_read_depth); 1365 1366 rqpair = calloc(1, sizeof(struct spdk_nvmf_rdma_qpair)); 1367 if (rqpair == NULL) { 1368 SPDK_ERRLOG("Could not allocate new connection.\n"); 1369 spdk_nvmf_rdma_event_reject(event->id, SPDK_NVMF_RDMA_ERROR_NO_RESOURCES); 1370 return -1; 1371 } 1372 1373 rqpair->device = port->device; 1374 rqpair->max_queue_depth = max_queue_depth; 1375 rqpair->max_read_depth = max_read_depth; 1376 rqpair->cm_id = event->id; 1377 rqpair->listen_id = event->listen_id; 1378 rqpair->qpair.transport = transport; 1379 STAILQ_INIT(&rqpair->ibv_events); 1380 /* use qid from the private data to determine the qpair type 1381 qid will be set to the appropriate value when the controller is created */ 1382 rqpair->qpair.qid = private_data->qid; 1383 1384 event->id->context = &rqpair->qpair; 1385 1386 cb_fn(&rqpair->qpair, cb_arg); 1387 1388 return 0; 1389 } 1390 1391 static int 1392 spdk_nvmf_rdma_mem_notify(void *cb_ctx, struct spdk_mem_map *map, 1393 enum spdk_mem_map_notify_action action, 1394 void *vaddr, size_t size) 1395 { 1396 struct ibv_pd *pd = cb_ctx; 1397 struct ibv_mr *mr; 1398 int rc; 1399 1400 switch (action) { 1401 case SPDK_MEM_MAP_NOTIFY_REGISTER: 1402 if (!g_nvmf_hooks.get_rkey) { 1403 mr = ibv_reg_mr(pd, vaddr, size, 1404 IBV_ACCESS_LOCAL_WRITE | 1405 IBV_ACCESS_REMOTE_READ | 1406 IBV_ACCESS_REMOTE_WRITE); 1407 if (mr == NULL) { 1408 SPDK_ERRLOG("ibv_reg_mr() failed\n"); 1409 return -1; 1410 } else { 1411 rc = spdk_mem_map_set_translation(map, (uint64_t)vaddr, size, (uint64_t)mr); 1412 } 1413 } else { 1414 rc = spdk_mem_map_set_translation(map, (uint64_t)vaddr, size, 1415 g_nvmf_hooks.get_rkey(pd, vaddr, size)); 1416 } 1417 break; 1418 case SPDK_MEM_MAP_NOTIFY_UNREGISTER: 1419 if (!g_nvmf_hooks.get_rkey) { 1420 mr = (struct ibv_mr *)spdk_mem_map_translate(map, (uint64_t)vaddr, NULL); 1421 if (mr) { 1422 ibv_dereg_mr(mr); 1423 } 1424 } 1425 rc = spdk_mem_map_clear_translation(map, (uint64_t)vaddr, size); 1426 break; 1427 default: 1428 SPDK_UNREACHABLE(); 1429 } 1430 1431 return rc; 1432 } 1433 1434 static int 1435 spdk_nvmf_rdma_check_contiguous_entries(uint64_t addr_1, uint64_t addr_2) 1436 { 1437 /* Two contiguous mappings will point to the same address which is the start of the RDMA MR. */ 1438 return addr_1 == addr_2; 1439 } 1440 1441 static inline void 1442 nvmf_rdma_setup_wr(struct ibv_send_wr *wr, struct ibv_send_wr *next, 1443 enum spdk_nvme_data_transfer xfer) 1444 { 1445 if (xfer == SPDK_NVME_DATA_CONTROLLER_TO_HOST) { 1446 wr->opcode = IBV_WR_RDMA_WRITE; 1447 wr->send_flags = 0; 1448 wr->next = next; 1449 } else if (xfer == SPDK_NVME_DATA_HOST_TO_CONTROLLER) { 1450 wr->opcode = IBV_WR_RDMA_READ; 1451 wr->send_flags = IBV_SEND_SIGNALED; 1452 wr->next = NULL; 1453 } else { 1454 assert(0); 1455 } 1456 } 1457 1458 static int 1459 nvmf_request_alloc_wrs(struct spdk_nvmf_rdma_transport *rtransport, 1460 struct spdk_nvmf_rdma_request *rdma_req, 1461 uint32_t num_sgl_descriptors) 1462 { 1463 struct spdk_nvmf_rdma_request_data *work_requests[SPDK_NVMF_MAX_SGL_ENTRIES]; 1464 struct spdk_nvmf_rdma_request_data *current_data_wr; 1465 uint32_t i; 1466 1467 if (num_sgl_descriptors > SPDK_NVMF_MAX_SGL_ENTRIES) { 1468 SPDK_ERRLOG("Requested too much entries (%u), the limit is %u\n", 1469 num_sgl_descriptors, SPDK_NVMF_MAX_SGL_ENTRIES); 1470 return -EINVAL; 1471 } 1472 1473 if (spdk_mempool_get_bulk(rtransport->data_wr_pool, (void **)work_requests, num_sgl_descriptors)) { 1474 return -ENOMEM; 1475 } 1476 1477 current_data_wr = &rdma_req->data; 1478 1479 for (i = 0; i < num_sgl_descriptors; i++) { 1480 nvmf_rdma_setup_wr(¤t_data_wr->wr, &work_requests[i]->wr, rdma_req->req.xfer); 1481 current_data_wr->wr.next = &work_requests[i]->wr; 1482 current_data_wr = work_requests[i]; 1483 current_data_wr->wr.sg_list = current_data_wr->sgl; 1484 current_data_wr->wr.wr_id = rdma_req->data.wr.wr_id; 1485 } 1486 1487 nvmf_rdma_setup_wr(¤t_data_wr->wr, &rdma_req->rsp.wr, rdma_req->req.xfer); 1488 1489 return 0; 1490 } 1491 1492 static inline void 1493 nvmf_rdma_setup_request(struct spdk_nvmf_rdma_request *rdma_req) 1494 { 1495 struct ibv_send_wr *wr = &rdma_req->data.wr; 1496 struct spdk_nvme_sgl_descriptor *sgl = &rdma_req->req.cmd->nvme_cmd.dptr.sgl1; 1497 1498 wr->wr.rdma.rkey = sgl->keyed.key; 1499 wr->wr.rdma.remote_addr = sgl->address; 1500 nvmf_rdma_setup_wr(wr, &rdma_req->rsp.wr, rdma_req->req.xfer); 1501 } 1502 1503 static inline void 1504 nvmf_rdma_update_remote_addr(struct spdk_nvmf_rdma_request *rdma_req, uint32_t num_wrs) 1505 { 1506 struct ibv_send_wr *wr = &rdma_req->data.wr; 1507 struct spdk_nvme_sgl_descriptor *sgl = &rdma_req->req.cmd->nvme_cmd.dptr.sgl1; 1508 uint32_t i; 1509 int j; 1510 uint64_t remote_addr_offset = 0; 1511 1512 for (i = 0; i < num_wrs; ++i) { 1513 wr->wr.rdma.rkey = sgl->keyed.key; 1514 wr->wr.rdma.remote_addr = sgl->address + remote_addr_offset; 1515 for (j = 0; j < wr->num_sge; ++j) { 1516 remote_addr_offset += wr->sg_list[j].length; 1517 } 1518 wr = wr->next; 1519 } 1520 } 1521 1522 /* This function is used in the rare case that we have a buffer split over multiple memory regions. */ 1523 static int 1524 nvmf_rdma_replace_buffer(struct spdk_nvmf_rdma_poll_group *rgroup, void **buf) 1525 { 1526 struct spdk_nvmf_transport_poll_group *group = &rgroup->group; 1527 struct spdk_nvmf_transport *transport = group->transport; 1528 struct spdk_nvmf_transport_pg_cache_buf *old_buf; 1529 void *new_buf; 1530 1531 if (!(STAILQ_EMPTY(&group->buf_cache))) { 1532 group->buf_cache_count--; 1533 new_buf = STAILQ_FIRST(&group->buf_cache); 1534 STAILQ_REMOVE_HEAD(&group->buf_cache, link); 1535 assert(*buf != NULL); 1536 } else { 1537 new_buf = spdk_mempool_get(transport->data_buf_pool); 1538 } 1539 1540 if (*buf == NULL) { 1541 return -ENOMEM; 1542 } 1543 1544 old_buf = *buf; 1545 STAILQ_INSERT_HEAD(&rgroup->retired_bufs, old_buf, link); 1546 *buf = new_buf; 1547 return 0; 1548 } 1549 1550 static bool 1551 nvmf_rdma_get_lkey(struct spdk_nvmf_rdma_device *device, struct iovec *iov, 1552 uint32_t *_lkey) 1553 { 1554 uint64_t translation_len; 1555 uint32_t lkey; 1556 1557 translation_len = iov->iov_len; 1558 1559 if (!g_nvmf_hooks.get_rkey) { 1560 lkey = ((struct ibv_mr *)spdk_mem_map_translate(device->map, 1561 (uint64_t)iov->iov_base, &translation_len))->lkey; 1562 } else { 1563 lkey = spdk_mem_map_translate(device->map, 1564 (uint64_t)iov->iov_base, &translation_len); 1565 } 1566 1567 if (spdk_unlikely(translation_len < iov->iov_len)) { 1568 return false; 1569 } 1570 1571 *_lkey = lkey; 1572 return true; 1573 } 1574 1575 static bool 1576 nvmf_rdma_fill_wr_sge(struct spdk_nvmf_rdma_device *device, 1577 struct iovec *iov, struct ibv_send_wr **_wr, 1578 uint32_t *_remaining_data_block, uint32_t *_offset, 1579 uint32_t *_num_extra_wrs, 1580 const struct spdk_dif_ctx *dif_ctx) 1581 { 1582 struct ibv_send_wr *wr = *_wr; 1583 struct ibv_sge *sg_ele = &wr->sg_list[wr->num_sge]; 1584 uint32_t lkey = 0; 1585 uint32_t remaining, data_block_size, md_size, sge_len; 1586 1587 if (spdk_unlikely(!nvmf_rdma_get_lkey(device, iov, &lkey))) { 1588 /* This is a very rare case that can occur when using DPDK version < 19.05 */ 1589 SPDK_ERRLOG("Data buffer split over multiple RDMA Memory Regions. Removing it from circulation.\n"); 1590 return false; 1591 } 1592 1593 if (spdk_likely(!dif_ctx)) { 1594 sg_ele->lkey = lkey; 1595 sg_ele->addr = (uintptr_t)(iov->iov_base); 1596 sg_ele->length = iov->iov_len; 1597 wr->num_sge++; 1598 } else { 1599 remaining = iov->iov_len - *_offset; 1600 data_block_size = dif_ctx->block_size - dif_ctx->md_size; 1601 md_size = dif_ctx->md_size; 1602 1603 while (remaining) { 1604 if (wr->num_sge >= SPDK_NVMF_MAX_SGL_ENTRIES) { 1605 if (*_num_extra_wrs > 0 && wr->next) { 1606 *_wr = wr->next; 1607 wr = *_wr; 1608 wr->num_sge = 0; 1609 sg_ele = &wr->sg_list[wr->num_sge]; 1610 (*_num_extra_wrs)--; 1611 } else { 1612 break; 1613 } 1614 } 1615 sg_ele->lkey = lkey; 1616 sg_ele->addr = (uintptr_t)((char *)iov->iov_base + *_offset); 1617 sge_len = spdk_min(remaining, *_remaining_data_block); 1618 sg_ele->length = sge_len; 1619 remaining -= sge_len; 1620 *_remaining_data_block -= sge_len; 1621 *_offset += sge_len; 1622 1623 sg_ele++; 1624 wr->num_sge++; 1625 1626 if (*_remaining_data_block == 0) { 1627 /* skip metadata */ 1628 *_offset += md_size; 1629 /* Metadata that do not fit this IO buffer will be included in the next IO buffer */ 1630 remaining -= spdk_min(remaining, md_size); 1631 *_remaining_data_block = data_block_size; 1632 } 1633 1634 if (remaining == 0) { 1635 /* By subtracting the size of the last IOV from the offset, we ensure that we skip 1636 the remaining metadata bits at the beginning of the next buffer */ 1637 *_offset -= iov->iov_len; 1638 } 1639 } 1640 } 1641 1642 return true; 1643 } 1644 1645 static int 1646 nvmf_rdma_fill_wr_sgl(struct spdk_nvmf_rdma_poll_group *rgroup, 1647 struct spdk_nvmf_rdma_device *device, 1648 struct spdk_nvmf_rdma_request *rdma_req, 1649 struct ibv_send_wr *wr, 1650 uint32_t length, 1651 uint32_t num_extra_wrs) 1652 { 1653 struct spdk_nvmf_request *req = &rdma_req->req; 1654 struct spdk_dif_ctx *dif_ctx = NULL; 1655 uint32_t remaining_data_block = 0; 1656 uint32_t offset = 0; 1657 1658 if (spdk_unlikely(rdma_req->req.dif.dif_insert_or_strip)) { 1659 dif_ctx = &rdma_req->req.dif.dif_ctx; 1660 remaining_data_block = dif_ctx->block_size - dif_ctx->md_size; 1661 } 1662 1663 wr->num_sge = 0; 1664 1665 while (length && (num_extra_wrs || wr->num_sge < SPDK_NVMF_MAX_SGL_ENTRIES)) { 1666 while (spdk_unlikely(!nvmf_rdma_fill_wr_sge(device, &req->iov[rdma_req->iovpos], &wr, 1667 &remaining_data_block, &offset, &num_extra_wrs, dif_ctx))) { 1668 if (nvmf_rdma_replace_buffer(rgroup, &req->buffers[rdma_req->iovpos]) == -ENOMEM) { 1669 return -ENOMEM; 1670 } 1671 req->iov[rdma_req->iovpos].iov_base = (void *)((uintptr_t)(req->buffers[rdma_req->iovpos] + 1672 NVMF_DATA_BUFFER_MASK) & 1673 ~NVMF_DATA_BUFFER_MASK); 1674 } 1675 1676 length -= req->iov[rdma_req->iovpos].iov_len; 1677 rdma_req->iovpos++; 1678 } 1679 1680 if (length) { 1681 SPDK_ERRLOG("Not enough SG entries to hold data buffer\n"); 1682 return -EINVAL; 1683 } 1684 1685 return 0; 1686 } 1687 1688 static inline uint32_t 1689 nvmf_rdma_calc_num_wrs(uint32_t length, uint32_t io_unit_size, uint32_t block_size) 1690 { 1691 /* estimate the number of SG entries and WRs needed to process the request */ 1692 uint32_t num_sge = 0; 1693 uint32_t i; 1694 uint32_t num_buffers = SPDK_CEIL_DIV(length, io_unit_size); 1695 1696 for (i = 0; i < num_buffers && length > 0; i++) { 1697 uint32_t buffer_len = spdk_min(length, io_unit_size); 1698 uint32_t num_sge_in_block = SPDK_CEIL_DIV(buffer_len, block_size); 1699 1700 if (num_sge_in_block * block_size > buffer_len) { 1701 ++num_sge_in_block; 1702 } 1703 num_sge += num_sge_in_block; 1704 length -= buffer_len; 1705 } 1706 return SPDK_CEIL_DIV(num_sge, SPDK_NVMF_MAX_SGL_ENTRIES); 1707 } 1708 1709 static int 1710 spdk_nvmf_rdma_request_fill_iovs(struct spdk_nvmf_rdma_transport *rtransport, 1711 struct spdk_nvmf_rdma_device *device, 1712 struct spdk_nvmf_rdma_request *rdma_req, 1713 uint32_t length) 1714 { 1715 struct spdk_nvmf_rdma_qpair *rqpair; 1716 struct spdk_nvmf_rdma_poll_group *rgroup; 1717 struct spdk_nvmf_request *req = &rdma_req->req; 1718 struct ibv_send_wr *wr = &rdma_req->data.wr; 1719 int rc; 1720 uint32_t num_wrs = 1; 1721 1722 rqpair = SPDK_CONTAINEROF(req->qpair, struct spdk_nvmf_rdma_qpair, qpair); 1723 rgroup = rqpair->poller->group; 1724 1725 /* rdma wr specifics */ 1726 nvmf_rdma_setup_request(rdma_req); 1727 1728 rc = spdk_nvmf_request_get_buffers(req, &rgroup->group, &rtransport->transport, 1729 length); 1730 if (rc != 0) { 1731 return rc; 1732 } 1733 1734 assert(req->iovcnt <= rqpair->max_send_sge); 1735 1736 rdma_req->iovpos = 0; 1737 1738 if (spdk_unlikely(req->dif.dif_insert_or_strip)) { 1739 num_wrs = nvmf_rdma_calc_num_wrs(length, rtransport->transport.opts.io_unit_size, 1740 req->dif.dif_ctx.block_size); 1741 if (num_wrs > 1) { 1742 rc = nvmf_request_alloc_wrs(rtransport, rdma_req, num_wrs - 1); 1743 if (rc != 0) { 1744 goto err_exit; 1745 } 1746 } 1747 } 1748 1749 rc = nvmf_rdma_fill_wr_sgl(rgroup, device, rdma_req, wr, length, num_wrs - 1); 1750 if (spdk_unlikely(rc != 0)) { 1751 goto err_exit; 1752 } 1753 1754 if (spdk_unlikely(num_wrs > 1)) { 1755 nvmf_rdma_update_remote_addr(rdma_req, num_wrs); 1756 } 1757 1758 /* set the number of outstanding data WRs for this request. */ 1759 rdma_req->num_outstanding_data_wr = num_wrs; 1760 1761 return rc; 1762 1763 err_exit: 1764 spdk_nvmf_request_free_buffers(req, &rgroup->group, &rtransport->transport); 1765 nvmf_rdma_request_free_data(rdma_req, rtransport); 1766 req->iovcnt = 0; 1767 return rc; 1768 } 1769 1770 static int 1771 nvmf_rdma_request_fill_iovs_multi_sgl(struct spdk_nvmf_rdma_transport *rtransport, 1772 struct spdk_nvmf_rdma_device *device, 1773 struct spdk_nvmf_rdma_request *rdma_req) 1774 { 1775 struct spdk_nvmf_rdma_qpair *rqpair; 1776 struct spdk_nvmf_rdma_poll_group *rgroup; 1777 struct ibv_send_wr *current_wr; 1778 struct spdk_nvmf_request *req = &rdma_req->req; 1779 struct spdk_nvme_sgl_descriptor *inline_segment, *desc; 1780 uint32_t num_sgl_descriptors; 1781 uint32_t lengths[SPDK_NVMF_MAX_SGL_ENTRIES]; 1782 uint32_t i; 1783 int rc; 1784 1785 rqpair = SPDK_CONTAINEROF(rdma_req->req.qpair, struct spdk_nvmf_rdma_qpair, qpair); 1786 rgroup = rqpair->poller->group; 1787 1788 inline_segment = &req->cmd->nvme_cmd.dptr.sgl1; 1789 assert(inline_segment->generic.type == SPDK_NVME_SGL_TYPE_LAST_SEGMENT); 1790 assert(inline_segment->unkeyed.subtype == SPDK_NVME_SGL_SUBTYPE_OFFSET); 1791 1792 num_sgl_descriptors = inline_segment->unkeyed.length / sizeof(struct spdk_nvme_sgl_descriptor); 1793 assert(num_sgl_descriptors <= SPDK_NVMF_MAX_SGL_ENTRIES); 1794 1795 if (nvmf_request_alloc_wrs(rtransport, rdma_req, num_sgl_descriptors - 1) != 0) { 1796 return -ENOMEM; 1797 } 1798 1799 desc = (struct spdk_nvme_sgl_descriptor *)rdma_req->recv->buf + inline_segment->address; 1800 for (i = 0; i < num_sgl_descriptors; i++) { 1801 if (spdk_likely(!req->dif.dif_insert_or_strip)) { 1802 lengths[i] = desc->keyed.length; 1803 } else { 1804 req->dif.orig_length += desc->keyed.length; 1805 lengths[i] = spdk_dif_get_length_with_md(desc->keyed.length, &req->dif.dif_ctx); 1806 req->dif.elba_length += lengths[i]; 1807 } 1808 desc++; 1809 } 1810 1811 rc = spdk_nvmf_request_get_buffers_multi(req, &rgroup->group, &rtransport->transport, 1812 lengths, num_sgl_descriptors); 1813 if (rc != 0) { 1814 nvmf_rdma_request_free_data(rdma_req, rtransport); 1815 return rc; 1816 } 1817 1818 /* The first WR must always be the embedded data WR. This is how we unwind them later. */ 1819 current_wr = &rdma_req->data.wr; 1820 assert(current_wr != NULL); 1821 1822 req->length = 0; 1823 rdma_req->iovpos = 0; 1824 desc = (struct spdk_nvme_sgl_descriptor *)rdma_req->recv->buf + inline_segment->address; 1825 for (i = 0; i < num_sgl_descriptors; i++) { 1826 /* The descriptors must be keyed data block descriptors with an address, not an offset. */ 1827 if (spdk_unlikely(desc->generic.type != SPDK_NVME_SGL_TYPE_KEYED_DATA_BLOCK || 1828 desc->keyed.subtype != SPDK_NVME_SGL_SUBTYPE_ADDRESS)) { 1829 rc = -EINVAL; 1830 goto err_exit; 1831 } 1832 1833 current_wr->num_sge = 0; 1834 1835 rc = nvmf_rdma_fill_wr_sgl(rgroup, device, rdma_req, current_wr, lengths[i], 0); 1836 if (rc != 0) { 1837 rc = -ENOMEM; 1838 goto err_exit; 1839 } 1840 1841 req->length += desc->keyed.length; 1842 current_wr->wr.rdma.rkey = desc->keyed.key; 1843 current_wr->wr.rdma.remote_addr = desc->address; 1844 current_wr = current_wr->next; 1845 desc++; 1846 } 1847 1848 #ifdef SPDK_CONFIG_RDMA_SEND_WITH_INVAL 1849 /* Go back to the last descriptor in the list. */ 1850 desc--; 1851 if ((device->attr.device_cap_flags & IBV_DEVICE_MEM_MGT_EXTENSIONS) != 0) { 1852 if (desc->keyed.subtype == SPDK_NVME_SGL_SUBTYPE_INVALIDATE_KEY) { 1853 rdma_req->rsp.wr.opcode = IBV_WR_SEND_WITH_INV; 1854 rdma_req->rsp.wr.imm_data = desc->keyed.key; 1855 } 1856 } 1857 #endif 1858 1859 rdma_req->num_outstanding_data_wr = num_sgl_descriptors; 1860 1861 return 0; 1862 1863 err_exit: 1864 spdk_nvmf_request_free_buffers(req, &rgroup->group, &rtransport->transport); 1865 nvmf_rdma_request_free_data(rdma_req, rtransport); 1866 return rc; 1867 } 1868 1869 static int 1870 spdk_nvmf_rdma_request_parse_sgl(struct spdk_nvmf_rdma_transport *rtransport, 1871 struct spdk_nvmf_rdma_device *device, 1872 struct spdk_nvmf_rdma_request *rdma_req) 1873 { 1874 struct spdk_nvmf_request *req = &rdma_req->req; 1875 struct spdk_nvme_cpl *rsp; 1876 struct spdk_nvme_sgl_descriptor *sgl; 1877 int rc; 1878 uint32_t length; 1879 1880 rsp = &req->rsp->nvme_cpl; 1881 sgl = &req->cmd->nvme_cmd.dptr.sgl1; 1882 1883 if (sgl->generic.type == SPDK_NVME_SGL_TYPE_KEYED_DATA_BLOCK && 1884 (sgl->keyed.subtype == SPDK_NVME_SGL_SUBTYPE_ADDRESS || 1885 sgl->keyed.subtype == SPDK_NVME_SGL_SUBTYPE_INVALIDATE_KEY)) { 1886 1887 length = sgl->keyed.length; 1888 if (length > rtransport->transport.opts.max_io_size) { 1889 SPDK_ERRLOG("SGL length 0x%x exceeds max io size 0x%x\n", 1890 length, rtransport->transport.opts.max_io_size); 1891 rsp->status.sc = SPDK_NVME_SC_DATA_SGL_LENGTH_INVALID; 1892 return -1; 1893 } 1894 #ifdef SPDK_CONFIG_RDMA_SEND_WITH_INVAL 1895 if ((device->attr.device_cap_flags & IBV_DEVICE_MEM_MGT_EXTENSIONS) != 0) { 1896 if (sgl->keyed.subtype == SPDK_NVME_SGL_SUBTYPE_INVALIDATE_KEY) { 1897 rdma_req->rsp.wr.opcode = IBV_WR_SEND_WITH_INV; 1898 rdma_req->rsp.wr.imm_data = sgl->keyed.key; 1899 } 1900 } 1901 #endif 1902 1903 /* fill request length and populate iovs */ 1904 req->length = length; 1905 1906 if (spdk_unlikely(req->dif.dif_insert_or_strip)) { 1907 req->dif.orig_length = length; 1908 length = spdk_dif_get_length_with_md(length, &req->dif.dif_ctx); 1909 req->dif.elba_length = length; 1910 } 1911 1912 rc = spdk_nvmf_rdma_request_fill_iovs(rtransport, device, rdma_req, length); 1913 if (spdk_unlikely(rc < 0)) { 1914 if (rc == -EINVAL) { 1915 SPDK_ERRLOG("SGL length exceeds the max I/O size\n"); 1916 rsp->status.sc = SPDK_NVME_SC_DATA_SGL_LENGTH_INVALID; 1917 return -1; 1918 } 1919 /* No available buffers. Queue this request up. */ 1920 SPDK_DEBUGLOG(SPDK_LOG_RDMA, "No available large data buffers. Queueing request %p\n", rdma_req); 1921 return 0; 1922 } 1923 1924 /* backward compatible */ 1925 req->data = req->iov[0].iov_base; 1926 1927 SPDK_DEBUGLOG(SPDK_LOG_RDMA, "Request %p took %d buffer/s from central pool\n", rdma_req, 1928 req->iovcnt); 1929 1930 return 0; 1931 } else if (sgl->generic.type == SPDK_NVME_SGL_TYPE_DATA_BLOCK && 1932 sgl->unkeyed.subtype == SPDK_NVME_SGL_SUBTYPE_OFFSET) { 1933 uint64_t offset = sgl->address; 1934 uint32_t max_len = rtransport->transport.opts.in_capsule_data_size; 1935 1936 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "In-capsule data: offset 0x%" PRIx64 ", length 0x%x\n", 1937 offset, sgl->unkeyed.length); 1938 1939 if (offset > max_len) { 1940 SPDK_ERRLOG("In-capsule offset 0x%" PRIx64 " exceeds capsule length 0x%x\n", 1941 offset, max_len); 1942 rsp->status.sc = SPDK_NVME_SC_INVALID_SGL_OFFSET; 1943 return -1; 1944 } 1945 max_len -= (uint32_t)offset; 1946 1947 if (sgl->unkeyed.length > max_len) { 1948 SPDK_ERRLOG("In-capsule data length 0x%x exceeds capsule length 0x%x\n", 1949 sgl->unkeyed.length, max_len); 1950 rsp->status.sc = SPDK_NVME_SC_DATA_SGL_LENGTH_INVALID; 1951 return -1; 1952 } 1953 1954 rdma_req->num_outstanding_data_wr = 0; 1955 req->data = rdma_req->recv->buf + offset; 1956 req->data_from_pool = false; 1957 req->length = sgl->unkeyed.length; 1958 1959 req->iov[0].iov_base = req->data; 1960 req->iov[0].iov_len = req->length; 1961 req->iovcnt = 1; 1962 1963 return 0; 1964 } else if (sgl->generic.type == SPDK_NVME_SGL_TYPE_LAST_SEGMENT && 1965 sgl->unkeyed.subtype == SPDK_NVME_SGL_SUBTYPE_OFFSET) { 1966 1967 rc = nvmf_rdma_request_fill_iovs_multi_sgl(rtransport, device, rdma_req); 1968 if (rc == -ENOMEM) { 1969 SPDK_DEBUGLOG(SPDK_LOG_RDMA, "No available large data buffers. Queueing request %p\n", rdma_req); 1970 return 0; 1971 } else if (rc == -EINVAL) { 1972 SPDK_ERRLOG("Multi SGL element request length exceeds the max I/O size\n"); 1973 rsp->status.sc = SPDK_NVME_SC_DATA_SGL_LENGTH_INVALID; 1974 return -1; 1975 } 1976 1977 /* backward compatible */ 1978 req->data = req->iov[0].iov_base; 1979 1980 SPDK_DEBUGLOG(SPDK_LOG_RDMA, "Request %p took %d buffer/s from central pool\n", rdma_req, 1981 req->iovcnt); 1982 1983 return 0; 1984 } 1985 1986 SPDK_ERRLOG("Invalid NVMf I/O Command SGL: Type 0x%x, Subtype 0x%x\n", 1987 sgl->generic.type, sgl->generic.subtype); 1988 rsp->status.sc = SPDK_NVME_SC_SGL_DESCRIPTOR_TYPE_INVALID; 1989 return -1; 1990 } 1991 1992 static void 1993 nvmf_rdma_request_free(struct spdk_nvmf_rdma_request *rdma_req, 1994 struct spdk_nvmf_rdma_transport *rtransport) 1995 { 1996 struct spdk_nvmf_rdma_qpair *rqpair; 1997 struct spdk_nvmf_rdma_poll_group *rgroup; 1998 1999 rqpair = SPDK_CONTAINEROF(rdma_req->req.qpair, struct spdk_nvmf_rdma_qpair, qpair); 2000 if (rdma_req->req.data_from_pool) { 2001 rgroup = rqpair->poller->group; 2002 2003 spdk_nvmf_request_free_buffers(&rdma_req->req, &rgroup->group, &rtransport->transport); 2004 } 2005 nvmf_rdma_request_free_data(rdma_req, rtransport); 2006 rdma_req->req.length = 0; 2007 rdma_req->req.iovcnt = 0; 2008 rdma_req->req.data = NULL; 2009 rdma_req->rsp.wr.next = NULL; 2010 rdma_req->data.wr.next = NULL; 2011 memset(&rdma_req->req.dif, 0, sizeof(rdma_req->req.dif)); 2012 rqpair->qd--; 2013 2014 STAILQ_INSERT_HEAD(&rqpair->resources->free_queue, rdma_req, state_link); 2015 rdma_req->state = RDMA_REQUEST_STATE_FREE; 2016 } 2017 2018 bool 2019 spdk_nvmf_rdma_request_process(struct spdk_nvmf_rdma_transport *rtransport, 2020 struct spdk_nvmf_rdma_request *rdma_req) 2021 { 2022 struct spdk_nvmf_rdma_qpair *rqpair; 2023 struct spdk_nvmf_rdma_device *device; 2024 struct spdk_nvmf_rdma_poll_group *rgroup; 2025 struct spdk_nvme_cpl *rsp = &rdma_req->req.rsp->nvme_cpl; 2026 int rc; 2027 struct spdk_nvmf_rdma_recv *rdma_recv; 2028 enum spdk_nvmf_rdma_request_state prev_state; 2029 bool progress = false; 2030 int data_posted; 2031 uint32_t num_blocks; 2032 2033 rqpair = SPDK_CONTAINEROF(rdma_req->req.qpair, struct spdk_nvmf_rdma_qpair, qpair); 2034 device = rqpair->device; 2035 rgroup = rqpair->poller->group; 2036 2037 assert(rdma_req->state != RDMA_REQUEST_STATE_FREE); 2038 2039 /* If the queue pair is in an error state, force the request to the completed state 2040 * to release resources. */ 2041 if (rqpair->ibv_state == IBV_QPS_ERR || rqpair->qpair.state != SPDK_NVMF_QPAIR_ACTIVE) { 2042 if (rdma_req->state == RDMA_REQUEST_STATE_NEED_BUFFER) { 2043 STAILQ_REMOVE(&rgroup->group.pending_buf_queue, &rdma_req->req, spdk_nvmf_request, buf_link); 2044 } else if (rdma_req->state == RDMA_REQUEST_STATE_DATA_TRANSFER_TO_CONTROLLER_PENDING) { 2045 STAILQ_REMOVE(&rqpair->pending_rdma_read_queue, rdma_req, spdk_nvmf_rdma_request, state_link); 2046 } else if (rdma_req->state == RDMA_REQUEST_STATE_DATA_TRANSFER_TO_HOST_PENDING) { 2047 STAILQ_REMOVE(&rqpair->pending_rdma_write_queue, rdma_req, spdk_nvmf_rdma_request, state_link); 2048 } 2049 rdma_req->state = RDMA_REQUEST_STATE_COMPLETED; 2050 } 2051 2052 /* The loop here is to allow for several back-to-back state changes. */ 2053 do { 2054 prev_state = rdma_req->state; 2055 2056 SPDK_DEBUGLOG(SPDK_LOG_RDMA, "Request %p entering state %d\n", rdma_req, prev_state); 2057 2058 switch (rdma_req->state) { 2059 case RDMA_REQUEST_STATE_FREE: 2060 /* Some external code must kick a request into RDMA_REQUEST_STATE_NEW 2061 * to escape this state. */ 2062 break; 2063 case RDMA_REQUEST_STATE_NEW: 2064 spdk_trace_record(TRACE_RDMA_REQUEST_STATE_NEW, 0, 0, 2065 (uintptr_t)rdma_req, (uintptr_t)rqpair->cm_id); 2066 rdma_recv = rdma_req->recv; 2067 2068 /* The first element of the SGL is the NVMe command */ 2069 rdma_req->req.cmd = (union nvmf_h2c_msg *)rdma_recv->sgl[0].addr; 2070 memset(rdma_req->req.rsp, 0, sizeof(*rdma_req->req.rsp)); 2071 2072 if (rqpair->ibv_state == IBV_QPS_ERR || rqpair->qpair.state != SPDK_NVMF_QPAIR_ACTIVE) { 2073 rdma_req->state = RDMA_REQUEST_STATE_COMPLETED; 2074 break; 2075 } 2076 2077 if (spdk_unlikely(spdk_nvmf_request_get_dif_ctx(&rdma_req->req, &rdma_req->req.dif.dif_ctx))) { 2078 rdma_req->req.dif.dif_insert_or_strip = true; 2079 } 2080 2081 #ifdef SPDK_CONFIG_RDMA_SEND_WITH_INVAL 2082 rdma_req->rsp.wr.opcode = IBV_WR_SEND; 2083 rdma_req->rsp.wr.imm_data = 0; 2084 #endif 2085 2086 /* The next state transition depends on the data transfer needs of this request. */ 2087 rdma_req->req.xfer = spdk_nvmf_req_get_xfer(&rdma_req->req); 2088 2089 /* If no data to transfer, ready to execute. */ 2090 if (rdma_req->req.xfer == SPDK_NVME_DATA_NONE) { 2091 rdma_req->state = RDMA_REQUEST_STATE_READY_TO_EXECUTE; 2092 break; 2093 } 2094 2095 rdma_req->state = RDMA_REQUEST_STATE_NEED_BUFFER; 2096 STAILQ_INSERT_TAIL(&rgroup->group.pending_buf_queue, &rdma_req->req, buf_link); 2097 break; 2098 case RDMA_REQUEST_STATE_NEED_BUFFER: 2099 spdk_trace_record(TRACE_RDMA_REQUEST_STATE_NEED_BUFFER, 0, 0, 2100 (uintptr_t)rdma_req, (uintptr_t)rqpair->cm_id); 2101 2102 assert(rdma_req->req.xfer != SPDK_NVME_DATA_NONE); 2103 2104 if (&rdma_req->req != STAILQ_FIRST(&rgroup->group.pending_buf_queue)) { 2105 /* This request needs to wait in line to obtain a buffer */ 2106 break; 2107 } 2108 2109 /* Try to get a data buffer */ 2110 rc = spdk_nvmf_rdma_request_parse_sgl(rtransport, device, rdma_req); 2111 if (rc < 0) { 2112 STAILQ_REMOVE_HEAD(&rgroup->group.pending_buf_queue, buf_link); 2113 rdma_req->state = RDMA_REQUEST_STATE_READY_TO_COMPLETE; 2114 break; 2115 } 2116 2117 if (!rdma_req->req.data) { 2118 /* No buffers available. */ 2119 rgroup->stat.pending_data_buffer++; 2120 break; 2121 } 2122 2123 STAILQ_REMOVE_HEAD(&rgroup->group.pending_buf_queue, buf_link); 2124 2125 /* If data is transferring from host to controller and the data didn't 2126 * arrive using in capsule data, we need to do a transfer from the host. 2127 */ 2128 if (rdma_req->req.xfer == SPDK_NVME_DATA_HOST_TO_CONTROLLER && 2129 rdma_req->req.data_from_pool) { 2130 STAILQ_INSERT_TAIL(&rqpair->pending_rdma_read_queue, rdma_req, state_link); 2131 rdma_req->state = RDMA_REQUEST_STATE_DATA_TRANSFER_TO_CONTROLLER_PENDING; 2132 break; 2133 } 2134 2135 rdma_req->state = RDMA_REQUEST_STATE_READY_TO_EXECUTE; 2136 break; 2137 case RDMA_REQUEST_STATE_DATA_TRANSFER_TO_CONTROLLER_PENDING: 2138 spdk_trace_record(TRACE_RDMA_REQUEST_STATE_DATA_TRANSFER_TO_CONTROLLER_PENDING, 0, 0, 2139 (uintptr_t)rdma_req, (uintptr_t)rqpair->cm_id); 2140 2141 if (rdma_req != STAILQ_FIRST(&rqpair->pending_rdma_read_queue)) { 2142 /* This request needs to wait in line to perform RDMA */ 2143 break; 2144 } 2145 if (rqpair->current_send_depth + rdma_req->num_outstanding_data_wr > rqpair->max_send_depth 2146 || rqpair->current_read_depth + rdma_req->num_outstanding_data_wr > rqpair->max_read_depth) { 2147 /* We can only have so many WRs outstanding. we have to wait until some finish. */ 2148 rqpair->poller->stat.pending_rdma_read++; 2149 break; 2150 } 2151 2152 /* We have already verified that this request is the head of the queue. */ 2153 STAILQ_REMOVE_HEAD(&rqpair->pending_rdma_read_queue, state_link); 2154 2155 rc = request_transfer_in(&rdma_req->req); 2156 if (!rc) { 2157 rdma_req->state = RDMA_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER; 2158 } else { 2159 rsp->status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR; 2160 rdma_req->state = RDMA_REQUEST_STATE_READY_TO_COMPLETE; 2161 } 2162 break; 2163 case RDMA_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER: 2164 spdk_trace_record(TRACE_RDMA_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER, 0, 0, 2165 (uintptr_t)rdma_req, (uintptr_t)rqpair->cm_id); 2166 /* Some external code must kick a request into RDMA_REQUEST_STATE_READY_TO_EXECUTE 2167 * to escape this state. */ 2168 break; 2169 case RDMA_REQUEST_STATE_READY_TO_EXECUTE: 2170 spdk_trace_record(TRACE_RDMA_REQUEST_STATE_READY_TO_EXECUTE, 0, 0, 2171 (uintptr_t)rdma_req, (uintptr_t)rqpair->cm_id); 2172 2173 if (spdk_unlikely(rdma_req->req.dif.dif_insert_or_strip)) { 2174 if (rdma_req->req.xfer == SPDK_NVME_DATA_HOST_TO_CONTROLLER) { 2175 /* generate DIF for write operation */ 2176 num_blocks = SPDK_CEIL_DIV(rdma_req->req.dif.elba_length, rdma_req->req.dif.dif_ctx.block_size); 2177 assert(num_blocks > 0); 2178 2179 rc = spdk_dif_generate(rdma_req->req.iov, rdma_req->req.iovcnt, 2180 num_blocks, &rdma_req->req.dif.dif_ctx); 2181 if (rc != 0) { 2182 SPDK_ERRLOG("DIF generation failed\n"); 2183 rdma_req->state = RDMA_REQUEST_STATE_COMPLETED; 2184 spdk_nvmf_rdma_start_disconnect(rqpair); 2185 break; 2186 } 2187 } 2188 2189 assert(rdma_req->req.dif.elba_length >= rdma_req->req.length); 2190 /* set extended length before IO operation */ 2191 rdma_req->req.length = rdma_req->req.dif.elba_length; 2192 } 2193 2194 rdma_req->state = RDMA_REQUEST_STATE_EXECUTING; 2195 spdk_nvmf_request_exec(&rdma_req->req); 2196 break; 2197 case RDMA_REQUEST_STATE_EXECUTING: 2198 spdk_trace_record(TRACE_RDMA_REQUEST_STATE_EXECUTING, 0, 0, 2199 (uintptr_t)rdma_req, (uintptr_t)rqpair->cm_id); 2200 /* Some external code must kick a request into RDMA_REQUEST_STATE_EXECUTED 2201 * to escape this state. */ 2202 break; 2203 case RDMA_REQUEST_STATE_EXECUTED: 2204 spdk_trace_record(TRACE_RDMA_REQUEST_STATE_EXECUTED, 0, 0, 2205 (uintptr_t)rdma_req, (uintptr_t)rqpair->cm_id); 2206 if (rdma_req->req.xfer == SPDK_NVME_DATA_CONTROLLER_TO_HOST) { 2207 STAILQ_INSERT_TAIL(&rqpair->pending_rdma_write_queue, rdma_req, state_link); 2208 rdma_req->state = RDMA_REQUEST_STATE_DATA_TRANSFER_TO_HOST_PENDING; 2209 } else { 2210 rdma_req->state = RDMA_REQUEST_STATE_READY_TO_COMPLETE; 2211 } 2212 if (spdk_unlikely(rdma_req->req.dif.dif_insert_or_strip)) { 2213 /* restore the original length */ 2214 rdma_req->req.length = rdma_req->req.dif.orig_length; 2215 2216 if (rdma_req->req.xfer == SPDK_NVME_DATA_CONTROLLER_TO_HOST) { 2217 struct spdk_dif_error error_blk; 2218 2219 num_blocks = SPDK_CEIL_DIV(rdma_req->req.dif.elba_length, rdma_req->req.dif.dif_ctx.block_size); 2220 2221 rc = spdk_dif_verify(rdma_req->req.iov, rdma_req->req.iovcnt, num_blocks, 2222 &rdma_req->req.dif.dif_ctx, &error_blk); 2223 if (rc) { 2224 struct spdk_nvme_cpl *rsp = &rdma_req->req.rsp->nvme_cpl; 2225 2226 SPDK_ERRLOG("DIF error detected. type=%d, offset=%" PRIu32 "\n", error_blk.err_type, 2227 error_blk.err_offset); 2228 rsp->status.sct = SPDK_NVME_SCT_MEDIA_ERROR; 2229 rsp->status.sc = spdk_nvmf_rdma_dif_error_to_compl_status(error_blk.err_type); 2230 rdma_req->state = RDMA_REQUEST_STATE_READY_TO_COMPLETE; 2231 STAILQ_REMOVE(&rqpair->pending_rdma_write_queue, rdma_req, spdk_nvmf_rdma_request, state_link); 2232 } 2233 } 2234 } 2235 break; 2236 case RDMA_REQUEST_STATE_DATA_TRANSFER_TO_HOST_PENDING: 2237 spdk_trace_record(TRACE_RDMA_REQUEST_STATE_DATA_TRANSFER_TO_HOST_PENDING, 0, 0, 2238 (uintptr_t)rdma_req, (uintptr_t)rqpair->cm_id); 2239 2240 if (rdma_req != STAILQ_FIRST(&rqpair->pending_rdma_write_queue)) { 2241 /* This request needs to wait in line to perform RDMA */ 2242 break; 2243 } 2244 if ((rqpair->current_send_depth + rdma_req->num_outstanding_data_wr + 1) > 2245 rqpair->max_send_depth) { 2246 /* We can only have so many WRs outstanding. we have to wait until some finish. 2247 * +1 since each request has an additional wr in the resp. */ 2248 rqpair->poller->stat.pending_rdma_write++; 2249 break; 2250 } 2251 2252 /* We have already verified that this request is the head of the queue. */ 2253 STAILQ_REMOVE_HEAD(&rqpair->pending_rdma_write_queue, state_link); 2254 2255 /* The data transfer will be kicked off from 2256 * RDMA_REQUEST_STATE_READY_TO_COMPLETE state. 2257 */ 2258 rdma_req->state = RDMA_REQUEST_STATE_READY_TO_COMPLETE; 2259 break; 2260 case RDMA_REQUEST_STATE_READY_TO_COMPLETE: 2261 spdk_trace_record(TRACE_RDMA_REQUEST_STATE_READY_TO_COMPLETE, 0, 0, 2262 (uintptr_t)rdma_req, (uintptr_t)rqpair->cm_id); 2263 rc = request_transfer_out(&rdma_req->req, &data_posted); 2264 assert(rc == 0); /* No good way to handle this currently */ 2265 if (rc) { 2266 rdma_req->state = RDMA_REQUEST_STATE_COMPLETED; 2267 } else { 2268 rdma_req->state = data_posted ? RDMA_REQUEST_STATE_TRANSFERRING_CONTROLLER_TO_HOST : 2269 RDMA_REQUEST_STATE_COMPLETING; 2270 } 2271 break; 2272 case RDMA_REQUEST_STATE_TRANSFERRING_CONTROLLER_TO_HOST: 2273 spdk_trace_record(TRACE_RDMA_REQUEST_STATE_TRANSFERRING_CONTROLLER_TO_HOST, 0, 0, 2274 (uintptr_t)rdma_req, (uintptr_t)rqpair->cm_id); 2275 /* Some external code must kick a request into RDMA_REQUEST_STATE_COMPLETED 2276 * to escape this state. */ 2277 break; 2278 case RDMA_REQUEST_STATE_COMPLETING: 2279 spdk_trace_record(TRACE_RDMA_REQUEST_STATE_COMPLETING, 0, 0, 2280 (uintptr_t)rdma_req, (uintptr_t)rqpair->cm_id); 2281 /* Some external code must kick a request into RDMA_REQUEST_STATE_COMPLETED 2282 * to escape this state. */ 2283 break; 2284 case RDMA_REQUEST_STATE_COMPLETED: 2285 spdk_trace_record(TRACE_RDMA_REQUEST_STATE_COMPLETED, 0, 0, 2286 (uintptr_t)rdma_req, (uintptr_t)rqpair->cm_id); 2287 2288 rqpair->poller->stat.request_latency += spdk_get_ticks() - rdma_req->receive_tsc; 2289 nvmf_rdma_request_free(rdma_req, rtransport); 2290 break; 2291 case RDMA_REQUEST_NUM_STATES: 2292 default: 2293 assert(0); 2294 break; 2295 } 2296 2297 if (rdma_req->state != prev_state) { 2298 progress = true; 2299 } 2300 } while (rdma_req->state != prev_state); 2301 2302 return progress; 2303 } 2304 2305 /* Public API callbacks begin here */ 2306 2307 #define SPDK_NVMF_RDMA_DEFAULT_MAX_QUEUE_DEPTH 128 2308 #define SPDK_NVMF_RDMA_DEFAULT_AQ_DEPTH 128 2309 #define SPDK_NVMF_RDMA_DEFAULT_SRQ_DEPTH 4096 2310 #define SPDK_NVMF_RDMA_DEFAULT_MAX_QPAIRS_PER_CTRLR 128 2311 #define SPDK_NVMF_RDMA_DEFAULT_IN_CAPSULE_DATA_SIZE 4096 2312 #define SPDK_NVMF_RDMA_DEFAULT_MAX_IO_SIZE 131072 2313 #define SPDK_NVMF_RDMA_MIN_IO_BUFFER_SIZE (SPDK_NVMF_RDMA_DEFAULT_MAX_IO_SIZE / SPDK_NVMF_MAX_SGL_ENTRIES) 2314 #define SPDK_NVMF_RDMA_DEFAULT_NUM_SHARED_BUFFERS 4095 2315 #define SPDK_NVMF_RDMA_DEFAULT_BUFFER_CACHE_SIZE 32 2316 #define SPDK_NVMF_RDMA_DEFAULT_NO_SRQ false 2317 #define SPDK_NVMF_RDMA_DIF_INSERT_OR_STRIP false 2318 2319 static void 2320 spdk_nvmf_rdma_opts_init(struct spdk_nvmf_transport_opts *opts) 2321 { 2322 opts->max_queue_depth = SPDK_NVMF_RDMA_DEFAULT_MAX_QUEUE_DEPTH; 2323 opts->max_qpairs_per_ctrlr = SPDK_NVMF_RDMA_DEFAULT_MAX_QPAIRS_PER_CTRLR; 2324 opts->in_capsule_data_size = SPDK_NVMF_RDMA_DEFAULT_IN_CAPSULE_DATA_SIZE; 2325 opts->max_io_size = SPDK_NVMF_RDMA_DEFAULT_MAX_IO_SIZE; 2326 opts->io_unit_size = SPDK_NVMF_RDMA_MIN_IO_BUFFER_SIZE; 2327 opts->max_aq_depth = SPDK_NVMF_RDMA_DEFAULT_AQ_DEPTH; 2328 opts->num_shared_buffers = SPDK_NVMF_RDMA_DEFAULT_NUM_SHARED_BUFFERS; 2329 opts->buf_cache_size = SPDK_NVMF_RDMA_DEFAULT_BUFFER_CACHE_SIZE; 2330 opts->max_srq_depth = SPDK_NVMF_RDMA_DEFAULT_SRQ_DEPTH; 2331 opts->no_srq = SPDK_NVMF_RDMA_DEFAULT_NO_SRQ; 2332 opts->dif_insert_or_strip = SPDK_NVMF_RDMA_DIF_INSERT_OR_STRIP; 2333 } 2334 2335 const struct spdk_mem_map_ops g_nvmf_rdma_map_ops = { 2336 .notify_cb = spdk_nvmf_rdma_mem_notify, 2337 .are_contiguous = spdk_nvmf_rdma_check_contiguous_entries 2338 }; 2339 2340 static int spdk_nvmf_rdma_destroy(struct spdk_nvmf_transport *transport); 2341 2342 static struct spdk_nvmf_transport * 2343 spdk_nvmf_rdma_create(struct spdk_nvmf_transport_opts *opts) 2344 { 2345 int rc; 2346 struct spdk_nvmf_rdma_transport *rtransport; 2347 struct spdk_nvmf_rdma_device *device, *tmp; 2348 struct ibv_context **contexts; 2349 uint32_t i; 2350 int flag; 2351 uint32_t sge_count; 2352 uint32_t min_shared_buffers; 2353 int max_device_sge = SPDK_NVMF_MAX_SGL_ENTRIES; 2354 pthread_mutexattr_t attr; 2355 2356 rtransport = calloc(1, sizeof(*rtransport)); 2357 if (!rtransport) { 2358 return NULL; 2359 } 2360 2361 if (pthread_mutexattr_init(&attr)) { 2362 SPDK_ERRLOG("pthread_mutexattr_init() failed\n"); 2363 free(rtransport); 2364 return NULL; 2365 } 2366 2367 if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE)) { 2368 SPDK_ERRLOG("pthread_mutexattr_settype() failed\n"); 2369 pthread_mutexattr_destroy(&attr); 2370 free(rtransport); 2371 return NULL; 2372 } 2373 2374 if (pthread_mutex_init(&rtransport->lock, &attr)) { 2375 SPDK_ERRLOG("pthread_mutex_init() failed\n"); 2376 pthread_mutexattr_destroy(&attr); 2377 free(rtransport); 2378 return NULL; 2379 } 2380 2381 pthread_mutexattr_destroy(&attr); 2382 2383 TAILQ_INIT(&rtransport->devices); 2384 TAILQ_INIT(&rtransport->ports); 2385 TAILQ_INIT(&rtransport->poll_groups); 2386 2387 rtransport->transport.ops = &spdk_nvmf_transport_rdma; 2388 2389 SPDK_INFOLOG(SPDK_LOG_RDMA, "*** RDMA Transport Init ***\n" 2390 " Transport opts: max_ioq_depth=%d, max_io_size=%d,\n" 2391 " max_qpairs_per_ctrlr=%d, io_unit_size=%d,\n" 2392 " in_capsule_data_size=%d, max_aq_depth=%d,\n" 2393 " num_shared_buffers=%d, max_srq_depth=%d, no_srq=%d\n", 2394 opts->max_queue_depth, 2395 opts->max_io_size, 2396 opts->max_qpairs_per_ctrlr, 2397 opts->io_unit_size, 2398 opts->in_capsule_data_size, 2399 opts->max_aq_depth, 2400 opts->num_shared_buffers, 2401 opts->max_srq_depth, 2402 opts->no_srq); 2403 2404 /* I/O unit size cannot be larger than max I/O size */ 2405 if (opts->io_unit_size > opts->max_io_size) { 2406 opts->io_unit_size = opts->max_io_size; 2407 } 2408 2409 if (opts->num_shared_buffers < (SPDK_NVMF_MAX_SGL_ENTRIES * 2)) { 2410 SPDK_ERRLOG("The number of shared data buffers (%d) is less than" 2411 "the minimum number required to guarantee that forward progress can be made (%d)\n", 2412 opts->num_shared_buffers, (SPDK_NVMF_MAX_SGL_ENTRIES * 2)); 2413 spdk_nvmf_rdma_destroy(&rtransport->transport); 2414 return NULL; 2415 } 2416 2417 min_shared_buffers = spdk_thread_get_count() * opts->buf_cache_size; 2418 if (min_shared_buffers > opts->num_shared_buffers) { 2419 SPDK_ERRLOG("There are not enough buffers to satisfy" 2420 "per-poll group caches for each thread. (%" PRIu32 ")" 2421 "supplied. (%" PRIu32 ") required\n", opts->num_shared_buffers, min_shared_buffers); 2422 SPDK_ERRLOG("Please specify a larger number of shared buffers\n"); 2423 spdk_nvmf_rdma_destroy(&rtransport->transport); 2424 return NULL; 2425 } 2426 2427 sge_count = opts->max_io_size / opts->io_unit_size; 2428 if (sge_count > NVMF_DEFAULT_TX_SGE) { 2429 SPDK_ERRLOG("Unsupported IO Unit size specified, %d bytes\n", opts->io_unit_size); 2430 spdk_nvmf_rdma_destroy(&rtransport->transport); 2431 return NULL; 2432 } 2433 2434 rtransport->event_channel = rdma_create_event_channel(); 2435 if (rtransport->event_channel == NULL) { 2436 SPDK_ERRLOG("rdma_create_event_channel() failed, %s\n", spdk_strerror(errno)); 2437 spdk_nvmf_rdma_destroy(&rtransport->transport); 2438 return NULL; 2439 } 2440 2441 flag = fcntl(rtransport->event_channel->fd, F_GETFL); 2442 if (fcntl(rtransport->event_channel->fd, F_SETFL, flag | O_NONBLOCK) < 0) { 2443 SPDK_ERRLOG("fcntl can't set nonblocking mode for socket, fd: %d (%s)\n", 2444 rtransport->event_channel->fd, spdk_strerror(errno)); 2445 spdk_nvmf_rdma_destroy(&rtransport->transport); 2446 return NULL; 2447 } 2448 2449 rtransport->data_wr_pool = spdk_mempool_create("spdk_nvmf_rdma_wr_data", 2450 opts->max_queue_depth * SPDK_NVMF_MAX_SGL_ENTRIES, 2451 sizeof(struct spdk_nvmf_rdma_request_data), 2452 SPDK_MEMPOOL_DEFAULT_CACHE_SIZE, 2453 SPDK_ENV_SOCKET_ID_ANY); 2454 if (!rtransport->data_wr_pool) { 2455 SPDK_ERRLOG("Unable to allocate work request pool for poll group\n"); 2456 spdk_nvmf_rdma_destroy(&rtransport->transport); 2457 return NULL; 2458 } 2459 2460 contexts = rdma_get_devices(NULL); 2461 if (contexts == NULL) { 2462 SPDK_ERRLOG("rdma_get_devices() failed: %s (%d)\n", spdk_strerror(errno), errno); 2463 spdk_nvmf_rdma_destroy(&rtransport->transport); 2464 return NULL; 2465 } 2466 2467 i = 0; 2468 rc = 0; 2469 while (contexts[i] != NULL) { 2470 device = calloc(1, sizeof(*device)); 2471 if (!device) { 2472 SPDK_ERRLOG("Unable to allocate memory for RDMA devices.\n"); 2473 rc = -ENOMEM; 2474 break; 2475 } 2476 device->context = contexts[i]; 2477 rc = ibv_query_device(device->context, &device->attr); 2478 if (rc < 0) { 2479 SPDK_ERRLOG("Failed to query RDMA device attributes.\n"); 2480 free(device); 2481 break; 2482 2483 } 2484 2485 max_device_sge = spdk_min(max_device_sge, device->attr.max_sge); 2486 2487 #ifdef SPDK_CONFIG_RDMA_SEND_WITH_INVAL 2488 if ((device->attr.device_cap_flags & IBV_DEVICE_MEM_MGT_EXTENSIONS) == 0) { 2489 SPDK_WARNLOG("The libibverbs on this system supports SEND_WITH_INVALIDATE,"); 2490 SPDK_WARNLOG("but the device with vendor ID %u does not.\n", device->attr.vendor_id); 2491 } 2492 2493 /** 2494 * The vendor ID is assigned by the IEEE and an ID of 0 implies Soft-RoCE. 2495 * The Soft-RoCE RXE driver does not currently support send with invalidate, 2496 * but incorrectly reports that it does. There are changes making their way 2497 * through the kernel now that will enable this feature. When they are merged, 2498 * we can conditionally enable this feature. 2499 * 2500 * TODO: enable this for versions of the kernel rxe driver that support it. 2501 */ 2502 if (device->attr.vendor_id == 0) { 2503 device->attr.device_cap_flags &= ~(IBV_DEVICE_MEM_MGT_EXTENSIONS); 2504 } 2505 #endif 2506 2507 /* set up device context async ev fd as NON_BLOCKING */ 2508 flag = fcntl(device->context->async_fd, F_GETFL); 2509 rc = fcntl(device->context->async_fd, F_SETFL, flag | O_NONBLOCK); 2510 if (rc < 0) { 2511 SPDK_ERRLOG("Failed to set context async fd to NONBLOCK.\n"); 2512 free(device); 2513 break; 2514 } 2515 2516 TAILQ_INSERT_TAIL(&rtransport->devices, device, link); 2517 i++; 2518 2519 if (g_nvmf_hooks.get_ibv_pd) { 2520 device->pd = g_nvmf_hooks.get_ibv_pd(NULL, device->context); 2521 } else { 2522 device->pd = ibv_alloc_pd(device->context); 2523 } 2524 2525 if (!device->pd) { 2526 SPDK_ERRLOG("Unable to allocate protection domain.\n"); 2527 rc = -ENOMEM; 2528 break; 2529 } 2530 2531 assert(device->map == NULL); 2532 2533 device->map = spdk_mem_map_alloc(0, &g_nvmf_rdma_map_ops, device->pd); 2534 if (!device->map) { 2535 SPDK_ERRLOG("Unable to allocate memory map for listen address\n"); 2536 rc = -ENOMEM; 2537 break; 2538 } 2539 2540 assert(device->map != NULL); 2541 assert(device->pd != NULL); 2542 } 2543 rdma_free_devices(contexts); 2544 2545 if (opts->io_unit_size * max_device_sge < opts->max_io_size) { 2546 /* divide and round up. */ 2547 opts->io_unit_size = (opts->max_io_size + max_device_sge - 1) / max_device_sge; 2548 2549 /* round up to the nearest 4k. */ 2550 opts->io_unit_size = (opts->io_unit_size + NVMF_DATA_BUFFER_ALIGNMENT - 1) & ~NVMF_DATA_BUFFER_MASK; 2551 2552 opts->io_unit_size = spdk_max(opts->io_unit_size, SPDK_NVMF_RDMA_MIN_IO_BUFFER_SIZE); 2553 SPDK_NOTICELOG("Adjusting the io unit size to fit the device's maximum I/O size. New I/O unit size %u\n", 2554 opts->io_unit_size); 2555 } 2556 2557 if (rc < 0) { 2558 spdk_nvmf_rdma_destroy(&rtransport->transport); 2559 return NULL; 2560 } 2561 2562 /* Set up poll descriptor array to monitor events from RDMA and IB 2563 * in a single poll syscall 2564 */ 2565 rtransport->npoll_fds = i + 1; 2566 i = 0; 2567 rtransport->poll_fds = calloc(rtransport->npoll_fds, sizeof(struct pollfd)); 2568 if (rtransport->poll_fds == NULL) { 2569 SPDK_ERRLOG("poll_fds allocation failed\n"); 2570 spdk_nvmf_rdma_destroy(&rtransport->transport); 2571 return NULL; 2572 } 2573 2574 rtransport->poll_fds[i].fd = rtransport->event_channel->fd; 2575 rtransport->poll_fds[i++].events = POLLIN; 2576 2577 TAILQ_FOREACH_SAFE(device, &rtransport->devices, link, tmp) { 2578 rtransport->poll_fds[i].fd = device->context->async_fd; 2579 rtransport->poll_fds[i++].events = POLLIN; 2580 } 2581 2582 return &rtransport->transport; 2583 } 2584 2585 static int 2586 spdk_nvmf_rdma_destroy(struct spdk_nvmf_transport *transport) 2587 { 2588 struct spdk_nvmf_rdma_transport *rtransport; 2589 struct spdk_nvmf_rdma_port *port, *port_tmp; 2590 struct spdk_nvmf_rdma_device *device, *device_tmp; 2591 2592 rtransport = SPDK_CONTAINEROF(transport, struct spdk_nvmf_rdma_transport, transport); 2593 2594 TAILQ_FOREACH_SAFE(port, &rtransport->ports, link, port_tmp) { 2595 TAILQ_REMOVE(&rtransport->ports, port, link); 2596 rdma_destroy_id(port->id); 2597 free(port); 2598 } 2599 2600 if (rtransport->poll_fds != NULL) { 2601 free(rtransport->poll_fds); 2602 } 2603 2604 if (rtransport->event_channel != NULL) { 2605 rdma_destroy_event_channel(rtransport->event_channel); 2606 } 2607 2608 TAILQ_FOREACH_SAFE(device, &rtransport->devices, link, device_tmp) { 2609 TAILQ_REMOVE(&rtransport->devices, device, link); 2610 if (device->map) { 2611 spdk_mem_map_free(&device->map); 2612 } 2613 if (device->pd) { 2614 if (!g_nvmf_hooks.get_ibv_pd) { 2615 ibv_dealloc_pd(device->pd); 2616 } 2617 } 2618 free(device); 2619 } 2620 2621 if (rtransport->data_wr_pool != NULL) { 2622 if (spdk_mempool_count(rtransport->data_wr_pool) != 2623 (transport->opts.max_queue_depth * SPDK_NVMF_MAX_SGL_ENTRIES)) { 2624 SPDK_ERRLOG("transport wr pool count is %zu but should be %u\n", 2625 spdk_mempool_count(rtransport->data_wr_pool), 2626 transport->opts.max_queue_depth * SPDK_NVMF_MAX_SGL_ENTRIES); 2627 } 2628 } 2629 2630 spdk_mempool_free(rtransport->data_wr_pool); 2631 2632 pthread_mutex_destroy(&rtransport->lock); 2633 free(rtransport); 2634 2635 return 0; 2636 } 2637 2638 static int 2639 spdk_nvmf_rdma_trid_from_cm_id(struct rdma_cm_id *id, 2640 struct spdk_nvme_transport_id *trid, 2641 bool peer); 2642 2643 static int 2644 spdk_nvmf_rdma_listen(struct spdk_nvmf_transport *transport, 2645 const struct spdk_nvme_transport_id *trid) 2646 { 2647 struct spdk_nvmf_rdma_transport *rtransport; 2648 struct spdk_nvmf_rdma_device *device; 2649 struct spdk_nvmf_rdma_port *port; 2650 struct addrinfo *res; 2651 struct addrinfo hints; 2652 int family; 2653 int rc; 2654 2655 rtransport = SPDK_CONTAINEROF(transport, struct spdk_nvmf_rdma_transport, transport); 2656 assert(rtransport->event_channel != NULL); 2657 2658 pthread_mutex_lock(&rtransport->lock); 2659 port = calloc(1, sizeof(*port)); 2660 if (!port) { 2661 SPDK_ERRLOG("Port allocation failed\n"); 2662 pthread_mutex_unlock(&rtransport->lock); 2663 return -ENOMEM; 2664 } 2665 2666 port->trid = trid; 2667 2668 switch (trid->adrfam) { 2669 case SPDK_NVMF_ADRFAM_IPV4: 2670 family = AF_INET; 2671 break; 2672 case SPDK_NVMF_ADRFAM_IPV6: 2673 family = AF_INET6; 2674 break; 2675 default: 2676 SPDK_ERRLOG("Unhandled ADRFAM %d\n", trid->adrfam); 2677 free(port); 2678 pthread_mutex_unlock(&rtransport->lock); 2679 return -EINVAL; 2680 } 2681 2682 memset(&hints, 0, sizeof(hints)); 2683 hints.ai_family = family; 2684 hints.ai_flags = AI_NUMERICSERV; 2685 hints.ai_socktype = SOCK_STREAM; 2686 hints.ai_protocol = 0; 2687 2688 rc = getaddrinfo(trid->traddr, trid->trsvcid, &hints, &res); 2689 if (rc) { 2690 SPDK_ERRLOG("getaddrinfo failed: %s (%d)\n", gai_strerror(rc), rc); 2691 free(port); 2692 pthread_mutex_unlock(&rtransport->lock); 2693 return -EINVAL; 2694 } 2695 2696 rc = rdma_create_id(rtransport->event_channel, &port->id, port, RDMA_PS_TCP); 2697 if (rc < 0) { 2698 SPDK_ERRLOG("rdma_create_id() failed\n"); 2699 freeaddrinfo(res); 2700 free(port); 2701 pthread_mutex_unlock(&rtransport->lock); 2702 return rc; 2703 } 2704 2705 rc = rdma_bind_addr(port->id, res->ai_addr); 2706 freeaddrinfo(res); 2707 2708 if (rc < 0) { 2709 SPDK_ERRLOG("rdma_bind_addr() failed\n"); 2710 rdma_destroy_id(port->id); 2711 free(port); 2712 pthread_mutex_unlock(&rtransport->lock); 2713 return rc; 2714 } 2715 2716 if (!port->id->verbs) { 2717 SPDK_ERRLOG("ibv_context is null\n"); 2718 rdma_destroy_id(port->id); 2719 free(port); 2720 pthread_mutex_unlock(&rtransport->lock); 2721 return -1; 2722 } 2723 2724 rc = rdma_listen(port->id, 10); /* 10 = backlog */ 2725 if (rc < 0) { 2726 SPDK_ERRLOG("rdma_listen() failed\n"); 2727 rdma_destroy_id(port->id); 2728 free(port); 2729 pthread_mutex_unlock(&rtransport->lock); 2730 return rc; 2731 } 2732 2733 TAILQ_FOREACH(device, &rtransport->devices, link) { 2734 if (device->context == port->id->verbs) { 2735 port->device = device; 2736 break; 2737 } 2738 } 2739 if (!port->device) { 2740 SPDK_ERRLOG("Accepted a connection with verbs %p, but unable to find a corresponding device.\n", 2741 port->id->verbs); 2742 rdma_destroy_id(port->id); 2743 free(port); 2744 pthread_mutex_unlock(&rtransport->lock); 2745 return -EINVAL; 2746 } 2747 2748 SPDK_NOTICELOG("*** NVMe/RDMA Target Listening on %s port %s ***\n", 2749 trid->traddr, trid->trsvcid); 2750 2751 TAILQ_INSERT_TAIL(&rtransport->ports, port, link); 2752 pthread_mutex_unlock(&rtransport->lock); 2753 return 0; 2754 } 2755 2756 static void 2757 spdk_nvmf_rdma_stop_listen(struct spdk_nvmf_transport *transport, 2758 const struct spdk_nvme_transport_id *trid) 2759 { 2760 struct spdk_nvmf_rdma_transport *rtransport; 2761 struct spdk_nvmf_rdma_port *port, *tmp; 2762 2763 rtransport = SPDK_CONTAINEROF(transport, struct spdk_nvmf_rdma_transport, transport); 2764 2765 pthread_mutex_lock(&rtransport->lock); 2766 TAILQ_FOREACH_SAFE(port, &rtransport->ports, link, tmp) { 2767 if (spdk_nvme_transport_id_compare(port->trid, trid) == 0) { 2768 TAILQ_REMOVE(&rtransport->ports, port, link); 2769 rdma_destroy_id(port->id); 2770 free(port); 2771 break; 2772 } 2773 } 2774 2775 pthread_mutex_unlock(&rtransport->lock); 2776 } 2777 2778 static void 2779 spdk_nvmf_rdma_qpair_process_pending(struct spdk_nvmf_rdma_transport *rtransport, 2780 struct spdk_nvmf_rdma_qpair *rqpair, bool drain) 2781 { 2782 struct spdk_nvmf_request *req, *tmp; 2783 struct spdk_nvmf_rdma_request *rdma_req, *req_tmp; 2784 struct spdk_nvmf_rdma_resources *resources; 2785 2786 /* We process I/O in the data transfer pending queue at the highest priority. RDMA reads first */ 2787 STAILQ_FOREACH_SAFE(rdma_req, &rqpair->pending_rdma_read_queue, state_link, req_tmp) { 2788 if (spdk_nvmf_rdma_request_process(rtransport, rdma_req) == false && drain == false) { 2789 break; 2790 } 2791 } 2792 2793 /* Then RDMA writes since reads have stronger restrictions than writes */ 2794 STAILQ_FOREACH_SAFE(rdma_req, &rqpair->pending_rdma_write_queue, state_link, req_tmp) { 2795 if (spdk_nvmf_rdma_request_process(rtransport, rdma_req) == false && drain == false) { 2796 break; 2797 } 2798 } 2799 2800 /* The second highest priority is I/O waiting on memory buffers. */ 2801 STAILQ_FOREACH_SAFE(req, &rqpair->poller->group->group.pending_buf_queue, buf_link, tmp) { 2802 rdma_req = SPDK_CONTAINEROF(req, struct spdk_nvmf_rdma_request, req); 2803 if (spdk_nvmf_rdma_request_process(rtransport, rdma_req) == false && drain == false) { 2804 break; 2805 } 2806 } 2807 2808 resources = rqpair->resources; 2809 while (!STAILQ_EMPTY(&resources->free_queue) && !STAILQ_EMPTY(&resources->incoming_queue)) { 2810 rdma_req = STAILQ_FIRST(&resources->free_queue); 2811 STAILQ_REMOVE_HEAD(&resources->free_queue, state_link); 2812 rdma_req->recv = STAILQ_FIRST(&resources->incoming_queue); 2813 STAILQ_REMOVE_HEAD(&resources->incoming_queue, link); 2814 2815 if (rqpair->srq != NULL) { 2816 rdma_req->req.qpair = &rdma_req->recv->qpair->qpair; 2817 rdma_req->recv->qpair->qd++; 2818 } else { 2819 rqpair->qd++; 2820 } 2821 2822 rdma_req->receive_tsc = rdma_req->recv->receive_tsc; 2823 rdma_req->state = RDMA_REQUEST_STATE_NEW; 2824 if (spdk_nvmf_rdma_request_process(rtransport, rdma_req) == false) { 2825 break; 2826 } 2827 } 2828 if (!STAILQ_EMPTY(&resources->incoming_queue) && STAILQ_EMPTY(&resources->free_queue)) { 2829 rqpair->poller->stat.pending_free_request++; 2830 } 2831 } 2832 2833 static void 2834 _nvmf_rdma_qpair_disconnect(void *ctx) 2835 { 2836 struct spdk_nvmf_qpair *qpair = ctx; 2837 2838 spdk_nvmf_qpair_disconnect(qpair, NULL, NULL); 2839 } 2840 2841 static void 2842 _nvmf_rdma_try_disconnect(void *ctx) 2843 { 2844 struct spdk_nvmf_qpair *qpair = ctx; 2845 struct spdk_nvmf_poll_group *group; 2846 2847 /* Read the group out of the qpair. This is normally set and accessed only from 2848 * the thread that created the group. Here, we're not on that thread necessarily. 2849 * The data member qpair->group begins it's life as NULL and then is assigned to 2850 * a pointer and never changes. So fortunately reading this and checking for 2851 * non-NULL is thread safe in the x86_64 memory model. */ 2852 group = qpair->group; 2853 2854 if (group == NULL) { 2855 /* The qpair hasn't been assigned to a group yet, so we can't 2856 * process a disconnect. Send a message to ourself and try again. */ 2857 spdk_thread_send_msg(spdk_get_thread(), _nvmf_rdma_try_disconnect, qpair); 2858 return; 2859 } 2860 2861 spdk_thread_send_msg(group->thread, _nvmf_rdma_qpair_disconnect, qpair); 2862 } 2863 2864 static inline void 2865 spdk_nvmf_rdma_start_disconnect(struct spdk_nvmf_rdma_qpair *rqpair) 2866 { 2867 if (!__atomic_test_and_set(&rqpair->disconnect_started, __ATOMIC_RELAXED)) { 2868 _nvmf_rdma_try_disconnect(&rqpair->qpair); 2869 } 2870 } 2871 2872 static void nvmf_rdma_destroy_drained_qpair(void *ctx) 2873 { 2874 struct spdk_nvmf_rdma_qpair *rqpair = ctx; 2875 struct spdk_nvmf_rdma_transport *rtransport = SPDK_CONTAINEROF(rqpair->qpair.transport, 2876 struct spdk_nvmf_rdma_transport, transport); 2877 2878 /* In non SRQ path, we will reach rqpair->max_queue_depth. In SRQ path, we will get the last_wqe event. */ 2879 if (rqpair->current_send_depth != 0) { 2880 return; 2881 } 2882 2883 if (rqpair->srq == NULL && rqpair->current_recv_depth != rqpair->max_queue_depth) { 2884 return; 2885 } 2886 2887 if (rqpair->srq != NULL && rqpair->last_wqe_reached == false) { 2888 return; 2889 } 2890 2891 spdk_nvmf_rdma_qpair_process_pending(rtransport, rqpair, true); 2892 2893 /* Qpair will be destroyed after nvmf layer closes this qpair */ 2894 if (rqpair->qpair.state != SPDK_NVMF_QPAIR_ERROR) { 2895 return; 2896 } 2897 2898 spdk_nvmf_rdma_qpair_destroy(rqpair); 2899 } 2900 2901 2902 static int 2903 nvmf_rdma_disconnect(struct rdma_cm_event *evt) 2904 { 2905 struct spdk_nvmf_qpair *qpair; 2906 struct spdk_nvmf_rdma_qpair *rqpair; 2907 2908 if (evt->id == NULL) { 2909 SPDK_ERRLOG("disconnect request: missing cm_id\n"); 2910 return -1; 2911 } 2912 2913 qpair = evt->id->context; 2914 if (qpair == NULL) { 2915 SPDK_ERRLOG("disconnect request: no active connection\n"); 2916 return -1; 2917 } 2918 2919 rqpair = SPDK_CONTAINEROF(qpair, struct spdk_nvmf_rdma_qpair, qpair); 2920 2921 spdk_trace_record(TRACE_RDMA_QP_DISCONNECT, 0, 0, (uintptr_t)rqpair->cm_id, 0); 2922 2923 spdk_nvmf_rdma_start_disconnect(rqpair); 2924 2925 return 0; 2926 } 2927 2928 #ifdef DEBUG 2929 static const char *CM_EVENT_STR[] = { 2930 "RDMA_CM_EVENT_ADDR_RESOLVED", 2931 "RDMA_CM_EVENT_ADDR_ERROR", 2932 "RDMA_CM_EVENT_ROUTE_RESOLVED", 2933 "RDMA_CM_EVENT_ROUTE_ERROR", 2934 "RDMA_CM_EVENT_CONNECT_REQUEST", 2935 "RDMA_CM_EVENT_CONNECT_RESPONSE", 2936 "RDMA_CM_EVENT_CONNECT_ERROR", 2937 "RDMA_CM_EVENT_UNREACHABLE", 2938 "RDMA_CM_EVENT_REJECTED", 2939 "RDMA_CM_EVENT_ESTABLISHED", 2940 "RDMA_CM_EVENT_DISCONNECTED", 2941 "RDMA_CM_EVENT_DEVICE_REMOVAL", 2942 "RDMA_CM_EVENT_MULTICAST_JOIN", 2943 "RDMA_CM_EVENT_MULTICAST_ERROR", 2944 "RDMA_CM_EVENT_ADDR_CHANGE", 2945 "RDMA_CM_EVENT_TIMEWAIT_EXIT" 2946 }; 2947 #endif /* DEBUG */ 2948 2949 static void 2950 nvmf_rdma_disconnect_qpairs_on_port(struct spdk_nvmf_rdma_transport *rtransport, 2951 struct spdk_nvmf_rdma_port *port) 2952 { 2953 struct spdk_nvmf_rdma_poll_group *rgroup; 2954 struct spdk_nvmf_rdma_poller *rpoller; 2955 struct spdk_nvmf_rdma_qpair *rqpair; 2956 2957 TAILQ_FOREACH(rgroup, &rtransport->poll_groups, link) { 2958 TAILQ_FOREACH(rpoller, &rgroup->pollers, link) { 2959 TAILQ_FOREACH(rqpair, &rpoller->qpairs, link) { 2960 if (rqpair->listen_id == port->id) { 2961 spdk_nvmf_rdma_start_disconnect(rqpair); 2962 } 2963 } 2964 } 2965 } 2966 } 2967 2968 static bool 2969 nvmf_rdma_handle_cm_event_addr_change(struct spdk_nvmf_transport *transport, 2970 struct rdma_cm_event *event) 2971 { 2972 const struct spdk_nvme_transport_id *trid; 2973 struct spdk_nvmf_rdma_port *port; 2974 struct spdk_nvmf_rdma_transport *rtransport; 2975 bool event_acked = false; 2976 2977 rtransport = SPDK_CONTAINEROF(transport, struct spdk_nvmf_rdma_transport, transport); 2978 TAILQ_FOREACH(port, &rtransport->ports, link) { 2979 if (port->id == event->id) { 2980 SPDK_ERRLOG("ADDR_CHANGE: IP %s:%s migrated\n", port->trid->traddr, port->trid->trsvcid); 2981 rdma_ack_cm_event(event); 2982 event_acked = true; 2983 trid = port->trid; 2984 break; 2985 } 2986 } 2987 2988 if (event_acked) { 2989 nvmf_rdma_disconnect_qpairs_on_port(rtransport, port); 2990 2991 spdk_nvmf_rdma_stop_listen(transport, trid); 2992 spdk_nvmf_rdma_listen(transport, trid); 2993 } 2994 2995 return event_acked; 2996 } 2997 2998 static void 2999 nvmf_rdma_handle_cm_event_port_removal(struct spdk_nvmf_transport *transport, 3000 struct rdma_cm_event *event) 3001 { 3002 struct spdk_nvmf_rdma_port *port; 3003 struct spdk_nvmf_rdma_transport *rtransport; 3004 3005 port = event->id->context; 3006 rtransport = SPDK_CONTAINEROF(transport, struct spdk_nvmf_rdma_transport, transport); 3007 3008 SPDK_NOTICELOG("Port %s:%s is being removed\n", port->trid->traddr, port->trid->trsvcid); 3009 3010 nvmf_rdma_disconnect_qpairs_on_port(rtransport, port); 3011 3012 rdma_ack_cm_event(event); 3013 3014 while (spdk_nvmf_transport_stop_listen(transport, port->trid) == 0) { 3015 ; 3016 } 3017 } 3018 3019 static void 3020 spdk_nvmf_process_cm_event(struct spdk_nvmf_transport *transport, new_qpair_fn cb_fn, void *cb_arg) 3021 { 3022 struct spdk_nvmf_rdma_transport *rtransport; 3023 struct rdma_cm_event *event; 3024 int rc; 3025 bool event_acked; 3026 3027 rtransport = SPDK_CONTAINEROF(transport, struct spdk_nvmf_rdma_transport, transport); 3028 3029 if (rtransport->event_channel == NULL) { 3030 return; 3031 } 3032 3033 while (1) { 3034 event_acked = false; 3035 rc = rdma_get_cm_event(rtransport->event_channel, &event); 3036 if (rc) { 3037 if (errno != EAGAIN && errno != EWOULDBLOCK) { 3038 SPDK_ERRLOG("Acceptor Event Error: %s\n", spdk_strerror(errno)); 3039 } 3040 break; 3041 } 3042 3043 SPDK_DEBUGLOG(SPDK_LOG_RDMA, "Acceptor Event: %s\n", CM_EVENT_STR[event->event]); 3044 3045 spdk_trace_record(TRACE_RDMA_CM_ASYNC_EVENT, 0, 0, 0, event->event); 3046 3047 switch (event->event) { 3048 case RDMA_CM_EVENT_ADDR_RESOLVED: 3049 case RDMA_CM_EVENT_ADDR_ERROR: 3050 case RDMA_CM_EVENT_ROUTE_RESOLVED: 3051 case RDMA_CM_EVENT_ROUTE_ERROR: 3052 /* No action required. The target never attempts to resolve routes. */ 3053 break; 3054 case RDMA_CM_EVENT_CONNECT_REQUEST: 3055 rc = nvmf_rdma_connect(transport, event, cb_fn, cb_arg); 3056 if (rc < 0) { 3057 SPDK_ERRLOG("Unable to process connect event. rc: %d\n", rc); 3058 break; 3059 } 3060 break; 3061 case RDMA_CM_EVENT_CONNECT_RESPONSE: 3062 /* The target never initiates a new connection. So this will not occur. */ 3063 break; 3064 case RDMA_CM_EVENT_CONNECT_ERROR: 3065 /* Can this happen? The docs say it can, but not sure what causes it. */ 3066 break; 3067 case RDMA_CM_EVENT_UNREACHABLE: 3068 case RDMA_CM_EVENT_REJECTED: 3069 /* These only occur on the client side. */ 3070 break; 3071 case RDMA_CM_EVENT_ESTABLISHED: 3072 /* TODO: Should we be waiting for this event anywhere? */ 3073 break; 3074 case RDMA_CM_EVENT_DISCONNECTED: 3075 rc = nvmf_rdma_disconnect(event); 3076 if (rc < 0) { 3077 SPDK_ERRLOG("Unable to process disconnect event. rc: %d\n", rc); 3078 break; 3079 } 3080 break; 3081 case RDMA_CM_EVENT_DEVICE_REMOVAL: 3082 /* In case of device removal, kernel IB part triggers IBV_EVENT_DEVICE_FATAL 3083 * which triggers RDMA_CM_EVENT_DEVICE_REMOVAL on all cma_id’s. 3084 * Once these events are sent to SPDK, we should release all IB resources and 3085 * don't make attempts to call any ibv_query/modify/create functions. We can only call 3086 * ibv_destory* functions to release user space memory allocated by IB. All kernel 3087 * resources are already cleaned. */ 3088 if (event->id->qp) { 3089 /* If rdma_cm event has a valid `qp` pointer then the event refers to the 3090 * corresponding qpair. Otherwise the event refers to a listening device */ 3091 rc = nvmf_rdma_disconnect(event); 3092 if (rc < 0) { 3093 SPDK_ERRLOG("Unable to process disconnect event. rc: %d\n", rc); 3094 break; 3095 } 3096 } else { 3097 nvmf_rdma_handle_cm_event_port_removal(transport, event); 3098 event_acked = true; 3099 } 3100 break; 3101 case RDMA_CM_EVENT_MULTICAST_JOIN: 3102 case RDMA_CM_EVENT_MULTICAST_ERROR: 3103 /* Multicast is not used */ 3104 break; 3105 case RDMA_CM_EVENT_ADDR_CHANGE: 3106 event_acked = nvmf_rdma_handle_cm_event_addr_change(transport, event); 3107 break; 3108 case RDMA_CM_EVENT_TIMEWAIT_EXIT: 3109 /* For now, do nothing. The target never re-uses queue pairs. */ 3110 break; 3111 default: 3112 SPDK_ERRLOG("Unexpected Acceptor Event [%d]\n", event->event); 3113 break; 3114 } 3115 if (!event_acked) { 3116 rdma_ack_cm_event(event); 3117 } 3118 } 3119 } 3120 3121 static void 3122 nvmf_rdma_handle_qp_fatal(struct spdk_nvmf_rdma_qpair *rqpair) 3123 { 3124 spdk_nvmf_rdma_update_ibv_state(rqpair); 3125 spdk_nvmf_rdma_start_disconnect(rqpair); 3126 } 3127 3128 static void 3129 nvmf_rdma_handle_last_wqe_reached(struct spdk_nvmf_rdma_qpair *rqpair) 3130 { 3131 rqpair->last_wqe_reached = true; 3132 nvmf_rdma_destroy_drained_qpair(rqpair); 3133 } 3134 3135 static void 3136 nvmf_rdma_handle_sq_drained(struct spdk_nvmf_rdma_qpair *rqpair) 3137 { 3138 spdk_nvmf_rdma_start_disconnect(rqpair); 3139 } 3140 3141 static void 3142 spdk_nvmf_rdma_qpair_process_ibv_event(void *ctx) 3143 { 3144 struct spdk_nvmf_rdma_ibv_event_ctx *event_ctx = ctx; 3145 3146 if (event_ctx->rqpair) { 3147 STAILQ_REMOVE(&event_ctx->rqpair->ibv_events, event_ctx, spdk_nvmf_rdma_ibv_event_ctx, link); 3148 if (event_ctx->cb_fn) { 3149 event_ctx->cb_fn(event_ctx->rqpair); 3150 } 3151 } 3152 free(event_ctx); 3153 } 3154 3155 static int 3156 spdk_nvmf_rdma_send_qpair_async_event(struct spdk_nvmf_rdma_qpair *rqpair, 3157 spdk_nvmf_rdma_qpair_ibv_event fn) 3158 { 3159 struct spdk_nvmf_rdma_ibv_event_ctx *ctx; 3160 3161 if (!rqpair->qpair.group) { 3162 return EINVAL; 3163 } 3164 3165 ctx = calloc(1, sizeof(*ctx)); 3166 if (!ctx) { 3167 return ENOMEM; 3168 } 3169 3170 ctx->rqpair = rqpair; 3171 ctx->cb_fn = fn; 3172 STAILQ_INSERT_TAIL(&rqpair->ibv_events, ctx, link); 3173 3174 return spdk_thread_send_msg(rqpair->qpair.group->thread, spdk_nvmf_rdma_qpair_process_ibv_event, 3175 ctx); 3176 } 3177 3178 static void 3179 spdk_nvmf_process_ib_event(struct spdk_nvmf_rdma_device *device) 3180 { 3181 int rc; 3182 struct spdk_nvmf_rdma_qpair *rqpair = NULL; 3183 struct ibv_async_event event; 3184 3185 rc = ibv_get_async_event(device->context, &event); 3186 3187 if (rc) { 3188 SPDK_ERRLOG("Failed to get async_event (%d): %s\n", 3189 errno, spdk_strerror(errno)); 3190 return; 3191 } 3192 3193 switch (event.event_type) { 3194 case IBV_EVENT_QP_FATAL: 3195 rqpair = event.element.qp->qp_context; 3196 SPDK_ERRLOG("Fatal event received for rqpair %p\n", rqpair); 3197 spdk_trace_record(TRACE_RDMA_IBV_ASYNC_EVENT, 0, 0, 3198 (uintptr_t)rqpair->cm_id, event.event_type); 3199 if (spdk_nvmf_rdma_send_qpair_async_event(rqpair, nvmf_rdma_handle_qp_fatal)) { 3200 SPDK_ERRLOG("Failed to send QP_FATAL event for rqpair %p\n", rqpair); 3201 nvmf_rdma_handle_qp_fatal(rqpair); 3202 } 3203 break; 3204 case IBV_EVENT_QP_LAST_WQE_REACHED: 3205 /* This event only occurs for shared receive queues. */ 3206 rqpair = event.element.qp->qp_context; 3207 SPDK_DEBUGLOG(SPDK_LOG_RDMA, "Last WQE reached event received for rqpair %p\n", rqpair); 3208 if (spdk_nvmf_rdma_send_qpair_async_event(rqpair, nvmf_rdma_handle_last_wqe_reached)) { 3209 SPDK_ERRLOG("Failed to send LAST_WQE_REACHED event for rqpair %p\n", rqpair); 3210 rqpair->last_wqe_reached = true; 3211 } 3212 break; 3213 case IBV_EVENT_SQ_DRAINED: 3214 /* This event occurs frequently in both error and non-error states. 3215 * Check if the qpair is in an error state before sending a message. */ 3216 rqpair = event.element.qp->qp_context; 3217 SPDK_DEBUGLOG(SPDK_LOG_RDMA, "Last sq drained event received for rqpair %p\n", rqpair); 3218 spdk_trace_record(TRACE_RDMA_IBV_ASYNC_EVENT, 0, 0, 3219 (uintptr_t)rqpair->cm_id, event.event_type); 3220 if (spdk_nvmf_rdma_update_ibv_state(rqpair) == IBV_QPS_ERR) { 3221 if (spdk_nvmf_rdma_send_qpair_async_event(rqpair, nvmf_rdma_handle_sq_drained)) { 3222 SPDK_ERRLOG("Failed to send SQ_DRAINED event for rqpair %p\n", rqpair); 3223 nvmf_rdma_handle_sq_drained(rqpair); 3224 } 3225 } 3226 break; 3227 case IBV_EVENT_QP_REQ_ERR: 3228 case IBV_EVENT_QP_ACCESS_ERR: 3229 case IBV_EVENT_COMM_EST: 3230 case IBV_EVENT_PATH_MIG: 3231 case IBV_EVENT_PATH_MIG_ERR: 3232 SPDK_NOTICELOG("Async event: %s\n", 3233 ibv_event_type_str(event.event_type)); 3234 rqpair = event.element.qp->qp_context; 3235 spdk_trace_record(TRACE_RDMA_IBV_ASYNC_EVENT, 0, 0, 3236 (uintptr_t)rqpair->cm_id, event.event_type); 3237 spdk_nvmf_rdma_update_ibv_state(rqpair); 3238 break; 3239 case IBV_EVENT_CQ_ERR: 3240 case IBV_EVENT_DEVICE_FATAL: 3241 case IBV_EVENT_PORT_ACTIVE: 3242 case IBV_EVENT_PORT_ERR: 3243 case IBV_EVENT_LID_CHANGE: 3244 case IBV_EVENT_PKEY_CHANGE: 3245 case IBV_EVENT_SM_CHANGE: 3246 case IBV_EVENT_SRQ_ERR: 3247 case IBV_EVENT_SRQ_LIMIT_REACHED: 3248 case IBV_EVENT_CLIENT_REREGISTER: 3249 case IBV_EVENT_GID_CHANGE: 3250 default: 3251 SPDK_NOTICELOG("Async event: %s\n", 3252 ibv_event_type_str(event.event_type)); 3253 spdk_trace_record(TRACE_RDMA_IBV_ASYNC_EVENT, 0, 0, 0, event.event_type); 3254 break; 3255 } 3256 ibv_ack_async_event(&event); 3257 } 3258 3259 static void 3260 spdk_nvmf_rdma_accept(struct spdk_nvmf_transport *transport, new_qpair_fn cb_fn, void *cb_arg) 3261 { 3262 int nfds, i = 0; 3263 struct spdk_nvmf_rdma_transport *rtransport; 3264 struct spdk_nvmf_rdma_device *device, *tmp; 3265 3266 rtransport = SPDK_CONTAINEROF(transport, struct spdk_nvmf_rdma_transport, transport); 3267 nfds = poll(rtransport->poll_fds, rtransport->npoll_fds, 0); 3268 3269 if (nfds <= 0) { 3270 return; 3271 } 3272 3273 /* The first poll descriptor is RDMA CM event */ 3274 if (rtransport->poll_fds[i++].revents & POLLIN) { 3275 spdk_nvmf_process_cm_event(transport, cb_fn, cb_arg); 3276 nfds--; 3277 } 3278 3279 if (nfds == 0) { 3280 return; 3281 } 3282 3283 /* Second and subsequent poll descriptors are IB async events */ 3284 TAILQ_FOREACH_SAFE(device, &rtransport->devices, link, tmp) { 3285 if (rtransport->poll_fds[i++].revents & POLLIN) { 3286 spdk_nvmf_process_ib_event(device); 3287 nfds--; 3288 } 3289 } 3290 /* check all flagged fd's have been served */ 3291 assert(nfds == 0); 3292 } 3293 3294 static void 3295 spdk_nvmf_rdma_discover(struct spdk_nvmf_transport *transport, 3296 struct spdk_nvme_transport_id *trid, 3297 struct spdk_nvmf_discovery_log_page_entry *entry) 3298 { 3299 entry->trtype = SPDK_NVMF_TRTYPE_RDMA; 3300 entry->adrfam = trid->adrfam; 3301 entry->treq.secure_channel = SPDK_NVMF_TREQ_SECURE_CHANNEL_NOT_REQUIRED; 3302 3303 spdk_strcpy_pad(entry->trsvcid, trid->trsvcid, sizeof(entry->trsvcid), ' '); 3304 spdk_strcpy_pad(entry->traddr, trid->traddr, sizeof(entry->traddr), ' '); 3305 3306 entry->tsas.rdma.rdma_qptype = SPDK_NVMF_RDMA_QPTYPE_RELIABLE_CONNECTED; 3307 entry->tsas.rdma.rdma_prtype = SPDK_NVMF_RDMA_PRTYPE_NONE; 3308 entry->tsas.rdma.rdma_cms = SPDK_NVMF_RDMA_CMS_RDMA_CM; 3309 } 3310 3311 static void 3312 spdk_nvmf_rdma_poll_group_destroy(struct spdk_nvmf_transport_poll_group *group); 3313 3314 static struct spdk_nvmf_transport_poll_group * 3315 spdk_nvmf_rdma_poll_group_create(struct spdk_nvmf_transport *transport) 3316 { 3317 struct spdk_nvmf_rdma_transport *rtransport; 3318 struct spdk_nvmf_rdma_poll_group *rgroup; 3319 struct spdk_nvmf_rdma_poller *poller; 3320 struct spdk_nvmf_rdma_device *device; 3321 struct ibv_srq_init_attr srq_init_attr; 3322 struct spdk_nvmf_rdma_resource_opts opts; 3323 int num_cqe; 3324 3325 rtransport = SPDK_CONTAINEROF(transport, struct spdk_nvmf_rdma_transport, transport); 3326 3327 rgroup = calloc(1, sizeof(*rgroup)); 3328 if (!rgroup) { 3329 return NULL; 3330 } 3331 3332 TAILQ_INIT(&rgroup->pollers); 3333 STAILQ_INIT(&rgroup->retired_bufs); 3334 3335 pthread_mutex_lock(&rtransport->lock); 3336 TAILQ_FOREACH(device, &rtransport->devices, link) { 3337 poller = calloc(1, sizeof(*poller)); 3338 if (!poller) { 3339 SPDK_ERRLOG("Unable to allocate memory for new RDMA poller\n"); 3340 spdk_nvmf_rdma_poll_group_destroy(&rgroup->group); 3341 pthread_mutex_unlock(&rtransport->lock); 3342 return NULL; 3343 } 3344 3345 poller->device = device; 3346 poller->group = rgroup; 3347 3348 TAILQ_INIT(&poller->qpairs); 3349 STAILQ_INIT(&poller->qpairs_pending_send); 3350 STAILQ_INIT(&poller->qpairs_pending_recv); 3351 3352 TAILQ_INSERT_TAIL(&rgroup->pollers, poller, link); 3353 if (transport->opts.no_srq == false && device->num_srq < device->attr.max_srq) { 3354 poller->max_srq_depth = transport->opts.max_srq_depth; 3355 3356 device->num_srq++; 3357 memset(&srq_init_attr, 0, sizeof(struct ibv_srq_init_attr)); 3358 srq_init_attr.attr.max_wr = poller->max_srq_depth; 3359 srq_init_attr.attr.max_sge = spdk_min(device->attr.max_sge, NVMF_DEFAULT_RX_SGE); 3360 poller->srq = ibv_create_srq(device->pd, &srq_init_attr); 3361 if (!poller->srq) { 3362 SPDK_ERRLOG("Unable to create shared receive queue, errno %d\n", errno); 3363 spdk_nvmf_rdma_poll_group_destroy(&rgroup->group); 3364 pthread_mutex_unlock(&rtransport->lock); 3365 return NULL; 3366 } 3367 3368 opts.qp = poller->srq; 3369 opts.pd = device->pd; 3370 opts.qpair = NULL; 3371 opts.shared = true; 3372 opts.max_queue_depth = poller->max_srq_depth; 3373 opts.in_capsule_data_size = transport->opts.in_capsule_data_size; 3374 3375 poller->resources = nvmf_rdma_resources_create(&opts); 3376 if (!poller->resources) { 3377 SPDK_ERRLOG("Unable to allocate resources for shared receive queue.\n"); 3378 spdk_nvmf_rdma_poll_group_destroy(&rgroup->group); 3379 pthread_mutex_unlock(&rtransport->lock); 3380 return NULL; 3381 } 3382 } 3383 3384 /* 3385 * When using an srq, we can limit the completion queue at startup. 3386 * The following formula represents the calculation: 3387 * num_cqe = num_recv + num_data_wr + num_send_wr. 3388 * where num_recv=num_data_wr=and num_send_wr=poller->max_srq_depth 3389 */ 3390 if (poller->srq) { 3391 num_cqe = poller->max_srq_depth * 3; 3392 } else { 3393 num_cqe = DEFAULT_NVMF_RDMA_CQ_SIZE; 3394 } 3395 3396 poller->cq = ibv_create_cq(device->context, num_cqe, poller, NULL, 0); 3397 if (!poller->cq) { 3398 SPDK_ERRLOG("Unable to create completion queue\n"); 3399 spdk_nvmf_rdma_poll_group_destroy(&rgroup->group); 3400 pthread_mutex_unlock(&rtransport->lock); 3401 return NULL; 3402 } 3403 poller->num_cqe = num_cqe; 3404 } 3405 3406 TAILQ_INSERT_TAIL(&rtransport->poll_groups, rgroup, link); 3407 if (rtransport->conn_sched.next_admin_pg == NULL) { 3408 rtransport->conn_sched.next_admin_pg = rgroup; 3409 rtransport->conn_sched.next_io_pg = rgroup; 3410 } 3411 3412 pthread_mutex_unlock(&rtransport->lock); 3413 return &rgroup->group; 3414 } 3415 3416 static struct spdk_nvmf_transport_poll_group * 3417 spdk_nvmf_rdma_get_optimal_poll_group(struct spdk_nvmf_qpair *qpair) 3418 { 3419 struct spdk_nvmf_rdma_transport *rtransport; 3420 struct spdk_nvmf_rdma_poll_group **pg; 3421 struct spdk_nvmf_transport_poll_group *result; 3422 3423 rtransport = SPDK_CONTAINEROF(qpair->transport, struct spdk_nvmf_rdma_transport, transport); 3424 3425 pthread_mutex_lock(&rtransport->lock); 3426 3427 if (TAILQ_EMPTY(&rtransport->poll_groups)) { 3428 pthread_mutex_unlock(&rtransport->lock); 3429 return NULL; 3430 } 3431 3432 if (qpair->qid == 0) { 3433 pg = &rtransport->conn_sched.next_admin_pg; 3434 } else { 3435 pg = &rtransport->conn_sched.next_io_pg; 3436 } 3437 3438 assert(*pg != NULL); 3439 3440 result = &(*pg)->group; 3441 3442 *pg = TAILQ_NEXT(*pg, link); 3443 if (*pg == NULL) { 3444 *pg = TAILQ_FIRST(&rtransport->poll_groups); 3445 } 3446 3447 pthread_mutex_unlock(&rtransport->lock); 3448 3449 return result; 3450 } 3451 3452 static void 3453 spdk_nvmf_rdma_poll_group_destroy(struct spdk_nvmf_transport_poll_group *group) 3454 { 3455 struct spdk_nvmf_rdma_poll_group *rgroup, *next_rgroup; 3456 struct spdk_nvmf_rdma_poller *poller, *tmp; 3457 struct spdk_nvmf_rdma_qpair *qpair, *tmp_qpair; 3458 struct spdk_nvmf_transport_pg_cache_buf *buf, *tmp_buf; 3459 struct spdk_nvmf_rdma_transport *rtransport; 3460 3461 rgroup = SPDK_CONTAINEROF(group, struct spdk_nvmf_rdma_poll_group, group); 3462 if (!rgroup) { 3463 return; 3464 } 3465 3466 /* free all retired buffers back to the transport so we don't short the mempool. */ 3467 STAILQ_FOREACH_SAFE(buf, &rgroup->retired_bufs, link, tmp_buf) { 3468 STAILQ_REMOVE(&rgroup->retired_bufs, buf, spdk_nvmf_transport_pg_cache_buf, link); 3469 assert(group->transport != NULL); 3470 spdk_mempool_put(group->transport->data_buf_pool, buf); 3471 } 3472 3473 TAILQ_FOREACH_SAFE(poller, &rgroup->pollers, link, tmp) { 3474 TAILQ_REMOVE(&rgroup->pollers, poller, link); 3475 3476 TAILQ_FOREACH_SAFE(qpair, &poller->qpairs, link, tmp_qpair) { 3477 spdk_nvmf_rdma_qpair_destroy(qpair); 3478 } 3479 3480 if (poller->srq) { 3481 if (poller->resources) { 3482 nvmf_rdma_resources_destroy(poller->resources); 3483 } 3484 ibv_destroy_srq(poller->srq); 3485 SPDK_DEBUGLOG(SPDK_LOG_RDMA, "Destroyed RDMA shared queue %p\n", poller->srq); 3486 } 3487 3488 if (poller->cq) { 3489 ibv_destroy_cq(poller->cq); 3490 } 3491 3492 free(poller); 3493 } 3494 3495 if (rgroup->group.transport == NULL) { 3496 /* Transport can be NULL when spdk_nvmf_rdma_poll_group_create() 3497 * calls this function directly in a failure path. */ 3498 free(rgroup); 3499 return; 3500 } 3501 3502 rtransport = SPDK_CONTAINEROF(rgroup->group.transport, struct spdk_nvmf_rdma_transport, transport); 3503 3504 pthread_mutex_lock(&rtransport->lock); 3505 next_rgroup = TAILQ_NEXT(rgroup, link); 3506 TAILQ_REMOVE(&rtransport->poll_groups, rgroup, link); 3507 if (next_rgroup == NULL) { 3508 next_rgroup = TAILQ_FIRST(&rtransport->poll_groups); 3509 } 3510 if (rtransport->conn_sched.next_admin_pg == rgroup) { 3511 rtransport->conn_sched.next_admin_pg = next_rgroup; 3512 } 3513 if (rtransport->conn_sched.next_io_pg == rgroup) { 3514 rtransport->conn_sched.next_io_pg = next_rgroup; 3515 } 3516 pthread_mutex_unlock(&rtransport->lock); 3517 3518 free(rgroup); 3519 } 3520 3521 static void 3522 spdk_nvmf_rdma_qpair_reject_connection(struct spdk_nvmf_rdma_qpair *rqpair) 3523 { 3524 if (rqpair->cm_id != NULL) { 3525 spdk_nvmf_rdma_event_reject(rqpair->cm_id, SPDK_NVMF_RDMA_ERROR_NO_RESOURCES); 3526 } 3527 spdk_nvmf_rdma_qpair_destroy(rqpair); 3528 } 3529 3530 static int 3531 spdk_nvmf_rdma_poll_group_add(struct spdk_nvmf_transport_poll_group *group, 3532 struct spdk_nvmf_qpair *qpair) 3533 { 3534 struct spdk_nvmf_rdma_poll_group *rgroup; 3535 struct spdk_nvmf_rdma_qpair *rqpair; 3536 struct spdk_nvmf_rdma_device *device; 3537 struct spdk_nvmf_rdma_poller *poller; 3538 int rc; 3539 3540 rgroup = SPDK_CONTAINEROF(group, struct spdk_nvmf_rdma_poll_group, group); 3541 rqpair = SPDK_CONTAINEROF(qpair, struct spdk_nvmf_rdma_qpair, qpair); 3542 3543 device = rqpair->device; 3544 3545 TAILQ_FOREACH(poller, &rgroup->pollers, link) { 3546 if (poller->device == device) { 3547 break; 3548 } 3549 } 3550 3551 if (!poller) { 3552 SPDK_ERRLOG("No poller found for device.\n"); 3553 return -1; 3554 } 3555 3556 TAILQ_INSERT_TAIL(&poller->qpairs, rqpair, link); 3557 rqpair->poller = poller; 3558 rqpair->srq = rqpair->poller->srq; 3559 3560 rc = spdk_nvmf_rdma_qpair_initialize(qpair); 3561 if (rc < 0) { 3562 SPDK_ERRLOG("Failed to initialize nvmf_rdma_qpair with qpair=%p\n", qpair); 3563 return -1; 3564 } 3565 3566 rc = spdk_nvmf_rdma_event_accept(rqpair->cm_id, rqpair); 3567 if (rc) { 3568 /* Try to reject, but we probably can't */ 3569 spdk_nvmf_rdma_qpair_reject_connection(rqpair); 3570 return -1; 3571 } 3572 3573 spdk_nvmf_rdma_update_ibv_state(rqpair); 3574 3575 return 0; 3576 } 3577 3578 static int 3579 spdk_nvmf_rdma_request_free(struct spdk_nvmf_request *req) 3580 { 3581 struct spdk_nvmf_rdma_request *rdma_req = SPDK_CONTAINEROF(req, struct spdk_nvmf_rdma_request, req); 3582 struct spdk_nvmf_rdma_transport *rtransport = SPDK_CONTAINEROF(req->qpair->transport, 3583 struct spdk_nvmf_rdma_transport, transport); 3584 3585 nvmf_rdma_request_free(rdma_req, rtransport); 3586 return 0; 3587 } 3588 3589 static int 3590 spdk_nvmf_rdma_request_complete(struct spdk_nvmf_request *req) 3591 { 3592 struct spdk_nvmf_rdma_transport *rtransport = SPDK_CONTAINEROF(req->qpair->transport, 3593 struct spdk_nvmf_rdma_transport, transport); 3594 struct spdk_nvmf_rdma_request *rdma_req = SPDK_CONTAINEROF(req, 3595 struct spdk_nvmf_rdma_request, req); 3596 struct spdk_nvmf_rdma_qpair *rqpair = SPDK_CONTAINEROF(rdma_req->req.qpair, 3597 struct spdk_nvmf_rdma_qpair, qpair); 3598 3599 if (rqpair->ibv_state != IBV_QPS_ERR) { 3600 /* The connection is alive, so process the request as normal */ 3601 rdma_req->state = RDMA_REQUEST_STATE_EXECUTED; 3602 } else { 3603 /* The connection is dead. Move the request directly to the completed state. */ 3604 rdma_req->state = RDMA_REQUEST_STATE_COMPLETED; 3605 } 3606 3607 spdk_nvmf_rdma_request_process(rtransport, rdma_req); 3608 3609 return 0; 3610 } 3611 3612 static int 3613 spdk_nvmf_rdma_destroy_defunct_qpair(void *ctx) 3614 { 3615 struct spdk_nvmf_rdma_qpair *rqpair = ctx; 3616 struct spdk_nvmf_rdma_transport *rtransport = SPDK_CONTAINEROF(rqpair->qpair.transport, 3617 struct spdk_nvmf_rdma_transport, transport); 3618 3619 SPDK_INFOLOG(SPDK_LOG_RDMA, "QP#%d hasn't been drained as expected, manually destroy it\n", 3620 rqpair->qpair.qid); 3621 3622 spdk_nvmf_rdma_qpair_process_pending(rtransport, rqpair, true); 3623 spdk_nvmf_rdma_qpair_destroy(rqpair); 3624 3625 return 0; 3626 } 3627 3628 static void 3629 spdk_nvmf_rdma_close_qpair(struct spdk_nvmf_qpair *qpair) 3630 { 3631 struct spdk_nvmf_rdma_qpair *rqpair = SPDK_CONTAINEROF(qpair, struct spdk_nvmf_rdma_qpair, qpair); 3632 3633 if (rqpair->disconnect_flags & RDMA_QP_DISCONNECTING) { 3634 return; 3635 } 3636 3637 rqpair->disconnect_flags |= RDMA_QP_DISCONNECTING; 3638 3639 /* This happens only when the qpair is disconnected before 3640 * it is added to the poll group. Since there is no poll group, 3641 * the RDMA qp has not been initialized yet and the RDMA CM 3642 * event has not yet been acknowledged, so we need to reject it. 3643 */ 3644 if (rqpair->qpair.state == SPDK_NVMF_QPAIR_UNINITIALIZED) { 3645 spdk_nvmf_rdma_qpair_reject_connection(rqpair); 3646 return; 3647 } 3648 3649 if (rqpair->ibv_state != IBV_QPS_ERR) { 3650 spdk_nvmf_rdma_set_ibv_state(rqpair, IBV_QPS_ERR); 3651 } 3652 3653 rqpair->destruct_poller = spdk_poller_register(spdk_nvmf_rdma_destroy_defunct_qpair, (void *)rqpair, 3654 NVMF_RDMA_QPAIR_DESTROY_TIMEOUT_US); 3655 } 3656 3657 static struct spdk_nvmf_rdma_qpair * 3658 get_rdma_qpair_from_wc(struct spdk_nvmf_rdma_poller *rpoller, struct ibv_wc *wc) 3659 { 3660 struct spdk_nvmf_rdma_qpair *rqpair; 3661 /* @todo: improve QP search */ 3662 TAILQ_FOREACH(rqpair, &rpoller->qpairs, link) { 3663 if (wc->qp_num == rqpair->cm_id->qp->qp_num) { 3664 return rqpair; 3665 } 3666 } 3667 SPDK_ERRLOG("Didn't find QP with qp_num %u\n", wc->qp_num); 3668 return NULL; 3669 } 3670 3671 #ifdef DEBUG 3672 static int 3673 spdk_nvmf_rdma_req_is_completing(struct spdk_nvmf_rdma_request *rdma_req) 3674 { 3675 return rdma_req->state == RDMA_REQUEST_STATE_TRANSFERRING_CONTROLLER_TO_HOST || 3676 rdma_req->state == RDMA_REQUEST_STATE_COMPLETING; 3677 } 3678 #endif 3679 3680 static void 3681 _poller_reset_failed_recvs(struct spdk_nvmf_rdma_poller *rpoller, struct ibv_recv_wr *bad_recv_wr, 3682 int rc) 3683 { 3684 struct spdk_nvmf_rdma_recv *rdma_recv; 3685 struct spdk_nvmf_rdma_wr *bad_rdma_wr; 3686 3687 SPDK_ERRLOG("Failed to post a recv for the poller %p with errno %d\n", rpoller, -rc); 3688 while (bad_recv_wr != NULL) { 3689 bad_rdma_wr = (struct spdk_nvmf_rdma_wr *)bad_recv_wr->wr_id; 3690 rdma_recv = SPDK_CONTAINEROF(bad_rdma_wr, struct spdk_nvmf_rdma_recv, rdma_wr); 3691 3692 rdma_recv->qpair->current_recv_depth++; 3693 bad_recv_wr = bad_recv_wr->next; 3694 SPDK_ERRLOG("Failed to post a recv for the qpair %p with errno %d\n", rdma_recv->qpair, -rc); 3695 spdk_nvmf_rdma_start_disconnect(rdma_recv->qpair); 3696 } 3697 } 3698 3699 static void 3700 _qp_reset_failed_recvs(struct spdk_nvmf_rdma_qpair *rqpair, struct ibv_recv_wr *bad_recv_wr, int rc) 3701 { 3702 SPDK_ERRLOG("Failed to post a recv for the qpair %p with errno %d\n", rqpair, -rc); 3703 while (bad_recv_wr != NULL) { 3704 bad_recv_wr = bad_recv_wr->next; 3705 rqpair->current_recv_depth++; 3706 } 3707 spdk_nvmf_rdma_start_disconnect(rqpair); 3708 } 3709 3710 static void 3711 _poller_submit_recvs(struct spdk_nvmf_rdma_transport *rtransport, 3712 struct spdk_nvmf_rdma_poller *rpoller) 3713 { 3714 struct spdk_nvmf_rdma_qpair *rqpair; 3715 struct ibv_recv_wr *bad_recv_wr; 3716 int rc; 3717 3718 if (rpoller->srq) { 3719 if (rpoller->resources->recvs_to_post.first != NULL) { 3720 rc = ibv_post_srq_recv(rpoller->srq, rpoller->resources->recvs_to_post.first, &bad_recv_wr); 3721 if (rc) { 3722 _poller_reset_failed_recvs(rpoller, bad_recv_wr, rc); 3723 } 3724 rpoller->resources->recvs_to_post.first = NULL; 3725 rpoller->resources->recvs_to_post.last = NULL; 3726 } 3727 } else { 3728 while (!STAILQ_EMPTY(&rpoller->qpairs_pending_recv)) { 3729 rqpair = STAILQ_FIRST(&rpoller->qpairs_pending_recv); 3730 assert(rqpair->resources->recvs_to_post.first != NULL); 3731 rc = ibv_post_recv(rqpair->cm_id->qp, rqpair->resources->recvs_to_post.first, &bad_recv_wr); 3732 if (rc) { 3733 _qp_reset_failed_recvs(rqpair, bad_recv_wr, rc); 3734 } 3735 rqpair->resources->recvs_to_post.first = NULL; 3736 rqpair->resources->recvs_to_post.last = NULL; 3737 STAILQ_REMOVE_HEAD(&rpoller->qpairs_pending_recv, recv_link); 3738 } 3739 } 3740 } 3741 3742 static void 3743 _qp_reset_failed_sends(struct spdk_nvmf_rdma_transport *rtransport, 3744 struct spdk_nvmf_rdma_qpair *rqpair, struct ibv_send_wr *bad_wr, int rc) 3745 { 3746 struct spdk_nvmf_rdma_wr *bad_rdma_wr; 3747 struct spdk_nvmf_rdma_request *prev_rdma_req = NULL, *cur_rdma_req = NULL; 3748 3749 SPDK_ERRLOG("Failed to post a send for the qpair %p with errno %d\n", rqpair, -rc); 3750 for (; bad_wr != NULL; bad_wr = bad_wr->next) { 3751 bad_rdma_wr = (struct spdk_nvmf_rdma_wr *)bad_wr->wr_id; 3752 assert(rqpair->current_send_depth > 0); 3753 rqpair->current_send_depth--; 3754 switch (bad_rdma_wr->type) { 3755 case RDMA_WR_TYPE_DATA: 3756 cur_rdma_req = SPDK_CONTAINEROF(bad_rdma_wr, struct spdk_nvmf_rdma_request, data.rdma_wr); 3757 if (bad_wr->opcode == IBV_WR_RDMA_READ) { 3758 assert(rqpair->current_read_depth > 0); 3759 rqpair->current_read_depth--; 3760 } 3761 break; 3762 case RDMA_WR_TYPE_SEND: 3763 cur_rdma_req = SPDK_CONTAINEROF(bad_rdma_wr, struct spdk_nvmf_rdma_request, rsp.rdma_wr); 3764 break; 3765 default: 3766 SPDK_ERRLOG("Found a RECV in the list of pending SEND requests for qpair %p\n", rqpair); 3767 prev_rdma_req = cur_rdma_req; 3768 continue; 3769 } 3770 3771 if (prev_rdma_req == cur_rdma_req) { 3772 /* this request was handled by an earlier wr. i.e. we were performing an nvme read. */ 3773 /* We only have to check against prev_wr since each requests wrs are contiguous in this list. */ 3774 continue; 3775 } 3776 3777 switch (cur_rdma_req->state) { 3778 case RDMA_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER: 3779 cur_rdma_req->req.rsp->nvme_cpl.status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR; 3780 cur_rdma_req->state = RDMA_REQUEST_STATE_READY_TO_COMPLETE; 3781 break; 3782 case RDMA_REQUEST_STATE_TRANSFERRING_CONTROLLER_TO_HOST: 3783 case RDMA_REQUEST_STATE_COMPLETING: 3784 cur_rdma_req->state = RDMA_REQUEST_STATE_COMPLETED; 3785 break; 3786 default: 3787 SPDK_ERRLOG("Found a request in a bad state %d when draining pending SEND requests for qpair %p\n", 3788 cur_rdma_req->state, rqpair); 3789 continue; 3790 } 3791 3792 spdk_nvmf_rdma_request_process(rtransport, cur_rdma_req); 3793 prev_rdma_req = cur_rdma_req; 3794 } 3795 3796 if (rqpair->qpair.state == SPDK_NVMF_QPAIR_ACTIVE) { 3797 /* Disconnect the connection. */ 3798 spdk_nvmf_rdma_start_disconnect(rqpair); 3799 } 3800 3801 } 3802 3803 static void 3804 _poller_submit_sends(struct spdk_nvmf_rdma_transport *rtransport, 3805 struct spdk_nvmf_rdma_poller *rpoller) 3806 { 3807 struct spdk_nvmf_rdma_qpair *rqpair; 3808 struct ibv_send_wr *bad_wr = NULL; 3809 int rc; 3810 3811 while (!STAILQ_EMPTY(&rpoller->qpairs_pending_send)) { 3812 rqpair = STAILQ_FIRST(&rpoller->qpairs_pending_send); 3813 assert(rqpair->sends_to_post.first != NULL); 3814 rc = ibv_post_send(rqpair->cm_id->qp, rqpair->sends_to_post.first, &bad_wr); 3815 3816 /* bad wr always points to the first wr that failed. */ 3817 if (rc) { 3818 _qp_reset_failed_sends(rtransport, rqpair, bad_wr, rc); 3819 } 3820 rqpair->sends_to_post.first = NULL; 3821 rqpair->sends_to_post.last = NULL; 3822 STAILQ_REMOVE_HEAD(&rpoller->qpairs_pending_send, send_link); 3823 } 3824 } 3825 3826 static int 3827 spdk_nvmf_rdma_poller_poll(struct spdk_nvmf_rdma_transport *rtransport, 3828 struct spdk_nvmf_rdma_poller *rpoller) 3829 { 3830 struct ibv_wc wc[32]; 3831 struct spdk_nvmf_rdma_wr *rdma_wr; 3832 struct spdk_nvmf_rdma_request *rdma_req; 3833 struct spdk_nvmf_rdma_recv *rdma_recv; 3834 struct spdk_nvmf_rdma_qpair *rqpair; 3835 int reaped, i; 3836 int count = 0; 3837 bool error = false; 3838 uint64_t poll_tsc = spdk_get_ticks(); 3839 3840 /* Poll for completing operations. */ 3841 reaped = ibv_poll_cq(rpoller->cq, 32, wc); 3842 if (reaped < 0) { 3843 SPDK_ERRLOG("Error polling CQ! (%d): %s\n", 3844 errno, spdk_strerror(errno)); 3845 return -1; 3846 } 3847 3848 rpoller->stat.polls++; 3849 rpoller->stat.completions += reaped; 3850 3851 for (i = 0; i < reaped; i++) { 3852 3853 rdma_wr = (struct spdk_nvmf_rdma_wr *)wc[i].wr_id; 3854 3855 switch (rdma_wr->type) { 3856 case RDMA_WR_TYPE_SEND: 3857 rdma_req = SPDK_CONTAINEROF(rdma_wr, struct spdk_nvmf_rdma_request, rsp.rdma_wr); 3858 rqpair = SPDK_CONTAINEROF(rdma_req->req.qpair, struct spdk_nvmf_rdma_qpair, qpair); 3859 3860 if (!wc[i].status) { 3861 count++; 3862 assert(wc[i].opcode == IBV_WC_SEND); 3863 assert(spdk_nvmf_rdma_req_is_completing(rdma_req)); 3864 } 3865 3866 rdma_req->state = RDMA_REQUEST_STATE_COMPLETED; 3867 /* RDMA_WRITE operation completed. +1 since it was chained with rsp WR */ 3868 rqpair->current_send_depth -= rdma_req->num_outstanding_data_wr + 1; 3869 rdma_req->num_outstanding_data_wr = 0; 3870 3871 spdk_nvmf_rdma_request_process(rtransport, rdma_req); 3872 break; 3873 case RDMA_WR_TYPE_RECV: 3874 /* rdma_recv->qpair will be invalid if using an SRQ. In that case we have to get the qpair from the wc. */ 3875 rdma_recv = SPDK_CONTAINEROF(rdma_wr, struct spdk_nvmf_rdma_recv, rdma_wr); 3876 if (rpoller->srq != NULL) { 3877 rdma_recv->qpair = get_rdma_qpair_from_wc(rpoller, &wc[i]); 3878 /* It is possible that there are still some completions for destroyed QP 3879 * associated with SRQ. We just ignore these late completions and re-post 3880 * receive WRs back to SRQ. 3881 */ 3882 if (spdk_unlikely(NULL == rdma_recv->qpair)) { 3883 struct ibv_recv_wr *bad_wr; 3884 int rc; 3885 3886 rdma_recv->wr.next = NULL; 3887 rc = ibv_post_srq_recv(rpoller->srq, 3888 &rdma_recv->wr, 3889 &bad_wr); 3890 if (rc) { 3891 SPDK_ERRLOG("Failed to re-post recv WR to SRQ, err %d\n", rc); 3892 } 3893 continue; 3894 } 3895 } 3896 rqpair = rdma_recv->qpair; 3897 3898 assert(rqpair != NULL); 3899 if (!wc[i].status) { 3900 assert(wc[i].opcode == IBV_WC_RECV); 3901 if (rqpair->current_recv_depth >= rqpair->max_queue_depth) { 3902 spdk_nvmf_rdma_start_disconnect(rqpair); 3903 break; 3904 } 3905 } 3906 3907 rdma_recv->wr.next = NULL; 3908 rqpair->current_recv_depth++; 3909 rdma_recv->receive_tsc = poll_tsc; 3910 rpoller->stat.requests++; 3911 STAILQ_INSERT_TAIL(&rqpair->resources->incoming_queue, rdma_recv, link); 3912 break; 3913 case RDMA_WR_TYPE_DATA: 3914 rdma_req = SPDK_CONTAINEROF(rdma_wr, struct spdk_nvmf_rdma_request, data.rdma_wr); 3915 rqpair = SPDK_CONTAINEROF(rdma_req->req.qpair, struct spdk_nvmf_rdma_qpair, qpair); 3916 3917 assert(rdma_req->num_outstanding_data_wr > 0); 3918 3919 rqpair->current_send_depth--; 3920 rdma_req->num_outstanding_data_wr--; 3921 if (!wc[i].status) { 3922 assert(wc[i].opcode == IBV_WC_RDMA_READ); 3923 rqpair->current_read_depth--; 3924 /* wait for all outstanding reads associated with the same rdma_req to complete before proceeding. */ 3925 if (rdma_req->num_outstanding_data_wr == 0) { 3926 rdma_req->state = RDMA_REQUEST_STATE_READY_TO_EXECUTE; 3927 spdk_nvmf_rdma_request_process(rtransport, rdma_req); 3928 } 3929 } else { 3930 /* If the data transfer fails still force the queue into the error state, 3931 * if we were performing an RDMA_READ, we need to force the request into a 3932 * completed state since it wasn't linked to a send. However, in the RDMA_WRITE 3933 * case, we should wait for the SEND to complete. */ 3934 if (rdma_req->data.wr.opcode == IBV_WR_RDMA_READ) { 3935 rqpair->current_read_depth--; 3936 if (rdma_req->num_outstanding_data_wr == 0) { 3937 rdma_req->state = RDMA_REQUEST_STATE_COMPLETED; 3938 } 3939 } 3940 } 3941 break; 3942 default: 3943 SPDK_ERRLOG("Received an unknown opcode on the CQ: %d\n", wc[i].opcode); 3944 continue; 3945 } 3946 3947 /* Handle error conditions */ 3948 if (wc[i].status) { 3949 SPDK_ERRLOG("Error on CQ %p, request 0x%lu, type %d, status: (%d): %s\n", 3950 rpoller->cq, wc[i].wr_id, rdma_wr->type, wc[i].status, ibv_wc_status_str(wc[i].status)); 3951 3952 error = true; 3953 3954 if (rqpair->qpair.state == SPDK_NVMF_QPAIR_ACTIVE) { 3955 /* Disconnect the connection. */ 3956 spdk_nvmf_rdma_start_disconnect(rqpair); 3957 } else { 3958 nvmf_rdma_destroy_drained_qpair(rqpair); 3959 } 3960 continue; 3961 } 3962 3963 spdk_nvmf_rdma_qpair_process_pending(rtransport, rqpair, false); 3964 3965 if (rqpair->qpair.state != SPDK_NVMF_QPAIR_ACTIVE) { 3966 nvmf_rdma_destroy_drained_qpair(rqpair); 3967 } 3968 } 3969 3970 if (error == true) { 3971 return -1; 3972 } 3973 3974 /* submit outstanding work requests. */ 3975 _poller_submit_recvs(rtransport, rpoller); 3976 _poller_submit_sends(rtransport, rpoller); 3977 3978 return count; 3979 } 3980 3981 static int 3982 spdk_nvmf_rdma_poll_group_poll(struct spdk_nvmf_transport_poll_group *group) 3983 { 3984 struct spdk_nvmf_rdma_transport *rtransport; 3985 struct spdk_nvmf_rdma_poll_group *rgroup; 3986 struct spdk_nvmf_rdma_poller *rpoller; 3987 int count, rc; 3988 3989 rtransport = SPDK_CONTAINEROF(group->transport, struct spdk_nvmf_rdma_transport, transport); 3990 rgroup = SPDK_CONTAINEROF(group, struct spdk_nvmf_rdma_poll_group, group); 3991 3992 count = 0; 3993 TAILQ_FOREACH(rpoller, &rgroup->pollers, link) { 3994 rc = spdk_nvmf_rdma_poller_poll(rtransport, rpoller); 3995 if (rc < 0) { 3996 return rc; 3997 } 3998 count += rc; 3999 } 4000 4001 return count; 4002 } 4003 4004 static int 4005 spdk_nvmf_rdma_trid_from_cm_id(struct rdma_cm_id *id, 4006 struct spdk_nvme_transport_id *trid, 4007 bool peer) 4008 { 4009 struct sockaddr *saddr; 4010 uint16_t port; 4011 4012 spdk_nvme_trid_populate_transport(trid, SPDK_NVME_TRANSPORT_RDMA); 4013 4014 if (peer) { 4015 saddr = rdma_get_peer_addr(id); 4016 } else { 4017 saddr = rdma_get_local_addr(id); 4018 } 4019 switch (saddr->sa_family) { 4020 case AF_INET: { 4021 struct sockaddr_in *saddr_in = (struct sockaddr_in *)saddr; 4022 4023 trid->adrfam = SPDK_NVMF_ADRFAM_IPV4; 4024 inet_ntop(AF_INET, &saddr_in->sin_addr, 4025 trid->traddr, sizeof(trid->traddr)); 4026 if (peer) { 4027 port = ntohs(rdma_get_dst_port(id)); 4028 } else { 4029 port = ntohs(rdma_get_src_port(id)); 4030 } 4031 snprintf(trid->trsvcid, sizeof(trid->trsvcid), "%u", port); 4032 break; 4033 } 4034 case AF_INET6: { 4035 struct sockaddr_in6 *saddr_in = (struct sockaddr_in6 *)saddr; 4036 trid->adrfam = SPDK_NVMF_ADRFAM_IPV6; 4037 inet_ntop(AF_INET6, &saddr_in->sin6_addr, 4038 trid->traddr, sizeof(trid->traddr)); 4039 if (peer) { 4040 port = ntohs(rdma_get_dst_port(id)); 4041 } else { 4042 port = ntohs(rdma_get_src_port(id)); 4043 } 4044 snprintf(trid->trsvcid, sizeof(trid->trsvcid), "%u", port); 4045 break; 4046 } 4047 default: 4048 return -1; 4049 4050 } 4051 4052 return 0; 4053 } 4054 4055 static int 4056 spdk_nvmf_rdma_qpair_get_peer_trid(struct spdk_nvmf_qpair *qpair, 4057 struct spdk_nvme_transport_id *trid) 4058 { 4059 struct spdk_nvmf_rdma_qpair *rqpair; 4060 4061 rqpair = SPDK_CONTAINEROF(qpair, struct spdk_nvmf_rdma_qpair, qpair); 4062 4063 return spdk_nvmf_rdma_trid_from_cm_id(rqpair->cm_id, trid, true); 4064 } 4065 4066 static int 4067 spdk_nvmf_rdma_qpair_get_local_trid(struct spdk_nvmf_qpair *qpair, 4068 struct spdk_nvme_transport_id *trid) 4069 { 4070 struct spdk_nvmf_rdma_qpair *rqpair; 4071 4072 rqpair = SPDK_CONTAINEROF(qpair, struct spdk_nvmf_rdma_qpair, qpair); 4073 4074 return spdk_nvmf_rdma_trid_from_cm_id(rqpair->cm_id, trid, false); 4075 } 4076 4077 static int 4078 spdk_nvmf_rdma_qpair_get_listen_trid(struct spdk_nvmf_qpair *qpair, 4079 struct spdk_nvme_transport_id *trid) 4080 { 4081 struct spdk_nvmf_rdma_qpair *rqpair; 4082 4083 rqpair = SPDK_CONTAINEROF(qpair, struct spdk_nvmf_rdma_qpair, qpair); 4084 4085 return spdk_nvmf_rdma_trid_from_cm_id(rqpair->listen_id, trid, false); 4086 } 4087 4088 void 4089 spdk_nvmf_rdma_init_hooks(struct spdk_nvme_rdma_hooks *hooks) 4090 { 4091 g_nvmf_hooks = *hooks; 4092 } 4093 4094 static int 4095 spdk_nvmf_rdma_poll_group_get_stat(struct spdk_nvmf_tgt *tgt, 4096 struct spdk_nvmf_transport_poll_group_stat **stat) 4097 { 4098 struct spdk_io_channel *ch; 4099 struct spdk_nvmf_poll_group *group; 4100 struct spdk_nvmf_transport_poll_group *tgroup; 4101 struct spdk_nvmf_rdma_poll_group *rgroup; 4102 struct spdk_nvmf_rdma_poller *rpoller; 4103 struct spdk_nvmf_rdma_device_stat *device_stat; 4104 uint64_t num_devices = 0; 4105 4106 if (tgt == NULL || stat == NULL) { 4107 return -EINVAL; 4108 } 4109 4110 ch = spdk_get_io_channel(tgt); 4111 group = spdk_io_channel_get_ctx(ch);; 4112 spdk_put_io_channel(ch); 4113 TAILQ_FOREACH(tgroup, &group->tgroups, link) { 4114 if (SPDK_NVME_TRANSPORT_RDMA == tgroup->transport->ops->type) { 4115 *stat = calloc(1, sizeof(struct spdk_nvmf_transport_poll_group_stat)); 4116 if (!*stat) { 4117 SPDK_ERRLOG("Failed to allocate memory for NVMf RDMA statistics\n"); 4118 return -ENOMEM; 4119 } 4120 (*stat)->trtype = SPDK_NVME_TRANSPORT_RDMA; 4121 4122 rgroup = SPDK_CONTAINEROF(tgroup, struct spdk_nvmf_rdma_poll_group, group); 4123 /* Count devices to allocate enough memory */ 4124 TAILQ_FOREACH(rpoller, &rgroup->pollers, link) { 4125 ++num_devices; 4126 } 4127 (*stat)->rdma.devices = calloc(num_devices, sizeof(struct spdk_nvmf_rdma_device_stat)); 4128 if (!(*stat)->rdma.devices) { 4129 SPDK_ERRLOG("Failed to allocate NVMf RDMA devices statistics\n"); 4130 free(*stat); 4131 return -ENOMEM; 4132 } 4133 4134 (*stat)->rdma.pending_data_buffer = rgroup->stat.pending_data_buffer; 4135 (*stat)->rdma.num_devices = num_devices; 4136 num_devices = 0; 4137 TAILQ_FOREACH(rpoller, &rgroup->pollers, link) { 4138 device_stat = &(*stat)->rdma.devices[num_devices++]; 4139 device_stat->name = ibv_get_device_name(rpoller->device->context->device); 4140 device_stat->polls = rpoller->stat.polls; 4141 device_stat->completions = rpoller->stat.completions; 4142 device_stat->requests = rpoller->stat.requests; 4143 device_stat->request_latency = rpoller->stat.request_latency; 4144 device_stat->pending_free_request = rpoller->stat.pending_free_request; 4145 device_stat->pending_rdma_read = rpoller->stat.pending_rdma_read; 4146 device_stat->pending_rdma_write = rpoller->stat.pending_rdma_write; 4147 } 4148 return 0; 4149 } 4150 } 4151 return -ENOENT; 4152 } 4153 4154 static void 4155 spdk_nvmf_rdma_poll_group_free_stat(struct spdk_nvmf_transport_poll_group_stat *stat) 4156 { 4157 if (stat) { 4158 free(stat->rdma.devices); 4159 } 4160 free(stat); 4161 } 4162 4163 const struct spdk_nvmf_transport_ops spdk_nvmf_transport_rdma = { 4164 .name = "RDMA", 4165 .type = SPDK_NVME_TRANSPORT_RDMA, 4166 .opts_init = spdk_nvmf_rdma_opts_init, 4167 .create = spdk_nvmf_rdma_create, 4168 .destroy = spdk_nvmf_rdma_destroy, 4169 4170 .listen = spdk_nvmf_rdma_listen, 4171 .stop_listen = spdk_nvmf_rdma_stop_listen, 4172 .accept = spdk_nvmf_rdma_accept, 4173 4174 .listener_discover = spdk_nvmf_rdma_discover, 4175 4176 .poll_group_create = spdk_nvmf_rdma_poll_group_create, 4177 .get_optimal_poll_group = spdk_nvmf_rdma_get_optimal_poll_group, 4178 .poll_group_destroy = spdk_nvmf_rdma_poll_group_destroy, 4179 .poll_group_add = spdk_nvmf_rdma_poll_group_add, 4180 .poll_group_poll = spdk_nvmf_rdma_poll_group_poll, 4181 4182 .req_free = spdk_nvmf_rdma_request_free, 4183 .req_complete = spdk_nvmf_rdma_request_complete, 4184 4185 .qpair_fini = spdk_nvmf_rdma_close_qpair, 4186 .qpair_get_peer_trid = spdk_nvmf_rdma_qpair_get_peer_trid, 4187 .qpair_get_local_trid = spdk_nvmf_rdma_qpair_get_local_trid, 4188 .qpair_get_listen_trid = spdk_nvmf_rdma_qpair_get_listen_trid, 4189 4190 .poll_group_get_stat = spdk_nvmf_rdma_poll_group_get_stat, 4191 .poll_group_free_stat = spdk_nvmf_rdma_poll_group_free_stat, 4192 }; 4193 4194 SPDK_NVMF_TRANSPORT_REGISTER(rdma, &spdk_nvmf_transport_rdma); 4195 SPDK_LOG_REGISTER_COMPONENT("rdma", SPDK_LOG_RDMA) 4196