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>
47 #include <infiniband/arch.h>
49 #define FIO_RDMA_MAX_IO_DEPTH 512
59 struct rdmaio_options {
60 struct thread_data *td;
62 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 port",
89 .off1 = offsetof(struct rdmaio_options, port),
92 .help = "Port to use for RDMA connections",
93 .category = FIO_OPT_C_ENGINE,
94 .group = FIO_OPT_G_RDMA,
98 .lname = "RDMA engine verb",
101 .off1 = offsetof(struct rdmaio_options, verb),
102 .help = "RDMA engine verb",
106 .oval = FIO_RDMA_MEM_WRITE,
107 .help = "Memory Write",
110 .oval = FIO_RDMA_MEM_READ,
111 .help = "Memory Read",
114 .oval = FIO_RDMA_CHA_SEND,
115 .help = "Posted Send",
118 .oval = FIO_RDMA_CHA_RECV,
119 .help = "Posted Receive",
122 .category = FIO_OPT_C_ENGINE,
123 .group = FIO_OPT_G_RDMA,
136 struct rdma_info_blk {
137 uint32_t mode; /* channel semantic or memory semantic */
138 uint32_t nr; /* client: io depth
139 server: number of records for memory semantic
141 uint32_t max_bs; /* maximum block size */
142 struct remote_u rmt_us[FIO_RDMA_MAX_IO_DEPTH];
145 struct rdma_io_u_data {
147 struct ibv_send_wr sq_wr;
148 struct ibv_recv_wr rq_wr;
149 struct ibv_sge rdma_sgl;
154 enum rdma_io_mode rdma_protocol;
156 struct sockaddr_in addr;
158 struct ibv_recv_wr rq_wr;
159 struct ibv_sge recv_sgl;
160 struct rdma_info_blk recv_buf;
161 struct ibv_mr *recv_mr;
163 struct ibv_send_wr sq_wr;
164 struct ibv_sge send_sgl;
165 struct rdma_info_blk send_buf;
166 struct ibv_mr *send_mr;
168 struct ibv_comp_channel *channel;
174 struct rdma_event_channel *cm_channel;
175 struct rdma_cm_id *cm_id;
176 struct rdma_cm_id *child_cm_id;
180 struct remote_u *rmt_us;
182 struct io_u **io_us_queued;
184 struct io_u **io_us_flight;
186 struct io_u **io_us_completed;
187 int io_u_completed_nr;
189 struct frand_state rand_state;
192 static int client_recv(struct thread_data *td, struct ibv_wc *wc)
194 struct rdmaio_data *rd = td->io_ops_data;
197 if (wc->byte_len != sizeof(rd->recv_buf)) {
198 log_err("Received bogus data, size %d\n", wc->byte_len);
202 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
203 if (max_bs > ntohl(rd->recv_buf.max_bs)) {
204 log_err("fio: Server's block size (%d) must be greater than or "
205 "equal to the client's block size (%d)!\n",
206 ntohl(rd->recv_buf.max_bs), max_bs);
210 /* store mr info for MEMORY semantic */
211 if ((rd->rdma_protocol == FIO_RDMA_MEM_WRITE) ||
212 (rd->rdma_protocol == FIO_RDMA_MEM_READ)) {
213 /* struct flist_head *entry; */
216 rd->rmt_nr = ntohl(rd->recv_buf.nr);
218 for (i = 0; i < rd->rmt_nr; i++) {
219 rd->rmt_us[i].buf = ntohll(rd->recv_buf.rmt_us[i].buf);
220 rd->rmt_us[i].rkey = ntohl(rd->recv_buf.rmt_us[i].rkey);
221 rd->rmt_us[i].size = ntohl(rd->recv_buf.rmt_us[i].size);
224 "fio: Received rkey %x addr %" PRIx64
225 " len %d from peer\n", rd->rmt_us[i].rkey,
226 rd->rmt_us[i].buf, rd->rmt_us[i].size);
233 static int server_recv(struct thread_data *td, struct ibv_wc *wc)
235 struct rdmaio_data *rd = td->io_ops_data;
238 if (wc->wr_id == FIO_RDMA_MAX_IO_DEPTH) {
239 rd->rdma_protocol = ntohl(rd->recv_buf.mode);
241 /* CHANNEL semantic, do nothing */
242 if (rd->rdma_protocol == FIO_RDMA_CHA_SEND)
243 rd->rdma_protocol = FIO_RDMA_CHA_RECV;
245 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
246 if (max_bs < ntohl(rd->recv_buf.max_bs)) {
247 log_err("fio: Server's block size (%d) must be greater than or "
248 "equal to the client's block size (%d)!\n",
249 ntohl(rd->recv_buf.max_bs), max_bs);
258 static int cq_event_handler(struct thread_data *td, enum ibv_wc_opcode opcode)
260 struct rdmaio_data *rd = td->io_ops_data;
262 struct rdma_io_u_data *r_io_u_d;
267 while ((ret = ibv_poll_cq(rd->cq, 1, &wc)) == 1) {
272 log_err("fio: cq completion status %d(%s)\n",
273 wc.status, ibv_wc_status_str(wc.status));
280 if (rd->is_client == 1)
281 ret = client_recv(td, &wc);
283 ret = server_recv(td, &wc);
288 if (wc.wr_id == FIO_RDMA_MAX_IO_DEPTH)
291 for (i = 0; i < rd->io_u_flight_nr; i++) {
292 r_io_u_d = rd->io_us_flight[i]->engine_data;
294 if (wc.wr_id == r_io_u_d->rq_wr.wr_id) {
295 rd->io_us_flight[i]->resid =
296 rd->io_us_flight[i]->buflen
299 rd->io_us_flight[i]->error = 0;
301 rd->io_us_completed[rd->
303 = rd->io_us_flight[i];
304 rd->io_u_completed_nr++;
308 if (i == rd->io_u_flight_nr)
309 log_err("fio: recv wr %" PRId64 " not found\n",
312 /* put the last one into middle of the list */
313 rd->io_us_flight[i] =
314 rd->io_us_flight[rd->io_u_flight_nr - 1];
315 rd->io_u_flight_nr--;
321 case IBV_WC_RDMA_WRITE:
322 case IBV_WC_RDMA_READ:
323 if (wc.wr_id == FIO_RDMA_MAX_IO_DEPTH)
326 for (i = 0; i < rd->io_u_flight_nr; i++) {
327 r_io_u_d = rd->io_us_flight[i]->engine_data;
329 if (wc.wr_id == r_io_u_d->sq_wr.wr_id) {
330 rd->io_us_completed[rd->
332 = rd->io_us_flight[i];
333 rd->io_u_completed_nr++;
337 if (i == rd->io_u_flight_nr)
338 log_err("fio: send wr %" PRId64 " not found\n",
341 /* put the last one into middle of the list */
342 rd->io_us_flight[i] =
343 rd->io_us_flight[rd->io_u_flight_nr - 1];
344 rd->io_u_flight_nr--;
350 log_info("fio: unknown completion event %d\n",
358 log_err("fio: poll error %d\n", ret);
366 * Return -1 for error and 'nr events' for a positive number
369 static int rdma_poll_wait(struct thread_data *td, enum ibv_wc_opcode opcode)
371 struct rdmaio_data *rd = td->io_ops_data;
372 struct ibv_cq *ev_cq;
376 if (rd->cq_event_num > 0) { /* previous left */
382 if (ibv_get_cq_event(rd->channel, &ev_cq, &ev_ctx) != 0) {
383 log_err("fio: Failed to get cq event!\n");
386 if (ev_cq != rd->cq) {
387 log_err("fio: Unknown CQ!\n");
390 if (ibv_req_notify_cq(rd->cq, 0) != 0) {
391 log_err("fio: Failed to set notify!\n");
395 ret = cq_event_handler(td, opcode);
399 ibv_ack_cq_events(rd->cq, ret);
406 static int fio_rdmaio_setup_qp(struct thread_data *td)
408 struct rdmaio_data *rd = td->io_ops_data;
409 struct ibv_qp_init_attr init_attr;
410 int qp_depth = td->o.iodepth * 2; /* 2 times of io depth */
412 if (rd->is_client == 0)
413 rd->pd = ibv_alloc_pd(rd->child_cm_id->verbs);
415 rd->pd = ibv_alloc_pd(rd->cm_id->verbs);
417 if (rd->pd == NULL) {
418 log_err("fio: ibv_alloc_pd fail: %m\n");
422 if (rd->is_client == 0)
423 rd->channel = ibv_create_comp_channel(rd->child_cm_id->verbs);
425 rd->channel = ibv_create_comp_channel(rd->cm_id->verbs);
426 if (rd->channel == NULL) {
427 log_err("fio: ibv_create_comp_channel fail: %m\n");
434 if (rd->is_client == 0)
435 rd->cq = ibv_create_cq(rd->child_cm_id->verbs,
436 qp_depth, rd, rd->channel, 0);
438 rd->cq = ibv_create_cq(rd->cm_id->verbs,
439 qp_depth, rd, rd->channel, 0);
440 if (rd->cq == NULL) {
441 log_err("fio: ibv_create_cq failed: %m\n");
445 if (ibv_req_notify_cq(rd->cq, 0) != 0) {
446 log_err("fio: ibv_req_notify_cq failed: %m\n");
450 /* create queue pair */
451 memset(&init_attr, 0, sizeof(init_attr));
452 init_attr.cap.max_send_wr = qp_depth;
453 init_attr.cap.max_recv_wr = qp_depth;
454 init_attr.cap.max_recv_sge = 1;
455 init_attr.cap.max_send_sge = 1;
456 init_attr.qp_type = IBV_QPT_RC;
457 init_attr.send_cq = rd->cq;
458 init_attr.recv_cq = rd->cq;
460 if (rd->is_client == 0) {
461 if (rdma_create_qp(rd->child_cm_id, rd->pd, &init_attr) != 0) {
462 log_err("fio: rdma_create_qp failed: %m\n");
465 rd->qp = rd->child_cm_id->qp;
467 if (rdma_create_qp(rd->cm_id, rd->pd, &init_attr) != 0) {
468 log_err("fio: rdma_create_qp failed: %m\n");
471 rd->qp = rd->cm_id->qp;
477 ibv_destroy_cq(rd->cq);
479 ibv_destroy_comp_channel(rd->channel);
481 ibv_dealloc_pd(rd->pd);
486 static int fio_rdmaio_setup_control_msg_buffers(struct thread_data *td)
488 struct rdmaio_data *rd = td->io_ops_data;
490 rd->recv_mr = ibv_reg_mr(rd->pd, &rd->recv_buf, sizeof(rd->recv_buf),
491 IBV_ACCESS_LOCAL_WRITE);
492 if (rd->recv_mr == NULL) {
493 log_err("fio: recv_buf reg_mr failed: %m\n");
497 rd->send_mr = ibv_reg_mr(rd->pd, &rd->send_buf, sizeof(rd->send_buf),
499 if (rd->send_mr == NULL) {
500 log_err("fio: send_buf reg_mr failed: %m\n");
501 ibv_dereg_mr(rd->recv_mr);
505 /* setup work request */
507 rd->recv_sgl.addr = (uint64_t) (unsigned long)&rd->recv_buf;
508 rd->recv_sgl.length = sizeof(rd->recv_buf);
509 rd->recv_sgl.lkey = rd->recv_mr->lkey;
510 rd->rq_wr.sg_list = &rd->recv_sgl;
511 rd->rq_wr.num_sge = 1;
512 rd->rq_wr.wr_id = FIO_RDMA_MAX_IO_DEPTH;
515 rd->send_sgl.addr = (uint64_t) (unsigned long)&rd->send_buf;
516 rd->send_sgl.length = sizeof(rd->send_buf);
517 rd->send_sgl.lkey = rd->send_mr->lkey;
519 rd->sq_wr.opcode = IBV_WR_SEND;
520 rd->sq_wr.send_flags = IBV_SEND_SIGNALED;
521 rd->sq_wr.sg_list = &rd->send_sgl;
522 rd->sq_wr.num_sge = 1;
523 rd->sq_wr.wr_id = FIO_RDMA_MAX_IO_DEPTH;
528 static int get_next_channel_event(struct thread_data *td,
529 struct rdma_event_channel *channel,
530 enum rdma_cm_event_type wait_event)
532 struct rdmaio_data *rd = td->io_ops_data;
533 struct rdma_cm_event *event;
536 ret = rdma_get_cm_event(channel, &event);
538 log_err("fio: rdma_get_cm_event: %d\n", ret);
542 if (event->event != wait_event) {
543 log_err("fio: event is %s instead of %s\n",
544 rdma_event_str(event->event),
545 rdma_event_str(wait_event));
549 switch (event->event) {
550 case RDMA_CM_EVENT_CONNECT_REQUEST:
551 rd->child_cm_id = event->id;
557 rdma_ack_cm_event(event);
562 static int fio_rdmaio_prep(struct thread_data *td, struct io_u *io_u)
564 struct rdmaio_data *rd = td->io_ops_data;
565 struct rdma_io_u_data *r_io_u_d;
567 r_io_u_d = io_u->engine_data;
569 switch (rd->rdma_protocol) {
570 case FIO_RDMA_MEM_WRITE:
571 case FIO_RDMA_MEM_READ:
572 r_io_u_d->rdma_sgl.addr = (uint64_t) (unsigned long)io_u->buf;
573 r_io_u_d->rdma_sgl.lkey = io_u->mr->lkey;
574 r_io_u_d->sq_wr.wr_id = r_io_u_d->wr_id;
575 r_io_u_d->sq_wr.send_flags = IBV_SEND_SIGNALED;
576 r_io_u_d->sq_wr.sg_list = &r_io_u_d->rdma_sgl;
577 r_io_u_d->sq_wr.num_sge = 1;
579 case FIO_RDMA_CHA_SEND:
580 r_io_u_d->rdma_sgl.addr = (uint64_t) (unsigned long)io_u->buf;
581 r_io_u_d->rdma_sgl.lkey = io_u->mr->lkey;
582 r_io_u_d->rdma_sgl.length = io_u->buflen;
583 r_io_u_d->sq_wr.wr_id = r_io_u_d->wr_id;
584 r_io_u_d->sq_wr.opcode = IBV_WR_SEND;
585 r_io_u_d->sq_wr.send_flags = IBV_SEND_SIGNALED;
586 r_io_u_d->sq_wr.sg_list = &r_io_u_d->rdma_sgl;
587 r_io_u_d->sq_wr.num_sge = 1;
589 case FIO_RDMA_CHA_RECV:
590 r_io_u_d->rdma_sgl.addr = (uint64_t) (unsigned long)io_u->buf;
591 r_io_u_d->rdma_sgl.lkey = io_u->mr->lkey;
592 r_io_u_d->rdma_sgl.length = io_u->buflen;
593 r_io_u_d->rq_wr.wr_id = r_io_u_d->wr_id;
594 r_io_u_d->rq_wr.sg_list = &r_io_u_d->rdma_sgl;
595 r_io_u_d->rq_wr.num_sge = 1;
598 log_err("fio: unknown rdma protocol - %d\n", rd->rdma_protocol);
605 static struct io_u *fio_rdmaio_event(struct thread_data *td, int event)
607 struct rdmaio_data *rd = td->io_ops_data;
611 io_u = rd->io_us_completed[0];
612 for (i = 0; i < rd->io_u_completed_nr - 1; i++)
613 rd->io_us_completed[i] = rd->io_us_completed[i + 1];
615 rd->io_u_completed_nr--;
617 dprint_io_u(io_u, "fio_rdmaio_event");
622 static int fio_rdmaio_getevents(struct thread_data *td, unsigned int min,
623 unsigned int max, const struct timespec *t)
625 struct rdmaio_data *rd = td->io_ops_data;
626 enum ibv_wc_opcode comp_opcode;
627 struct ibv_cq *ev_cq;
630 comp_opcode = IBV_WC_RDMA_WRITE;
632 switch (rd->rdma_protocol) {
633 case FIO_RDMA_MEM_WRITE:
634 comp_opcode = IBV_WC_RDMA_WRITE;
636 case FIO_RDMA_MEM_READ:
637 comp_opcode = IBV_WC_RDMA_READ;
639 case FIO_RDMA_CHA_SEND:
640 comp_opcode = IBV_WC_SEND;
642 case FIO_RDMA_CHA_RECV:
643 comp_opcode = IBV_WC_RECV;
646 log_err("fio: unknown rdma protocol - %d\n", rd->rdma_protocol);
650 if (rd->cq_event_num > 0) { /* previous left */
656 if (ibv_get_cq_event(rd->channel, &ev_cq, &ev_ctx) != 0) {
657 log_err("fio: Failed to get cq event!\n");
660 if (ev_cq != rd->cq) {
661 log_err("fio: Unknown CQ!\n");
664 if (ibv_req_notify_cq(rd->cq, 0) != 0) {
665 log_err("fio: Failed to set notify!\n");
669 ret = cq_event_handler(td, comp_opcode);
673 ibv_ack_cq_events(rd->cq, ret);
679 rd->cq_event_num -= r;
684 static int fio_rdmaio_send(struct thread_data *td, struct io_u **io_us,
687 struct rdmaio_data *rd = td->io_ops_data;
688 struct ibv_send_wr *bad_wr;
690 enum ibv_wc_opcode comp_opcode;
691 comp_opcode = IBV_WC_RDMA_WRITE;
695 struct rdma_io_u_data *r_io_u_d;
699 for (i = 0; i < nr; i++) {
700 /* RDMA_WRITE or RDMA_READ */
701 switch (rd->rdma_protocol) {
702 case FIO_RDMA_MEM_WRITE:
703 /* compose work request */
704 r_io_u_d = io_us[i]->engine_data;
705 index = __rand(&rd->rand_state) % rd->rmt_nr;
706 r_io_u_d->sq_wr.opcode = IBV_WR_RDMA_WRITE;
707 r_io_u_d->sq_wr.wr.rdma.rkey = rd->rmt_us[index].rkey;
708 r_io_u_d->sq_wr.wr.rdma.remote_addr = \
709 rd->rmt_us[index].buf;
710 r_io_u_d->sq_wr.sg_list->length = io_us[i]->buflen;
712 case FIO_RDMA_MEM_READ:
713 /* compose work request */
714 r_io_u_d = io_us[i]->engine_data;
715 index = __rand(&rd->rand_state) % rd->rmt_nr;
716 r_io_u_d->sq_wr.opcode = IBV_WR_RDMA_READ;
717 r_io_u_d->sq_wr.wr.rdma.rkey = rd->rmt_us[index].rkey;
718 r_io_u_d->sq_wr.wr.rdma.remote_addr = \
719 rd->rmt_us[index].buf;
720 r_io_u_d->sq_wr.sg_list->length = io_us[i]->buflen;
722 case FIO_RDMA_CHA_SEND:
723 r_io_u_d = io_us[i]->engine_data;
724 r_io_u_d->sq_wr.opcode = IBV_WR_SEND;
725 r_io_u_d->sq_wr.send_flags = IBV_SEND_SIGNALED;
728 log_err("fio: unknown rdma protocol - %d\n",
733 if (ibv_post_send(rd->qp, &r_io_u_d->sq_wr, &bad_wr) != 0) {
734 log_err("fio: ibv_post_send fail: %m\n");
738 dprint_io_u(io_us[i], "fio_rdmaio_send");
741 /* wait for completion
742 rdma_poll_wait(td, comp_opcode); */
747 static int fio_rdmaio_recv(struct thread_data *td, struct io_u **io_us,
750 struct rdmaio_data *rd = td->io_ops_data;
751 struct ibv_recv_wr *bad_wr;
752 struct rdma_io_u_data *r_io_u_d;
756 if (rd->rdma_protocol == FIO_RDMA_CHA_RECV) {
757 /* post io_u into recv queue */
758 for (i = 0; i < nr; i++) {
759 r_io_u_d = io_us[i]->engine_data;
760 if (ibv_post_recv(rd->qp, &r_io_u_d->rq_wr, &bad_wr) !=
762 log_err("fio: ibv_post_recv fail: %m\n");
766 } else if ((rd->rdma_protocol == FIO_RDMA_MEM_READ)
767 || (rd->rdma_protocol == FIO_RDMA_MEM_WRITE)) {
768 /* re-post the rq_wr */
769 if (ibv_post_recv(rd->qp, &rd->rq_wr, &bad_wr) != 0) {
770 log_err("fio: ibv_post_recv fail: %m\n");
774 rdma_poll_wait(td, IBV_WC_RECV);
776 dprint(FD_IO, "fio: recv FINISH message\n");
784 static int fio_rdmaio_queue(struct thread_data *td, struct io_u *io_u)
786 struct rdmaio_data *rd = td->io_ops_data;
788 fio_ro_check(td, io_u);
790 if (rd->io_u_queued_nr == (int)td->o.iodepth)
793 rd->io_us_queued[rd->io_u_queued_nr] = io_u;
794 rd->io_u_queued_nr++;
796 dprint_io_u(io_u, "fio_rdmaio_queue");
801 static void fio_rdmaio_queued(struct thread_data *td, struct io_u **io_us,
804 struct rdmaio_data *rd = td->io_ops_data;
808 if (!fio_fill_issue_time(td))
811 fio_gettime(&now, NULL);
813 for (i = 0; i < nr; i++) {
814 struct io_u *io_u = io_us[i];
816 /* queued -> flight */
817 rd->io_us_flight[rd->io_u_flight_nr] = io_u;
818 rd->io_u_flight_nr++;
820 memcpy(&io_u->issue_time, &now, sizeof(now));
821 io_u_queued(td, io_u);
825 static int fio_rdmaio_commit(struct thread_data *td)
827 struct rdmaio_data *rd = td->io_ops_data;
831 if (!rd->io_us_queued)
834 io_us = rd->io_us_queued;
836 /* RDMA_WRITE or RDMA_READ */
838 ret = fio_rdmaio_send(td, io_us, rd->io_u_queued_nr);
839 else if (!rd->is_client)
840 ret = fio_rdmaio_recv(td, io_us, rd->io_u_queued_nr);
842 ret = 0; /* must be a SYNC */
845 fio_rdmaio_queued(td, io_us, ret);
846 io_u_mark_submit(td, ret);
847 rd->io_u_queued_nr -= ret;
852 } while (rd->io_u_queued_nr);
857 static int fio_rdmaio_connect(struct thread_data *td, struct fio_file *f)
859 struct rdmaio_data *rd = td->io_ops_data;
860 struct rdma_conn_param conn_param;
861 struct ibv_send_wr *bad_wr;
863 memset(&conn_param, 0, sizeof(conn_param));
864 conn_param.responder_resources = 1;
865 conn_param.initiator_depth = 1;
866 conn_param.retry_count = 10;
868 if (rdma_connect(rd->cm_id, &conn_param) != 0) {
869 log_err("fio: rdma_connect fail: %m\n");
873 if (get_next_channel_event
874 (td, rd->cm_channel, RDMA_CM_EVENT_ESTABLISHED) != 0) {
875 log_err("fio: wait for RDMA_CM_EVENT_ESTABLISHED\n");
879 /* send task request */
880 rd->send_buf.mode = htonl(rd->rdma_protocol);
881 rd->send_buf.nr = htonl(td->o.iodepth);
883 if (ibv_post_send(rd->qp, &rd->sq_wr, &bad_wr) != 0) {
884 log_err("fio: ibv_post_send fail: %m");
888 if (rdma_poll_wait(td, IBV_WC_SEND) < 0)
891 /* wait for remote MR info from server side */
892 if (rdma_poll_wait(td, IBV_WC_RECV) < 0)
895 /* In SEND/RECV test, it's a good practice to setup the iodepth of
896 * of the RECV side deeper than that of the SEND side to
897 * avoid RNR (receiver not ready) error. The
898 * SEND side may send so many unsolicited message before
899 * RECV side commits sufficient recv buffers into recv queue.
900 * This may lead to RNR error. Here, SEND side pauses for a while
901 * during which RECV side commits sufficient recv buffers.
908 static int fio_rdmaio_accept(struct thread_data *td, struct fio_file *f)
910 struct rdmaio_data *rd = td->io_ops_data;
911 struct rdma_conn_param conn_param;
912 struct ibv_send_wr *bad_wr;
915 /* rdma_accept() - then wait for accept success */
916 memset(&conn_param, 0, sizeof(conn_param));
917 conn_param.responder_resources = 1;
918 conn_param.initiator_depth = 1;
920 if (rdma_accept(rd->child_cm_id, &conn_param) != 0) {
921 log_err("fio: rdma_accept: %m\n");
925 if (get_next_channel_event
926 (td, rd->cm_channel, RDMA_CM_EVENT_ESTABLISHED) != 0) {
927 log_err("fio: wait for RDMA_CM_EVENT_ESTABLISHED\n");
931 /* wait for request */
932 ret = rdma_poll_wait(td, IBV_WC_RECV) < 0;
934 if (ibv_post_send(rd->qp, &rd->sq_wr, &bad_wr) != 0) {
935 log_err("fio: ibv_post_send fail: %m");
939 if (rdma_poll_wait(td, IBV_WC_SEND) < 0)
945 static int fio_rdmaio_open_file(struct thread_data *td, struct fio_file *f)
948 return fio_rdmaio_accept(td, f);
950 return fio_rdmaio_connect(td, f);
953 static int fio_rdmaio_close_file(struct thread_data *td, struct fio_file *f)
955 struct rdmaio_data *rd = td->io_ops_data;
956 struct ibv_send_wr *bad_wr;
958 /* unregister rdma buffer */
961 * Client sends notification to the server side
963 /* refer to: http://linux.die.net/man/7/rdma_cm */
964 if ((rd->is_client == 1) && ((rd->rdma_protocol == FIO_RDMA_MEM_WRITE)
965 || (rd->rdma_protocol ==
966 FIO_RDMA_MEM_READ))) {
967 if (ibv_post_send(rd->qp, &rd->sq_wr, &bad_wr) != 0) {
968 log_err("fio: ibv_post_send fail: %m");
972 dprint(FD_IO, "fio: close information sent success\n");
973 rdma_poll_wait(td, IBV_WC_SEND);
976 if (rd->is_client == 1)
977 rdma_disconnect(rd->cm_id);
979 rdma_disconnect(rd->child_cm_id);
981 rdma_disconnect(rd->cm_id);
986 if (get_next_channel_event(td, rd->cm_channel, RDMA_CM_EVENT_DISCONNECTED) != 0) {
987 log_err("fio: wait for RDMA_CM_EVENT_DISCONNECTED\n");
992 ibv_destroy_cq(rd->cq);
993 ibv_destroy_qp(rd->qp);
995 if (rd->is_client == 1)
996 rdma_destroy_id(rd->cm_id);
998 rdma_destroy_id(rd->child_cm_id);
999 rdma_destroy_id(rd->cm_id);
1002 ibv_destroy_comp_channel(rd->channel);
1003 ibv_dealloc_pd(rd->pd);
1008 static int fio_rdmaio_setup_connect(struct thread_data *td, const char *host,
1009 unsigned short port)
1011 struct rdmaio_data *rd = td->io_ops_data;
1012 struct ibv_recv_wr *bad_wr;
1015 rd->addr.sin_family = AF_INET;
1016 rd->addr.sin_port = htons(port);
1018 if (inet_aton(host, &rd->addr.sin_addr) != 1) {
1019 struct hostent *hent;
1021 hent = gethostbyname(host);
1023 td_verror(td, errno, "gethostbyname");
1027 memcpy(&rd->addr.sin_addr, hent->h_addr, 4);
1031 err = rdma_resolve_addr(rd->cm_id, NULL, (struct sockaddr *)&rd->addr, 2000);
1033 log_err("fio: rdma_resolve_addr: %d\n", err);
1037 err = get_next_channel_event(td, rd->cm_channel, RDMA_CM_EVENT_ADDR_RESOLVED);
1039 log_err("fio: get_next_channel_event: %d\n", err);
1044 err = rdma_resolve_route(rd->cm_id, 2000);
1046 log_err("fio: rdma_resolve_route: %d\n", err);
1050 err = get_next_channel_event(td, rd->cm_channel, RDMA_CM_EVENT_ROUTE_RESOLVED);
1052 log_err("fio: get_next_channel_event: %d\n", err);
1056 /* create qp and buffer */
1057 if (fio_rdmaio_setup_qp(td) != 0)
1060 if (fio_rdmaio_setup_control_msg_buffers(td) != 0)
1064 err = ibv_post_recv(rd->qp, &rd->rq_wr, &bad_wr);
1066 log_err("fio: ibv_post_recv fail: %d\n", err);
1073 static int fio_rdmaio_setup_listen(struct thread_data *td, short port)
1075 struct rdmaio_data *rd = td->io_ops_data;
1076 struct ibv_recv_wr *bad_wr;
1077 int state = td->runstate;
1079 td_set_runstate(td, TD_SETTING_UP);
1081 rd->addr.sin_family = AF_INET;
1082 rd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
1083 rd->addr.sin_port = htons(port);
1086 if (rdma_bind_addr(rd->cm_id, (struct sockaddr *)&rd->addr) != 0) {
1087 log_err("fio: rdma_bind_addr fail: %m\n");
1091 if (rdma_listen(rd->cm_id, 3) != 0) {
1092 log_err("fio: rdma_listen fail: %m\n");
1096 log_info("fio: waiting for connection\n");
1098 /* wait for CONNECT_REQUEST */
1099 if (get_next_channel_event
1100 (td, rd->cm_channel, RDMA_CM_EVENT_CONNECT_REQUEST) != 0) {
1101 log_err("fio: wait for RDMA_CM_EVENT_CONNECT_REQUEST\n");
1105 if (fio_rdmaio_setup_qp(td) != 0)
1108 if (fio_rdmaio_setup_control_msg_buffers(td) != 0)
1112 if (ibv_post_recv(rd->qp, &rd->rq_wr, &bad_wr) != 0) {
1113 log_err("fio: ibv_post_recv fail: %m\n");
1117 td_set_runstate(td, state);
1121 static int check_set_rlimits(struct thread_data *td)
1123 #ifdef CONFIG_RLIMIT_MEMLOCK
1126 /* check RLIMIT_MEMLOCK */
1127 if (getrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
1128 log_err("fio: getrlimit fail: %d(%s)\n",
1129 errno, strerror(errno));
1134 if ((rl.rlim_cur != RLIM_INFINITY)
1135 && (rl.rlim_cur < td->orig_buffer_size)) {
1136 log_err("fio: soft RLIMIT_MEMLOCK is: %" PRId64 "\n",
1138 log_err("fio: total block size is: %zd\n",
1139 td->orig_buffer_size);
1140 /* try to set larger RLIMIT_MEMLOCK */
1141 rl.rlim_cur = rl.rlim_max;
1142 if (setrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
1143 log_err("fio: setrlimit fail: %d(%s)\n",
1144 errno, strerror(errno));
1145 log_err("fio: you may try enlarge MEMLOCK by root\n");
1146 log_err("# ulimit -l unlimited\n");
1155 static int compat_options(struct thread_data *td)
1157 // The original RDMA engine had an ugly / seperator
1158 // on the filename for it's options. This function
1159 // retains backwards compatibility with it.100
1161 struct rdmaio_options *o = td->eo;
1162 char *modep, *portp;
1163 char *filename = td->o.filename;
1168 portp = strchr(filename, '/');
1175 o->port = strtol(portp, NULL, 10);
1176 if (!o->port || o->port > 65535)
1179 modep = strchr(portp, '/');
1180 if (modep != NULL) {
1186 if (!strncmp("rdma_write", modep, strlen(modep)) ||
1187 !strncmp("RDMA_WRITE", modep, strlen(modep)))
1188 o->verb = FIO_RDMA_MEM_WRITE;
1189 else if (!strncmp("rdma_read", modep, strlen(modep)) ||
1190 !strncmp("RDMA_READ", modep, strlen(modep)))
1191 o->verb = FIO_RDMA_MEM_READ;
1192 else if (!strncmp("send", modep, strlen(modep)) ||
1193 !strncmp("SEND", modep, strlen(modep)))
1194 o->verb = FIO_RDMA_CHA_SEND;
1198 o->verb = FIO_RDMA_MEM_WRITE;
1204 log_err("fio: bad rdma host/port/protocol: %s\n", td->o.filename);
1208 static int fio_rdmaio_init(struct thread_data *td)
1210 struct rdmaio_data *rd = td->io_ops_data;
1211 struct rdmaio_options *o = td->eo;
1212 unsigned int max_bs;
1216 log_err("fio: rdma connections must be read OR write\n");
1219 if (td_random(td)) {
1220 log_err("fio: RDMA network IO can't be random\n");
1224 if (compat_options(td))
1228 log_err("fio: no port has been specified which is required "
1229 "for the rdma engine\n");
1233 if (check_set_rlimits(td))
1236 rd->rdma_protocol = o->verb;
1237 rd->cq_event_num = 0;
1239 rd->cm_channel = rdma_create_event_channel();
1240 if (!rd->cm_channel) {
1241 log_err("fio: rdma_create_event_channel fail: %m\n");
1245 ret = rdma_create_id(rd->cm_channel, &rd->cm_id, rd, RDMA_PS_TCP);
1247 log_err("fio: rdma_create_id fail: %m\n");
1251 if ((rd->rdma_protocol == FIO_RDMA_MEM_WRITE) ||
1252 (rd->rdma_protocol == FIO_RDMA_MEM_READ)) {
1254 malloc(FIO_RDMA_MAX_IO_DEPTH * sizeof(struct remote_u));
1255 memset(rd->rmt_us, 0,
1256 FIO_RDMA_MAX_IO_DEPTH * sizeof(struct remote_u));
1260 rd->io_us_queued = malloc(td->o.iodepth * sizeof(struct io_u *));
1261 memset(rd->io_us_queued, 0, td->o.iodepth * sizeof(struct io_u *));
1262 rd->io_u_queued_nr = 0;
1264 rd->io_us_flight = malloc(td->o.iodepth * sizeof(struct io_u *));
1265 memset(rd->io_us_flight, 0, td->o.iodepth * sizeof(struct io_u *));
1266 rd->io_u_flight_nr = 0;
1268 rd->io_us_completed = malloc(td->o.iodepth * sizeof(struct io_u *));
1269 memset(rd->io_us_completed, 0, td->o.iodepth * sizeof(struct io_u *));
1270 rd->io_u_completed_nr = 0;
1272 if (td_read(td)) { /* READ as the server */
1274 td->flags |= TD_F_NO_PROGRESS;
1275 /* server rd->rdma_buf_len will be setup after got request */
1276 ret = fio_rdmaio_setup_listen(td, o->port);
1277 } else { /* WRITE as the client */
1279 ret = fio_rdmaio_setup_connect(td, td->o.filename, o->port);
1282 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
1283 rd->send_buf.max_bs = htonl(max_bs);
1285 /* register each io_u in the free list */
1286 for (i = 0; i < td->io_u_freelist.nr; i++) {
1287 struct io_u *io_u = td->io_u_freelist.io_us[i];
1289 io_u->engine_data = malloc(sizeof(struct rdma_io_u_data));
1290 memset(io_u->engine_data, 0, sizeof(struct rdma_io_u_data));
1291 ((struct rdma_io_u_data *)io_u->engine_data)->wr_id = i;
1293 io_u->mr = ibv_reg_mr(rd->pd, io_u->buf, max_bs,
1294 IBV_ACCESS_LOCAL_WRITE |
1295 IBV_ACCESS_REMOTE_READ |
1296 IBV_ACCESS_REMOTE_WRITE);
1297 if (io_u->mr == NULL) {
1298 log_err("fio: ibv_reg_mr io_u failed: %m\n");
1302 rd->send_buf.rmt_us[i].buf =
1303 htonll((uint64_t) (unsigned long)io_u->buf);
1304 rd->send_buf.rmt_us[i].rkey = htonl(io_u->mr->rkey);
1305 rd->send_buf.rmt_us[i].size = htonl(max_bs);
1308 log_info("fio: Send rkey %x addr %" PRIx64 " len %d to client\n", io_u->mr->rkey, io_u->buf, max_bs); */
1312 rd->send_buf.nr = htonl(i);
1317 static void fio_rdmaio_cleanup(struct thread_data *td)
1319 struct rdmaio_data *rd = td->io_ops_data;
1325 static int fio_rdmaio_setup(struct thread_data *td)
1327 struct rdmaio_data *rd;
1329 if (!td->files_index) {
1330 add_file(td, td->o.filename ?: "rdma", 0, 0);
1331 td->o.nr_files = td->o.nr_files ?: 1;
1335 if (!td->io_ops_data) {
1336 rd = malloc(sizeof(*rd));
1338 memset(rd, 0, sizeof(*rd));
1339 init_rand_seed(&rd->rand_state, (unsigned int) GOLDEN_RATIO_PRIME, 0);
1340 td->io_ops_data = rd;
1346 static struct ioengine_ops ioengine_rw = {
1348 .version = FIO_IOOPS_VERSION,
1349 .setup = fio_rdmaio_setup,
1350 .init = fio_rdmaio_init,
1351 .prep = fio_rdmaio_prep,
1352 .queue = fio_rdmaio_queue,
1353 .commit = fio_rdmaio_commit,
1354 .getevents = fio_rdmaio_getevents,
1355 .event = fio_rdmaio_event,
1356 .cleanup = fio_rdmaio_cleanup,
1357 .open_file = fio_rdmaio_open_file,
1358 .close_file = fio_rdmaio_close_file,
1359 .flags = FIO_DISKLESSIO | FIO_UNIDIR | FIO_PIPEIO,
1361 .option_struct_size = sizeof(struct rdmaio_options),
1364 static void fio_init fio_rdmaio_register(void)
1366 register_ioengine(&ioengine_rw);
1369 static void fio_exit fio_rdmaio_unregister(void)
1371 unregister_ioengine(&ioengine_rw);