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 * This I/O engine is disabled by default. To enable it, execute:
10 * $ export EXTFLAGS+=" -DFIO_HAVE_RDMA "
11 * $ export EXTLIBS+=" -libverbs -lrdmacm "
13 * before running make. You will need the Linux RDMA software as well, either
14 * from your Linux distributor or directly from openfabrics.org:
16 * http://www.openfabrics.org/downloads/OFED/
18 * Exchanging steps of RDMA ioengine control messages:
19 * 1. client side sends test mode (RDMA_WRITE/RDMA_READ/SEND)
21 * 2. server side parses test mode, and sends back confirmation
22 * to client side. In RDMA WRITE/READ test, this confirmation
23 * includes memory information, such as rkey, address.
24 * 3. client side initiates test loop.
25 * 4. In RDMA WRITE/READ test, client side sends a completion
26 * notification to server side. Server side updates its
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
39 #include <sys/types.h>
40 #include <sys/socket.h>
42 #include <sys/resource.h>
52 #include <rdma/rdma_cma.h>
53 #include <infiniband/arch.h>
55 #define FIO_RDMA_MAX_IO_DEPTH 512
71 struct rdma_info_blk {
72 uint32_t mode; /* channel semantic or memory semantic */
73 uint32_t nr; /* client: io depth
74 server: number of records for memory semantic
76 struct remote_u rmt_us[FIO_RDMA_MAX_IO_DEPTH];
79 struct rdma_io_u_data {
81 struct ibv_send_wr sq_wr;
82 struct ibv_recv_wr rq_wr;
83 struct ibv_sge rdma_sgl;
88 enum rdma_io_mode rdma_protocol;
90 struct sockaddr_in addr;
92 struct ibv_recv_wr rq_wr;
93 struct ibv_sge recv_sgl;
94 struct rdma_info_blk recv_buf;
95 struct ibv_mr *recv_mr;
97 struct ibv_send_wr sq_wr;
98 struct ibv_sge send_sgl;
99 struct rdma_info_blk send_buf;
100 struct ibv_mr *send_mr;
102 struct ibv_comp_channel *channel;
108 struct rdma_event_channel *cm_channel;
109 struct rdma_cm_id *cm_id;
110 struct rdma_cm_id *child_cm_id;
114 struct remote_u *rmt_us;
116 struct io_u **io_us_queued;
118 struct io_u **io_us_flight;
120 struct io_u **io_us_completed;
121 int io_u_completed_nr;
124 static int client_recv(struct thread_data *td, struct ibv_wc *wc)
126 struct rdmaio_data *rd = td->io_ops->data;
128 if (wc->byte_len != sizeof(rd->recv_buf)) {
129 log_err("Received bogus data, size %d\n", wc->byte_len);
133 /* store mr info for MEMORY semantic */
134 if ((rd->rdma_protocol == FIO_RDMA_MEM_WRITE) ||
135 (rd->rdma_protocol == FIO_RDMA_MEM_READ)) {
136 /* struct flist_head *entry; */
139 rd->rmt_nr = ntohl(rd->recv_buf.nr);
141 for (i = 0; i < rd->rmt_nr; i++) {
142 rd->rmt_us[i].buf = ntohll(rd->recv_buf.rmt_us[i].buf);
143 rd->rmt_us[i].rkey = ntohl(rd->recv_buf.rmt_us[i].rkey);
144 rd->rmt_us[i].size = ntohl(rd->recv_buf.rmt_us[i].size);
147 "fio: Received rkey %x addr %" PRIx64
148 " len %d from peer\n", rd->rmt_us[i].rkey,
149 rd->rmt_us[i].buf, rd->rmt_us[i].size);
156 static int server_recv(struct thread_data *td, struct ibv_wc *wc)
158 struct rdmaio_data *rd = td->io_ops->data;
160 if (wc->wr_id == FIO_RDMA_MAX_IO_DEPTH) {
161 rd->rdma_protocol = ntohl(rd->recv_buf.mode);
163 /* CHANNEL semantic, do nothing */
164 if (rd->rdma_protocol == FIO_RDMA_CHA_SEND)
165 rd->rdma_protocol = FIO_RDMA_CHA_RECV;
171 static int cq_event_handler(struct thread_data *td, enum ibv_wc_opcode opcode)
173 struct rdmaio_data *rd = td->io_ops->data;
175 struct rdma_io_u_data *r_io_u_d;
180 while ((ret = ibv_poll_cq(rd->cq, 1, &wc)) == 1) {
185 log_err("fio: cq completion status %d(%s)\n",
186 wc.status, ibv_wc_status_str(wc.status));
193 if (rd->is_client == 1)
194 client_recv(td, &wc);
196 server_recv(td, &wc);
198 if (wc.wr_id == FIO_RDMA_MAX_IO_DEPTH)
201 for (i = 0; i < rd->io_u_flight_nr; i++) {
202 r_io_u_d = rd->io_us_flight[i]->engine_data;
204 if (wc.wr_id == r_io_u_d->rq_wr.wr_id) {
205 rd->io_us_flight[i]->resid =
206 rd->io_us_flight[i]->buflen
209 rd->io_us_flight[i]->error = 0;
211 rd->io_us_completed[rd->
213 = rd->io_us_flight[i];
214 rd->io_u_completed_nr++;
218 if (i == rd->io_u_flight_nr)
219 log_err("fio: recv wr %" PRId64 " not found\n",
222 /* put the last one into middle of the list */
223 rd->io_us_flight[i] =
224 rd->io_us_flight[rd->io_u_flight_nr - 1];
225 rd->io_u_flight_nr--;
231 case IBV_WC_RDMA_WRITE:
232 case IBV_WC_RDMA_READ:
233 if (wc.wr_id == FIO_RDMA_MAX_IO_DEPTH)
236 for (i = 0; i < rd->io_u_flight_nr; i++) {
237 r_io_u_d = rd->io_us_flight[i]->engine_data;
239 if (wc.wr_id == r_io_u_d->sq_wr.wr_id) {
240 rd->io_us_completed[rd->
242 = rd->io_us_flight[i];
243 rd->io_u_completed_nr++;
247 if (i == rd->io_u_flight_nr)
248 log_err("fio: send wr %" PRId64 " not found\n",
251 /* put the last one into middle of the list */
252 rd->io_us_flight[i] =
253 rd->io_us_flight[rd->io_u_flight_nr - 1];
254 rd->io_u_flight_nr--;
260 log_info("fio: unknown completion event %d\n",
267 log_err("fio: poll error %d\n", ret);
275 * Return -1 for error and 'nr events' for a positive number
278 static int rdma_poll_wait(struct thread_data *td, enum ibv_wc_opcode opcode)
280 struct rdmaio_data *rd = td->io_ops->data;
281 struct ibv_cq *ev_cq;
285 if (rd->cq_event_num > 0) { /* previous left */
291 if (ibv_get_cq_event(rd->channel, &ev_cq, &ev_ctx) != 0) {
292 log_err("fio: Failed to get cq event!\n");
295 if (ev_cq != rd->cq) {
296 log_err("fio: Unknown CQ!\n");
299 if (ibv_req_notify_cq(rd->cq, 0) != 0) {
300 log_err("fio: Failed to set notify!\n");
304 ret = cq_event_handler(td, opcode);
308 ibv_ack_cq_events(rd->cq, ret);
315 static int fio_rdmaio_setup_qp(struct thread_data *td)
317 struct rdmaio_data *rd = td->io_ops->data;
318 struct ibv_qp_init_attr init_attr;
319 int qp_depth = td->o.iodepth * 2; /* 2 times of io depth */
321 if (rd->is_client == 0)
322 rd->pd = ibv_alloc_pd(rd->child_cm_id->verbs);
324 rd->pd = ibv_alloc_pd(rd->cm_id->verbs);
325 if (rd->pd == NULL) {
326 log_err("fio: ibv_alloc_pd fail\n");
330 if (rd->is_client == 0)
331 rd->channel = ibv_create_comp_channel(rd->child_cm_id->verbs);
333 rd->channel = ibv_create_comp_channel(rd->cm_id->verbs);
334 if (rd->channel == NULL) {
335 log_err("fio: ibv_create_comp_channel fail\n");
342 if (rd->is_client == 0)
343 rd->cq = ibv_create_cq(rd->child_cm_id->verbs,
344 qp_depth, rd, rd->channel, 0);
346 rd->cq = ibv_create_cq(rd->cm_id->verbs,
347 qp_depth, rd, rd->channel, 0);
348 if (rd->cq == NULL) {
349 log_err("fio: ibv_create_cq failed\n");
353 if (ibv_req_notify_cq(rd->cq, 0) != 0) {
354 log_err("fio: ibv_create_cq failed\n");
358 /* create queue pair */
359 memset(&init_attr, 0, sizeof(init_attr));
360 init_attr.cap.max_send_wr = qp_depth;
361 init_attr.cap.max_recv_wr = qp_depth;
362 init_attr.cap.max_recv_sge = 1;
363 init_attr.cap.max_send_sge = 1;
364 init_attr.qp_type = IBV_QPT_RC;
365 init_attr.send_cq = rd->cq;
366 init_attr.recv_cq = rd->cq;
368 if (rd->is_client == 0) {
369 if (rdma_create_qp(rd->child_cm_id, rd->pd, &init_attr) != 0) {
370 log_err("fio: rdma_create_qp failed\n");
373 rd->qp = rd->child_cm_id->qp;
375 if (rdma_create_qp(rd->cm_id, rd->pd, &init_attr) != 0) {
376 log_err("fio: rdma_create_qp failed\n");
379 rd->qp = rd->cm_id->qp;
385 ibv_destroy_cq(rd->cq);
387 ibv_destroy_comp_channel(rd->channel);
389 ibv_dealloc_pd(rd->pd);
394 static int fio_rdmaio_setup_control_msg_buffers(struct thread_data *td)
396 struct rdmaio_data *rd = td->io_ops->data;
398 rd->recv_mr = ibv_reg_mr(rd->pd, &rd->recv_buf, sizeof(rd->recv_buf),
399 IBV_ACCESS_LOCAL_WRITE);
400 if (rd->recv_mr == NULL) {
401 log_err("fio: recv_buf reg_mr failed\n");
405 rd->send_mr = ibv_reg_mr(rd->pd, &rd->send_buf, sizeof(rd->send_buf),
407 if (rd->send_mr == NULL) {
408 log_err("fio: send_buf reg_mr failed\n");
409 ibv_dereg_mr(rd->recv_mr);
413 /* setup work request */
415 rd->recv_sgl.addr = (uint64_t) (unsigned long)&rd->recv_buf;
416 rd->recv_sgl.length = sizeof rd->recv_buf;
417 rd->recv_sgl.lkey = rd->recv_mr->lkey;
418 rd->rq_wr.sg_list = &rd->recv_sgl;
419 rd->rq_wr.num_sge = 1;
420 rd->rq_wr.wr_id = FIO_RDMA_MAX_IO_DEPTH;
423 rd->send_sgl.addr = (uint64_t) (unsigned long)&rd->send_buf;
424 rd->send_sgl.length = sizeof rd->send_buf;
425 rd->send_sgl.lkey = rd->send_mr->lkey;
427 rd->sq_wr.opcode = IBV_WR_SEND;
428 rd->sq_wr.send_flags = IBV_SEND_SIGNALED;
429 rd->sq_wr.sg_list = &rd->send_sgl;
430 rd->sq_wr.num_sge = 1;
431 rd->sq_wr.wr_id = FIO_RDMA_MAX_IO_DEPTH;
436 static int get_next_channel_event(struct thread_data *td,
437 struct rdma_event_channel *channel,
438 enum rdma_cm_event_type wait_event)
440 struct rdmaio_data *rd = td->io_ops->data;
443 struct rdma_cm_event *event;
445 ret = rdma_get_cm_event(channel, &event);
447 log_err("fio: rdma_get_cm_event");
451 if (event->event != wait_event) {
452 log_err("fio: event is %s instead of %s\n",
453 rdma_event_str(event->event),
454 rdma_event_str(wait_event));
458 switch (event->event) {
459 case RDMA_CM_EVENT_CONNECT_REQUEST:
460 rd->child_cm_id = event->id;
466 rdma_ack_cm_event(event);
471 static int fio_rdmaio_prep(struct thread_data *td, struct io_u *io_u)
473 struct rdmaio_data *rd = td->io_ops->data;
474 struct rdma_io_u_data *r_io_u_d;
476 r_io_u_d = io_u->engine_data;
478 switch (rd->rdma_protocol) {
479 case FIO_RDMA_MEM_WRITE:
480 case FIO_RDMA_MEM_READ:
481 r_io_u_d->rdma_sgl.addr = (uint64_t) (unsigned long)io_u->buf;
482 r_io_u_d->rdma_sgl.lkey = io_u->mr->lkey;
483 r_io_u_d->sq_wr.wr_id = r_io_u_d->wr_id;
484 r_io_u_d->sq_wr.send_flags = IBV_SEND_SIGNALED;
485 r_io_u_d->sq_wr.sg_list = &r_io_u_d->rdma_sgl;
486 r_io_u_d->sq_wr.num_sge = 1;
488 case FIO_RDMA_CHA_SEND:
489 r_io_u_d->rdma_sgl.addr = (uint64_t) (unsigned long)io_u->buf;
490 r_io_u_d->rdma_sgl.lkey = io_u->mr->lkey;
491 r_io_u_d->rdma_sgl.length = io_u->buflen;
492 r_io_u_d->sq_wr.wr_id = r_io_u_d->wr_id;
493 r_io_u_d->sq_wr.opcode = IBV_WR_SEND;
494 r_io_u_d->sq_wr.send_flags = IBV_SEND_SIGNALED;
495 r_io_u_d->sq_wr.sg_list = &r_io_u_d->rdma_sgl;
496 r_io_u_d->sq_wr.num_sge = 1;
498 case FIO_RDMA_CHA_RECV:
499 r_io_u_d->rdma_sgl.addr = (uint64_t) (unsigned long)io_u->buf;
500 r_io_u_d->rdma_sgl.lkey = io_u->mr->lkey;
501 r_io_u_d->rdma_sgl.length = io_u->buflen;
502 r_io_u_d->rq_wr.wr_id = r_io_u_d->wr_id;
503 r_io_u_d->rq_wr.sg_list = &r_io_u_d->rdma_sgl;
504 r_io_u_d->rq_wr.num_sge = 1;
507 log_err("fio: unknown rdma protocol - %d\n", rd->rdma_protocol);
514 static struct io_u *fio_rdmaio_event(struct thread_data *td, int event)
516 struct rdmaio_data *rd = td->io_ops->data;
520 io_u = rd->io_us_completed[0];
521 for (i = 0; i < rd->io_u_completed_nr - 1; i++) {
522 rd->io_us_completed[i] = rd->io_us_completed[i + 1];
524 rd->io_u_completed_nr--;
526 dprint_io_u(io_u, "fio_rdmaio_event");
531 static int fio_rdmaio_getevents(struct thread_data *td, unsigned int min,
532 unsigned int max, struct timespec *t)
534 struct rdmaio_data *rd = td->io_ops->data;
536 enum ibv_wc_opcode comp_opcode;
537 comp_opcode = IBV_WC_RDMA_WRITE;
538 struct ibv_cq *ev_cq;
544 switch (rd->rdma_protocol) {
545 case FIO_RDMA_MEM_WRITE:
546 comp_opcode = IBV_WC_RDMA_WRITE;
548 case FIO_RDMA_MEM_READ:
549 comp_opcode = IBV_WC_RDMA_READ;
551 case FIO_RDMA_CHA_SEND:
552 comp_opcode = IBV_WC_SEND;
554 case FIO_RDMA_CHA_RECV:
555 comp_opcode = IBV_WC_RECV;
558 log_err("fio: unknown rdma protocol - %d\n", rd->rdma_protocol);
562 if (rd->cq_event_num > 0) { /* previous left */
568 if (ibv_get_cq_event(rd->channel, &ev_cq, &ev_ctx) != 0) {
569 log_err("fio: Failed to get cq event!\n");
572 if (ev_cq != rd->cq) {
573 log_err("fio: Unknown CQ!\n");
576 if (ibv_req_notify_cq(rd->cq, 0) != 0) {
577 log_err("fio: Failed to set notify!\n");
581 ret = cq_event_handler(td, comp_opcode);
585 ibv_ack_cq_events(rd->cq, ret);
591 rd->cq_event_num -= r;
596 static int fio_rdmaio_send(struct thread_data *td, struct io_u **io_us,
599 struct rdmaio_data *rd = td->io_ops->data;
600 struct ibv_send_wr *bad_wr;
602 enum ibv_wc_opcode comp_opcode;
603 comp_opcode = IBV_WC_RDMA_WRITE;
607 struct rdma_io_u_data *r_io_u_d;
611 for (i = 0; i < nr; i++) {
612 /* RDMA_WRITE or RDMA_READ */
613 switch (rd->rdma_protocol) {
614 case FIO_RDMA_MEM_WRITE:
615 /* compose work request */
616 r_io_u_d = io_us[i]->engine_data;
617 if (td->o.use_os_rand)
618 index = os_random_long(&td->random_state) % rd->rmt_nr;
620 index = __rand(&td->__random_state) % rd->rmt_nr;
621 r_io_u_d->sq_wr.opcode = IBV_WR_RDMA_WRITE;
622 r_io_u_d->sq_wr.wr.rdma.rkey = rd->rmt_us[index].rkey;
623 r_io_u_d->sq_wr.wr.rdma.remote_addr = \
624 rd->rmt_us[index].buf;
625 r_io_u_d->sq_wr.sg_list->length = io_us[i]->buflen;
627 case FIO_RDMA_MEM_READ:
628 /* compose work request */
629 r_io_u_d = io_us[i]->engine_data;
630 if (td->o.use_os_rand)
631 index = os_random_long(&td->random_state) % rd->rmt_nr;
633 index = __rand(&td->__random_state) % rd->rmt_nr;
634 r_io_u_d->sq_wr.opcode = IBV_WR_RDMA_READ;
635 r_io_u_d->sq_wr.wr.rdma.rkey = rd->rmt_us[index].rkey;
636 r_io_u_d->sq_wr.wr.rdma.remote_addr = \
637 rd->rmt_us[index].buf;
638 r_io_u_d->sq_wr.sg_list->length = io_us[i]->buflen;
640 case FIO_RDMA_CHA_SEND:
641 r_io_u_d = io_us[i]->engine_data;
642 r_io_u_d->sq_wr.opcode = IBV_WR_SEND;
643 r_io_u_d->sq_wr.send_flags = IBV_SEND_SIGNALED;
646 log_err("fio: unknown rdma protocol - %d\n",
651 if (ibv_post_send(rd->qp, &r_io_u_d->sq_wr, &bad_wr) != 0) {
652 log_err("fio: ibv_post_send fail\n");
656 dprint_io_u(io_us[i], "fio_rdmaio_send");
659 /* wait for completion
660 rdma_poll_wait(td, comp_opcode); */
665 static int fio_rdmaio_recv(struct thread_data *td, struct io_u **io_us,
668 struct rdmaio_data *rd = td->io_ops->data;
669 struct ibv_recv_wr *bad_wr;
670 struct rdma_io_u_data *r_io_u_d;
674 if (rd->rdma_protocol == FIO_RDMA_CHA_RECV) {
675 /* post io_u into recv queue */
676 for (i = 0; i < nr; i++) {
677 r_io_u_d = io_us[i]->engine_data;
678 if (ibv_post_recv(rd->qp, &r_io_u_d->rq_wr, &bad_wr) !=
680 log_err("fio: ibv_post_recv fail\n");
684 } else if ((rd->rdma_protocol == FIO_RDMA_MEM_READ)
685 || (rd->rdma_protocol == FIO_RDMA_MEM_WRITE)) {
686 /* re-post the rq_wr */
687 if (ibv_post_recv(rd->qp, &rd->rq_wr, &bad_wr) != 0) {
688 log_err("fio: ibv_post_recv fail\n");
692 rdma_poll_wait(td, IBV_WC_RECV);
694 dprint(FD_IO, "fio: recv FINISH message\n");
702 static int fio_rdmaio_queue(struct thread_data *td, struct io_u *io_u)
704 struct rdmaio_data *rd = td->io_ops->data;
706 fio_ro_check(td, io_u);
708 if (rd->io_u_queued_nr == (int)td->o.iodepth)
711 rd->io_us_queued[rd->io_u_queued_nr] = io_u;
712 rd->io_u_queued_nr++;
714 dprint_io_u(io_u, "fio_rdmaio_queue");
719 static void fio_rdmaio_queued(struct thread_data *td, struct io_u **io_us,
722 struct rdmaio_data *rd = td->io_ops->data;
726 if (!fio_fill_issue_time(td))
729 fio_gettime(&now, NULL);
731 for (i = 0; i < nr; i++) {
732 struct io_u *io_u = io_us[i];
734 /* queued -> flight */
735 rd->io_us_flight[rd->io_u_flight_nr] = io_u;
736 rd->io_u_flight_nr++;
738 memcpy(&io_u->issue_time, &now, sizeof(now));
739 io_u_queued(td, io_u);
743 static int fio_rdmaio_commit(struct thread_data *td)
745 struct rdmaio_data *rd = td->io_ops->data;
749 if (!rd->io_us_queued)
752 io_us = rd->io_us_queued;
754 /* RDMA_WRITE or RDMA_READ */
756 ret = fio_rdmaio_send(td, io_us, rd->io_u_queued_nr);
757 } else if (!rd->is_client) {
758 ret = fio_rdmaio_recv(td, io_us, rd->io_u_queued_nr);
760 ret = 0; /* must be a SYNC */
763 fio_rdmaio_queued(td, io_us, ret);
764 io_u_mark_submit(td, ret);
765 rd->io_u_queued_nr -= ret;
770 } while (rd->io_u_queued_nr);
775 static int fio_rdmaio_connect(struct thread_data *td, struct fio_file *f)
777 struct rdmaio_data *rd = td->io_ops->data;
778 struct rdma_conn_param conn_param;
779 struct ibv_send_wr *bad_wr;
781 memset(&conn_param, 0, sizeof conn_param);
782 conn_param.responder_resources = 1;
783 conn_param.initiator_depth = 1;
784 conn_param.retry_count = 10;
786 if (rdma_connect(rd->cm_id, &conn_param) != 0) {
787 log_err("fio: rdma_connect fail\n");
791 if (get_next_channel_event
792 (td, rd->cm_channel, RDMA_CM_EVENT_ESTABLISHED) != 0) {
793 log_err("fio: wait for RDMA_CM_EVENT_ESTABLISHED\n");
797 /* send task request */
798 rd->send_buf.mode = htonl(rd->rdma_protocol);
799 rd->send_buf.nr = htonl(td->o.iodepth);
801 if (ibv_post_send(rd->qp, &rd->sq_wr, &bad_wr) != 0) {
802 log_err("fio: ibv_post_send fail");
806 rdma_poll_wait(td, IBV_WC_SEND);
808 /* wait for remote MR info from server side */
809 rdma_poll_wait(td, IBV_WC_RECV);
811 /* In SEND/RECV test, it's a good practice to setup the iodepth of
812 * of the RECV side deeper than that of the SEND side to
813 * avoid RNR (receiver not ready) error. The
814 * SEND side may send so many unsolicited message before
815 * RECV side commits sufficient recv buffers into recv queue.
816 * This may lead to RNR error. Here, SEND side pauses for a while
817 * during which RECV side commits sufficient recv buffers.
824 static int fio_rdmaio_accept(struct thread_data *td, struct fio_file *f)
826 struct rdmaio_data *rd = td->io_ops->data;
827 struct rdma_conn_param conn_param;
828 struct ibv_send_wr *bad_wr;
830 /* rdma_accept() - then wait for accept success */
831 memset(&conn_param, 0, sizeof conn_param);
832 conn_param.responder_resources = 1;
833 conn_param.initiator_depth = 1;
835 if (rdma_accept(rd->child_cm_id, &conn_param) != 0) {
836 log_err("fio: rdma_accept\n");
840 if (get_next_channel_event
841 (td, rd->cm_channel, RDMA_CM_EVENT_ESTABLISHED) != 0) {
842 log_err("fio: wait for RDMA_CM_EVENT_ESTABLISHED\n");
846 /* wait for request */
847 rdma_poll_wait(td, IBV_WC_RECV);
849 if (ibv_post_send(rd->qp, &rd->sq_wr, &bad_wr) != 0) {
850 log_err("fio: ibv_post_send fail");
854 rdma_poll_wait(td, IBV_WC_SEND);
859 static int fio_rdmaio_open_file(struct thread_data *td, struct fio_file *f)
862 return fio_rdmaio_accept(td, f);
864 return fio_rdmaio_connect(td, f);
867 static int fio_rdmaio_close_file(struct thread_data *td, struct fio_file *f)
869 struct rdmaio_data *rd = td->io_ops->data;
870 struct ibv_send_wr *bad_wr;
872 /* unregister rdma buffer */
875 * Client sends notification to the server side
877 /* refer to: http://linux.die.net/man/7/rdma_cm */
878 if ((rd->is_client == 1) && ((rd->rdma_protocol == FIO_RDMA_MEM_WRITE)
879 || (rd->rdma_protocol ==
880 FIO_RDMA_MEM_READ))) {
881 if (ibv_post_send(rd->qp, &rd->sq_wr, &bad_wr) != 0) {
882 log_err("fio: ibv_post_send fail");
886 dprint(FD_IO, "fio: close infomation sent success\n");
887 rdma_poll_wait(td, IBV_WC_SEND);
890 if (rd->is_client == 1)
891 rdma_disconnect(rd->cm_id);
893 rdma_disconnect(rd->child_cm_id);
894 /* rdma_disconnect(rd->cm_id); */
897 /* if (get_next_channel_event(td, rd->cm_channel, RDMA_CM_EVENT_DISCONNECTED) != 0)
899 log_err("fio: wait for RDMA_CM_EVENT_DISCONNECTED\n");
903 ibv_destroy_cq(rd->cq);
904 ibv_destroy_qp(rd->qp);
906 if (rd->is_client == 1)
907 rdma_destroy_id(rd->cm_id);
909 rdma_destroy_id(rd->child_cm_id);
910 rdma_destroy_id(rd->cm_id);
913 ibv_destroy_comp_channel(rd->channel);
914 ibv_dealloc_pd(rd->pd);
919 static int fio_rdmaio_setup_connect(struct thread_data *td, const char *host,
922 struct rdmaio_data *rd = td->io_ops->data;
923 struct ibv_recv_wr *bad_wr;
925 rd->addr.sin_family = AF_INET;
926 rd->addr.sin_port = htons(port);
928 if (inet_aton(host, &rd->addr.sin_addr) != 1) {
929 struct hostent *hent;
931 hent = gethostbyname(host);
933 td_verror(td, errno, "gethostbyname");
937 memcpy(&rd->addr.sin_addr, hent->h_addr, 4);
941 if (rdma_resolve_addr(rd->cm_id, NULL,
942 (struct sockaddr *)&rd->addr, 2000) != 0) {
943 log_err("fio: rdma_resolve_addr");
947 if (get_next_channel_event
948 (td, rd->cm_channel, RDMA_CM_EVENT_ADDR_RESOLVED)
950 log_err("fio: get_next_channel_event");
955 if (rdma_resolve_route(rd->cm_id, 2000) != 0) {
956 log_err("fio: rdma_resolve_route");
960 if (get_next_channel_event
961 (td, rd->cm_channel, RDMA_CM_EVENT_ROUTE_RESOLVED) != 0) {
962 log_err("fio: get_next_channel_event");
966 /* create qp and buffer */
967 if (fio_rdmaio_setup_qp(td) != 0)
970 if (fio_rdmaio_setup_control_msg_buffers(td) != 0)
974 if (ibv_post_recv(rd->qp, &rd->rq_wr, &bad_wr) != 0) {
975 log_err("fio: ibv_post_recv fail\n");
982 static int fio_rdmaio_setup_listen(struct thread_data *td, short port)
984 struct rdmaio_data *rd = td->io_ops->data;
985 struct ibv_recv_wr *bad_wr;
987 rd->addr.sin_family = AF_INET;
988 rd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
989 rd->addr.sin_port = htons(port);
992 if (rdma_bind_addr(rd->cm_id, (struct sockaddr *)&rd->addr) != 0) {
993 log_err("fio: rdma_bind_addr fail\n");
997 if (rdma_listen(rd->cm_id, 3) != 0) {
998 log_err("fio: rdma_listen fail\n");
1002 /* wait for CONNECT_REQUEST */
1003 if (get_next_channel_event
1004 (td, rd->cm_channel, RDMA_CM_EVENT_CONNECT_REQUEST) != 0) {
1005 log_err("fio: wait for RDMA_CM_EVENT_CONNECT_REQUEST\n");
1009 if (fio_rdmaio_setup_qp(td) != 0)
1012 if (fio_rdmaio_setup_control_msg_buffers(td) != 0)
1016 if (ibv_post_recv(rd->qp, &rd->rq_wr, &bad_wr) != 0) {
1017 log_err("fio: ibv_post_recv fail\n");
1024 static int fio_rdmaio_init(struct thread_data *td)
1026 struct rdmaio_data *rd = td->io_ops->data;
1028 char host[64], buf[128];
1029 char *sep, *portp, *modep;
1034 log_err("fio: rdma connections must be read OR write\n");
1037 if (td_random(td)) {
1038 log_err("fio: RDMA network IO can't be random\n");
1042 /* check RLIMIT_MEMLOCK */
1043 if (getrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
1044 log_err("fio: getrlimit fail: %d(%s)\n",
1045 errno, strerror(errno));
1050 if ((rl.rlim_cur != RLIM_INFINITY)
1051 && (rl.rlim_cur < td->orig_buffer_size)) {
1052 log_err("fio: soft RLIMIT_MEMLOCK is: %" PRId64 "\n",
1054 log_err("fio: total block size is: %zd\n",
1055 td->orig_buffer_size);
1056 /* try to set larger RLIMIT_MEMLOCK */
1057 rl.rlim_cur = rl.rlim_max;
1058 if (setrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
1059 log_err("fio: setrlimit fail: %d(%s)\n",
1060 errno, strerror(errno));
1061 log_err("fio: you may try enlarge MEMLOCK by root\n");
1062 log_err("# ulimit -l unlimited\n");
1067 strcpy(buf, td->o.filename);
1069 sep = strchr(buf, '/');
1081 sep = strchr(portp, '/');
1087 port = strtol(portp, NULL, 10);
1088 if (!port || port > 65535)
1092 if (!strncmp("rdma_write", modep, strlen(modep)) ||
1093 !strncmp("RDMA_WRITE", modep, strlen(modep)))
1094 rd->rdma_protocol = FIO_RDMA_MEM_WRITE;
1095 else if (!strncmp("rdma_read", modep, strlen(modep)) ||
1096 !strncmp("RDMA_READ", modep, strlen(modep)))
1097 rd->rdma_protocol = FIO_RDMA_MEM_READ;
1098 else if (!strncmp("send", modep, strlen(modep)) ||
1099 !strncmp("SEND", modep, strlen(modep)))
1100 rd->rdma_protocol = FIO_RDMA_CHA_SEND;
1104 rd->rdma_protocol = FIO_RDMA_MEM_WRITE;
1106 rd->cq_event_num = 0;
1108 rd->cm_channel = rdma_create_event_channel();
1109 if (!rd->cm_channel) {
1110 log_err("fio: rdma_create_event_channel fail\n");
1114 ret = rdma_create_id(rd->cm_channel, &rd->cm_id, rd, RDMA_PS_TCP);
1116 log_err("fio: rdma_create_id fail\n");
1120 if ((rd->rdma_protocol == FIO_RDMA_MEM_WRITE) ||
1121 (rd->rdma_protocol == FIO_RDMA_MEM_READ)) {
1123 malloc(FIO_RDMA_MAX_IO_DEPTH * sizeof(struct remote_u));
1124 memset(rd->rmt_us, 0,
1125 FIO_RDMA_MAX_IO_DEPTH * sizeof(struct remote_u));
1129 rd->io_us_queued = malloc(td->o.iodepth * sizeof(struct io_u *));
1130 memset(rd->io_us_queued, 0, td->o.iodepth * sizeof(struct io_u *));
1131 rd->io_u_queued_nr = 0;
1133 rd->io_us_flight = malloc(td->o.iodepth * sizeof(struct io_u *));
1134 memset(rd->io_us_flight, 0, td->o.iodepth * sizeof(struct io_u *));
1135 rd->io_u_flight_nr = 0;
1137 rd->io_us_completed = malloc(td->o.iodepth * sizeof(struct io_u *));
1138 memset(rd->io_us_completed, 0, td->o.iodepth * sizeof(struct io_u *));
1139 rd->io_u_completed_nr = 0;
1141 if (td_read(td)) { /* READ as the server */
1143 /* server rd->rdma_buf_len will be setup after got request */
1144 ret = fio_rdmaio_setup_listen(td, port);
1145 } else { /* WRITE as the client */
1147 ret = fio_rdmaio_setup_connect(td, host, port);
1150 struct flist_head *entry;
1151 unsigned int max_bs;
1152 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
1153 /* register each io_u in the free list */
1155 flist_for_each(entry, &td->io_u_freelist) {
1156 struct io_u *io_u = flist_entry(entry, struct io_u, list);
1158 io_u->engine_data = malloc(sizeof(struct rdma_io_u_data));
1159 memset(io_u->engine_data, 0, sizeof(struct rdma_io_u_data));
1160 ((struct rdma_io_u_data *)io_u->engine_data)->wr_id = i;
1162 io_u->mr = ibv_reg_mr(rd->pd, io_u->buf, max_bs,
1163 IBV_ACCESS_LOCAL_WRITE |
1164 IBV_ACCESS_REMOTE_READ |
1165 IBV_ACCESS_REMOTE_WRITE);
1166 if (io_u->mr == NULL) {
1167 log_err("fio: ibv_reg_mr io_u failed\n");
1171 rd->send_buf.rmt_us[i].buf =
1172 htonll((uint64_t) (unsigned long)io_u->buf);
1173 rd->send_buf.rmt_us[i].rkey = htonl(io_u->mr->rkey);
1174 rd->send_buf.rmt_us[i].size = htonl(max_bs);
1176 /* log_info("fio: Send rkey %x addr %" PRIx64 " len %d to client\n",
1177 io_u->mr->rkey, io_u->buf, max_bs); */
1181 rd->send_buf.nr = htonl(i);
1185 log_err("fio: bad rdma host/port/protocol: %s\n", td->o.filename);
1189 static void fio_rdmaio_cleanup(struct thread_data *td)
1191 struct rdmaio_data *rd = td->io_ops->data;
1194 /* if (nd->listenfd != -1)
1195 close(nd->listenfd);
1196 if (nd->pipes[0] != -1)
1197 close(nd->pipes[0]);
1198 if (nd->pipes[1] != -1)
1199 close(nd->pipes[1]);
1205 static int fio_rdmaio_setup(struct thread_data *td)
1207 struct rdmaio_data *rd;
1209 if (!td->io_ops->data) {
1210 rd = malloc(sizeof(*rd));;
1212 memset(rd, 0, sizeof(*rd));
1213 td->io_ops->data = rd;
1219 static struct ioengine_ops ioengine_rw = {
1221 .version = FIO_IOOPS_VERSION,
1222 .setup = fio_rdmaio_setup,
1223 .init = fio_rdmaio_init,
1224 .prep = fio_rdmaio_prep,
1225 .queue = fio_rdmaio_queue,
1226 .commit = fio_rdmaio_commit,
1227 .getevents = fio_rdmaio_getevents,
1228 .event = fio_rdmaio_event,
1229 .cleanup = fio_rdmaio_cleanup,
1230 .open_file = fio_rdmaio_open_file,
1231 .close_file = fio_rdmaio_close_file,
1232 .flags = FIO_DISKLESSIO | FIO_UNIDIR | FIO_PIPEIO,
1235 #else /* FIO_HAVE_RDMA */
1237 static int fio_rdmaio_open_file(struct thread_data *td, struct fio_file *f)
1242 static int fio_rdmaio_close_file(struct thread_data *td, struct fio_file *f)
1247 static int fio_rdmaio_queue(struct thread_data *td, struct io_u *io_u)
1249 return FIO_Q_COMPLETED;
1252 static int fio_rdmaio_init(struct thread_data fio_unused * td)
1254 log_err("fio: rdma(librdmacm libibverbs) not available\n");
1255 log_err(" You haven't compiled rdma ioengine into fio.\n");
1256 log_err(" If you want to try rdma ioengine,\n");
1257 log_err(" make sure OFED is installed,\n");
1258 log_err(" $ ofed_info\n");
1259 log_err(" then try to make fio as follows:\n");
1260 log_err(" $ export EXTFLAGS+=\" -DFIO_HAVE_RDMA \"\n");
1261 log_err(" $ export EXTLIBS+=\" -libverbs -lrdmacm \"\n");
1262 log_err(" $ make clean && make\n");
1266 static struct ioengine_ops ioengine_rw = {
1268 .version = FIO_IOOPS_VERSION,
1269 .init = fio_rdmaio_init,
1270 .queue = fio_rdmaio_queue,
1271 .open_file = fio_rdmaio_open_file,
1272 .close_file = fio_rdmaio_close_file,
1273 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR | FIO_PIPEIO,
1278 static void fio_init fio_rdmaio_register(void)
1280 register_ioengine(&ioengine_rw);
1283 static void fio_exit fio_rdmaio_unregister(void)
1285 unregister_ioengine(&ioengine_rw);