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>
51 #include <rdma/rdma_cma.h>
52 #include <infiniband/arch.h>
54 #define FIO_RDMA_MAX_IO_DEPTH 512
70 struct rdma_info_blk {
71 uint32_t mode; /* channel semantic or memory semantic */
72 uint32_t nr; /* client: io depth
73 server: number of records for memory semantic
75 struct remote_u rmt_us[FIO_RDMA_MAX_IO_DEPTH];
78 struct rdma_io_u_data {
80 struct ibv_send_wr sq_wr;
81 struct ibv_recv_wr rq_wr;
82 struct ibv_sge rdma_sgl;
87 enum rdma_io_mode rdma_protocol;
89 struct sockaddr_in addr;
91 struct ibv_recv_wr rq_wr;
92 struct ibv_sge recv_sgl;
93 struct rdma_info_blk recv_buf;
94 struct ibv_mr *recv_mr;
96 struct ibv_send_wr sq_wr;
97 struct ibv_sge send_sgl;
98 struct rdma_info_blk send_buf;
99 struct ibv_mr *send_mr;
101 struct ibv_comp_channel *channel;
107 struct rdma_event_channel *cm_channel;
108 struct rdma_cm_id *cm_id;
109 struct rdma_cm_id *child_cm_id;
113 struct remote_u *rmt_us;
115 struct io_u **io_us_queued;
117 struct io_u **io_us_flight;
119 struct io_u **io_us_completed;
120 int io_u_completed_nr;
122 struct frand_state rand_state;
125 static int client_recv(struct thread_data *td, struct ibv_wc *wc)
127 struct rdmaio_data *rd = td->io_ops->data;
129 if (wc->byte_len != sizeof(rd->recv_buf)) {
130 log_err("Received bogus data, size %d\n", wc->byte_len);
134 /* store mr info for MEMORY semantic */
135 if ((rd->rdma_protocol == FIO_RDMA_MEM_WRITE) ||
136 (rd->rdma_protocol == FIO_RDMA_MEM_READ)) {
137 /* struct flist_head *entry; */
140 rd->rmt_nr = ntohl(rd->recv_buf.nr);
142 for (i = 0; i < rd->rmt_nr; i++) {
143 rd->rmt_us[i].buf = ntohll(rd->recv_buf.rmt_us[i].buf);
144 rd->rmt_us[i].rkey = ntohl(rd->recv_buf.rmt_us[i].rkey);
145 rd->rmt_us[i].size = ntohl(rd->recv_buf.rmt_us[i].size);
148 "fio: Received rkey %x addr %" PRIx64
149 " len %d from peer\n", rd->rmt_us[i].rkey,
150 rd->rmt_us[i].buf, rd->rmt_us[i].size);
157 static int server_recv(struct thread_data *td, struct ibv_wc *wc)
159 struct rdmaio_data *rd = td->io_ops->data;
161 if (wc->wr_id == FIO_RDMA_MAX_IO_DEPTH) {
162 rd->rdma_protocol = ntohl(rd->recv_buf.mode);
164 /* CHANNEL semantic, do nothing */
165 if (rd->rdma_protocol == FIO_RDMA_CHA_SEND)
166 rd->rdma_protocol = FIO_RDMA_CHA_RECV;
172 static int cq_event_handler(struct thread_data *td, enum ibv_wc_opcode opcode)
174 struct rdmaio_data *rd = td->io_ops->data;
176 struct rdma_io_u_data *r_io_u_d;
181 while ((ret = ibv_poll_cq(rd->cq, 1, &wc)) == 1) {
186 log_err("fio: cq completion status %d(%s)\n",
187 wc.status, ibv_wc_status_str(wc.status));
194 if (rd->is_client == 1)
195 client_recv(td, &wc);
197 server_recv(td, &wc);
199 if (wc.wr_id == FIO_RDMA_MAX_IO_DEPTH)
202 for (i = 0; i < rd->io_u_flight_nr; i++) {
203 r_io_u_d = rd->io_us_flight[i]->engine_data;
205 if (wc.wr_id == r_io_u_d->rq_wr.wr_id) {
206 rd->io_us_flight[i]->resid =
207 rd->io_us_flight[i]->buflen
210 rd->io_us_flight[i]->error = 0;
212 rd->io_us_completed[rd->
214 = rd->io_us_flight[i];
215 rd->io_u_completed_nr++;
219 if (i == rd->io_u_flight_nr)
220 log_err("fio: recv wr %" PRId64 " not found\n",
223 /* put the last one into middle of the list */
224 rd->io_us_flight[i] =
225 rd->io_us_flight[rd->io_u_flight_nr - 1];
226 rd->io_u_flight_nr--;
232 case IBV_WC_RDMA_WRITE:
233 case IBV_WC_RDMA_READ:
234 if (wc.wr_id == FIO_RDMA_MAX_IO_DEPTH)
237 for (i = 0; i < rd->io_u_flight_nr; i++) {
238 r_io_u_d = rd->io_us_flight[i]->engine_data;
240 if (wc.wr_id == r_io_u_d->sq_wr.wr_id) {
241 rd->io_us_completed[rd->
243 = rd->io_us_flight[i];
244 rd->io_u_completed_nr++;
248 if (i == rd->io_u_flight_nr)
249 log_err("fio: send wr %" PRId64 " not found\n",
252 /* put the last one into middle of the list */
253 rd->io_us_flight[i] =
254 rd->io_us_flight[rd->io_u_flight_nr - 1];
255 rd->io_u_flight_nr--;
261 log_info("fio: unknown completion event %d\n",
268 log_err("fio: poll error %d\n", ret);
276 * Return -1 for error and 'nr events' for a positive number
279 static int rdma_poll_wait(struct thread_data *td, enum ibv_wc_opcode opcode)
281 struct rdmaio_data *rd = td->io_ops->data;
282 struct ibv_cq *ev_cq;
286 if (rd->cq_event_num > 0) { /* previous left */
292 if (ibv_get_cq_event(rd->channel, &ev_cq, &ev_ctx) != 0) {
293 log_err("fio: Failed to get cq event!\n");
296 if (ev_cq != rd->cq) {
297 log_err("fio: Unknown CQ!\n");
300 if (ibv_req_notify_cq(rd->cq, 0) != 0) {
301 log_err("fio: Failed to set notify!\n");
305 ret = cq_event_handler(td, opcode);
309 ibv_ack_cq_events(rd->cq, ret);
316 static int fio_rdmaio_setup_qp(struct thread_data *td)
318 struct rdmaio_data *rd = td->io_ops->data;
319 struct ibv_qp_init_attr init_attr;
320 int qp_depth = td->o.iodepth * 2; /* 2 times of io depth */
322 if (rd->is_client == 0)
323 rd->pd = ibv_alloc_pd(rd->child_cm_id->verbs);
325 rd->pd = ibv_alloc_pd(rd->cm_id->verbs);
327 if (rd->pd == NULL) {
328 log_err("fio: ibv_alloc_pd fail\n");
332 if (rd->is_client == 0)
333 rd->channel = ibv_create_comp_channel(rd->child_cm_id->verbs);
335 rd->channel = ibv_create_comp_channel(rd->cm_id->verbs);
336 if (rd->channel == NULL) {
337 log_err("fio: ibv_create_comp_channel fail\n");
344 if (rd->is_client == 0)
345 rd->cq = ibv_create_cq(rd->child_cm_id->verbs,
346 qp_depth, rd, rd->channel, 0);
348 rd->cq = ibv_create_cq(rd->cm_id->verbs,
349 qp_depth, rd, rd->channel, 0);
350 if (rd->cq == NULL) {
351 log_err("fio: ibv_create_cq failed\n");
355 if (ibv_req_notify_cq(rd->cq, 0) != 0) {
356 log_err("fio: ibv_create_cq failed\n");
360 /* create queue pair */
361 memset(&init_attr, 0, sizeof(init_attr));
362 init_attr.cap.max_send_wr = qp_depth;
363 init_attr.cap.max_recv_wr = qp_depth;
364 init_attr.cap.max_recv_sge = 1;
365 init_attr.cap.max_send_sge = 1;
366 init_attr.qp_type = IBV_QPT_RC;
367 init_attr.send_cq = rd->cq;
368 init_attr.recv_cq = rd->cq;
370 if (rd->is_client == 0) {
371 if (rdma_create_qp(rd->child_cm_id, rd->pd, &init_attr) != 0) {
372 log_err("fio: rdma_create_qp failed\n");
375 rd->qp = rd->child_cm_id->qp;
377 if (rdma_create_qp(rd->cm_id, rd->pd, &init_attr) != 0) {
378 log_err("fio: rdma_create_qp failed\n");
381 rd->qp = rd->cm_id->qp;
387 ibv_destroy_cq(rd->cq);
389 ibv_destroy_comp_channel(rd->channel);
391 ibv_dealloc_pd(rd->pd);
396 static int fio_rdmaio_setup_control_msg_buffers(struct thread_data *td)
398 struct rdmaio_data *rd = td->io_ops->data;
400 rd->recv_mr = ibv_reg_mr(rd->pd, &rd->recv_buf, sizeof(rd->recv_buf),
401 IBV_ACCESS_LOCAL_WRITE);
402 if (rd->recv_mr == NULL) {
403 log_err("fio: recv_buf reg_mr failed\n");
407 rd->send_mr = ibv_reg_mr(rd->pd, &rd->send_buf, sizeof(rd->send_buf),
409 if (rd->send_mr == NULL) {
410 log_err("fio: send_buf reg_mr failed\n");
411 ibv_dereg_mr(rd->recv_mr);
415 /* setup work request */
417 rd->recv_sgl.addr = (uint64_t) (unsigned long)&rd->recv_buf;
418 rd->recv_sgl.length = sizeof(rd->recv_buf);
419 rd->recv_sgl.lkey = rd->recv_mr->lkey;
420 rd->rq_wr.sg_list = &rd->recv_sgl;
421 rd->rq_wr.num_sge = 1;
422 rd->rq_wr.wr_id = FIO_RDMA_MAX_IO_DEPTH;
425 rd->send_sgl.addr = (uint64_t) (unsigned long)&rd->send_buf;
426 rd->send_sgl.length = sizeof(rd->send_buf);
427 rd->send_sgl.lkey = rd->send_mr->lkey;
429 rd->sq_wr.opcode = IBV_WR_SEND;
430 rd->sq_wr.send_flags = IBV_SEND_SIGNALED;
431 rd->sq_wr.sg_list = &rd->send_sgl;
432 rd->sq_wr.num_sge = 1;
433 rd->sq_wr.wr_id = FIO_RDMA_MAX_IO_DEPTH;
438 static int get_next_channel_event(struct thread_data *td,
439 struct rdma_event_channel *channel,
440 enum rdma_cm_event_type wait_event)
442 struct rdmaio_data *rd = td->io_ops->data;
443 struct rdma_cm_event *event;
446 ret = rdma_get_cm_event(channel, &event);
448 log_err("fio: rdma_get_cm_event: %d\n", ret);
452 if (event->event != wait_event) {
453 log_err("fio: event is %s instead of %s\n",
454 rdma_event_str(event->event),
455 rdma_event_str(wait_event));
459 switch (event->event) {
460 case RDMA_CM_EVENT_CONNECT_REQUEST:
461 rd->child_cm_id = event->id;
467 rdma_ack_cm_event(event);
472 static int fio_rdmaio_prep(struct thread_data *td, struct io_u *io_u)
474 struct rdmaio_data *rd = td->io_ops->data;
475 struct rdma_io_u_data *r_io_u_d;
477 r_io_u_d = io_u->engine_data;
479 switch (rd->rdma_protocol) {
480 case FIO_RDMA_MEM_WRITE:
481 case FIO_RDMA_MEM_READ:
482 r_io_u_d->rdma_sgl.addr = (uint64_t) (unsigned long)io_u->buf;
483 r_io_u_d->rdma_sgl.lkey = io_u->mr->lkey;
484 r_io_u_d->sq_wr.wr_id = r_io_u_d->wr_id;
485 r_io_u_d->sq_wr.send_flags = IBV_SEND_SIGNALED;
486 r_io_u_d->sq_wr.sg_list = &r_io_u_d->rdma_sgl;
487 r_io_u_d->sq_wr.num_sge = 1;
489 case FIO_RDMA_CHA_SEND:
490 r_io_u_d->rdma_sgl.addr = (uint64_t) (unsigned long)io_u->buf;
491 r_io_u_d->rdma_sgl.lkey = io_u->mr->lkey;
492 r_io_u_d->rdma_sgl.length = io_u->buflen;
493 r_io_u_d->sq_wr.wr_id = r_io_u_d->wr_id;
494 r_io_u_d->sq_wr.opcode = IBV_WR_SEND;
495 r_io_u_d->sq_wr.send_flags = IBV_SEND_SIGNALED;
496 r_io_u_d->sq_wr.sg_list = &r_io_u_d->rdma_sgl;
497 r_io_u_d->sq_wr.num_sge = 1;
499 case FIO_RDMA_CHA_RECV:
500 r_io_u_d->rdma_sgl.addr = (uint64_t) (unsigned long)io_u->buf;
501 r_io_u_d->rdma_sgl.lkey = io_u->mr->lkey;
502 r_io_u_d->rdma_sgl.length = io_u->buflen;
503 r_io_u_d->rq_wr.wr_id = r_io_u_d->wr_id;
504 r_io_u_d->rq_wr.sg_list = &r_io_u_d->rdma_sgl;
505 r_io_u_d->rq_wr.num_sge = 1;
508 log_err("fio: unknown rdma protocol - %d\n", rd->rdma_protocol);
515 static struct io_u *fio_rdmaio_event(struct thread_data *td, int event)
517 struct rdmaio_data *rd = td->io_ops->data;
521 io_u = rd->io_us_completed[0];
522 for (i = 0; i < rd->io_u_completed_nr - 1; i++)
523 rd->io_us_completed[i] = rd->io_us_completed[i + 1];
525 rd->io_u_completed_nr--;
527 dprint_io_u(io_u, "fio_rdmaio_event");
532 static int fio_rdmaio_getevents(struct thread_data *td, unsigned int min,
533 unsigned int max, struct timespec *t)
535 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;
542 switch (rd->rdma_protocol) {
543 case FIO_RDMA_MEM_WRITE:
544 comp_opcode = IBV_WC_RDMA_WRITE;
546 case FIO_RDMA_MEM_READ:
547 comp_opcode = IBV_WC_RDMA_READ;
549 case FIO_RDMA_CHA_SEND:
550 comp_opcode = IBV_WC_SEND;
552 case FIO_RDMA_CHA_RECV:
553 comp_opcode = IBV_WC_RECV;
556 log_err("fio: unknown rdma protocol - %d\n", rd->rdma_protocol);
560 if (rd->cq_event_num > 0) { /* previous left */
566 if (ibv_get_cq_event(rd->channel, &ev_cq, &ev_ctx) != 0) {
567 log_err("fio: Failed to get cq event!\n");
570 if (ev_cq != rd->cq) {
571 log_err("fio: Unknown CQ!\n");
574 if (ibv_req_notify_cq(rd->cq, 0) != 0) {
575 log_err("fio: Failed to set notify!\n");
579 ret = cq_event_handler(td, comp_opcode);
583 ibv_ack_cq_events(rd->cq, ret);
589 rd->cq_event_num -= r;
594 static int fio_rdmaio_send(struct thread_data *td, struct io_u **io_us,
597 struct rdmaio_data *rd = td->io_ops->data;
598 struct ibv_send_wr *bad_wr;
600 enum ibv_wc_opcode comp_opcode;
601 comp_opcode = IBV_WC_RDMA_WRITE;
605 struct rdma_io_u_data *r_io_u_d;
609 for (i = 0; i < nr; i++) {
610 /* RDMA_WRITE or RDMA_READ */
611 switch (rd->rdma_protocol) {
612 case FIO_RDMA_MEM_WRITE:
613 /* compose work request */
614 r_io_u_d = io_us[i]->engine_data;
615 index = __rand(&rd->rand_state) % rd->rmt_nr;
616 r_io_u_d->sq_wr.opcode = IBV_WR_RDMA_WRITE;
617 r_io_u_d->sq_wr.wr.rdma.rkey = rd->rmt_us[index].rkey;
618 r_io_u_d->sq_wr.wr.rdma.remote_addr = \
619 rd->rmt_us[index].buf;
620 r_io_u_d->sq_wr.sg_list->length = io_us[i]->buflen;
622 case FIO_RDMA_MEM_READ:
623 /* compose work request */
624 r_io_u_d = io_us[i]->engine_data;
625 index = __rand(&rd->rand_state) % rd->rmt_nr;
626 r_io_u_d->sq_wr.opcode = IBV_WR_RDMA_READ;
627 r_io_u_d->sq_wr.wr.rdma.rkey = rd->rmt_us[index].rkey;
628 r_io_u_d->sq_wr.wr.rdma.remote_addr = \
629 rd->rmt_us[index].buf;
630 r_io_u_d->sq_wr.sg_list->length = io_us[i]->buflen;
632 case FIO_RDMA_CHA_SEND:
633 r_io_u_d = io_us[i]->engine_data;
634 r_io_u_d->sq_wr.opcode = IBV_WR_SEND;
635 r_io_u_d->sq_wr.send_flags = IBV_SEND_SIGNALED;
638 log_err("fio: unknown rdma protocol - %d\n",
643 if (ibv_post_send(rd->qp, &r_io_u_d->sq_wr, &bad_wr) != 0) {
644 log_err("fio: ibv_post_send fail\n");
648 dprint_io_u(io_us[i], "fio_rdmaio_send");
651 /* wait for completion
652 rdma_poll_wait(td, comp_opcode); */
657 static int fio_rdmaio_recv(struct thread_data *td, struct io_u **io_us,
660 struct rdmaio_data *rd = td->io_ops->data;
661 struct ibv_recv_wr *bad_wr;
662 struct rdma_io_u_data *r_io_u_d;
666 if (rd->rdma_protocol == FIO_RDMA_CHA_RECV) {
667 /* post io_u into recv queue */
668 for (i = 0; i < nr; i++) {
669 r_io_u_d = io_us[i]->engine_data;
670 if (ibv_post_recv(rd->qp, &r_io_u_d->rq_wr, &bad_wr) !=
672 log_err("fio: ibv_post_recv fail\n");
676 } else if ((rd->rdma_protocol == FIO_RDMA_MEM_READ)
677 || (rd->rdma_protocol == FIO_RDMA_MEM_WRITE)) {
678 /* re-post the rq_wr */
679 if (ibv_post_recv(rd->qp, &rd->rq_wr, &bad_wr) != 0) {
680 log_err("fio: ibv_post_recv fail\n");
684 rdma_poll_wait(td, IBV_WC_RECV);
686 dprint(FD_IO, "fio: recv FINISH message\n");
694 static int fio_rdmaio_queue(struct thread_data *td, struct io_u *io_u)
696 struct rdmaio_data *rd = td->io_ops->data;
698 fio_ro_check(td, io_u);
700 if (rd->io_u_queued_nr == (int)td->o.iodepth)
703 rd->io_us_queued[rd->io_u_queued_nr] = io_u;
704 rd->io_u_queued_nr++;
706 dprint_io_u(io_u, "fio_rdmaio_queue");
711 static void fio_rdmaio_queued(struct thread_data *td, struct io_u **io_us,
714 struct rdmaio_data *rd = td->io_ops->data;
718 if (!fio_fill_issue_time(td))
721 fio_gettime(&now, NULL);
723 for (i = 0; i < nr; i++) {
724 struct io_u *io_u = io_us[i];
726 /* queued -> flight */
727 rd->io_us_flight[rd->io_u_flight_nr] = io_u;
728 rd->io_u_flight_nr++;
730 memcpy(&io_u->issue_time, &now, sizeof(now));
731 io_u_queued(td, io_u);
735 static int fio_rdmaio_commit(struct thread_data *td)
737 struct rdmaio_data *rd = td->io_ops->data;
741 if (!rd->io_us_queued)
744 io_us = rd->io_us_queued;
746 /* RDMA_WRITE or RDMA_READ */
748 ret = fio_rdmaio_send(td, io_us, rd->io_u_queued_nr);
749 else if (!rd->is_client)
750 ret = fio_rdmaio_recv(td, io_us, rd->io_u_queued_nr);
752 ret = 0; /* must be a SYNC */
755 fio_rdmaio_queued(td, io_us, ret);
756 io_u_mark_submit(td, ret);
757 rd->io_u_queued_nr -= ret;
762 } while (rd->io_u_queued_nr);
767 static int fio_rdmaio_connect(struct thread_data *td, struct fio_file *f)
769 struct rdmaio_data *rd = td->io_ops->data;
770 struct rdma_conn_param conn_param;
771 struct ibv_send_wr *bad_wr;
773 memset(&conn_param, 0, sizeof(conn_param));
774 conn_param.responder_resources = 1;
775 conn_param.initiator_depth = 1;
776 conn_param.retry_count = 10;
778 if (rdma_connect(rd->cm_id, &conn_param) != 0) {
779 log_err("fio: rdma_connect fail\n");
783 if (get_next_channel_event
784 (td, rd->cm_channel, RDMA_CM_EVENT_ESTABLISHED) != 0) {
785 log_err("fio: wait for RDMA_CM_EVENT_ESTABLISHED\n");
789 /* send task request */
790 rd->send_buf.mode = htonl(rd->rdma_protocol);
791 rd->send_buf.nr = htonl(td->o.iodepth);
793 if (ibv_post_send(rd->qp, &rd->sq_wr, &bad_wr) != 0) {
794 log_err("fio: ibv_post_send fail");
798 rdma_poll_wait(td, IBV_WC_SEND);
800 /* wait for remote MR info from server side */
801 rdma_poll_wait(td, IBV_WC_RECV);
803 /* In SEND/RECV test, it's a good practice to setup the iodepth of
804 * of the RECV side deeper than that of the SEND side to
805 * avoid RNR (receiver not ready) error. The
806 * SEND side may send so many unsolicited message before
807 * RECV side commits sufficient recv buffers into recv queue.
808 * This may lead to RNR error. Here, SEND side pauses for a while
809 * during which RECV side commits sufficient recv buffers.
816 static int fio_rdmaio_accept(struct thread_data *td, struct fio_file *f)
818 struct rdmaio_data *rd = td->io_ops->data;
819 struct rdma_conn_param conn_param;
820 struct ibv_send_wr *bad_wr;
822 /* rdma_accept() - then wait for accept success */
823 memset(&conn_param, 0, sizeof(conn_param));
824 conn_param.responder_resources = 1;
825 conn_param.initiator_depth = 1;
827 if (rdma_accept(rd->child_cm_id, &conn_param) != 0) {
828 log_err("fio: rdma_accept\n");
832 if (get_next_channel_event
833 (td, rd->cm_channel, RDMA_CM_EVENT_ESTABLISHED) != 0) {
834 log_err("fio: wait for RDMA_CM_EVENT_ESTABLISHED\n");
838 /* wait for request */
839 rdma_poll_wait(td, IBV_WC_RECV);
841 if (ibv_post_send(rd->qp, &rd->sq_wr, &bad_wr) != 0) {
842 log_err("fio: ibv_post_send fail");
846 rdma_poll_wait(td, IBV_WC_SEND);
851 static int fio_rdmaio_open_file(struct thread_data *td, struct fio_file *f)
854 return fio_rdmaio_accept(td, f);
856 return fio_rdmaio_connect(td, f);
859 static int fio_rdmaio_close_file(struct thread_data *td, struct fio_file *f)
861 struct rdmaio_data *rd = td->io_ops->data;
862 struct ibv_send_wr *bad_wr;
864 /* unregister rdma buffer */
867 * Client sends notification to the server side
869 /* refer to: http://linux.die.net/man/7/rdma_cm */
870 if ((rd->is_client == 1) && ((rd->rdma_protocol == FIO_RDMA_MEM_WRITE)
871 || (rd->rdma_protocol ==
872 FIO_RDMA_MEM_READ))) {
873 if (ibv_post_send(rd->qp, &rd->sq_wr, &bad_wr) != 0) {
874 log_err("fio: ibv_post_send fail");
878 dprint(FD_IO, "fio: close infomation sent success\n");
879 rdma_poll_wait(td, IBV_WC_SEND);
882 if (rd->is_client == 1)
883 rdma_disconnect(rd->cm_id);
885 rdma_disconnect(rd->child_cm_id);
887 rdma_disconnect(rd->cm_id);
892 if (get_next_channel_event(td, rd->cm_channel, RDMA_CM_EVENT_DISCONNECTED) != 0) {
893 log_err("fio: wait for RDMA_CM_EVENT_DISCONNECTED\n");
898 ibv_destroy_cq(rd->cq);
899 ibv_destroy_qp(rd->qp);
901 if (rd->is_client == 1)
902 rdma_destroy_id(rd->cm_id);
904 rdma_destroy_id(rd->child_cm_id);
905 rdma_destroy_id(rd->cm_id);
908 ibv_destroy_comp_channel(rd->channel);
909 ibv_dealloc_pd(rd->pd);
914 static int fio_rdmaio_setup_connect(struct thread_data *td, const char *host,
917 struct rdmaio_data *rd = td->io_ops->data;
918 struct ibv_recv_wr *bad_wr;
921 rd->addr.sin_family = AF_INET;
922 rd->addr.sin_port = htons(port);
924 if (inet_aton(host, &rd->addr.sin_addr) != 1) {
925 struct hostent *hent;
927 hent = gethostbyname(host);
929 td_verror(td, errno, "gethostbyname");
933 memcpy(&rd->addr.sin_addr, hent->h_addr, 4);
937 err = rdma_resolve_addr(rd->cm_id, NULL, (struct sockaddr *)&rd->addr, 2000);
939 log_err("fio: rdma_resolve_addr: %d\n", err);
943 err = get_next_channel_event(td, rd->cm_channel, RDMA_CM_EVENT_ADDR_RESOLVED);
945 log_err("fio: get_next_channel_event: %d\n", err);
950 err = rdma_resolve_route(rd->cm_id, 2000);
952 log_err("fio: rdma_resolve_route: %d\n", err);
956 err = get_next_channel_event(td, rd->cm_channel, RDMA_CM_EVENT_ROUTE_RESOLVED);
958 log_err("fio: get_next_channel_event: %d\n", err);
962 /* create qp and buffer */
963 if (fio_rdmaio_setup_qp(td) != 0)
966 if (fio_rdmaio_setup_control_msg_buffers(td) != 0)
970 err = ibv_post_recv(rd->qp, &rd->rq_wr, &bad_wr);
972 log_err("fio: ibv_post_recv fail: %d\n", err);
979 static int fio_rdmaio_setup_listen(struct thread_data *td, short port)
981 struct rdmaio_data *rd = td->io_ops->data;
982 struct ibv_recv_wr *bad_wr;
984 rd->addr.sin_family = AF_INET;
985 rd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
986 rd->addr.sin_port = htons(port);
989 if (rdma_bind_addr(rd->cm_id, (struct sockaddr *)&rd->addr) != 0) {
990 log_err("fio: rdma_bind_addr fail\n");
994 if (rdma_listen(rd->cm_id, 3) != 0) {
995 log_err("fio: rdma_listen fail\n");
999 /* wait for CONNECT_REQUEST */
1000 if (get_next_channel_event
1001 (td, rd->cm_channel, RDMA_CM_EVENT_CONNECT_REQUEST) != 0) {
1002 log_err("fio: wait for RDMA_CM_EVENT_CONNECT_REQUEST\n");
1006 if (fio_rdmaio_setup_qp(td) != 0)
1009 if (fio_rdmaio_setup_control_msg_buffers(td) != 0)
1013 if (ibv_post_recv(rd->qp, &rd->rq_wr, &bad_wr) != 0) {
1014 log_err("fio: ibv_post_recv fail\n");
1021 static int fio_rdmaio_init(struct thread_data *td)
1023 struct rdmaio_data *rd = td->io_ops->data;
1024 struct flist_head *entry;
1025 unsigned int max_bs;
1027 char host[64], buf[128];
1028 char *sep, *portp, *modep;
1033 log_err("fio: rdma connections must be read OR write\n");
1036 if (td_random(td)) {
1037 log_err("fio: RDMA network IO can't be random\n");
1041 /* check RLIMIT_MEMLOCK */
1042 if (getrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
1043 log_err("fio: getrlimit fail: %d(%s)\n",
1044 errno, strerror(errno));
1049 if ((rl.rlim_cur != RLIM_INFINITY)
1050 && (rl.rlim_cur < td->orig_buffer_size)) {
1051 log_err("fio: soft RLIMIT_MEMLOCK is: %" PRId64 "\n",
1053 log_err("fio: total block size is: %zd\n",
1054 td->orig_buffer_size);
1055 /* try to set larger RLIMIT_MEMLOCK */
1056 rl.rlim_cur = rl.rlim_max;
1057 if (setrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
1058 log_err("fio: setrlimit fail: %d(%s)\n",
1059 errno, strerror(errno));
1060 log_err("fio: you may try enlarge MEMLOCK by root\n");
1061 log_err("# ulimit -l unlimited\n");
1066 strcpy(buf, td->o.filename);
1068 sep = strchr(buf, '/');
1080 sep = strchr(portp, '/');
1086 port = strtol(portp, NULL, 10);
1087 if (!port || port > 65535)
1091 if (!strncmp("rdma_write", modep, strlen(modep)) ||
1092 !strncmp("RDMA_WRITE", modep, strlen(modep)))
1093 rd->rdma_protocol = FIO_RDMA_MEM_WRITE;
1094 else if (!strncmp("rdma_read", modep, strlen(modep)) ||
1095 !strncmp("RDMA_READ", modep, strlen(modep)))
1096 rd->rdma_protocol = FIO_RDMA_MEM_READ;
1097 else if (!strncmp("send", modep, strlen(modep)) ||
1098 !strncmp("SEND", modep, strlen(modep)))
1099 rd->rdma_protocol = FIO_RDMA_CHA_SEND;
1103 rd->rdma_protocol = FIO_RDMA_MEM_WRITE;
1105 rd->cq_event_num = 0;
1107 rd->cm_channel = rdma_create_event_channel();
1108 if (!rd->cm_channel) {
1109 log_err("fio: rdma_create_event_channel fail\n");
1113 ret = rdma_create_id(rd->cm_channel, &rd->cm_id, rd, RDMA_PS_TCP);
1115 log_err("fio: rdma_create_id fail\n");
1119 if ((rd->rdma_protocol == FIO_RDMA_MEM_WRITE) ||
1120 (rd->rdma_protocol == FIO_RDMA_MEM_READ)) {
1122 malloc(FIO_RDMA_MAX_IO_DEPTH * sizeof(struct remote_u));
1123 memset(rd->rmt_us, 0,
1124 FIO_RDMA_MAX_IO_DEPTH * sizeof(struct remote_u));
1128 rd->io_us_queued = malloc(td->o.iodepth * sizeof(struct io_u *));
1129 memset(rd->io_us_queued, 0, td->o.iodepth * sizeof(struct io_u *));
1130 rd->io_u_queued_nr = 0;
1132 rd->io_us_flight = malloc(td->o.iodepth * sizeof(struct io_u *));
1133 memset(rd->io_us_flight, 0, td->o.iodepth * sizeof(struct io_u *));
1134 rd->io_u_flight_nr = 0;
1136 rd->io_us_completed = malloc(td->o.iodepth * sizeof(struct io_u *));
1137 memset(rd->io_us_completed, 0, td->o.iodepth * sizeof(struct io_u *));
1138 rd->io_u_completed_nr = 0;
1140 if (td_read(td)) { /* READ as the server */
1142 /* server rd->rdma_buf_len will be setup after got request */
1143 ret = fio_rdmaio_setup_listen(td, port);
1144 } else { /* WRITE as the client */
1146 ret = fio_rdmaio_setup_connect(td, host, port);
1149 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
1150 /* register each io_u in the free list */
1151 flist_for_each(entry, &td->io_u_freelist) {
1152 struct io_u *io_u = flist_entry(entry, struct io_u, list);
1154 io_u->engine_data = malloc(sizeof(struct rdma_io_u_data));
1155 memset(io_u->engine_data, 0, sizeof(struct rdma_io_u_data));
1156 ((struct rdma_io_u_data *)io_u->engine_data)->wr_id = i;
1158 io_u->mr = ibv_reg_mr(rd->pd, io_u->buf, max_bs,
1159 IBV_ACCESS_LOCAL_WRITE |
1160 IBV_ACCESS_REMOTE_READ |
1161 IBV_ACCESS_REMOTE_WRITE);
1162 if (io_u->mr == NULL) {
1163 log_err("fio: ibv_reg_mr io_u failed\n");
1167 rd->send_buf.rmt_us[i].buf =
1168 htonll((uint64_t) (unsigned long)io_u->buf);
1169 rd->send_buf.rmt_us[i].rkey = htonl(io_u->mr->rkey);
1170 rd->send_buf.rmt_us[i].size = htonl(max_bs);
1173 log_info("fio: Send rkey %x addr %" PRIx64 " len %d to client\n", io_u->mr->rkey, io_u->buf, max_bs); */
1178 rd->send_buf.nr = htonl(i);
1182 log_err("fio: bad rdma host/port/protocol: %s\n", td->o.filename);
1186 static void fio_rdmaio_cleanup(struct thread_data *td)
1188 struct rdmaio_data *rd = td->io_ops->data;
1194 static int fio_rdmaio_setup(struct thread_data *td)
1196 struct rdmaio_data *rd;
1198 if (!td->io_ops->data) {
1199 rd = malloc(sizeof(*rd));
1201 memset(rd, 0, sizeof(*rd));
1202 init_rand_seed(&rd->rand_state, (unsigned int) GOLDEN_RATIO_PRIME);
1203 td->io_ops->data = rd;
1209 static struct ioengine_ops ioengine_rw = {
1211 .version = FIO_IOOPS_VERSION,
1212 .setup = fio_rdmaio_setup,
1213 .init = fio_rdmaio_init,
1214 .prep = fio_rdmaio_prep,
1215 .queue = fio_rdmaio_queue,
1216 .commit = fio_rdmaio_commit,
1217 .getevents = fio_rdmaio_getevents,
1218 .event = fio_rdmaio_event,
1219 .cleanup = fio_rdmaio_cleanup,
1220 .open_file = fio_rdmaio_open_file,
1221 .close_file = fio_rdmaio_close_file,
1222 .flags = FIO_DISKLESSIO | FIO_UNIDIR | FIO_PIPEIO,
1225 static void fio_init fio_rdmaio_register(void)
1227 register_ioengine(&ioengine_rw);
1230 static void fio_exit fio_rdmaio_unregister(void)
1232 unregister_ioengine(&ioengine_rw);