4 * RDMA IO engine using OFED library.
5 * Support both RDMA memory semantic and channel semantic
6 * in InfiniBand, RoCE and iWarp environment.
8 * This is currently disabled. To enable it, execute:
10 * $ export EXTFLAGS="-DFIO_HAVE_RDMA"
11 * $ export EXTLIBS="-libverbs -lrdmacm"
13 * before running make. You'll need the OFED as well:
15 * http://www.openfabrics.org/downloads/OFED/
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
27 #include <sys/types.h>
28 #include <sys/socket.h>
30 #include <sys/resource.h>
40 #include <rdma/rdma_cma.h>
41 #include <infiniband/arch.h>
43 #define FIO_RDMA_MAX_IO_DEPTH 128
59 struct rdma_info_blk {
60 uint32_t mode; /* channel semantic or memory semantic */
61 uint32_t nr; /* client: io depth
62 server: number of records for memory semantic
64 struct remote_u rmt_us[FIO_RDMA_MAX_IO_DEPTH];
67 struct rdma_io_u_data {
69 struct ibv_send_wr sq_wr;
70 struct ibv_recv_wr rq_wr;
71 struct ibv_sge rdma_sgl;
76 enum rdma_io_mode rdma_protocol;
78 struct sockaddr_in addr;
80 struct ibv_recv_wr rq_wr;
81 struct ibv_sge recv_sgl;
82 struct rdma_info_blk recv_buf;
83 struct ibv_mr *recv_mr;
85 struct ibv_send_wr sq_wr;
86 struct ibv_sge send_sgl;
87 struct rdma_info_blk send_buf;
88 struct ibv_mr *send_mr;
90 struct ibv_comp_channel *channel;
96 struct rdma_event_channel *cm_channel;
97 struct rdma_cm_id *cm_id;
98 struct rdma_cm_id *child_cm_id;
102 struct remote_u *rmt_us;
104 struct io_u **io_us_queued;
106 struct io_u **io_us_flight;
108 struct io_u **io_us_completed;
109 int io_u_completed_nr;
112 static int client_recv(struct thread_data *td, struct ibv_wc *wc)
114 struct rdmaio_data *rd = td->io_ops->data;
116 if (wc->byte_len != sizeof(rd->recv_buf)) {
117 log_err("Received bogus data, size %d\n", wc->byte_len);
121 /* store mr info for MEMORY semantic */
122 if ((rd->rdma_protocol == FIO_RDMA_MEM_WRITE) ||
123 (rd->rdma_protocol == FIO_RDMA_MEM_READ)) {
124 /* struct flist_head *entry; */
127 rd->rmt_nr = ntohl(rd->recv_buf.nr);
129 for (i = 0; i < rd->rmt_nr; i++) {
130 rd->rmt_us[i].buf = ntohll(rd->recv_buf.rmt_us[i].buf);
131 rd->rmt_us[i].rkey = ntohl(rd->recv_buf.rmt_us[i].rkey);
132 rd->rmt_us[i].size = ntohl(rd->recv_buf.rmt_us[i].size);
135 "fio: Received rkey %x addr %" PRIx64
136 " len %d from peer\n", rd->rmt_us[i].rkey,
137 rd->rmt_us[i].buf, rd->rmt_us[i].size);
144 static int server_recv(struct thread_data *td, struct ibv_wc *wc)
146 struct rdmaio_data *rd = td->io_ops->data;
148 if (wc->wr_id == FIO_RDMA_MAX_IO_DEPTH) {
149 rd->rdma_protocol = ntohl(rd->recv_buf.mode);
151 /* CHANNEL semantic, do nothing */
152 if (rd->rdma_protocol == FIO_RDMA_CHA_SEND)
153 rd->rdma_protocol = FIO_RDMA_CHA_RECV;
159 static int cq_event_handler(struct thread_data *td, enum ibv_wc_opcode opcode)
161 struct rdmaio_data *rd = td->io_ops->data;
163 struct rdma_io_u_data *r_io_u_d;
168 while ((ret = ibv_poll_cq(rd->cq, 1, &wc)) == 1) {
173 log_err("fio: cq completion status %d(%s)\n",
174 wc.status, ibv_wc_status_str(wc.status));
181 if (rd->is_client == 1)
182 client_recv(td, &wc);
184 server_recv(td, &wc);
186 if (wc.wr_id == FIO_RDMA_MAX_IO_DEPTH)
189 for (i = 0; i < rd->io_u_flight_nr; i++) {
190 r_io_u_d = rd->io_us_flight[i]->engine_data;
192 if (wc.wr_id == r_io_u_d->rq_wr.wr_id) {
193 rd->io_us_flight[i]->resid =
194 rd->io_us_flight[i]->buflen
197 rd->io_us_flight[i]->error = 0;
199 rd->io_us_completed[rd->
201 = rd->io_us_flight[i];
202 rd->io_u_completed_nr++;
206 if (i == rd->io_u_flight_nr)
207 log_err("fio: recv wr %ld not found\n",
210 /* put the last one into middle of the list */
211 rd->io_us_flight[i] =
212 rd->io_us_flight[rd->io_u_flight_nr - 1];
213 rd->io_u_flight_nr--;
219 case IBV_WC_RDMA_WRITE:
220 case IBV_WC_RDMA_READ:
221 if (wc.wr_id == FIO_RDMA_MAX_IO_DEPTH)
224 for (i = 0; i < rd->io_u_flight_nr; i++) {
225 r_io_u_d = rd->io_us_flight[i]->engine_data;
227 if (wc.wr_id == r_io_u_d->sq_wr.wr_id) {
228 rd->io_us_completed[rd->
230 = rd->io_us_flight[i];
231 rd->io_u_completed_nr++;
235 if (i == rd->io_u_flight_nr)
236 log_err("fio: send wr %ld not found\n",
239 /* put the last one into middle of the list */
240 rd->io_us_flight[i] =
241 rd->io_us_flight[rd->io_u_flight_nr - 1];
242 rd->io_u_flight_nr--;
248 log_info("fio: unknown completion event %d\n",
255 log_err("fio: poll error %d\n", ret);
263 * Return -1 for error and 'nr events' for a positive number
266 static int rdma_poll_wait(struct thread_data *td, enum ibv_wc_opcode opcode)
268 struct rdmaio_data *rd = td->io_ops->data;
269 struct ibv_cq *ev_cq;
273 if (rd->cq_event_num > 0) { /* previous left */
279 if (ibv_get_cq_event(rd->channel, &ev_cq, &ev_ctx) != 0) {
280 log_err("fio: Failed to get cq event!\n");
283 if (ev_cq != rd->cq) {
284 log_err("fio: Unknown CQ!\n");
287 if (ibv_req_notify_cq(rd->cq, 0) != 0) {
288 log_err("fio: Failed to set notify!\n");
292 ret = cq_event_handler(td, opcode);
296 ibv_ack_cq_events(rd->cq, ret);
303 static int fio_rdmaio_setup_qp(struct thread_data *td)
305 struct rdmaio_data *rd = td->io_ops->data;
306 struct ibv_qp_init_attr init_attr;
307 int qp_depth = td->o.iodepth * 2; /* 2 times of io depth */
309 if (rd->is_client == 0)
310 rd->pd = ibv_alloc_pd(rd->child_cm_id->verbs);
312 rd->pd = ibv_alloc_pd(rd->cm_id->verbs);
313 if (rd->pd == NULL) {
314 log_err("fio: ibv_alloc_pd fail\n");
318 if (rd->is_client == 0)
319 rd->channel = ibv_create_comp_channel(rd->child_cm_id->verbs);
321 rd->channel = ibv_create_comp_channel(rd->cm_id->verbs);
322 if (rd->channel == NULL) {
323 log_err("fio: ibv_create_comp_channel fail\n");
330 if (rd->is_client == 0)
331 rd->cq = ibv_create_cq(rd->child_cm_id->verbs,
332 qp_depth, rd, rd->channel, 0);
334 rd->cq = ibv_create_cq(rd->cm_id->verbs,
335 qp_depth, rd, rd->channel, 0);
336 if (rd->cq == NULL) {
337 log_err("fio: ibv_create_cq failed\n");
341 if (ibv_req_notify_cq(rd->cq, 0) != 0) {
342 log_err("fio: ibv_create_cq failed\n");
346 /* create queue pair */
347 memset(&init_attr, 0, sizeof(init_attr));
348 init_attr.cap.max_send_wr = qp_depth;
349 init_attr.cap.max_recv_wr = qp_depth;
350 init_attr.cap.max_recv_sge = 1;
351 init_attr.cap.max_send_sge = 1;
352 init_attr.qp_type = IBV_QPT_RC;
353 init_attr.send_cq = rd->cq;
354 init_attr.recv_cq = rd->cq;
356 if (rd->is_client == 0) {
357 if (rdma_create_qp(rd->child_cm_id, rd->pd, &init_attr) != 0) {
358 log_err("fio: rdma_create_qp failed\n");
361 rd->qp = rd->child_cm_id->qp;
363 if (rdma_create_qp(rd->cm_id, rd->pd, &init_attr) != 0) {
364 log_err("fio: rdma_create_qp failed\n");
367 rd->qp = rd->cm_id->qp;
373 ibv_destroy_cq(rd->cq);
375 ibv_destroy_comp_channel(rd->channel);
377 ibv_dealloc_pd(rd->pd);
382 static int fio_rdmaio_setup_control_msg_buffers(struct thread_data *td)
384 struct rdmaio_data *rd = td->io_ops->data;
386 rd->recv_mr = ibv_reg_mr(rd->pd, &rd->recv_buf, sizeof(rd->recv_buf),
387 IBV_ACCESS_LOCAL_WRITE);
388 if (rd->recv_mr == NULL) {
389 log_err("fio: recv_buf reg_mr failed\n");
393 rd->send_mr = ibv_reg_mr(rd->pd, &rd->send_buf, sizeof(rd->send_buf),
395 if (rd->send_mr == NULL) {
396 log_err("fio: send_buf reg_mr failed\n");
397 ibv_dereg_mr(rd->recv_mr);
401 /* setup work request */
403 rd->recv_sgl.addr = (uint64_t) (unsigned long)&rd->recv_buf;
404 rd->recv_sgl.length = sizeof rd->recv_buf;
405 rd->recv_sgl.lkey = rd->recv_mr->lkey;
406 rd->rq_wr.sg_list = &rd->recv_sgl;
407 rd->rq_wr.num_sge = 1;
408 rd->rq_wr.wr_id = FIO_RDMA_MAX_IO_DEPTH;
411 rd->send_sgl.addr = (uint64_t) (unsigned long)&rd->send_buf;
412 rd->send_sgl.length = sizeof rd->send_buf;
413 rd->send_sgl.lkey = rd->send_mr->lkey;
415 rd->sq_wr.opcode = IBV_WR_SEND;
416 rd->sq_wr.send_flags = IBV_SEND_SIGNALED;
417 rd->sq_wr.sg_list = &rd->send_sgl;
418 rd->sq_wr.num_sge = 1;
419 rd->sq_wr.wr_id = FIO_RDMA_MAX_IO_DEPTH;
424 static int get_next_channel_event(struct thread_data *td,
425 struct rdma_event_channel *channel,
426 enum rdma_cm_event_type wait_event)
428 struct rdmaio_data *rd = td->io_ops->data;
431 struct rdma_cm_event *event;
433 ret = rdma_get_cm_event(channel, &event);
435 log_err("fio: rdma_get_cm_event");
439 if (event->event != wait_event) {
440 log_err("fio: event is %s instead of %s\n",
441 rdma_event_str(event->event),
442 rdma_event_str(wait_event));
446 switch (event->event) {
447 case RDMA_CM_EVENT_CONNECT_REQUEST:
448 rd->child_cm_id = event->id;
454 rdma_ack_cm_event(event);
459 static int fio_rdmaio_prep(struct thread_data *td, struct io_u *io_u)
461 struct rdmaio_data *rd = td->io_ops->data;
462 struct rdma_io_u_data *r_io_u_d;
464 r_io_u_d = io_u->engine_data;
466 switch (rd->rdma_protocol) {
467 case FIO_RDMA_MEM_WRITE:
468 case FIO_RDMA_MEM_READ:
469 r_io_u_d->rdma_sgl.addr = (uint64_t) (unsigned long)io_u->buf;
470 r_io_u_d->rdma_sgl.lkey = io_u->mr->lkey;
471 r_io_u_d->sq_wr.wr_id = r_io_u_d->wr_id;
472 r_io_u_d->sq_wr.send_flags = IBV_SEND_SIGNALED;
473 r_io_u_d->sq_wr.sg_list = &r_io_u_d->rdma_sgl;
474 r_io_u_d->sq_wr.num_sge = 1;
476 case FIO_RDMA_CHA_SEND:
477 r_io_u_d->rdma_sgl.addr = (uint64_t) (unsigned long)io_u->buf;
478 r_io_u_d->rdma_sgl.lkey = io_u->mr->lkey;
479 r_io_u_d->rdma_sgl.length = io_u->buflen;
480 r_io_u_d->sq_wr.wr_id = r_io_u_d->wr_id;
481 r_io_u_d->sq_wr.opcode = IBV_WR_SEND;
482 r_io_u_d->sq_wr.send_flags = IBV_SEND_SIGNALED;
483 r_io_u_d->sq_wr.sg_list = &r_io_u_d->rdma_sgl;
484 r_io_u_d->sq_wr.num_sge = 1;
486 case FIO_RDMA_CHA_RECV:
487 r_io_u_d->rdma_sgl.addr = (uint64_t) (unsigned long)io_u->buf;
488 r_io_u_d->rdma_sgl.lkey = io_u->mr->lkey;
489 r_io_u_d->rdma_sgl.length = io_u->buflen;
490 r_io_u_d->rq_wr.wr_id = r_io_u_d->wr_id;
491 r_io_u_d->rq_wr.sg_list = &r_io_u_d->rdma_sgl;
492 r_io_u_d->rq_wr.num_sge = 1;
495 log_err("fio: unknown rdma protocol - %d\n", rd->rdma_protocol);
502 static struct io_u *fio_rdmaio_event(struct thread_data *td, int event)
504 struct rdmaio_data *rd = td->io_ops->data;
508 io_u = rd->io_us_completed[0];
509 for (i = 0; i < rd->io_u_completed_nr - 1; i++) {
510 rd->io_us_completed[i] = rd->io_us_completed[i + 1];
512 rd->io_u_completed_nr--;
514 dprint_io_u(io_u, "fio_rdmaio_event");
519 static int fio_rdmaio_getevents(struct thread_data *td, unsigned int min,
520 unsigned int max, struct timespec *t)
522 struct rdmaio_data *rd = td->io_ops->data;
524 enum ibv_wc_opcode comp_opcode;
525 comp_opcode = IBV_WC_RDMA_WRITE;
526 struct ibv_cq *ev_cq;
532 switch (rd->rdma_protocol) {
533 case FIO_RDMA_MEM_WRITE:
534 comp_opcode = IBV_WC_RDMA_WRITE;
536 case FIO_RDMA_MEM_READ:
537 comp_opcode = IBV_WC_RDMA_READ;
539 case FIO_RDMA_CHA_SEND:
540 comp_opcode = IBV_WC_SEND;
542 case FIO_RDMA_CHA_RECV:
543 comp_opcode = IBV_WC_RECV;
546 log_err("fio: unknown rdma protocol - %d\n", rd->rdma_protocol);
550 if (rd->cq_event_num > 0) { /* previous left */
556 if (ibv_get_cq_event(rd->channel, &ev_cq, &ev_ctx) != 0) {
557 log_err("fio: Failed to get cq event!\n");
560 if (ev_cq != rd->cq) {
561 log_err("fio: Unknown CQ!\n");
564 if (ibv_req_notify_cq(rd->cq, 0) != 0) {
565 log_err("fio: Failed to set notify!\n");
569 ret = cq_event_handler(td, comp_opcode);
573 ibv_ack_cq_events(rd->cq, ret);
579 rd->cq_event_num -= r;
584 static int fio_rdmaio_send(struct thread_data *td, struct io_u **io_us,
587 struct rdmaio_data *rd = td->io_ops->data;
588 struct ibv_send_wr *bad_wr;
589 enum ibv_wc_opcode comp_opcode;
590 comp_opcode = IBV_WC_RDMA_WRITE;
592 struct rdma_io_u_data *r_io_u_d;
596 for (i = 0; i < nr; i++) {
597 /* RDMA_WRITE or RDMA_READ */
598 switch (rd->rdma_protocol) {
599 case FIO_RDMA_MEM_WRITE:
600 /* compose work request */
601 r_io_u_d = io_us[i]->engine_data;
602 index = rand() % rd->rmt_nr;
603 r_io_u_d->sq_wr.opcode = IBV_WR_RDMA_WRITE;
604 r_io_u_d->sq_wr.wr.rdma.rkey = rd->rmt_us[index].rkey;
605 r_io_u_d->sq_wr.wr.rdma.remote_addr = \
606 rd->rmt_us[index].buf;
607 r_io_u_d->sq_wr.sg_list->length = io_us[i]->buflen;
609 case FIO_RDMA_MEM_READ:
610 /* compose work request */
611 r_io_u_d = io_us[i]->engine_data;
612 index = rand() % rd->rmt_nr;
613 r_io_u_d->sq_wr.opcode = IBV_WR_RDMA_READ;
614 r_io_u_d->sq_wr.wr.rdma.rkey = rd->rmt_us[index].rkey;
615 r_io_u_d->sq_wr.wr.rdma.remote_addr = \
616 rd->rmt_us[index].buf;
617 r_io_u_d->sq_wr.sg_list->length = io_us[i]->buflen;
619 case FIO_RDMA_CHA_SEND:
620 r_io_u_d = io_us[i]->engine_data;
621 r_io_u_d->sq_wr.opcode = IBV_WR_SEND;
622 r_io_u_d->sq_wr.send_flags = IBV_SEND_SIGNALED;
625 log_err("fio: unknown rdma protocol - %d\n",
630 if (ibv_post_send(rd->qp, &r_io_u_d->sq_wr, &bad_wr) != 0) {
631 log_err("fio: ibv_post_send fail\n");
635 dprint_io_u(io_us[i], "fio_rdmaio_send");
638 /* wait for completion
639 rdma_poll_wait(td, comp_opcode); */
644 static int fio_rdmaio_recv(struct thread_data *td, struct io_u **io_us,
647 struct rdmaio_data *rd = td->io_ops->data;
648 struct ibv_recv_wr *bad_wr;
649 struct rdma_io_u_data *r_io_u_d;
653 if (rd->rdma_protocol == FIO_RDMA_CHA_RECV) {
654 /* post io_u into recv queue */
655 for (i = 0; i < nr; i++) {
656 r_io_u_d = io_us[i]->engine_data;
657 if (ibv_post_recv(rd->qp, &r_io_u_d->rq_wr, &bad_wr) !=
659 log_err("fio: ibv_post_recv fail\n");
663 } else if ((rd->rdma_protocol == FIO_RDMA_MEM_READ)
664 || (rd->rdma_protocol == FIO_RDMA_MEM_WRITE)) {
665 /* re-post the rq_wr */
666 if (ibv_post_recv(rd->qp, &rd->rq_wr, &bad_wr) != 0) {
667 log_err("fio: ibv_post_recv fail\n");
671 rdma_poll_wait(td, IBV_WC_RECV);
673 dprint(FD_IO, "fio: recv FINISH message\n");
680 static int fio_rdmaio_queue(struct thread_data *td, struct io_u *io_u)
682 struct rdmaio_data *rd = td->io_ops->data;
684 fio_ro_check(td, io_u);
686 if (rd->io_u_queued_nr == (int)td->o.iodepth)
689 rd->io_us_queued[rd->io_u_queued_nr] = io_u;
690 rd->io_u_queued_nr++;
692 dprint_io_u(io_u, "fio_rdmaio_queue");
697 static void fio_rdmaio_queued(struct thread_data *td, struct io_u **io_us,
700 struct rdmaio_data *rd = td->io_ops->data;
704 if (!fio_fill_issue_time(td))
707 fio_gettime(&now, NULL);
709 for (i = 0; i < nr; i++) {
710 struct io_u *io_u = io_us[i];
712 /* queued -> flight */
713 rd->io_us_flight[rd->io_u_flight_nr] = io_u;
714 rd->io_u_flight_nr++;
716 memcpy(&io_u->issue_time, &now, sizeof(now));
717 io_u_queued(td, io_u);
721 static int fio_rdmaio_commit(struct thread_data *td)
723 struct rdmaio_data *rd = td->io_ops->data;
727 if (!rd->io_us_queued)
730 io_us = rd->io_us_queued;
732 /* RDMA_WRITE or RDMA_READ */
734 ret = fio_rdmaio_send(td, io_us, rd->io_u_queued_nr);
735 } else if (!rd->is_client) {
736 ret = fio_rdmaio_recv(td, io_us, rd->io_u_queued_nr);
738 ret = 0; /* must be a SYNC */
741 fio_rdmaio_queued(td, io_us, ret);
742 io_u_mark_submit(td, ret);
743 rd->io_u_queued_nr -= ret;
748 } while (rd->io_u_queued_nr);
753 static int fio_rdmaio_connect(struct thread_data *td, struct fio_file *f)
755 struct rdmaio_data *rd = td->io_ops->data;
756 struct rdma_conn_param conn_param;
757 struct ibv_send_wr *bad_wr;
759 memset(&conn_param, 0, sizeof conn_param);
760 conn_param.responder_resources = 1;
761 conn_param.initiator_depth = 1;
762 conn_param.retry_count = 10;
764 if (rdma_connect(rd->cm_id, &conn_param) != 0) {
765 log_err("fio: rdma_connect fail\n");
769 if (get_next_channel_event
770 (td, rd->cm_channel, RDMA_CM_EVENT_ESTABLISHED) != 0) {
771 log_err("fio: wait for RDMA_CM_EVENT_ESTABLISHED\n");
775 /* send task request */
776 rd->send_buf.mode = htonl(rd->rdma_protocol);
777 rd->send_buf.nr = htonl(td->o.iodepth);
779 if (ibv_post_send(rd->qp, &rd->sq_wr, &bad_wr) != 0) {
780 log_err("fio: ibv_post_send fail");
784 rdma_poll_wait(td, IBV_WC_SEND);
786 /* wait for remote MR info from server side */
787 rdma_poll_wait(td, IBV_WC_RECV);
792 static int fio_rdmaio_accept(struct thread_data *td, struct fio_file *f)
794 struct rdmaio_data *rd = td->io_ops->data;
795 struct rdma_conn_param conn_param;
796 struct ibv_send_wr *bad_wr;
798 /* rdma_accept() - then wait for accept success */
799 memset(&conn_param, 0, sizeof conn_param);
800 conn_param.responder_resources = 1;
801 conn_param.initiator_depth = 1;
803 if (rdma_accept(rd->child_cm_id, &conn_param) != 0) {
804 log_err("fio: rdma_accept\n");
808 if (get_next_channel_event
809 (td, rd->cm_channel, RDMA_CM_EVENT_ESTABLISHED) != 0) {
810 log_err("fio: wait for RDMA_CM_EVENT_ESTABLISHED\n");
814 /* wait for request */
815 rdma_poll_wait(td, IBV_WC_RECV);
817 if (ibv_post_send(rd->qp, &rd->sq_wr, &bad_wr) != 0) {
818 log_err("fio: ibv_post_send fail");
822 rdma_poll_wait(td, IBV_WC_SEND);
827 static int fio_rdmaio_open_file(struct thread_data *td, struct fio_file *f)
830 return fio_rdmaio_accept(td, f);
832 return fio_rdmaio_connect(td, f);
835 static int fio_rdmaio_close_file(struct thread_data *td, struct fio_file *f)
837 struct rdmaio_data *rd = td->io_ops->data;
838 struct ibv_send_wr *bad_wr;
840 /* unregister rdma buffer */
843 * Client sends notification to the server side
845 /* refer to: http://linux.die.net/man/7/rdma_cm */
846 if ((rd->is_client == 1) && ((rd->rdma_protocol == FIO_RDMA_MEM_WRITE)
847 || (rd->rdma_protocol ==
848 FIO_RDMA_MEM_READ))) {
849 if (ibv_post_send(rd->qp, &rd->sq_wr, &bad_wr) != 0) {
850 log_err("fio: ibv_post_send fail");
854 dprint(FD_IO, "fio: close infomation sent success\n");
855 rdma_poll_wait(td, IBV_WC_SEND);
858 if (rd->is_client == 1)
859 rdma_disconnect(rd->cm_id);
861 rdma_disconnect(rd->child_cm_id);
862 /* rdma_disconnect(rd->cm_id); */
865 /* if (get_next_channel_event(td, rd->cm_channel, RDMA_CM_EVENT_DISCONNECTED) != 0)
867 log_err("fio: wait for RDMA_CM_EVENT_DISCONNECTED\n");
871 ibv_destroy_qp(rd->qp);
872 ibv_destroy_cq(rd->cq);
874 if (rd->is_client == 1)
875 rdma_destroy_id(rd->cm_id);
877 rdma_destroy_id(rd->child_cm_id);
878 rdma_destroy_id(rd->cm_id);
881 ibv_destroy_comp_channel(rd->channel);
882 ibv_dealloc_pd(rd->pd);
887 static int fio_rdmaio_setup_connect(struct thread_data *td, const char *host,
890 struct rdmaio_data *rd = td->io_ops->data;
891 struct ibv_recv_wr *bad_wr;
893 rd->addr.sin_family = AF_INET;
894 rd->addr.sin_port = htons(port);
896 if (inet_aton(host, &rd->addr.sin_addr) != 1) {
897 struct hostent *hent;
899 hent = gethostbyname(host);
901 td_verror(td, errno, "gethostbyname");
905 memcpy(&rd->addr.sin_addr, hent->h_addr, 4);
909 if (rdma_resolve_addr(rd->cm_id, NULL,
910 (struct sockaddr *)&rd->addr, 2000) != 0) {
911 log_err("fio: rdma_resolve_addr");
915 if (get_next_channel_event
916 (td, rd->cm_channel, RDMA_CM_EVENT_ADDR_RESOLVED)
918 log_err("fio: get_next_channel_event");
923 if (rdma_resolve_route(rd->cm_id, 2000) != 0) {
924 log_err("fio: rdma_resolve_route");
928 if (get_next_channel_event
929 (td, rd->cm_channel, RDMA_CM_EVENT_ROUTE_RESOLVED) != 0) {
930 log_err("fio: get_next_channel_event");
934 /* create qp and buffer */
935 if (fio_rdmaio_setup_qp(td) != 0)
938 if (fio_rdmaio_setup_control_msg_buffers(td) != 0)
942 if (ibv_post_recv(rd->qp, &rd->rq_wr, &bad_wr) != 0) {
943 log_err("fio: ibv_post_recv fail\n");
950 static int fio_rdmaio_setup_listen(struct thread_data *td, short port)
952 struct rdmaio_data *rd = td->io_ops->data;
953 struct ibv_recv_wr *bad_wr;
955 rd->addr.sin_family = AF_INET;
956 rd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
957 rd->addr.sin_port = htons(port);
960 if (rdma_bind_addr(rd->cm_id, (struct sockaddr *)&rd->addr) != 0) {
961 log_err("fio: rdma_bind_addr fail\n");
965 if (rdma_listen(rd->cm_id, 3) != 0) {
966 log_err("fio: rdma_listen fail\n");
970 /* wait for CONNECT_REQUEST */
971 if (get_next_channel_event
972 (td, rd->cm_channel, RDMA_CM_EVENT_CONNECT_REQUEST) != 0) {
973 log_err("fio: wait for RDMA_CM_EVENT_CONNECT_REQUEST\n");
977 if (fio_rdmaio_setup_qp(td) != 0)
980 if (fio_rdmaio_setup_control_msg_buffers(td) != 0)
984 if (ibv_post_recv(rd->qp, &rd->rq_wr, &bad_wr) != 0) {
985 log_err("fio: ibv_post_recv fail\n");
992 static int fio_rdmaio_init(struct thread_data *td)
994 struct rdmaio_data *rd = td->io_ops->data;
996 char host[64], buf[128];
997 char *sep, *portp, *modep;
1002 log_err("fio: rdma connections must be read OR write\n");
1005 if (td_random(td)) {
1006 log_err("fio: RDMA network IO can't be random\n");
1010 /* check RLIMIT_MEMLOCK */
1011 if (getrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
1012 log_err("fio: getrlimit fail: %d(%s)\n",
1013 errno, strerror(errno));
1018 if ((rl.rlim_cur != RLIM_INFINITY)
1019 && (rl.rlim_cur < td->orig_buffer_size)) {
1020 log_err("fio: soft RLIMIT_MEMLOCK is: %ld\n", rl.rlim_cur);
1021 log_err("fio: total block size is: %ld\n",
1022 td->orig_buffer_size);
1023 /* try to set larger RLIMIT_MEMLOCK */
1024 rl.rlim_cur = rl.rlim_max;
1025 if (setrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
1026 log_err("fio: setrlimit fail: %d(%s)\n",
1027 errno, strerror(errno));
1028 log_err("fio: you may try enlarge MEMLOCK by root\n");
1029 log_err("# ulimit -l unlimited\n");
1034 strcpy(buf, td->o.filename);
1036 sep = strchr(buf, '/');
1048 sep = strchr(portp, '/');
1054 port = strtol(portp, NULL, 10);
1055 if (!port || port > 65535)
1059 if (!strncmp("rdma_write", modep, strlen(modep)) ||
1060 !strncmp("RDMA_WRITE", modep, strlen(modep)))
1061 rd->rdma_protocol = FIO_RDMA_MEM_WRITE;
1062 else if (!strncmp("rdma_read", modep, strlen(modep)) ||
1063 !strncmp("RDMA_READ", modep, strlen(modep)))
1064 rd->rdma_protocol = FIO_RDMA_MEM_READ;
1065 else if (!strncmp("send", modep, strlen(modep)) ||
1066 !strncmp("SEND", modep, strlen(modep)))
1067 rd->rdma_protocol = FIO_RDMA_CHA_SEND;
1071 rd->rdma_protocol = FIO_RDMA_MEM_WRITE;
1073 rd->cq_event_num = 0;
1075 rd->cm_channel = rdma_create_event_channel();
1076 if (!rd->cm_channel) {
1077 log_err("fio: rdma_create_event_channel fail\n");
1081 ret = rdma_create_id(rd->cm_channel, &rd->cm_id, rd, RDMA_PS_TCP);
1083 log_err("fio: rdma_create_id fail\n");
1087 if ((rd->rdma_protocol == FIO_RDMA_MEM_WRITE) ||
1088 (rd->rdma_protocol == FIO_RDMA_MEM_READ)) {
1090 malloc(FIO_RDMA_MAX_IO_DEPTH * sizeof(struct remote_u));
1091 memset(rd->rmt_us, 0,
1092 FIO_RDMA_MAX_IO_DEPTH * sizeof(struct remote_u));
1096 rd->io_us_queued = malloc(td->o.iodepth * sizeof(struct io_u *));
1097 memset(rd->io_us_queued, 0, td->o.iodepth * sizeof(struct io_u *));
1098 rd->io_u_queued_nr = 0;
1100 rd->io_us_flight = malloc(td->o.iodepth * sizeof(struct io_u *));
1101 memset(rd->io_us_flight, 0, td->o.iodepth * sizeof(struct io_u *));
1102 rd->io_u_flight_nr = 0;
1104 rd->io_us_completed = malloc(td->o.iodepth * sizeof(struct io_u *));
1105 memset(rd->io_us_completed, 0, td->o.iodepth * sizeof(struct io_u *));
1106 rd->io_u_completed_nr = 0;
1108 if (td_read(td)) { /* READ as the server */
1110 /* server rd->rdma_buf_len will be setup after got request */
1111 ret = fio_rdmaio_setup_listen(td, port);
1112 } else { /* WRITE as the client */
1114 ret = fio_rdmaio_setup_connect(td, host, port);
1117 struct flist_head *entry;
1118 unsigned int max_bs;
1119 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
1120 /* register each io_u in the free list */
1122 flist_for_each(entry, &td->io_u_freelist) {
1123 struct io_u *io_u = flist_entry(entry, struct io_u, list);
1125 io_u->engine_data = malloc(sizeof(struct rdma_io_u_data));
1126 memset(io_u->engine_data, 0, sizeof(struct rdma_io_u_data));
1127 ((struct rdma_io_u_data *)io_u->engine_data)->wr_id = i;
1129 io_u->mr = ibv_reg_mr(rd->pd, io_u->buf, max_bs,
1130 IBV_ACCESS_LOCAL_WRITE |
1131 IBV_ACCESS_REMOTE_READ |
1132 IBV_ACCESS_REMOTE_WRITE);
1133 if (io_u->mr == NULL) {
1134 log_err("fio: ibv_reg_mr io_u failed\n");
1138 rd->send_buf.rmt_us[i].buf =
1139 htonll((uint64_t) (unsigned long)io_u->buf);
1140 rd->send_buf.rmt_us[i].rkey = htonl(io_u->mr->rkey);
1141 rd->send_buf.rmt_us[i].size = htonl(max_bs);
1143 /* log_info("fio: Send rkey %x addr %" PRIx64 " len %d to client\n",
1144 io_u->mr->rkey, io_u->buf, max_bs); */
1148 rd->send_buf.nr = htonl(i);
1152 log_err("fio: bad rdma host/port/protocol: %s\n", td->o.filename);
1156 static void fio_rdmaio_cleanup(struct thread_data *td)
1158 struct rdmaio_data *rd = td->io_ops->data;
1161 /* if (nd->listenfd != -1)
1162 close(nd->listenfd);
1163 if (nd->pipes[0] != -1)
1164 close(nd->pipes[0]);
1165 if (nd->pipes[1] != -1)
1166 close(nd->pipes[1]);
1172 static int fio_rdmaio_setup(struct thread_data *td)
1174 struct rdmaio_data *rd;
1176 if (!td->io_ops->data) {
1177 rd = malloc(sizeof(*rd));;
1179 memset(rd, 0, sizeof(*rd));
1180 td->io_ops->data = rd;
1186 static struct ioengine_ops ioengine_rw = {
1188 .version = FIO_IOOPS_VERSION,
1189 .setup = fio_rdmaio_setup,
1190 .init = fio_rdmaio_init,
1191 .prep = fio_rdmaio_prep,
1192 .queue = fio_rdmaio_queue,
1193 .commit = fio_rdmaio_commit,
1194 .getevents = fio_rdmaio_getevents,
1195 .event = fio_rdmaio_event,
1196 .cleanup = fio_rdmaio_cleanup,
1197 .open_file = fio_rdmaio_open_file,
1198 .close_file = fio_rdmaio_close_file,
1199 .flags = FIO_DISKLESSIO | FIO_UNIDIR | FIO_PIPEIO,
1202 #else /* FIO_HAVE_RDMA */
1204 static int fio_rdmaio_open_file(struct thread_data *td, struct fio_file *f)
1209 static int fio_rdmaio_close_file(struct thread_data *td, struct fio_file *f)
1214 static int fio_rdmaio_queue(struct thread_data *td, struct io_u *io_u)
1216 return FIO_Q_COMPLETED;
1219 static int fio_rdmaio_init(struct thread_data fio_unused * td)
1221 log_err("fio: rdma(librdmacm libibverbs) not available\n");
1222 log_err(" You haven't compiled rdma ioengine into fio.\n");
1223 log_err(" If you want to try rdma ioengine,\n");
1224 log_err(" make sure OFED is installed,\n");
1225 log_err(" $ ofed_info\n");
1226 log_err(" then try to make fio as follows:\n");
1227 log_err(" $ export EXTFLAGS=\"-DFIO_HAVE_RDMA\"\n");
1228 log_err(" $ export EXTLIBS=\"-libverbs -lrdmacm\"\n");
1229 log_err(" $ make clean && make\n");
1233 static struct ioengine_ops ioengine_rw = {
1235 .version = FIO_IOOPS_VERSION,
1236 .init = fio_rdmaio_init,
1237 .queue = fio_rdmaio_queue,
1238 .open_file = fio_rdmaio_open_file,
1239 .close_file = fio_rdmaio_close_file,
1240 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR | FIO_PIPEIO,
1245 static void fio_init fio_rdmaio_register(void)
1247 register_ioengine(&ioengine_rw);
1250 static void fio_exit fio_rdmaio_unregister(void)
1252 unregister_ioengine(&ioengine_rw);