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>
53 #include <rdma/rdma_cma.h>
54 #include <infiniband/arch.h>
56 #define FIO_RDMA_MAX_IO_DEPTH 512
72 struct rdma_info_blk {
73 uint32_t mode; /* channel semantic or memory semantic */
74 uint32_t nr; /* client: io depth
75 server: number of records for memory semantic
77 struct remote_u rmt_us[FIO_RDMA_MAX_IO_DEPTH];
80 struct rdma_io_u_data {
82 struct ibv_send_wr sq_wr;
83 struct ibv_recv_wr rq_wr;
84 struct ibv_sge rdma_sgl;
89 enum rdma_io_mode rdma_protocol;
91 struct sockaddr_in addr;
93 struct ibv_recv_wr rq_wr;
94 struct ibv_sge recv_sgl;
95 struct rdma_info_blk recv_buf;
96 struct ibv_mr *recv_mr;
98 struct ibv_send_wr sq_wr;
99 struct ibv_sge send_sgl;
100 struct rdma_info_blk send_buf;
101 struct ibv_mr *send_mr;
103 struct ibv_comp_channel *channel;
109 struct rdma_event_channel *cm_channel;
110 struct rdma_cm_id *cm_id;
111 struct rdma_cm_id *child_cm_id;
115 struct remote_u *rmt_us;
117 struct io_u **io_us_queued;
119 struct io_u **io_us_flight;
121 struct io_u **io_us_completed;
122 int io_u_completed_nr;
124 struct frand_state rand_state;
127 static int client_recv(struct thread_data *td, struct ibv_wc *wc)
129 struct rdmaio_data *rd = td->io_ops->data;
131 if (wc->byte_len != sizeof(rd->recv_buf)) {
132 log_err("Received bogus data, size %d\n", wc->byte_len);
136 /* store mr info for MEMORY semantic */
137 if ((rd->rdma_protocol == FIO_RDMA_MEM_WRITE) ||
138 (rd->rdma_protocol == FIO_RDMA_MEM_READ)) {
139 /* struct flist_head *entry; */
142 rd->rmt_nr = ntohl(rd->recv_buf.nr);
144 for (i = 0; i < rd->rmt_nr; i++) {
145 rd->rmt_us[i].buf = ntohll(rd->recv_buf.rmt_us[i].buf);
146 rd->rmt_us[i].rkey = ntohl(rd->recv_buf.rmt_us[i].rkey);
147 rd->rmt_us[i].size = ntohl(rd->recv_buf.rmt_us[i].size);
150 "fio: Received rkey %x addr %" PRIx64
151 " len %d from peer\n", rd->rmt_us[i].rkey,
152 rd->rmt_us[i].buf, rd->rmt_us[i].size);
159 static int server_recv(struct thread_data *td, struct ibv_wc *wc)
161 struct rdmaio_data *rd = td->io_ops->data;
163 if (wc->wr_id == FIO_RDMA_MAX_IO_DEPTH) {
164 rd->rdma_protocol = ntohl(rd->recv_buf.mode);
166 /* CHANNEL semantic, do nothing */
167 if (rd->rdma_protocol == FIO_RDMA_CHA_SEND)
168 rd->rdma_protocol = FIO_RDMA_CHA_RECV;
174 static int cq_event_handler(struct thread_data *td, enum ibv_wc_opcode opcode)
176 struct rdmaio_data *rd = td->io_ops->data;
178 struct rdma_io_u_data *r_io_u_d;
183 while ((ret = ibv_poll_cq(rd->cq, 1, &wc)) == 1) {
188 log_err("fio: cq completion status %d(%s)\n",
189 wc.status, ibv_wc_status_str(wc.status));
196 if (rd->is_client == 1)
197 client_recv(td, &wc);
199 server_recv(td, &wc);
201 if (wc.wr_id == FIO_RDMA_MAX_IO_DEPTH)
204 for (i = 0; i < rd->io_u_flight_nr; i++) {
205 r_io_u_d = rd->io_us_flight[i]->engine_data;
207 if (wc.wr_id == r_io_u_d->rq_wr.wr_id) {
208 rd->io_us_flight[i]->resid =
209 rd->io_us_flight[i]->buflen
212 rd->io_us_flight[i]->error = 0;
214 rd->io_us_completed[rd->
216 = rd->io_us_flight[i];
217 rd->io_u_completed_nr++;
221 if (i == rd->io_u_flight_nr)
222 log_err("fio: recv wr %" PRId64 " not found\n",
225 /* put the last one into middle of the list */
226 rd->io_us_flight[i] =
227 rd->io_us_flight[rd->io_u_flight_nr - 1];
228 rd->io_u_flight_nr--;
234 case IBV_WC_RDMA_WRITE:
235 case IBV_WC_RDMA_READ:
236 if (wc.wr_id == FIO_RDMA_MAX_IO_DEPTH)
239 for (i = 0; i < rd->io_u_flight_nr; i++) {
240 r_io_u_d = rd->io_us_flight[i]->engine_data;
242 if (wc.wr_id == r_io_u_d->sq_wr.wr_id) {
243 rd->io_us_completed[rd->
245 = rd->io_us_flight[i];
246 rd->io_u_completed_nr++;
250 if (i == rd->io_u_flight_nr)
251 log_err("fio: send wr %" PRId64 " not found\n",
254 /* put the last one into middle of the list */
255 rd->io_us_flight[i] =
256 rd->io_us_flight[rd->io_u_flight_nr - 1];
257 rd->io_u_flight_nr--;
263 log_info("fio: unknown completion event %d\n",
270 log_err("fio: poll error %d\n", ret);
278 * Return -1 for error and 'nr events' for a positive number
281 static int rdma_poll_wait(struct thread_data *td, enum ibv_wc_opcode opcode)
283 struct rdmaio_data *rd = td->io_ops->data;
284 struct ibv_cq *ev_cq;
288 if (rd->cq_event_num > 0) { /* previous left */
294 if (ibv_get_cq_event(rd->channel, &ev_cq, &ev_ctx) != 0) {
295 log_err("fio: Failed to get cq event!\n");
298 if (ev_cq != rd->cq) {
299 log_err("fio: Unknown CQ!\n");
302 if (ibv_req_notify_cq(rd->cq, 0) != 0) {
303 log_err("fio: Failed to set notify!\n");
307 ret = cq_event_handler(td, opcode);
311 ibv_ack_cq_events(rd->cq, ret);
318 static int fio_rdmaio_setup_qp(struct thread_data *td)
320 struct rdmaio_data *rd = td->io_ops->data;
321 struct ibv_qp_init_attr init_attr;
322 int qp_depth = td->o.iodepth * 2; /* 2 times of io depth */
324 if (rd->is_client == 0)
325 rd->pd = ibv_alloc_pd(rd->child_cm_id->verbs);
327 rd->pd = ibv_alloc_pd(rd->cm_id->verbs);
329 if (rd->pd == NULL) {
330 log_err("fio: ibv_alloc_pd fail\n");
334 if (rd->is_client == 0)
335 rd->channel = ibv_create_comp_channel(rd->child_cm_id->verbs);
337 rd->channel = ibv_create_comp_channel(rd->cm_id->verbs);
338 if (rd->channel == NULL) {
339 log_err("fio: ibv_create_comp_channel fail\n");
346 if (rd->is_client == 0)
347 rd->cq = ibv_create_cq(rd->child_cm_id->verbs,
348 qp_depth, rd, rd->channel, 0);
350 rd->cq = ibv_create_cq(rd->cm_id->verbs,
351 qp_depth, rd, rd->channel, 0);
352 if (rd->cq == NULL) {
353 log_err("fio: ibv_create_cq failed\n");
357 if (ibv_req_notify_cq(rd->cq, 0) != 0) {
358 log_err("fio: ibv_create_cq failed\n");
362 /* create queue pair */
363 memset(&init_attr, 0, sizeof(init_attr));
364 init_attr.cap.max_send_wr = qp_depth;
365 init_attr.cap.max_recv_wr = qp_depth;
366 init_attr.cap.max_recv_sge = 1;
367 init_attr.cap.max_send_sge = 1;
368 init_attr.qp_type = IBV_QPT_RC;
369 init_attr.send_cq = rd->cq;
370 init_attr.recv_cq = rd->cq;
372 if (rd->is_client == 0) {
373 if (rdma_create_qp(rd->child_cm_id, rd->pd, &init_attr) != 0) {
374 log_err("fio: rdma_create_qp failed\n");
377 rd->qp = rd->child_cm_id->qp;
379 if (rdma_create_qp(rd->cm_id, rd->pd, &init_attr) != 0) {
380 log_err("fio: rdma_create_qp failed\n");
383 rd->qp = rd->cm_id->qp;
389 ibv_destroy_cq(rd->cq);
391 ibv_destroy_comp_channel(rd->channel);
393 ibv_dealloc_pd(rd->pd);
398 static int fio_rdmaio_setup_control_msg_buffers(struct thread_data *td)
400 struct rdmaio_data *rd = td->io_ops->data;
402 rd->recv_mr = ibv_reg_mr(rd->pd, &rd->recv_buf, sizeof(rd->recv_buf),
403 IBV_ACCESS_LOCAL_WRITE);
404 if (rd->recv_mr == NULL) {
405 log_err("fio: recv_buf reg_mr failed\n");
409 rd->send_mr = ibv_reg_mr(rd->pd, &rd->send_buf, sizeof(rd->send_buf),
411 if (rd->send_mr == NULL) {
412 log_err("fio: send_buf reg_mr failed\n");
413 ibv_dereg_mr(rd->recv_mr);
417 /* setup work request */
419 rd->recv_sgl.addr = (uint64_t) (unsigned long)&rd->recv_buf;
420 rd->recv_sgl.length = sizeof(rd->recv_buf);
421 rd->recv_sgl.lkey = rd->recv_mr->lkey;
422 rd->rq_wr.sg_list = &rd->recv_sgl;
423 rd->rq_wr.num_sge = 1;
424 rd->rq_wr.wr_id = FIO_RDMA_MAX_IO_DEPTH;
427 rd->send_sgl.addr = (uint64_t) (unsigned long)&rd->send_buf;
428 rd->send_sgl.length = sizeof(rd->send_buf);
429 rd->send_sgl.lkey = rd->send_mr->lkey;
431 rd->sq_wr.opcode = IBV_WR_SEND;
432 rd->sq_wr.send_flags = IBV_SEND_SIGNALED;
433 rd->sq_wr.sg_list = &rd->send_sgl;
434 rd->sq_wr.num_sge = 1;
435 rd->sq_wr.wr_id = FIO_RDMA_MAX_IO_DEPTH;
440 static int get_next_channel_event(struct thread_data *td,
441 struct rdma_event_channel *channel,
442 enum rdma_cm_event_type wait_event)
444 struct rdmaio_data *rd = td->io_ops->data;
445 struct rdma_cm_event *event;
448 ret = rdma_get_cm_event(channel, &event);
450 log_err("fio: rdma_get_cm_event: %d\n", ret);
454 if (event->event != wait_event) {
455 log_err("fio: event is %s instead of %s\n",
456 rdma_event_str(event->event),
457 rdma_event_str(wait_event));
461 switch (event->event) {
462 case RDMA_CM_EVENT_CONNECT_REQUEST:
463 rd->child_cm_id = event->id;
469 rdma_ack_cm_event(event);
474 static int fio_rdmaio_prep(struct thread_data *td, struct io_u *io_u)
476 struct rdmaio_data *rd = td->io_ops->data;
477 struct rdma_io_u_data *r_io_u_d;
479 r_io_u_d = io_u->engine_data;
481 switch (rd->rdma_protocol) {
482 case FIO_RDMA_MEM_WRITE:
483 case FIO_RDMA_MEM_READ:
484 r_io_u_d->rdma_sgl.addr = (uint64_t) (unsigned long)io_u->buf;
485 r_io_u_d->rdma_sgl.lkey = io_u->mr->lkey;
486 r_io_u_d->sq_wr.wr_id = r_io_u_d->wr_id;
487 r_io_u_d->sq_wr.send_flags = IBV_SEND_SIGNALED;
488 r_io_u_d->sq_wr.sg_list = &r_io_u_d->rdma_sgl;
489 r_io_u_d->sq_wr.num_sge = 1;
491 case FIO_RDMA_CHA_SEND:
492 r_io_u_d->rdma_sgl.addr = (uint64_t) (unsigned long)io_u->buf;
493 r_io_u_d->rdma_sgl.lkey = io_u->mr->lkey;
494 r_io_u_d->rdma_sgl.length = io_u->buflen;
495 r_io_u_d->sq_wr.wr_id = r_io_u_d->wr_id;
496 r_io_u_d->sq_wr.opcode = IBV_WR_SEND;
497 r_io_u_d->sq_wr.send_flags = IBV_SEND_SIGNALED;
498 r_io_u_d->sq_wr.sg_list = &r_io_u_d->rdma_sgl;
499 r_io_u_d->sq_wr.num_sge = 1;
501 case FIO_RDMA_CHA_RECV:
502 r_io_u_d->rdma_sgl.addr = (uint64_t) (unsigned long)io_u->buf;
503 r_io_u_d->rdma_sgl.lkey = io_u->mr->lkey;
504 r_io_u_d->rdma_sgl.length = io_u->buflen;
505 r_io_u_d->rq_wr.wr_id = r_io_u_d->wr_id;
506 r_io_u_d->rq_wr.sg_list = &r_io_u_d->rdma_sgl;
507 r_io_u_d->rq_wr.num_sge = 1;
510 log_err("fio: unknown rdma protocol - %d\n", rd->rdma_protocol);
517 static struct io_u *fio_rdmaio_event(struct thread_data *td, int event)
519 struct rdmaio_data *rd = td->io_ops->data;
523 io_u = rd->io_us_completed[0];
524 for (i = 0; i < rd->io_u_completed_nr - 1; i++)
525 rd->io_us_completed[i] = rd->io_us_completed[i + 1];
527 rd->io_u_completed_nr--;
529 dprint_io_u(io_u, "fio_rdmaio_event");
534 static int fio_rdmaio_getevents(struct thread_data *td, unsigned int min,
535 unsigned int max, struct timespec *t)
537 struct rdmaio_data *rd = td->io_ops->data;
538 enum ibv_wc_opcode comp_opcode;
539 comp_opcode = IBV_WC_RDMA_WRITE;
540 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 index = __rand(&rd->rand_state) % rd->rmt_nr;
618 r_io_u_d->sq_wr.opcode = IBV_WR_RDMA_WRITE;
619 r_io_u_d->sq_wr.wr.rdma.rkey = rd->rmt_us[index].rkey;
620 r_io_u_d->sq_wr.wr.rdma.remote_addr = \
621 rd->rmt_us[index].buf;
622 r_io_u_d->sq_wr.sg_list->length = io_us[i]->buflen;
624 case FIO_RDMA_MEM_READ:
625 /* compose work request */
626 r_io_u_d = io_us[i]->engine_data;
627 index = __rand(&rd->rand_state) % rd->rmt_nr;
628 r_io_u_d->sq_wr.opcode = IBV_WR_RDMA_READ;
629 r_io_u_d->sq_wr.wr.rdma.rkey = rd->rmt_us[index].rkey;
630 r_io_u_d->sq_wr.wr.rdma.remote_addr = \
631 rd->rmt_us[index].buf;
632 r_io_u_d->sq_wr.sg_list->length = io_us[i]->buflen;
634 case FIO_RDMA_CHA_SEND:
635 r_io_u_d = io_us[i]->engine_data;
636 r_io_u_d->sq_wr.opcode = IBV_WR_SEND;
637 r_io_u_d->sq_wr.send_flags = IBV_SEND_SIGNALED;
640 log_err("fio: unknown rdma protocol - %d\n",
645 if (ibv_post_send(rd->qp, &r_io_u_d->sq_wr, &bad_wr) != 0) {
646 log_err("fio: ibv_post_send fail\n");
650 dprint_io_u(io_us[i], "fio_rdmaio_send");
653 /* wait for completion
654 rdma_poll_wait(td, comp_opcode); */
659 static int fio_rdmaio_recv(struct thread_data *td, struct io_u **io_us,
662 struct rdmaio_data *rd = td->io_ops->data;
663 struct ibv_recv_wr *bad_wr;
664 struct rdma_io_u_data *r_io_u_d;
668 if (rd->rdma_protocol == FIO_RDMA_CHA_RECV) {
669 /* post io_u into recv queue */
670 for (i = 0; i < nr; i++) {
671 r_io_u_d = io_us[i]->engine_data;
672 if (ibv_post_recv(rd->qp, &r_io_u_d->rq_wr, &bad_wr) !=
674 log_err("fio: ibv_post_recv fail\n");
678 } else if ((rd->rdma_protocol == FIO_RDMA_MEM_READ)
679 || (rd->rdma_protocol == FIO_RDMA_MEM_WRITE)) {
680 /* re-post the rq_wr */
681 if (ibv_post_recv(rd->qp, &rd->rq_wr, &bad_wr) != 0) {
682 log_err("fio: ibv_post_recv fail\n");
686 rdma_poll_wait(td, IBV_WC_RECV);
688 dprint(FD_IO, "fio: recv FINISH message\n");
696 static int fio_rdmaio_queue(struct thread_data *td, struct io_u *io_u)
698 struct rdmaio_data *rd = td->io_ops->data;
700 fio_ro_check(td, io_u);
702 if (rd->io_u_queued_nr == (int)td->o.iodepth)
705 rd->io_us_queued[rd->io_u_queued_nr] = io_u;
706 rd->io_u_queued_nr++;
708 dprint_io_u(io_u, "fio_rdmaio_queue");
713 static void fio_rdmaio_queued(struct thread_data *td, struct io_u **io_us,
716 struct rdmaio_data *rd = td->io_ops->data;
720 if (!fio_fill_issue_time(td))
723 fio_gettime(&now, NULL);
725 for (i = 0; i < nr; i++) {
726 struct io_u *io_u = io_us[i];
728 /* queued -> flight */
729 rd->io_us_flight[rd->io_u_flight_nr] = io_u;
730 rd->io_u_flight_nr++;
732 memcpy(&io_u->issue_time, &now, sizeof(now));
733 io_u_queued(td, io_u);
737 static int fio_rdmaio_commit(struct thread_data *td)
739 struct rdmaio_data *rd = td->io_ops->data;
743 if (!rd->io_us_queued)
746 io_us = rd->io_us_queued;
748 /* RDMA_WRITE or RDMA_READ */
750 ret = fio_rdmaio_send(td, io_us, rd->io_u_queued_nr);
751 else if (!rd->is_client)
752 ret = fio_rdmaio_recv(td, io_us, rd->io_u_queued_nr);
754 ret = 0; /* must be a SYNC */
757 fio_rdmaio_queued(td, io_us, ret);
758 io_u_mark_submit(td, ret);
759 rd->io_u_queued_nr -= ret;
764 } while (rd->io_u_queued_nr);
769 static int fio_rdmaio_connect(struct thread_data *td, struct fio_file *f)
771 struct rdmaio_data *rd = td->io_ops->data;
772 struct rdma_conn_param conn_param;
773 struct ibv_send_wr *bad_wr;
775 memset(&conn_param, 0, sizeof(conn_param));
776 conn_param.responder_resources = 1;
777 conn_param.initiator_depth = 1;
778 conn_param.retry_count = 10;
780 if (rdma_connect(rd->cm_id, &conn_param) != 0) {
781 log_err("fio: rdma_connect fail\n");
785 if (get_next_channel_event
786 (td, rd->cm_channel, RDMA_CM_EVENT_ESTABLISHED) != 0) {
787 log_err("fio: wait for RDMA_CM_EVENT_ESTABLISHED\n");
791 /* send task request */
792 rd->send_buf.mode = htonl(rd->rdma_protocol);
793 rd->send_buf.nr = htonl(td->o.iodepth);
795 if (ibv_post_send(rd->qp, &rd->sq_wr, &bad_wr) != 0) {
796 log_err("fio: ibv_post_send fail");
800 rdma_poll_wait(td, IBV_WC_SEND);
802 /* wait for remote MR info from server side */
803 rdma_poll_wait(td, IBV_WC_RECV);
805 /* In SEND/RECV test, it's a good practice to setup the iodepth of
806 * of the RECV side deeper than that of the SEND side to
807 * avoid RNR (receiver not ready) error. The
808 * SEND side may send so many unsolicited message before
809 * RECV side commits sufficient recv buffers into recv queue.
810 * This may lead to RNR error. Here, SEND side pauses for a while
811 * during which RECV side commits sufficient recv buffers.
818 static int fio_rdmaio_accept(struct thread_data *td, struct fio_file *f)
820 struct rdmaio_data *rd = td->io_ops->data;
821 struct rdma_conn_param conn_param;
822 struct ibv_send_wr *bad_wr;
824 /* rdma_accept() - then wait for accept success */
825 memset(&conn_param, 0, sizeof(conn_param));
826 conn_param.responder_resources = 1;
827 conn_param.initiator_depth = 1;
829 if (rdma_accept(rd->child_cm_id, &conn_param) != 0) {
830 log_err("fio: rdma_accept\n");
834 if (get_next_channel_event
835 (td, rd->cm_channel, RDMA_CM_EVENT_ESTABLISHED) != 0) {
836 log_err("fio: wait for RDMA_CM_EVENT_ESTABLISHED\n");
840 /* wait for request */
841 rdma_poll_wait(td, IBV_WC_RECV);
843 if (ibv_post_send(rd->qp, &rd->sq_wr, &bad_wr) != 0) {
844 log_err("fio: ibv_post_send fail");
848 rdma_poll_wait(td, IBV_WC_SEND);
853 static int fio_rdmaio_open_file(struct thread_data *td, struct fio_file *f)
856 return fio_rdmaio_accept(td, f);
858 return fio_rdmaio_connect(td, f);
861 static int fio_rdmaio_close_file(struct thread_data *td, struct fio_file *f)
863 struct rdmaio_data *rd = td->io_ops->data;
864 struct ibv_send_wr *bad_wr;
866 /* unregister rdma buffer */
869 * Client sends notification to the server side
871 /* refer to: http://linux.die.net/man/7/rdma_cm */
872 if ((rd->is_client == 1) && ((rd->rdma_protocol == FIO_RDMA_MEM_WRITE)
873 || (rd->rdma_protocol ==
874 FIO_RDMA_MEM_READ))) {
875 if (ibv_post_send(rd->qp, &rd->sq_wr, &bad_wr) != 0) {
876 log_err("fio: ibv_post_send fail");
880 dprint(FD_IO, "fio: close infomation sent success\n");
881 rdma_poll_wait(td, IBV_WC_SEND);
884 if (rd->is_client == 1)
885 rdma_disconnect(rd->cm_id);
887 rdma_disconnect(rd->child_cm_id);
889 rdma_disconnect(rd->cm_id);
894 if (get_next_channel_event(td, rd->cm_channel, RDMA_CM_EVENT_DISCONNECTED) != 0) {
895 log_err("fio: wait for RDMA_CM_EVENT_DISCONNECTED\n");
900 ibv_destroy_cq(rd->cq);
901 ibv_destroy_qp(rd->qp);
903 if (rd->is_client == 1)
904 rdma_destroy_id(rd->cm_id);
906 rdma_destroy_id(rd->child_cm_id);
907 rdma_destroy_id(rd->cm_id);
910 ibv_destroy_comp_channel(rd->channel);
911 ibv_dealloc_pd(rd->pd);
916 static int fio_rdmaio_setup_connect(struct thread_data *td, const char *host,
919 struct rdmaio_data *rd = td->io_ops->data;
920 struct ibv_recv_wr *bad_wr;
923 rd->addr.sin_family = AF_INET;
924 rd->addr.sin_port = htons(port);
926 if (inet_aton(host, &rd->addr.sin_addr) != 1) {
927 struct hostent *hent;
929 hent = gethostbyname(host);
931 td_verror(td, errno, "gethostbyname");
935 memcpy(&rd->addr.sin_addr, hent->h_addr, 4);
939 err = rdma_resolve_addr(rd->cm_id, NULL, (struct sockaddr *)&rd->addr, 2000);
941 log_err("fio: rdma_resolve_addr: %d\n", err);
945 err = get_next_channel_event(td, rd->cm_channel, RDMA_CM_EVENT_ADDR_RESOLVED);
947 log_err("fio: get_next_channel_event: %d\n", err);
952 err = rdma_resolve_route(rd->cm_id, 2000);
954 log_err("fio: rdma_resolve_route: %d\n", err);
958 err = get_next_channel_event(td, rd->cm_channel, RDMA_CM_EVENT_ROUTE_RESOLVED);
960 log_err("fio: get_next_channel_event: %d\n", err);
964 /* create qp and buffer */
965 if (fio_rdmaio_setup_qp(td) != 0)
968 if (fio_rdmaio_setup_control_msg_buffers(td) != 0)
972 err = ibv_post_recv(rd->qp, &rd->rq_wr, &bad_wr);
974 log_err("fio: ibv_post_recv fail: %d\n", err);
981 static int fio_rdmaio_setup_listen(struct thread_data *td, short port)
983 struct rdmaio_data *rd = td->io_ops->data;
984 struct ibv_recv_wr *bad_wr;
986 rd->addr.sin_family = AF_INET;
987 rd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
988 rd->addr.sin_port = htons(port);
991 if (rdma_bind_addr(rd->cm_id, (struct sockaddr *)&rd->addr) != 0) {
992 log_err("fio: rdma_bind_addr fail\n");
996 if (rdma_listen(rd->cm_id, 3) != 0) {
997 log_err("fio: rdma_listen fail\n");
1001 /* wait for CONNECT_REQUEST */
1002 if (get_next_channel_event
1003 (td, rd->cm_channel, RDMA_CM_EVENT_CONNECT_REQUEST) != 0) {
1004 log_err("fio: wait for RDMA_CM_EVENT_CONNECT_REQUEST\n");
1008 if (fio_rdmaio_setup_qp(td) != 0)
1011 if (fio_rdmaio_setup_control_msg_buffers(td) != 0)
1015 if (ibv_post_recv(rd->qp, &rd->rq_wr, &bad_wr) != 0) {
1016 log_err("fio: ibv_post_recv fail\n");
1023 static int fio_rdmaio_init(struct thread_data *td)
1025 struct rdmaio_data *rd = td->io_ops->data;
1026 struct flist_head *entry;
1027 unsigned int max_bs;
1029 char host[64], buf[128];
1030 char *sep, *portp, *modep;
1035 log_err("fio: rdma connections must be read OR write\n");
1038 if (td_random(td)) {
1039 log_err("fio: RDMA network IO can't be random\n");
1043 /* check RLIMIT_MEMLOCK */
1044 if (getrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
1045 log_err("fio: getrlimit fail: %d(%s)\n",
1046 errno, strerror(errno));
1051 if ((rl.rlim_cur != RLIM_INFINITY)
1052 && (rl.rlim_cur < td->orig_buffer_size)) {
1053 log_err("fio: soft RLIMIT_MEMLOCK is: %" PRId64 "\n",
1055 log_err("fio: total block size is: %zd\n",
1056 td->orig_buffer_size);
1057 /* try to set larger RLIMIT_MEMLOCK */
1058 rl.rlim_cur = rl.rlim_max;
1059 if (setrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
1060 log_err("fio: setrlimit fail: %d(%s)\n",
1061 errno, strerror(errno));
1062 log_err("fio: you may try enlarge MEMLOCK by root\n");
1063 log_err("# ulimit -l unlimited\n");
1068 strcpy(buf, td->o.filename);
1070 sep = strchr(buf, '/');
1082 sep = strchr(portp, '/');
1088 port = strtol(portp, NULL, 10);
1089 if (!port || port > 65535)
1093 if (!strncmp("rdma_write", modep, strlen(modep)) ||
1094 !strncmp("RDMA_WRITE", modep, strlen(modep)))
1095 rd->rdma_protocol = FIO_RDMA_MEM_WRITE;
1096 else if (!strncmp("rdma_read", modep, strlen(modep)) ||
1097 !strncmp("RDMA_READ", modep, strlen(modep)))
1098 rd->rdma_protocol = FIO_RDMA_MEM_READ;
1099 else if (!strncmp("send", modep, strlen(modep)) ||
1100 !strncmp("SEND", modep, strlen(modep)))
1101 rd->rdma_protocol = FIO_RDMA_CHA_SEND;
1105 rd->rdma_protocol = FIO_RDMA_MEM_WRITE;
1107 rd->cq_event_num = 0;
1109 rd->cm_channel = rdma_create_event_channel();
1110 if (!rd->cm_channel) {
1111 log_err("fio: rdma_create_event_channel fail\n");
1115 ret = rdma_create_id(rd->cm_channel, &rd->cm_id, rd, RDMA_PS_TCP);
1117 log_err("fio: rdma_create_id fail\n");
1121 if ((rd->rdma_protocol == FIO_RDMA_MEM_WRITE) ||
1122 (rd->rdma_protocol == FIO_RDMA_MEM_READ)) {
1124 malloc(FIO_RDMA_MAX_IO_DEPTH * sizeof(struct remote_u));
1125 memset(rd->rmt_us, 0,
1126 FIO_RDMA_MAX_IO_DEPTH * sizeof(struct remote_u));
1130 rd->io_us_queued = malloc(td->o.iodepth * sizeof(struct io_u *));
1131 memset(rd->io_us_queued, 0, td->o.iodepth * sizeof(struct io_u *));
1132 rd->io_u_queued_nr = 0;
1134 rd->io_us_flight = malloc(td->o.iodepth * sizeof(struct io_u *));
1135 memset(rd->io_us_flight, 0, td->o.iodepth * sizeof(struct io_u *));
1136 rd->io_u_flight_nr = 0;
1138 rd->io_us_completed = malloc(td->o.iodepth * sizeof(struct io_u *));
1139 memset(rd->io_us_completed, 0, td->o.iodepth * sizeof(struct io_u *));
1140 rd->io_u_completed_nr = 0;
1142 if (td_read(td)) { /* READ as the server */
1144 /* server rd->rdma_buf_len will be setup after got request */
1145 ret = fio_rdmaio_setup_listen(td, port);
1146 } else { /* WRITE as the client */
1148 ret = fio_rdmaio_setup_connect(td, host, port);
1151 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
1152 /* register each io_u in the free list */
1153 flist_for_each(entry, &td->io_u_freelist) {
1154 struct io_u *io_u = flist_entry(entry, struct io_u, list);
1156 io_u->engine_data = malloc(sizeof(struct rdma_io_u_data));
1157 memset(io_u->engine_data, 0, sizeof(struct rdma_io_u_data));
1158 ((struct rdma_io_u_data *)io_u->engine_data)->wr_id = i;
1160 io_u->mr = ibv_reg_mr(rd->pd, io_u->buf, max_bs,
1161 IBV_ACCESS_LOCAL_WRITE |
1162 IBV_ACCESS_REMOTE_READ |
1163 IBV_ACCESS_REMOTE_WRITE);
1164 if (io_u->mr == NULL) {
1165 log_err("fio: ibv_reg_mr io_u failed\n");
1169 rd->send_buf.rmt_us[i].buf =
1170 htonll((uint64_t) (unsigned long)io_u->buf);
1171 rd->send_buf.rmt_us[i].rkey = htonl(io_u->mr->rkey);
1172 rd->send_buf.rmt_us[i].size = htonl(max_bs);
1175 log_info("fio: Send rkey %x addr %" PRIx64 " len %d to client\n", io_u->mr->rkey, io_u->buf, max_bs); */
1180 rd->send_buf.nr = htonl(i);
1184 log_err("fio: bad rdma host/port/protocol: %s\n", td->o.filename);
1188 static void fio_rdmaio_cleanup(struct thread_data *td)
1190 struct rdmaio_data *rd = td->io_ops->data;
1196 static int fio_rdmaio_setup(struct thread_data *td)
1198 struct rdmaio_data *rd;
1200 if (!td->io_ops->data) {
1201 rd = malloc(sizeof(*rd));
1203 memset(rd, 0, sizeof(*rd));
1204 init_rand_seed(&rd->rand_state, (unsigned int) GOLDEN_RATIO_PRIME);
1205 td->io_ops->data = rd;
1211 static struct ioengine_ops ioengine_rw = {
1213 .version = FIO_IOOPS_VERSION,
1214 .setup = fio_rdmaio_setup,
1215 .init = fio_rdmaio_init,
1216 .prep = fio_rdmaio_prep,
1217 .queue = fio_rdmaio_queue,
1218 .commit = fio_rdmaio_commit,
1219 .getevents = fio_rdmaio_getevents,
1220 .event = fio_rdmaio_event,
1221 .cleanup = fio_rdmaio_cleanup,
1222 .open_file = fio_rdmaio_open_file,
1223 .close_file = fio_rdmaio_close_file,
1224 .flags = FIO_DISKLESSIO | FIO_UNIDIR | FIO_PIPEIO,
1227 #else /* FIO_HAVE_RDMA */
1229 static int fio_rdmaio_open_file(struct thread_data *td, struct fio_file *f)
1234 static int fio_rdmaio_close_file(struct thread_data *td, struct fio_file *f)
1239 static int fio_rdmaio_queue(struct thread_data *td, struct io_u *io_u)
1241 return FIO_Q_COMPLETED;
1244 static int fio_rdmaio_init(struct thread_data fio_unused * td)
1246 log_err("fio: rdma(librdmacm libibverbs) not available\n");
1247 log_err(" You haven't compiled rdma ioengine into fio.\n");
1248 log_err(" If you want to try rdma ioengine,\n");
1249 log_err(" make sure OFED is installed,\n");
1250 log_err(" $ ofed_info\n");
1251 log_err(" then try to make fio as follows:\n");
1252 log_err(" $ export EXTFLAGS+=\" -DFIO_HAVE_RDMA \"\n");
1253 log_err(" $ export EXTLIBS+=\" -libverbs -lrdmacm \"\n");
1254 log_err(" $ make clean && make\n");
1258 static struct ioengine_ops ioengine_rw = {
1260 .version = FIO_IOOPS_VERSION,
1261 .init = fio_rdmaio_init,
1262 .queue = fio_rdmaio_queue,
1263 .open_file = fio_rdmaio_open_file,
1264 .close_file = fio_rdmaio_close_file,
1265 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR | FIO_PIPEIO,
1270 static void fio_init fio_rdmaio_register(void)
1272 register_ioengine(&ioengine_rw);
1275 static void fio_exit fio_rdmaio_unregister(void)
1277 unregister_ioengine(&ioengine_rw);