4 * RDMA I/O engine based on the IB verbs and RDMA/CM user space libraries.
5 * Supports both RDMA memory semantics and channel semantics
6 * for the InfiniBand, RoCE and iWARP protocols.
8 * You will need the Linux RDMA software installed, either
9 * from your Linux distributor or directly from openfabrics.org:
11 * http://www.openfabrics.org/downloads/OFED/
13 * Exchanging steps of RDMA ioengine control messages:
14 * 1. client side sends test mode (RDMA_WRITE/RDMA_READ/SEND)
16 * 2. server side parses test mode, and sends back confirmation
17 * to client side. In RDMA WRITE/READ test, this confirmation
18 * includes memory information, such as rkey, address.
19 * 3. client side initiates test loop.
20 * 4. In RDMA WRITE/READ test, client side sends a completion
21 * notification to server side. Server side updates its
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
37 #include <sys/resource.h>
44 #include "../optgroup.h"
46 #include <rdma/rdma_cma.h>
48 #define FIO_RDMA_MAX_IO_DEPTH 512
58 struct rdmaio_options {
59 struct thread_data *td;
61 enum rdma_io_mode verb;
65 static int str_hostname_cb(void *data, const char *input)
67 struct rdmaio_options *o = data;
69 if (o->td->o.filename)
70 free(o->td->o.filename);
71 o->td->o.filename = strdup(input);
75 static struct fio_option options[] = {
78 .lname = "rdma engine hostname",
79 .type = FIO_OPT_STR_STORE,
80 .cb = str_hostname_cb,
81 .help = "Hostname for RDMA IO engine",
82 .category = FIO_OPT_C_ENGINE,
83 .group = FIO_OPT_G_RDMA,
87 .lname = "rdma engine bindname",
88 .type = FIO_OPT_STR_STORE,
89 .off1 = offsetof(struct rdmaio_options, bindname),
90 .help = "Bind for RDMA IO engine",
92 .category = FIO_OPT_C_ENGINE,
93 .group = FIO_OPT_G_RDMA,
97 .lname = "rdma engine port",
99 .off1 = offsetof(struct rdmaio_options, port),
102 .help = "Port to use for RDMA connections",
103 .category = FIO_OPT_C_ENGINE,
104 .group = FIO_OPT_G_RDMA,
108 .lname = "RDMA engine verb",
111 .off1 = offsetof(struct rdmaio_options, verb),
112 .help = "RDMA engine verb",
116 .oval = FIO_RDMA_MEM_WRITE,
117 .help = "Memory Write",
120 .oval = FIO_RDMA_MEM_READ,
121 .help = "Memory Read",
124 .oval = FIO_RDMA_CHA_SEND,
125 .help = "Posted Send",
128 .oval = FIO_RDMA_CHA_RECV,
129 .help = "Posted Receive",
132 .category = FIO_OPT_C_ENGINE,
133 .group = FIO_OPT_G_RDMA,
146 struct rdma_info_blk {
147 uint32_t mode; /* channel semantic or memory semantic */
148 uint32_t nr; /* client: io depth
149 server: number of records for memory semantic
151 uint32_t max_bs; /* maximum block size */
152 struct remote_u rmt_us[FIO_RDMA_MAX_IO_DEPTH];
155 struct rdma_io_u_data {
157 struct ibv_send_wr sq_wr;
158 struct ibv_recv_wr rq_wr;
159 struct ibv_sge rdma_sgl;
164 enum rdma_io_mode rdma_protocol;
166 struct sockaddr_in addr;
168 struct ibv_recv_wr rq_wr;
169 struct ibv_sge recv_sgl;
170 struct rdma_info_blk recv_buf;
171 struct ibv_mr *recv_mr;
173 struct ibv_send_wr sq_wr;
174 struct ibv_sge send_sgl;
175 struct rdma_info_blk send_buf;
176 struct ibv_mr *send_mr;
178 struct ibv_comp_channel *channel;
184 struct rdma_event_channel *cm_channel;
185 struct rdma_cm_id *cm_id;
186 struct rdma_cm_id *child_cm_id;
190 struct remote_u *rmt_us;
192 struct io_u **io_us_queued;
194 struct io_u **io_us_flight;
196 struct io_u **io_us_completed;
197 int io_u_completed_nr;
199 struct frand_state rand_state;
202 static int client_recv(struct thread_data *td, struct ibv_wc *wc)
204 struct rdmaio_data *rd = td->io_ops_data;
207 if (wc->byte_len != sizeof(rd->recv_buf)) {
208 log_err("Received bogus data, size %d\n", wc->byte_len);
212 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
213 if (max_bs > ntohl(rd->recv_buf.max_bs)) {
214 log_err("fio: Server's block size (%d) must be greater than or "
215 "equal to the client's block size (%d)!\n",
216 ntohl(rd->recv_buf.max_bs), max_bs);
220 /* store mr info for MEMORY semantic */
221 if ((rd->rdma_protocol == FIO_RDMA_MEM_WRITE) ||
222 (rd->rdma_protocol == FIO_RDMA_MEM_READ)) {
223 /* struct flist_head *entry; */
226 rd->rmt_nr = ntohl(rd->recv_buf.nr);
228 for (i = 0; i < rd->rmt_nr; i++) {
229 rd->rmt_us[i].buf = __be64_to_cpu(
230 rd->recv_buf.rmt_us[i].buf);
231 rd->rmt_us[i].rkey = ntohl(rd->recv_buf.rmt_us[i].rkey);
232 rd->rmt_us[i].size = ntohl(rd->recv_buf.rmt_us[i].size);
235 "fio: Received rkey %x addr %" PRIx64
236 " len %d from peer\n", rd->rmt_us[i].rkey,
237 rd->rmt_us[i].buf, rd->rmt_us[i].size);
244 static int server_recv(struct thread_data *td, struct ibv_wc *wc)
246 struct rdmaio_data *rd = td->io_ops_data;
249 if (wc->wr_id == FIO_RDMA_MAX_IO_DEPTH) {
250 rd->rdma_protocol = ntohl(rd->recv_buf.mode);
252 /* CHANNEL semantic, do nothing */
253 if (rd->rdma_protocol == FIO_RDMA_CHA_SEND)
254 rd->rdma_protocol = FIO_RDMA_CHA_RECV;
256 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
257 if (max_bs < ntohl(rd->recv_buf.max_bs)) {
258 log_err("fio: Server's block size (%d) must be greater than or "
259 "equal to the client's block size (%d)!\n",
260 ntohl(rd->recv_buf.max_bs), max_bs);
269 static int cq_event_handler(struct thread_data *td, enum ibv_wc_opcode opcode)
271 struct rdmaio_data *rd = td->io_ops_data;
273 struct rdma_io_u_data *r_io_u_d;
278 while ((ret = ibv_poll_cq(rd->cq, 1, &wc)) == 1) {
283 log_err("fio: cq completion status %d(%s)\n",
284 wc.status, ibv_wc_status_str(wc.status));
291 if (rd->is_client == 1)
292 ret = client_recv(td, &wc);
294 ret = server_recv(td, &wc);
299 if (wc.wr_id == FIO_RDMA_MAX_IO_DEPTH)
302 for (i = 0; i < rd->io_u_flight_nr; i++) {
303 r_io_u_d = rd->io_us_flight[i]->engine_data;
305 if (wc.wr_id == r_io_u_d->rq_wr.wr_id) {
306 rd->io_us_flight[i]->resid =
307 rd->io_us_flight[i]->buflen
310 rd->io_us_flight[i]->error = 0;
312 rd->io_us_completed[rd->
314 = rd->io_us_flight[i];
315 rd->io_u_completed_nr++;
319 if (i == rd->io_u_flight_nr)
320 log_err("fio: recv wr %" PRId64 " not found\n",
323 /* put the last one into middle of the list */
324 rd->io_us_flight[i] =
325 rd->io_us_flight[rd->io_u_flight_nr - 1];
326 rd->io_u_flight_nr--;
332 case IBV_WC_RDMA_WRITE:
333 case IBV_WC_RDMA_READ:
334 if (wc.wr_id == FIO_RDMA_MAX_IO_DEPTH)
337 for (i = 0; i < rd->io_u_flight_nr; i++) {
338 r_io_u_d = rd->io_us_flight[i]->engine_data;
340 if (wc.wr_id == r_io_u_d->sq_wr.wr_id) {
341 rd->io_us_completed[rd->
343 = rd->io_us_flight[i];
344 rd->io_u_completed_nr++;
348 if (i == rd->io_u_flight_nr)
349 log_err("fio: send wr %" PRId64 " not found\n",
352 /* put the last one into middle of the list */
353 rd->io_us_flight[i] =
354 rd->io_us_flight[rd->io_u_flight_nr - 1];
355 rd->io_u_flight_nr--;
361 log_info("fio: unknown completion event %d\n",
369 log_err("fio: poll error %d\n", ret);
377 * Return -1 for error and 'nr events' for a positive number
380 static int rdma_poll_wait(struct thread_data *td, enum ibv_wc_opcode opcode)
382 struct rdmaio_data *rd = td->io_ops_data;
383 struct ibv_cq *ev_cq;
387 if (rd->cq_event_num > 0) { /* previous left */
393 if (ibv_get_cq_event(rd->channel, &ev_cq, &ev_ctx) != 0) {
394 log_err("fio: Failed to get cq event!\n");
397 if (ev_cq != rd->cq) {
398 log_err("fio: Unknown CQ!\n");
401 if (ibv_req_notify_cq(rd->cq, 0) != 0) {
402 log_err("fio: Failed to set notify!\n");
406 ret = cq_event_handler(td, opcode);
410 ibv_ack_cq_events(rd->cq, ret);
417 static int fio_rdmaio_setup_qp(struct thread_data *td)
419 struct rdmaio_data *rd = td->io_ops_data;
420 struct ibv_qp_init_attr init_attr;
421 int qp_depth = td->o.iodepth * 2; /* 2 times of io depth */
423 if (rd->is_client == 0)
424 rd->pd = ibv_alloc_pd(rd->child_cm_id->verbs);
426 rd->pd = ibv_alloc_pd(rd->cm_id->verbs);
428 if (rd->pd == NULL) {
429 log_err("fio: ibv_alloc_pd fail: %m\n");
433 if (rd->is_client == 0)
434 rd->channel = ibv_create_comp_channel(rd->child_cm_id->verbs);
436 rd->channel = ibv_create_comp_channel(rd->cm_id->verbs);
437 if (rd->channel == NULL) {
438 log_err("fio: ibv_create_comp_channel fail: %m\n");
445 if (rd->is_client == 0)
446 rd->cq = ibv_create_cq(rd->child_cm_id->verbs,
447 qp_depth, rd, rd->channel, 0);
449 rd->cq = ibv_create_cq(rd->cm_id->verbs,
450 qp_depth, rd, rd->channel, 0);
451 if (rd->cq == NULL) {
452 log_err("fio: ibv_create_cq failed: %m\n");
456 if (ibv_req_notify_cq(rd->cq, 0) != 0) {
457 log_err("fio: ibv_req_notify_cq failed: %m\n");
461 /* create queue pair */
462 memset(&init_attr, 0, sizeof(init_attr));
463 init_attr.cap.max_send_wr = qp_depth;
464 init_attr.cap.max_recv_wr = qp_depth;
465 init_attr.cap.max_recv_sge = 1;
466 init_attr.cap.max_send_sge = 1;
467 init_attr.qp_type = IBV_QPT_RC;
468 init_attr.send_cq = rd->cq;
469 init_attr.recv_cq = rd->cq;
471 if (rd->is_client == 0) {
472 if (rdma_create_qp(rd->child_cm_id, rd->pd, &init_attr) != 0) {
473 log_err("fio: rdma_create_qp failed: %m\n");
476 rd->qp = rd->child_cm_id->qp;
478 if (rdma_create_qp(rd->cm_id, rd->pd, &init_attr) != 0) {
479 log_err("fio: rdma_create_qp failed: %m\n");
482 rd->qp = rd->cm_id->qp;
488 ibv_destroy_cq(rd->cq);
490 ibv_destroy_comp_channel(rd->channel);
492 ibv_dealloc_pd(rd->pd);
497 static int fio_rdmaio_setup_control_msg_buffers(struct thread_data *td)
499 struct rdmaio_data *rd = td->io_ops_data;
501 rd->recv_mr = ibv_reg_mr(rd->pd, &rd->recv_buf, sizeof(rd->recv_buf),
502 IBV_ACCESS_LOCAL_WRITE);
503 if (rd->recv_mr == NULL) {
504 log_err("fio: recv_buf reg_mr failed: %m\n");
508 rd->send_mr = ibv_reg_mr(rd->pd, &rd->send_buf, sizeof(rd->send_buf),
510 if (rd->send_mr == NULL) {
511 log_err("fio: send_buf reg_mr failed: %m\n");
512 ibv_dereg_mr(rd->recv_mr);
516 /* setup work request */
518 rd->recv_sgl.addr = (uint64_t) (unsigned long)&rd->recv_buf;
519 rd->recv_sgl.length = sizeof(rd->recv_buf);
520 rd->recv_sgl.lkey = rd->recv_mr->lkey;
521 rd->rq_wr.sg_list = &rd->recv_sgl;
522 rd->rq_wr.num_sge = 1;
523 rd->rq_wr.wr_id = FIO_RDMA_MAX_IO_DEPTH;
526 rd->send_sgl.addr = (uint64_t) (unsigned long)&rd->send_buf;
527 rd->send_sgl.length = sizeof(rd->send_buf);
528 rd->send_sgl.lkey = rd->send_mr->lkey;
530 rd->sq_wr.opcode = IBV_WR_SEND;
531 rd->sq_wr.send_flags = IBV_SEND_SIGNALED;
532 rd->sq_wr.sg_list = &rd->send_sgl;
533 rd->sq_wr.num_sge = 1;
534 rd->sq_wr.wr_id = FIO_RDMA_MAX_IO_DEPTH;
539 static int get_next_channel_event(struct thread_data *td,
540 struct rdma_event_channel *channel,
541 enum rdma_cm_event_type wait_event)
543 struct rdmaio_data *rd = td->io_ops_data;
544 struct rdma_cm_event *event;
547 ret = rdma_get_cm_event(channel, &event);
549 log_err("fio: rdma_get_cm_event: %d\n", ret);
553 if (event->event != wait_event) {
554 log_err("fio: event is %s instead of %s\n",
555 rdma_event_str(event->event),
556 rdma_event_str(wait_event));
560 switch (event->event) {
561 case RDMA_CM_EVENT_CONNECT_REQUEST:
562 rd->child_cm_id = event->id;
568 rdma_ack_cm_event(event);
573 static int fio_rdmaio_prep(struct thread_data *td, struct io_u *io_u)
575 struct rdmaio_data *rd = td->io_ops_data;
576 struct rdma_io_u_data *r_io_u_d;
578 r_io_u_d = io_u->engine_data;
580 switch (rd->rdma_protocol) {
581 case FIO_RDMA_MEM_WRITE:
582 case FIO_RDMA_MEM_READ:
583 r_io_u_d->rdma_sgl.addr = (uint64_t) (unsigned long)io_u->buf;
584 r_io_u_d->rdma_sgl.lkey = io_u->mr->lkey;
585 r_io_u_d->sq_wr.wr_id = r_io_u_d->wr_id;
586 r_io_u_d->sq_wr.send_flags = IBV_SEND_SIGNALED;
587 r_io_u_d->sq_wr.sg_list = &r_io_u_d->rdma_sgl;
588 r_io_u_d->sq_wr.num_sge = 1;
590 case FIO_RDMA_CHA_SEND:
591 r_io_u_d->rdma_sgl.addr = (uint64_t) (unsigned long)io_u->buf;
592 r_io_u_d->rdma_sgl.lkey = io_u->mr->lkey;
593 r_io_u_d->rdma_sgl.length = io_u->buflen;
594 r_io_u_d->sq_wr.wr_id = r_io_u_d->wr_id;
595 r_io_u_d->sq_wr.opcode = IBV_WR_SEND;
596 r_io_u_d->sq_wr.send_flags = IBV_SEND_SIGNALED;
597 r_io_u_d->sq_wr.sg_list = &r_io_u_d->rdma_sgl;
598 r_io_u_d->sq_wr.num_sge = 1;
600 case FIO_RDMA_CHA_RECV:
601 r_io_u_d->rdma_sgl.addr = (uint64_t) (unsigned long)io_u->buf;
602 r_io_u_d->rdma_sgl.lkey = io_u->mr->lkey;
603 r_io_u_d->rdma_sgl.length = io_u->buflen;
604 r_io_u_d->rq_wr.wr_id = r_io_u_d->wr_id;
605 r_io_u_d->rq_wr.sg_list = &r_io_u_d->rdma_sgl;
606 r_io_u_d->rq_wr.num_sge = 1;
609 log_err("fio: unknown rdma protocol - %d\n", rd->rdma_protocol);
616 static struct io_u *fio_rdmaio_event(struct thread_data *td, int event)
618 struct rdmaio_data *rd = td->io_ops_data;
622 io_u = rd->io_us_completed[0];
623 for (i = 0; i < rd->io_u_completed_nr - 1; i++)
624 rd->io_us_completed[i] = rd->io_us_completed[i + 1];
626 rd->io_u_completed_nr--;
628 dprint_io_u(io_u, "fio_rdmaio_event");
633 static int fio_rdmaio_getevents(struct thread_data *td, unsigned int min,
634 unsigned int max, const struct timespec *t)
636 struct rdmaio_data *rd = td->io_ops_data;
637 enum ibv_wc_opcode comp_opcode;
638 struct ibv_cq *ev_cq;
641 comp_opcode = IBV_WC_RDMA_WRITE;
643 switch (rd->rdma_protocol) {
644 case FIO_RDMA_MEM_WRITE:
645 comp_opcode = IBV_WC_RDMA_WRITE;
647 case FIO_RDMA_MEM_READ:
648 comp_opcode = IBV_WC_RDMA_READ;
650 case FIO_RDMA_CHA_SEND:
651 comp_opcode = IBV_WC_SEND;
653 case FIO_RDMA_CHA_RECV:
654 comp_opcode = IBV_WC_RECV;
657 log_err("fio: unknown rdma protocol - %d\n", rd->rdma_protocol);
661 if (rd->cq_event_num > 0) { /* previous left */
667 if (ibv_get_cq_event(rd->channel, &ev_cq, &ev_ctx) != 0) {
668 log_err("fio: Failed to get cq event!\n");
671 if (ev_cq != rd->cq) {
672 log_err("fio: Unknown CQ!\n");
675 if (ibv_req_notify_cq(rd->cq, 0) != 0) {
676 log_err("fio: Failed to set notify!\n");
680 ret = cq_event_handler(td, comp_opcode);
684 ibv_ack_cq_events(rd->cq, ret);
690 rd->cq_event_num -= r;
695 static int fio_rdmaio_send(struct thread_data *td, struct io_u **io_us,
698 struct rdmaio_data *rd = td->io_ops_data;
699 struct ibv_send_wr *bad_wr;
701 enum ibv_wc_opcode comp_opcode;
702 comp_opcode = IBV_WC_RDMA_WRITE;
706 struct rdma_io_u_data *r_io_u_d;
710 for (i = 0; i < nr; i++) {
711 /* RDMA_WRITE or RDMA_READ */
712 switch (rd->rdma_protocol) {
713 case FIO_RDMA_MEM_WRITE:
714 /* compose work request */
715 r_io_u_d = io_us[i]->engine_data;
716 index = __rand(&rd->rand_state) % rd->rmt_nr;
717 r_io_u_d->sq_wr.opcode = IBV_WR_RDMA_WRITE;
718 r_io_u_d->sq_wr.wr.rdma.rkey = rd->rmt_us[index].rkey;
719 r_io_u_d->sq_wr.wr.rdma.remote_addr = \
720 rd->rmt_us[index].buf;
721 r_io_u_d->sq_wr.sg_list->length = io_us[i]->buflen;
723 case FIO_RDMA_MEM_READ:
724 /* compose work request */
725 r_io_u_d = io_us[i]->engine_data;
726 index = __rand(&rd->rand_state) % rd->rmt_nr;
727 r_io_u_d->sq_wr.opcode = IBV_WR_RDMA_READ;
728 r_io_u_d->sq_wr.wr.rdma.rkey = rd->rmt_us[index].rkey;
729 r_io_u_d->sq_wr.wr.rdma.remote_addr = \
730 rd->rmt_us[index].buf;
731 r_io_u_d->sq_wr.sg_list->length = io_us[i]->buflen;
733 case FIO_RDMA_CHA_SEND:
734 r_io_u_d = io_us[i]->engine_data;
735 r_io_u_d->sq_wr.opcode = IBV_WR_SEND;
736 r_io_u_d->sq_wr.send_flags = IBV_SEND_SIGNALED;
739 log_err("fio: unknown rdma protocol - %d\n",
744 if (ibv_post_send(rd->qp, &r_io_u_d->sq_wr, &bad_wr) != 0) {
745 log_err("fio: ibv_post_send fail: %m\n");
749 dprint_io_u(io_us[i], "fio_rdmaio_send");
752 /* wait for completion
753 rdma_poll_wait(td, comp_opcode); */
758 static int fio_rdmaio_recv(struct thread_data *td, struct io_u **io_us,
761 struct rdmaio_data *rd = td->io_ops_data;
762 struct ibv_recv_wr *bad_wr;
763 struct rdma_io_u_data *r_io_u_d;
767 if (rd->rdma_protocol == FIO_RDMA_CHA_RECV) {
768 /* post io_u into recv queue */
769 for (i = 0; i < nr; i++) {
770 r_io_u_d = io_us[i]->engine_data;
771 if (ibv_post_recv(rd->qp, &r_io_u_d->rq_wr, &bad_wr) !=
773 log_err("fio: ibv_post_recv fail: %m\n");
777 } else if ((rd->rdma_protocol == FIO_RDMA_MEM_READ)
778 || (rd->rdma_protocol == FIO_RDMA_MEM_WRITE)) {
779 /* re-post the rq_wr */
780 if (ibv_post_recv(rd->qp, &rd->rq_wr, &bad_wr) != 0) {
781 log_err("fio: ibv_post_recv fail: %m\n");
785 rdma_poll_wait(td, IBV_WC_RECV);
787 dprint(FD_IO, "fio: recv FINISH message\n");
795 static enum fio_q_status fio_rdmaio_queue(struct thread_data *td,
798 struct rdmaio_data *rd = td->io_ops_data;
800 fio_ro_check(td, io_u);
802 if (rd->io_u_queued_nr == (int)td->o.iodepth)
805 rd->io_us_queued[rd->io_u_queued_nr] = io_u;
806 rd->io_u_queued_nr++;
808 dprint_io_u(io_u, "fio_rdmaio_queue");
813 static void fio_rdmaio_queued(struct thread_data *td, struct io_u **io_us,
816 struct rdmaio_data *rd = td->io_ops_data;
820 if (!fio_fill_issue_time(td))
823 fio_gettime(&now, NULL);
825 for (i = 0; i < nr; i++) {
826 struct io_u *io_u = io_us[i];
828 /* queued -> flight */
829 rd->io_us_flight[rd->io_u_flight_nr] = io_u;
830 rd->io_u_flight_nr++;
832 memcpy(&io_u->issue_time, &now, sizeof(now));
833 io_u_queued(td, io_u);
837 * only used for iolog
839 if (td->o.read_iolog_file)
840 memcpy(&td->last_issue, &now, sizeof(now));
843 static int fio_rdmaio_commit(struct thread_data *td)
845 struct rdmaio_data *rd = td->io_ops_data;
849 if (!rd->io_us_queued)
852 io_us = rd->io_us_queued;
854 /* RDMA_WRITE or RDMA_READ */
856 ret = fio_rdmaio_send(td, io_us, rd->io_u_queued_nr);
857 else if (!rd->is_client)
858 ret = fio_rdmaio_recv(td, io_us, rd->io_u_queued_nr);
861 fio_rdmaio_queued(td, io_us, ret);
862 io_u_mark_submit(td, ret);
863 rd->io_u_queued_nr -= ret;
868 } while (rd->io_u_queued_nr);
873 static int fio_rdmaio_connect(struct thread_data *td, struct fio_file *f)
875 struct rdmaio_data *rd = td->io_ops_data;
876 struct rdma_conn_param conn_param;
877 struct ibv_send_wr *bad_wr;
879 memset(&conn_param, 0, sizeof(conn_param));
880 conn_param.responder_resources = 1;
881 conn_param.initiator_depth = 1;
882 conn_param.retry_count = 10;
884 if (rdma_connect(rd->cm_id, &conn_param) != 0) {
885 log_err("fio: rdma_connect fail: %m\n");
889 if (get_next_channel_event
890 (td, rd->cm_channel, RDMA_CM_EVENT_ESTABLISHED) != 0) {
891 log_err("fio: wait for RDMA_CM_EVENT_ESTABLISHED\n");
895 /* send task request */
896 rd->send_buf.mode = htonl(rd->rdma_protocol);
897 rd->send_buf.nr = htonl(td->o.iodepth);
899 if (ibv_post_send(rd->qp, &rd->sq_wr, &bad_wr) != 0) {
900 log_err("fio: ibv_post_send fail: %m\n");
904 if (rdma_poll_wait(td, IBV_WC_SEND) < 0)
907 /* wait for remote MR info from server side */
908 if (rdma_poll_wait(td, IBV_WC_RECV) < 0)
911 /* In SEND/RECV test, it's a good practice to setup the iodepth of
912 * of the RECV side deeper than that of the SEND side to
913 * avoid RNR (receiver not ready) error. The
914 * SEND side may send so many unsolicited message before
915 * RECV side commits sufficient recv buffers into recv queue.
916 * This may lead to RNR error. Here, SEND side pauses for a while
917 * during which RECV side commits sufficient recv buffers.
924 static int fio_rdmaio_accept(struct thread_data *td, struct fio_file *f)
926 struct rdmaio_data *rd = td->io_ops_data;
927 struct rdma_conn_param conn_param;
928 struct ibv_send_wr *bad_wr;
931 /* rdma_accept() - then wait for accept success */
932 memset(&conn_param, 0, sizeof(conn_param));
933 conn_param.responder_resources = 1;
934 conn_param.initiator_depth = 1;
936 if (rdma_accept(rd->child_cm_id, &conn_param) != 0) {
937 log_err("fio: rdma_accept: %m\n");
941 if (get_next_channel_event
942 (td, rd->cm_channel, RDMA_CM_EVENT_ESTABLISHED) != 0) {
943 log_err("fio: wait for RDMA_CM_EVENT_ESTABLISHED\n");
947 /* wait for request */
948 ret = rdma_poll_wait(td, IBV_WC_RECV) < 0;
950 if (ibv_post_send(rd->qp, &rd->sq_wr, &bad_wr) != 0) {
951 log_err("fio: ibv_post_send fail: %m\n");
955 if (rdma_poll_wait(td, IBV_WC_SEND) < 0)
961 static int fio_rdmaio_open_file(struct thread_data *td, struct fio_file *f)
964 return fio_rdmaio_accept(td, f);
966 return fio_rdmaio_connect(td, f);
969 static int fio_rdmaio_close_file(struct thread_data *td, struct fio_file *f)
971 struct rdmaio_data *rd = td->io_ops_data;
972 struct ibv_send_wr *bad_wr;
974 /* unregister rdma buffer */
977 * Client sends notification to the server side
979 /* refer to: http://linux.die.net/man/7/rdma_cm */
980 if ((rd->is_client == 1) && ((rd->rdma_protocol == FIO_RDMA_MEM_WRITE)
981 || (rd->rdma_protocol ==
982 FIO_RDMA_MEM_READ))) {
983 if (ibv_post_send(rd->qp, &rd->sq_wr, &bad_wr) != 0) {
984 log_err("fio: ibv_post_send fail: %m\n");
988 dprint(FD_IO, "fio: close information sent success\n");
989 rdma_poll_wait(td, IBV_WC_SEND);
992 if (rd->is_client == 1)
993 rdma_disconnect(rd->cm_id);
995 rdma_disconnect(rd->child_cm_id);
997 rdma_disconnect(rd->cm_id);
1002 if (get_next_channel_event(td, rd->cm_channel, RDMA_CM_EVENT_DISCONNECTED) != 0) {
1003 log_err("fio: wait for RDMA_CM_EVENT_DISCONNECTED\n");
1008 ibv_destroy_cq(rd->cq);
1009 ibv_destroy_qp(rd->qp);
1011 if (rd->is_client == 1)
1012 rdma_destroy_id(rd->cm_id);
1014 rdma_destroy_id(rd->child_cm_id);
1015 rdma_destroy_id(rd->cm_id);
1018 ibv_destroy_comp_channel(rd->channel);
1019 ibv_dealloc_pd(rd->pd);
1024 static int aton(struct thread_data *td, const char *host,
1025 struct sockaddr_in *addr)
1027 if (inet_aton(host, &addr->sin_addr) != 1) {
1028 struct hostent *hent;
1030 hent = gethostbyname(host);
1032 td_verror(td, errno, "gethostbyname");
1036 memcpy(&addr->sin_addr, hent->h_addr, 4);
1041 static int fio_rdmaio_setup_connect(struct thread_data *td, const char *host,
1042 unsigned short port)
1044 struct rdmaio_data *rd = td->io_ops_data;
1045 struct rdmaio_options *o = td->eo;
1046 struct sockaddr_storage addrb;
1047 struct ibv_recv_wr *bad_wr;
1050 rd->addr.sin_family = AF_INET;
1051 rd->addr.sin_port = htons(port);
1053 err = aton(td, host, &rd->addr);
1058 if (o->bindname && strlen(o->bindname)) {
1059 addrb.ss_family = AF_INET;
1060 err = aton(td, o->bindname, (struct sockaddr_in *)&addrb);
1063 err = rdma_resolve_addr(rd->cm_id, (struct sockaddr *)&addrb,
1064 (struct sockaddr *)&rd->addr, 2000);
1067 err = rdma_resolve_addr(rd->cm_id, NULL,
1068 (struct sockaddr *)&rd->addr, 2000);
1072 log_err("fio: rdma_resolve_addr: %d\n", err);
1076 err = get_next_channel_event(td, rd->cm_channel, RDMA_CM_EVENT_ADDR_RESOLVED);
1078 log_err("fio: get_next_channel_event: %d\n", err);
1083 err = rdma_resolve_route(rd->cm_id, 2000);
1085 log_err("fio: rdma_resolve_route: %d\n", err);
1089 err = get_next_channel_event(td, rd->cm_channel, RDMA_CM_EVENT_ROUTE_RESOLVED);
1091 log_err("fio: get_next_channel_event: %d\n", err);
1095 /* create qp and buffer */
1096 if (fio_rdmaio_setup_qp(td) != 0)
1099 if (fio_rdmaio_setup_control_msg_buffers(td) != 0)
1103 err = ibv_post_recv(rd->qp, &rd->rq_wr, &bad_wr);
1105 log_err("fio: ibv_post_recv fail: %d\n", err);
1112 static int fio_rdmaio_setup_listen(struct thread_data *td, short port)
1114 struct rdmaio_data *rd = td->io_ops_data;
1115 struct rdmaio_options *o = td->eo;
1116 struct ibv_recv_wr *bad_wr;
1117 int state = td->runstate;
1119 td_set_runstate(td, TD_SETTING_UP);
1121 rd->addr.sin_family = AF_INET;
1122 rd->addr.sin_port = htons(port);
1124 if (!o->bindname || !strlen(o->bindname))
1125 rd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
1127 rd->addr.sin_addr.s_addr = htonl(*o->bindname);
1130 if (rdma_bind_addr(rd->cm_id, (struct sockaddr *)&rd->addr) != 0) {
1131 log_err("fio: rdma_bind_addr fail: %m\n");
1135 if (rdma_listen(rd->cm_id, 3) != 0) {
1136 log_err("fio: rdma_listen fail: %m\n");
1140 log_info("fio: waiting for connection\n");
1142 /* wait for CONNECT_REQUEST */
1143 if (get_next_channel_event
1144 (td, rd->cm_channel, RDMA_CM_EVENT_CONNECT_REQUEST) != 0) {
1145 log_err("fio: wait for RDMA_CM_EVENT_CONNECT_REQUEST\n");
1149 if (fio_rdmaio_setup_qp(td) != 0)
1152 if (fio_rdmaio_setup_control_msg_buffers(td) != 0)
1156 if (ibv_post_recv(rd->qp, &rd->rq_wr, &bad_wr) != 0) {
1157 log_err("fio: ibv_post_recv fail: %m\n");
1161 td_set_runstate(td, state);
1165 static int check_set_rlimits(struct thread_data *td)
1167 #ifdef CONFIG_RLIMIT_MEMLOCK
1170 /* check RLIMIT_MEMLOCK */
1171 if (getrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
1172 log_err("fio: getrlimit fail: %d(%s)\n",
1173 errno, strerror(errno));
1178 if ((rl.rlim_cur != RLIM_INFINITY)
1179 && (rl.rlim_cur < td->orig_buffer_size)) {
1180 log_err("fio: soft RLIMIT_MEMLOCK is: %" PRId64 "\n",
1182 log_err("fio: total block size is: %zd\n",
1183 td->orig_buffer_size);
1184 /* try to set larger RLIMIT_MEMLOCK */
1185 rl.rlim_cur = rl.rlim_max;
1186 if (setrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
1187 log_err("fio: setrlimit fail: %d(%s)\n",
1188 errno, strerror(errno));
1189 log_err("fio: you may try enlarge MEMLOCK by root\n");
1190 log_err("# ulimit -l unlimited\n");
1199 static int compat_options(struct thread_data *td)
1201 // The original RDMA engine had an ugly / separator
1202 // on the filename for it's options. This function
1203 // retains backwards compatibility with it. Note we do not
1204 // support setting the bindname option is this legacy mode.
1206 struct rdmaio_options *o = td->eo;
1207 char *modep, *portp;
1208 char *filename = td->o.filename;
1213 portp = strchr(filename, '/');
1220 o->port = strtol(portp, NULL, 10);
1221 if (!o->port || o->port > 65535)
1224 modep = strchr(portp, '/');
1225 if (modep != NULL) {
1231 if (!strncmp("rdma_write", modep, strlen(modep)) ||
1232 !strncmp("RDMA_WRITE", modep, strlen(modep)))
1233 o->verb = FIO_RDMA_MEM_WRITE;
1234 else if (!strncmp("rdma_read", modep, strlen(modep)) ||
1235 !strncmp("RDMA_READ", modep, strlen(modep)))
1236 o->verb = FIO_RDMA_MEM_READ;
1237 else if (!strncmp("send", modep, strlen(modep)) ||
1238 !strncmp("SEND", modep, strlen(modep)))
1239 o->verb = FIO_RDMA_CHA_SEND;
1243 o->verb = FIO_RDMA_MEM_WRITE;
1249 log_err("fio: bad rdma host/port/protocol: %s\n", td->o.filename);
1253 static int fio_rdmaio_init(struct thread_data *td)
1255 struct rdmaio_data *rd = td->io_ops_data;
1256 struct rdmaio_options *o = td->eo;
1260 log_err("fio: rdma connections must be read OR write\n");
1263 if (td_random(td)) {
1264 log_err("fio: RDMA network IO can't be random\n");
1268 if (compat_options(td))
1272 log_err("fio: no port has been specified which is required "
1273 "for the rdma engine\n");
1277 if (check_set_rlimits(td))
1280 rd->rdma_protocol = o->verb;
1281 rd->cq_event_num = 0;
1283 rd->cm_channel = rdma_create_event_channel();
1284 if (!rd->cm_channel) {
1285 log_err("fio: rdma_create_event_channel fail: %m\n");
1289 ret = rdma_create_id(rd->cm_channel, &rd->cm_id, rd, RDMA_PS_TCP);
1291 log_err("fio: rdma_create_id fail: %m\n");
1295 if ((rd->rdma_protocol == FIO_RDMA_MEM_WRITE) ||
1296 (rd->rdma_protocol == FIO_RDMA_MEM_READ)) {
1297 rd->rmt_us = calloc(FIO_RDMA_MAX_IO_DEPTH,
1298 sizeof(struct remote_u));
1302 rd->io_us_queued = calloc(td->o.iodepth, sizeof(struct io_u *));
1303 rd->io_u_queued_nr = 0;
1305 rd->io_us_flight = calloc(td->o.iodepth, sizeof(struct io_u *));
1306 rd->io_u_flight_nr = 0;
1308 rd->io_us_completed = calloc(td->o.iodepth, sizeof(struct io_u *));
1309 rd->io_u_completed_nr = 0;
1311 if (td_read(td)) { /* READ as the server */
1313 td->flags |= TD_F_NO_PROGRESS;
1314 /* server rd->rdma_buf_len will be setup after got request */
1315 ret = fio_rdmaio_setup_listen(td, o->port);
1316 } else { /* WRITE as the client */
1318 ret = fio_rdmaio_setup_connect(td, td->o.filename, o->port);
1322 static int fio_rdmaio_post_init(struct thread_data *td)
1324 unsigned int max_bs;
1326 struct rdmaio_data *rd = td->io_ops_data;
1328 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
1329 rd->send_buf.max_bs = htonl(max_bs);
1331 /* register each io_u in the free list */
1332 for (i = 0; i < td->io_u_freelist.nr; i++) {
1333 struct io_u *io_u = td->io_u_freelist.io_us[i];
1335 io_u->engine_data = calloc(1, sizeof(struct rdma_io_u_data));
1336 ((struct rdma_io_u_data *)io_u->engine_data)->wr_id = i;
1338 io_u->mr = ibv_reg_mr(rd->pd, io_u->buf, max_bs,
1339 IBV_ACCESS_LOCAL_WRITE |
1340 IBV_ACCESS_REMOTE_READ |
1341 IBV_ACCESS_REMOTE_WRITE);
1342 if (io_u->mr == NULL) {
1343 log_err("fio: ibv_reg_mr io_u failed: %m\n");
1347 rd->send_buf.rmt_us[i].buf =
1348 cpu_to_be64((uint64_t) (unsigned long)io_u->buf);
1349 rd->send_buf.rmt_us[i].rkey = htonl(io_u->mr->rkey);
1350 rd->send_buf.rmt_us[i].size = htonl(max_bs);
1353 log_info("fio: Send rkey %x addr %" PRIx64 " len %d to client\n", io_u->mr->rkey, io_u->buf, max_bs); */
1357 rd->send_buf.nr = htonl(i);
1362 static void fio_rdmaio_cleanup(struct thread_data *td)
1364 struct rdmaio_data *rd = td->io_ops_data;
1370 static int fio_rdmaio_setup(struct thread_data *td)
1372 struct rdmaio_data *rd;
1374 if (!td->files_index) {
1375 add_file(td, td->o.filename ?: "rdma", 0, 0);
1376 td->o.nr_files = td->o.nr_files ?: 1;
1380 if (!td->io_ops_data) {
1381 rd = calloc(1, sizeof(*rd));
1382 init_rand_seed(&rd->rand_state, (unsigned int) GOLDEN_RATIO_64, 0);
1383 td->io_ops_data = rd;
1389 FIO_STATIC struct ioengine_ops ioengine = {
1391 .version = FIO_IOOPS_VERSION,
1392 .setup = fio_rdmaio_setup,
1393 .init = fio_rdmaio_init,
1394 .post_init = fio_rdmaio_post_init,
1395 .prep = fio_rdmaio_prep,
1396 .queue = fio_rdmaio_queue,
1397 .commit = fio_rdmaio_commit,
1398 .getevents = fio_rdmaio_getevents,
1399 .event = fio_rdmaio_event,
1400 .cleanup = fio_rdmaio_cleanup,
1401 .open_file = fio_rdmaio_open_file,
1402 .close_file = fio_rdmaio_close_file,
1403 .flags = FIO_DISKLESSIO | FIO_UNIDIR | FIO_PIPEIO |
1404 FIO_ASYNCIO_SETS_ISSUE_TIME,
1406 .option_struct_size = sizeof(struct rdmaio_options),
1409 static void fio_init fio_rdmaio_register(void)
1411 register_ioengine(&ioengine);
1414 static void fio_exit fio_rdmaio_unregister(void)
1416 unregister_ioengine(&ioengine);