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);
328 if (rd->pd == NULL) {
329 log_err("fio: ibv_alloc_pd fail\n");
333 if (rd->is_client == 0)
334 rd->channel = ibv_create_comp_channel(rd->child_cm_id->verbs);
336 rd->channel = ibv_create_comp_channel(rd->cm_id->verbs);
337 if (rd->channel == NULL) {
338 log_err("fio: ibv_create_comp_channel fail\n");
345 if (rd->is_client == 0)
346 rd->cq = ibv_create_cq(rd->child_cm_id->verbs,
347 qp_depth, rd, rd->channel, 0);
349 rd->cq = ibv_create_cq(rd->cm_id->verbs,
350 qp_depth, rd, rd->channel, 0);
351 if (rd->cq == NULL) {
352 log_err("fio: ibv_create_cq failed\n");
356 if (ibv_req_notify_cq(rd->cq, 0) != 0) {
357 log_err("fio: ibv_create_cq failed\n");
361 /* create queue pair */
362 memset(&init_attr, 0, sizeof(init_attr));
363 init_attr.cap.max_send_wr = qp_depth;
364 init_attr.cap.max_recv_wr = qp_depth;
365 init_attr.cap.max_recv_sge = 1;
366 init_attr.cap.max_send_sge = 1;
367 init_attr.qp_type = IBV_QPT_RC;
368 init_attr.send_cq = rd->cq;
369 init_attr.recv_cq = rd->cq;
371 if (rd->is_client == 0) {
372 if (rdma_create_qp(rd->child_cm_id, rd->pd, &init_attr) != 0) {
373 log_err("fio: rdma_create_qp failed\n");
376 rd->qp = rd->child_cm_id->qp;
378 if (rdma_create_qp(rd->cm_id, rd->pd, &init_attr) != 0) {
379 log_err("fio: rdma_create_qp failed\n");
382 rd->qp = rd->cm_id->qp;
388 ibv_destroy_cq(rd->cq);
390 ibv_destroy_comp_channel(rd->channel);
392 ibv_dealloc_pd(rd->pd);
397 static int fio_rdmaio_setup_control_msg_buffers(struct thread_data *td)
399 struct rdmaio_data *rd = td->io_ops->data;
401 rd->recv_mr = ibv_reg_mr(rd->pd, &rd->recv_buf, sizeof(rd->recv_buf),
402 IBV_ACCESS_LOCAL_WRITE);
403 if (rd->recv_mr == NULL) {
404 log_err("fio: recv_buf reg_mr failed\n");
408 rd->send_mr = ibv_reg_mr(rd->pd, &rd->send_buf, sizeof(rd->send_buf),
410 if (rd->send_mr == NULL) {
411 log_err("fio: send_buf reg_mr failed\n");
412 ibv_dereg_mr(rd->recv_mr);
416 /* setup work request */
418 rd->recv_sgl.addr = (uint64_t) (unsigned long)&rd->recv_buf;
419 rd->recv_sgl.length = sizeof rd->recv_buf;
420 rd->recv_sgl.lkey = rd->recv_mr->lkey;
421 rd->rq_wr.sg_list = &rd->recv_sgl;
422 rd->rq_wr.num_sge = 1;
423 rd->rq_wr.wr_id = FIO_RDMA_MAX_IO_DEPTH;
426 rd->send_sgl.addr = (uint64_t) (unsigned long)&rd->send_buf;
427 rd->send_sgl.length = sizeof rd->send_buf;
428 rd->send_sgl.lkey = rd->send_mr->lkey;
430 rd->sq_wr.opcode = IBV_WR_SEND;
431 rd->sq_wr.send_flags = IBV_SEND_SIGNALED;
432 rd->sq_wr.sg_list = &rd->send_sgl;
433 rd->sq_wr.num_sge = 1;
434 rd->sq_wr.wr_id = FIO_RDMA_MAX_IO_DEPTH;
439 static int get_next_channel_event(struct thread_data *td,
440 struct rdma_event_channel *channel,
441 enum rdma_cm_event_type wait_event)
443 struct rdmaio_data *rd = td->io_ops->data;
446 struct rdma_cm_event *event;
448 ret = rdma_get_cm_event(channel, &event);
450 log_err("fio: rdma_get_cm_event");
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;
539 enum ibv_wc_opcode comp_opcode;
540 comp_opcode = IBV_WC_RDMA_WRITE;
541 struct ibv_cq *ev_cq;
547 switch (rd->rdma_protocol) {
548 case FIO_RDMA_MEM_WRITE:
549 comp_opcode = IBV_WC_RDMA_WRITE;
551 case FIO_RDMA_MEM_READ:
552 comp_opcode = IBV_WC_RDMA_READ;
554 case FIO_RDMA_CHA_SEND:
555 comp_opcode = IBV_WC_SEND;
557 case FIO_RDMA_CHA_RECV:
558 comp_opcode = IBV_WC_RECV;
561 log_err("fio: unknown rdma protocol - %d\n", rd->rdma_protocol);
565 if (rd->cq_event_num > 0) { /* previous left */
571 if (ibv_get_cq_event(rd->channel, &ev_cq, &ev_ctx) != 0) {
572 log_err("fio: Failed to get cq event!\n");
575 if (ev_cq != rd->cq) {
576 log_err("fio: Unknown CQ!\n");
579 if (ibv_req_notify_cq(rd->cq, 0) != 0) {
580 log_err("fio: Failed to set notify!\n");
584 ret = cq_event_handler(td, comp_opcode);
588 ibv_ack_cq_events(rd->cq, ret);
594 rd->cq_event_num -= r;
599 static int fio_rdmaio_send(struct thread_data *td, struct io_u **io_us,
602 struct rdmaio_data *rd = td->io_ops->data;
603 struct ibv_send_wr *bad_wr;
605 enum ibv_wc_opcode comp_opcode;
606 comp_opcode = IBV_WC_RDMA_WRITE;
610 struct rdma_io_u_data *r_io_u_d;
614 for (i = 0; i < nr; i++) {
615 /* RDMA_WRITE or RDMA_READ */
616 switch (rd->rdma_protocol) {
617 case FIO_RDMA_MEM_WRITE:
618 /* compose work request */
619 r_io_u_d = io_us[i]->engine_data;
620 if (td->o.use_os_rand)
621 index = os_random_long(&td->random_state) % rd->rmt_nr;
623 index = __rand(&rd->rand_state) % rd->rmt_nr;
624 r_io_u_d->sq_wr.opcode = IBV_WR_RDMA_WRITE;
625 r_io_u_d->sq_wr.wr.rdma.rkey = rd->rmt_us[index].rkey;
626 r_io_u_d->sq_wr.wr.rdma.remote_addr = \
627 rd->rmt_us[index].buf;
628 r_io_u_d->sq_wr.sg_list->length = io_us[i]->buflen;
630 case FIO_RDMA_MEM_READ:
631 /* compose work request */
632 r_io_u_d = io_us[i]->engine_data;
633 if (td->o.use_os_rand)
634 index = os_random_long(&td->random_state) % rd->rmt_nr;
636 index = __rand(&rd->rand_state) % rd->rmt_nr;
637 r_io_u_d->sq_wr.opcode = IBV_WR_RDMA_READ;
638 r_io_u_d->sq_wr.wr.rdma.rkey = rd->rmt_us[index].rkey;
639 r_io_u_d->sq_wr.wr.rdma.remote_addr = \
640 rd->rmt_us[index].buf;
641 r_io_u_d->sq_wr.sg_list->length = io_us[i]->buflen;
643 case FIO_RDMA_CHA_SEND:
644 r_io_u_d = io_us[i]->engine_data;
645 r_io_u_d->sq_wr.opcode = IBV_WR_SEND;
646 r_io_u_d->sq_wr.send_flags = IBV_SEND_SIGNALED;
649 log_err("fio: unknown rdma protocol - %d\n",
654 if (ibv_post_send(rd->qp, &r_io_u_d->sq_wr, &bad_wr) != 0) {
655 log_err("fio: ibv_post_send fail\n");
659 dprint_io_u(io_us[i], "fio_rdmaio_send");
662 /* wait for completion
663 rdma_poll_wait(td, comp_opcode); */
668 static int fio_rdmaio_recv(struct thread_data *td, struct io_u **io_us,
671 struct rdmaio_data *rd = td->io_ops->data;
672 struct ibv_recv_wr *bad_wr;
673 struct rdma_io_u_data *r_io_u_d;
677 if (rd->rdma_protocol == FIO_RDMA_CHA_RECV) {
678 /* post io_u into recv queue */
679 for (i = 0; i < nr; i++) {
680 r_io_u_d = io_us[i]->engine_data;
681 if (ibv_post_recv(rd->qp, &r_io_u_d->rq_wr, &bad_wr) !=
683 log_err("fio: ibv_post_recv fail\n");
687 } else if ((rd->rdma_protocol == FIO_RDMA_MEM_READ)
688 || (rd->rdma_protocol == FIO_RDMA_MEM_WRITE)) {
689 /* re-post the rq_wr */
690 if (ibv_post_recv(rd->qp, &rd->rq_wr, &bad_wr) != 0) {
691 log_err("fio: ibv_post_recv fail\n");
695 rdma_poll_wait(td, IBV_WC_RECV);
697 dprint(FD_IO, "fio: recv FINISH message\n");
705 static int fio_rdmaio_queue(struct thread_data *td, struct io_u *io_u)
707 struct rdmaio_data *rd = td->io_ops->data;
709 fio_ro_check(td, io_u);
711 if (rd->io_u_queued_nr == (int)td->o.iodepth)
714 rd->io_us_queued[rd->io_u_queued_nr] = io_u;
715 rd->io_u_queued_nr++;
717 dprint_io_u(io_u, "fio_rdmaio_queue");
722 static void fio_rdmaio_queued(struct thread_data *td, struct io_u **io_us,
725 struct rdmaio_data *rd = td->io_ops->data;
729 if (!fio_fill_issue_time(td))
732 fio_gettime(&now, NULL);
734 for (i = 0; i < nr; i++) {
735 struct io_u *io_u = io_us[i];
737 /* queued -> flight */
738 rd->io_us_flight[rd->io_u_flight_nr] = io_u;
739 rd->io_u_flight_nr++;
741 memcpy(&io_u->issue_time, &now, sizeof(now));
742 io_u_queued(td, io_u);
746 static int fio_rdmaio_commit(struct thread_data *td)
748 struct rdmaio_data *rd = td->io_ops->data;
752 if (!rd->io_us_queued)
755 io_us = rd->io_us_queued;
757 /* RDMA_WRITE or RDMA_READ */
759 ret = fio_rdmaio_send(td, io_us, rd->io_u_queued_nr);
760 } else if (!rd->is_client) {
761 ret = fio_rdmaio_recv(td, io_us, rd->io_u_queued_nr);
763 ret = 0; /* must be a SYNC */
766 fio_rdmaio_queued(td, io_us, ret);
767 io_u_mark_submit(td, ret);
768 rd->io_u_queued_nr -= ret;
773 } while (rd->io_u_queued_nr);
778 static int fio_rdmaio_connect(struct thread_data *td, struct fio_file *f)
780 struct rdmaio_data *rd = td->io_ops->data;
781 struct rdma_conn_param conn_param;
782 struct ibv_send_wr *bad_wr;
784 memset(&conn_param, 0, sizeof conn_param);
785 conn_param.responder_resources = 1;
786 conn_param.initiator_depth = 1;
787 conn_param.retry_count = 10;
789 if (rdma_connect(rd->cm_id, &conn_param) != 0) {
790 log_err("fio: rdma_connect fail\n");
794 if (get_next_channel_event
795 (td, rd->cm_channel, RDMA_CM_EVENT_ESTABLISHED) != 0) {
796 log_err("fio: wait for RDMA_CM_EVENT_ESTABLISHED\n");
800 /* send task request */
801 rd->send_buf.mode = htonl(rd->rdma_protocol);
802 rd->send_buf.nr = htonl(td->o.iodepth);
804 if (ibv_post_send(rd->qp, &rd->sq_wr, &bad_wr) != 0) {
805 log_err("fio: ibv_post_send fail");
809 rdma_poll_wait(td, IBV_WC_SEND);
811 /* wait for remote MR info from server side */
812 rdma_poll_wait(td, IBV_WC_RECV);
814 /* In SEND/RECV test, it's a good practice to setup the iodepth of
815 * of the RECV side deeper than that of the SEND side to
816 * avoid RNR (receiver not ready) error. The
817 * SEND side may send so many unsolicited message before
818 * RECV side commits sufficient recv buffers into recv queue.
819 * This may lead to RNR error. Here, SEND side pauses for a while
820 * during which RECV side commits sufficient recv buffers.
827 static int fio_rdmaio_accept(struct thread_data *td, struct fio_file *f)
829 struct rdmaio_data *rd = td->io_ops->data;
830 struct rdma_conn_param conn_param;
831 struct ibv_send_wr *bad_wr;
833 /* rdma_accept() - then wait for accept success */
834 memset(&conn_param, 0, sizeof conn_param);
835 conn_param.responder_resources = 1;
836 conn_param.initiator_depth = 1;
838 if (rdma_accept(rd->child_cm_id, &conn_param) != 0) {
839 log_err("fio: rdma_accept\n");
843 if (get_next_channel_event
844 (td, rd->cm_channel, RDMA_CM_EVENT_ESTABLISHED) != 0) {
845 log_err("fio: wait for RDMA_CM_EVENT_ESTABLISHED\n");
849 /* wait for request */
850 rdma_poll_wait(td, IBV_WC_RECV);
852 if (ibv_post_send(rd->qp, &rd->sq_wr, &bad_wr) != 0) {
853 log_err("fio: ibv_post_send fail");
857 rdma_poll_wait(td, IBV_WC_SEND);
862 static int fio_rdmaio_open_file(struct thread_data *td, struct fio_file *f)
865 return fio_rdmaio_accept(td, f);
867 return fio_rdmaio_connect(td, f);
870 static int fio_rdmaio_close_file(struct thread_data *td, struct fio_file *f)
872 struct rdmaio_data *rd = td->io_ops->data;
873 struct ibv_send_wr *bad_wr;
875 /* unregister rdma buffer */
878 * Client sends notification to the server side
880 /* refer to: http://linux.die.net/man/7/rdma_cm */
881 if ((rd->is_client == 1) && ((rd->rdma_protocol == FIO_RDMA_MEM_WRITE)
882 || (rd->rdma_protocol ==
883 FIO_RDMA_MEM_READ))) {
884 if (ibv_post_send(rd->qp, &rd->sq_wr, &bad_wr) != 0) {
885 log_err("fio: ibv_post_send fail");
889 dprint(FD_IO, "fio: close infomation sent success\n");
890 rdma_poll_wait(td, IBV_WC_SEND);
893 if (rd->is_client == 1)
894 rdma_disconnect(rd->cm_id);
896 rdma_disconnect(rd->child_cm_id);
897 /* rdma_disconnect(rd->cm_id); */
900 /* if (get_next_channel_event(td, rd->cm_channel, RDMA_CM_EVENT_DISCONNECTED) != 0)
902 log_err("fio: wait for RDMA_CM_EVENT_DISCONNECTED\n");
906 ibv_destroy_cq(rd->cq);
907 ibv_destroy_qp(rd->qp);
909 if (rd->is_client == 1)
910 rdma_destroy_id(rd->cm_id);
912 rdma_destroy_id(rd->child_cm_id);
913 rdma_destroy_id(rd->cm_id);
916 ibv_destroy_comp_channel(rd->channel);
917 ibv_dealloc_pd(rd->pd);
922 static int fio_rdmaio_setup_connect(struct thread_data *td, const char *host,
925 struct rdmaio_data *rd = td->io_ops->data;
926 struct ibv_recv_wr *bad_wr;
928 rd->addr.sin_family = AF_INET;
929 rd->addr.sin_port = htons(port);
931 if (inet_aton(host, &rd->addr.sin_addr) != 1) {
932 struct hostent *hent;
934 hent = gethostbyname(host);
936 td_verror(td, errno, "gethostbyname");
940 memcpy(&rd->addr.sin_addr, hent->h_addr, 4);
944 if (rdma_resolve_addr(rd->cm_id, NULL,
945 (struct sockaddr *)&rd->addr, 2000) != 0) {
946 log_err("fio: rdma_resolve_addr");
950 if (get_next_channel_event
951 (td, rd->cm_channel, RDMA_CM_EVENT_ADDR_RESOLVED)
953 log_err("fio: get_next_channel_event");
958 if (rdma_resolve_route(rd->cm_id, 2000) != 0) {
959 log_err("fio: rdma_resolve_route");
963 if (get_next_channel_event
964 (td, rd->cm_channel, RDMA_CM_EVENT_ROUTE_RESOLVED) != 0) {
965 log_err("fio: get_next_channel_event");
969 /* create qp and buffer */
970 if (fio_rdmaio_setup_qp(td) != 0)
973 if (fio_rdmaio_setup_control_msg_buffers(td) != 0)
977 if (ibv_post_recv(rd->qp, &rd->rq_wr, &bad_wr) != 0) {
978 log_err("fio: ibv_post_recv fail\n");
985 static int fio_rdmaio_setup_listen(struct thread_data *td, short port)
987 struct rdmaio_data *rd = td->io_ops->data;
988 struct ibv_recv_wr *bad_wr;
990 rd->addr.sin_family = AF_INET;
991 rd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
992 rd->addr.sin_port = htons(port);
995 if (rdma_bind_addr(rd->cm_id, (struct sockaddr *)&rd->addr) != 0) {
996 log_err("fio: rdma_bind_addr fail\n");
1000 if (rdma_listen(rd->cm_id, 3) != 0) {
1001 log_err("fio: rdma_listen fail\n");
1005 /* wait for CONNECT_REQUEST */
1006 if (get_next_channel_event
1007 (td, rd->cm_channel, RDMA_CM_EVENT_CONNECT_REQUEST) != 0) {
1008 log_err("fio: wait for RDMA_CM_EVENT_CONNECT_REQUEST\n");
1012 if (fio_rdmaio_setup_qp(td) != 0)
1015 if (fio_rdmaio_setup_control_msg_buffers(td) != 0)
1019 if (ibv_post_recv(rd->qp, &rd->rq_wr, &bad_wr) != 0) {
1020 log_err("fio: ibv_post_recv fail\n");
1027 static int fio_rdmaio_init(struct thread_data *td)
1029 struct rdmaio_data *rd = td->io_ops->data;
1031 char host[64], buf[128];
1032 char *sep, *portp, *modep;
1037 log_err("fio: rdma connections must be read OR write\n");
1040 if (td_random(td)) {
1041 log_err("fio: RDMA network IO can't be random\n");
1045 /* check RLIMIT_MEMLOCK */
1046 if (getrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
1047 log_err("fio: getrlimit fail: %d(%s)\n",
1048 errno, strerror(errno));
1053 if ((rl.rlim_cur != RLIM_INFINITY)
1054 && (rl.rlim_cur < td->orig_buffer_size)) {
1055 log_err("fio: soft RLIMIT_MEMLOCK is: %" PRId64 "\n",
1057 log_err("fio: total block size is: %zd\n",
1058 td->orig_buffer_size);
1059 /* try to set larger RLIMIT_MEMLOCK */
1060 rl.rlim_cur = rl.rlim_max;
1061 if (setrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
1062 log_err("fio: setrlimit fail: %d(%s)\n",
1063 errno, strerror(errno));
1064 log_err("fio: you may try enlarge MEMLOCK by root\n");
1065 log_err("# ulimit -l unlimited\n");
1070 strcpy(buf, td->o.filename);
1072 sep = strchr(buf, '/');
1084 sep = strchr(portp, '/');
1090 port = strtol(portp, NULL, 10);
1091 if (!port || port > 65535)
1095 if (!strncmp("rdma_write", modep, strlen(modep)) ||
1096 !strncmp("RDMA_WRITE", modep, strlen(modep)))
1097 rd->rdma_protocol = FIO_RDMA_MEM_WRITE;
1098 else if (!strncmp("rdma_read", modep, strlen(modep)) ||
1099 !strncmp("RDMA_READ", modep, strlen(modep)))
1100 rd->rdma_protocol = FIO_RDMA_MEM_READ;
1101 else if (!strncmp("send", modep, strlen(modep)) ||
1102 !strncmp("SEND", modep, strlen(modep)))
1103 rd->rdma_protocol = FIO_RDMA_CHA_SEND;
1107 rd->rdma_protocol = FIO_RDMA_MEM_WRITE;
1109 rd->cq_event_num = 0;
1111 rd->cm_channel = rdma_create_event_channel();
1112 if (!rd->cm_channel) {
1113 log_err("fio: rdma_create_event_channel fail\n");
1117 ret = rdma_create_id(rd->cm_channel, &rd->cm_id, rd, RDMA_PS_TCP);
1119 log_err("fio: rdma_create_id fail\n");
1123 if ((rd->rdma_protocol == FIO_RDMA_MEM_WRITE) ||
1124 (rd->rdma_protocol == FIO_RDMA_MEM_READ)) {
1126 malloc(FIO_RDMA_MAX_IO_DEPTH * sizeof(struct remote_u));
1127 memset(rd->rmt_us, 0,
1128 FIO_RDMA_MAX_IO_DEPTH * sizeof(struct remote_u));
1132 rd->io_us_queued = malloc(td->o.iodepth * sizeof(struct io_u *));
1133 memset(rd->io_us_queued, 0, td->o.iodepth * sizeof(struct io_u *));
1134 rd->io_u_queued_nr = 0;
1136 rd->io_us_flight = malloc(td->o.iodepth * sizeof(struct io_u *));
1137 memset(rd->io_us_flight, 0, td->o.iodepth * sizeof(struct io_u *));
1138 rd->io_u_flight_nr = 0;
1140 rd->io_us_completed = malloc(td->o.iodepth * sizeof(struct io_u *));
1141 memset(rd->io_us_completed, 0, td->o.iodepth * sizeof(struct io_u *));
1142 rd->io_u_completed_nr = 0;
1144 if (td_read(td)) { /* READ as the server */
1146 /* server rd->rdma_buf_len will be setup after got request */
1147 ret = fio_rdmaio_setup_listen(td, port);
1148 } else { /* WRITE as the client */
1150 ret = fio_rdmaio_setup_connect(td, host, port);
1153 struct flist_head *entry;
1154 unsigned int max_bs;
1155 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
1156 /* register each io_u in the free list */
1158 flist_for_each(entry, &td->io_u_freelist) {
1159 struct io_u *io_u = flist_entry(entry, struct io_u, list);
1161 io_u->engine_data = malloc(sizeof(struct rdma_io_u_data));
1162 memset(io_u->engine_data, 0, sizeof(struct rdma_io_u_data));
1163 ((struct rdma_io_u_data *)io_u->engine_data)->wr_id = i;
1165 io_u->mr = ibv_reg_mr(rd->pd, io_u->buf, max_bs,
1166 IBV_ACCESS_LOCAL_WRITE |
1167 IBV_ACCESS_REMOTE_READ |
1168 IBV_ACCESS_REMOTE_WRITE);
1169 if (io_u->mr == NULL) {
1170 log_err("fio: ibv_reg_mr io_u failed\n");
1174 rd->send_buf.rmt_us[i].buf =
1175 htonll((uint64_t) (unsigned long)io_u->buf);
1176 rd->send_buf.rmt_us[i].rkey = htonl(io_u->mr->rkey);
1177 rd->send_buf.rmt_us[i].size = htonl(max_bs);
1179 /* log_info("fio: Send rkey %x addr %" PRIx64 " len %d to client\n",
1180 io_u->mr->rkey, io_u->buf, max_bs); */
1184 rd->send_buf.nr = htonl(i);
1188 log_err("fio: bad rdma host/port/protocol: %s\n", td->o.filename);
1192 static void fio_rdmaio_cleanup(struct thread_data *td)
1194 struct rdmaio_data *rd = td->io_ops->data;
1197 /* if (nd->listenfd != -1)
1198 close(nd->listenfd);
1199 if (nd->pipes[0] != -1)
1200 close(nd->pipes[0]);
1201 if (nd->pipes[1] != -1)
1202 close(nd->pipes[1]);
1208 static int fio_rdmaio_setup(struct thread_data *td)
1210 struct rdmaio_data *rd;
1212 if (!td->io_ops->data) {
1213 rd = malloc(sizeof(*rd));;
1215 memset(rd, 0, sizeof(*rd));
1216 init_rand_seed(&rd->rand_state, (unsigned int) GOLDEN_RATIO_PRIME);
1217 td->io_ops->data = rd;
1223 static struct ioengine_ops ioengine_rw = {
1225 .version = FIO_IOOPS_VERSION,
1226 .setup = fio_rdmaio_setup,
1227 .init = fio_rdmaio_init,
1228 .prep = fio_rdmaio_prep,
1229 .queue = fio_rdmaio_queue,
1230 .commit = fio_rdmaio_commit,
1231 .getevents = fio_rdmaio_getevents,
1232 .event = fio_rdmaio_event,
1233 .cleanup = fio_rdmaio_cleanup,
1234 .open_file = fio_rdmaio_open_file,
1235 .close_file = fio_rdmaio_close_file,
1236 .flags = FIO_DISKLESSIO | FIO_UNIDIR | FIO_PIPEIO,
1239 #else /* FIO_HAVE_RDMA */
1241 static int fio_rdmaio_open_file(struct thread_data *td, struct fio_file *f)
1246 static int fio_rdmaio_close_file(struct thread_data *td, struct fio_file *f)
1251 static int fio_rdmaio_queue(struct thread_data *td, struct io_u *io_u)
1253 return FIO_Q_COMPLETED;
1256 static int fio_rdmaio_init(struct thread_data fio_unused * td)
1258 log_err("fio: rdma(librdmacm libibverbs) not available\n");
1259 log_err(" You haven't compiled rdma ioengine into fio.\n");
1260 log_err(" If you want to try rdma ioengine,\n");
1261 log_err(" make sure OFED is installed,\n");
1262 log_err(" $ ofed_info\n");
1263 log_err(" then try to make fio as follows:\n");
1264 log_err(" $ export EXTFLAGS+=\" -DFIO_HAVE_RDMA \"\n");
1265 log_err(" $ export EXTLIBS+=\" -libverbs -lrdmacm \"\n");
1266 log_err(" $ make clean && make\n");
1270 static struct ioengine_ops ioengine_rw = {
1272 .version = FIO_IOOPS_VERSION,
1273 .init = fio_rdmaio_init,
1274 .queue = fio_rdmaio_queue,
1275 .open_file = fio_rdmaio_open_file,
1276 .close_file = fio_rdmaio_close_file,
1277 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR | FIO_PIPEIO,
1282 static void fio_init fio_rdmaio_register(void)
1284 register_ioengine(&ioengine_rw);
1287 static void fio_exit fio_rdmaio_unregister(void)
1289 unregister_ioengine(&ioengine_rw);