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 * You will need the Linux RDMA software installed, either
9 * from your Linux distributor or directly from openfabrics.org:
11 * http://www.openfabrics.org/downloads/OFED/
13 * Exchanging steps of RDMA ioengine control messages:
14 * 1. client side sends test mode (RDMA_WRITE/RDMA_READ/SEND)
16 * 2. server side parses test mode, and sends back confirmation
17 * to client side. In RDMA WRITE/READ test, this confirmation
18 * includes memory information, such as rkey, address.
19 * 3. client side initiates test loop.
20 * 4. In RDMA WRITE/READ test, client side sends a completion
21 * notification to server side. Server side updates its
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
37 #include <sys/resource.h>
45 #include <rdma/rdma_cma.h>
46 #include <infiniband/arch.h>
48 #define FIO_RDMA_MAX_IO_DEPTH 512
58 struct rdmaio_options {
59 struct thread_data *td;
61 enum rdma_io_mode verb;
64 static int str_hostname_cb(void *data, const char *input)
66 struct rdmaio_options *o = data;
68 if (o->td->o.filename)
69 free(o->td->o.filename);
70 o->td->o.filename = strdup(input);
74 static struct fio_option options[] = {
77 .lname = "rdma engine hostname",
78 .type = FIO_OPT_STR_STORE,
79 .cb = str_hostname_cb,
80 .help = "Hostname for RDMA IO engine",
81 .category = FIO_OPT_C_ENGINE,
82 .group = FIO_OPT_G_RDMA,
86 .lname = "rdma engine port",
88 .off1 = offsetof(struct rdmaio_options, port),
91 .help = "Port to use for RDMA connections",
92 .category = FIO_OPT_C_ENGINE,
93 .group = FIO_OPT_G_RDMA,
97 .lname = "RDMA engine verb",
100 .off1 = offsetof(struct rdmaio_options, verb),
101 .help = "RDMA engine verb",
105 .oval = FIO_RDMA_MEM_WRITE,
106 .help = "Memory Write",
109 .oval = FIO_RDMA_MEM_READ,
110 .help = "Memory Read",
113 .oval = FIO_RDMA_CHA_SEND,
114 .help = "Posted Send",
117 .oval = FIO_RDMA_CHA_RECV,
118 .help = "Posted Recieve",
121 .category = FIO_OPT_C_ENGINE,
122 .group = FIO_OPT_G_RDMA,
135 struct rdma_info_blk {
136 uint32_t mode; /* channel semantic or memory semantic */
137 uint32_t nr; /* client: io depth
138 server: number of records for memory semantic
140 struct remote_u rmt_us[FIO_RDMA_MAX_IO_DEPTH];
143 struct rdma_io_u_data {
145 struct ibv_send_wr sq_wr;
146 struct ibv_recv_wr rq_wr;
147 struct ibv_sge rdma_sgl;
152 enum rdma_io_mode rdma_protocol;
154 struct sockaddr_in addr;
156 struct ibv_recv_wr rq_wr;
157 struct ibv_sge recv_sgl;
158 struct rdma_info_blk recv_buf;
159 struct ibv_mr *recv_mr;
161 struct ibv_send_wr sq_wr;
162 struct ibv_sge send_sgl;
163 struct rdma_info_blk send_buf;
164 struct ibv_mr *send_mr;
166 struct ibv_comp_channel *channel;
172 struct rdma_event_channel *cm_channel;
173 struct rdma_cm_id *cm_id;
174 struct rdma_cm_id *child_cm_id;
178 struct remote_u *rmt_us;
180 struct io_u **io_us_queued;
182 struct io_u **io_us_flight;
184 struct io_u **io_us_completed;
185 int io_u_completed_nr;
187 struct frand_state rand_state;
190 static int client_recv(struct thread_data *td, struct ibv_wc *wc)
192 struct rdmaio_data *rd = td->io_ops->data;
194 if (wc->byte_len != sizeof(rd->recv_buf)) {
195 log_err("Received bogus data, size %d\n", wc->byte_len);
199 /* store mr info for MEMORY semantic */
200 if ((rd->rdma_protocol == FIO_RDMA_MEM_WRITE) ||
201 (rd->rdma_protocol == FIO_RDMA_MEM_READ)) {
202 /* struct flist_head *entry; */
205 rd->rmt_nr = ntohl(rd->recv_buf.nr);
207 for (i = 0; i < rd->rmt_nr; i++) {
208 rd->rmt_us[i].buf = ntohll(rd->recv_buf.rmt_us[i].buf);
209 rd->rmt_us[i].rkey = ntohl(rd->recv_buf.rmt_us[i].rkey);
210 rd->rmt_us[i].size = ntohl(rd->recv_buf.rmt_us[i].size);
213 "fio: Received rkey %x addr %" PRIx64
214 " len %d from peer\n", rd->rmt_us[i].rkey,
215 rd->rmt_us[i].buf, rd->rmt_us[i].size);
222 static int server_recv(struct thread_data *td, struct ibv_wc *wc)
224 struct rdmaio_data *rd = td->io_ops->data;
226 if (wc->wr_id == FIO_RDMA_MAX_IO_DEPTH) {
227 rd->rdma_protocol = ntohl(rd->recv_buf.mode);
229 /* CHANNEL semantic, do nothing */
230 if (rd->rdma_protocol == FIO_RDMA_CHA_SEND)
231 rd->rdma_protocol = FIO_RDMA_CHA_RECV;
237 static int cq_event_handler(struct thread_data *td, enum ibv_wc_opcode opcode)
239 struct rdmaio_data *rd = td->io_ops->data;
241 struct rdma_io_u_data *r_io_u_d;
246 while ((ret = ibv_poll_cq(rd->cq, 1, &wc)) == 1) {
251 log_err("fio: cq completion status %d(%s)\n",
252 wc.status, ibv_wc_status_str(wc.status));
259 if (rd->is_client == 1)
260 client_recv(td, &wc);
262 server_recv(td, &wc);
264 if (wc.wr_id == FIO_RDMA_MAX_IO_DEPTH)
267 for (i = 0; i < rd->io_u_flight_nr; i++) {
268 r_io_u_d = rd->io_us_flight[i]->engine_data;
270 if (wc.wr_id == r_io_u_d->rq_wr.wr_id) {
271 rd->io_us_flight[i]->resid =
272 rd->io_us_flight[i]->buflen
275 rd->io_us_flight[i]->error = 0;
277 rd->io_us_completed[rd->
279 = rd->io_us_flight[i];
280 rd->io_u_completed_nr++;
284 if (i == rd->io_u_flight_nr)
285 log_err("fio: recv wr %" PRId64 " not found\n",
288 /* put the last one into middle of the list */
289 rd->io_us_flight[i] =
290 rd->io_us_flight[rd->io_u_flight_nr - 1];
291 rd->io_u_flight_nr--;
297 case IBV_WC_RDMA_WRITE:
298 case IBV_WC_RDMA_READ:
299 if (wc.wr_id == FIO_RDMA_MAX_IO_DEPTH)
302 for (i = 0; i < rd->io_u_flight_nr; i++) {
303 r_io_u_d = rd->io_us_flight[i]->engine_data;
305 if (wc.wr_id == r_io_u_d->sq_wr.wr_id) {
306 rd->io_us_completed[rd->
308 = rd->io_us_flight[i];
309 rd->io_u_completed_nr++;
313 if (i == rd->io_u_flight_nr)
314 log_err("fio: send wr %" PRId64 " not found\n",
317 /* put the last one into middle of the list */
318 rd->io_us_flight[i] =
319 rd->io_us_flight[rd->io_u_flight_nr - 1];
320 rd->io_u_flight_nr--;
326 log_info("fio: unknown completion event %d\n",
333 log_err("fio: poll error %d\n", ret);
341 * Return -1 for error and 'nr events' for a positive number
344 static int rdma_poll_wait(struct thread_data *td, enum ibv_wc_opcode opcode)
346 struct rdmaio_data *rd = td->io_ops->data;
347 struct ibv_cq *ev_cq;
351 if (rd->cq_event_num > 0) { /* previous left */
357 if (ibv_get_cq_event(rd->channel, &ev_cq, &ev_ctx) != 0) {
358 log_err("fio: Failed to get cq event!\n");
361 if (ev_cq != rd->cq) {
362 log_err("fio: Unknown CQ!\n");
365 if (ibv_req_notify_cq(rd->cq, 0) != 0) {
366 log_err("fio: Failed to set notify!\n");
370 ret = cq_event_handler(td, opcode);
374 ibv_ack_cq_events(rd->cq, ret);
381 static int fio_rdmaio_setup_qp(struct thread_data *td)
383 struct rdmaio_data *rd = td->io_ops->data;
384 struct ibv_qp_init_attr init_attr;
385 int qp_depth = td->o.iodepth * 2; /* 2 times of io depth */
387 if (rd->is_client == 0)
388 rd->pd = ibv_alloc_pd(rd->child_cm_id->verbs);
390 rd->pd = ibv_alloc_pd(rd->cm_id->verbs);
392 if (rd->pd == NULL) {
393 log_err("fio: ibv_alloc_pd fail\n");
397 if (rd->is_client == 0)
398 rd->channel = ibv_create_comp_channel(rd->child_cm_id->verbs);
400 rd->channel = ibv_create_comp_channel(rd->cm_id->verbs);
401 if (rd->channel == NULL) {
402 log_err("fio: ibv_create_comp_channel fail\n");
409 if (rd->is_client == 0)
410 rd->cq = ibv_create_cq(rd->child_cm_id->verbs,
411 qp_depth, rd, rd->channel, 0);
413 rd->cq = ibv_create_cq(rd->cm_id->verbs,
414 qp_depth, rd, rd->channel, 0);
415 if (rd->cq == NULL) {
416 log_err("fio: ibv_create_cq failed\n");
420 if (ibv_req_notify_cq(rd->cq, 0) != 0) {
421 log_err("fio: ibv_create_cq failed\n");
425 /* create queue pair */
426 memset(&init_attr, 0, sizeof(init_attr));
427 init_attr.cap.max_send_wr = qp_depth;
428 init_attr.cap.max_recv_wr = qp_depth;
429 init_attr.cap.max_recv_sge = 1;
430 init_attr.cap.max_send_sge = 1;
431 init_attr.qp_type = IBV_QPT_RC;
432 init_attr.send_cq = rd->cq;
433 init_attr.recv_cq = rd->cq;
435 if (rd->is_client == 0) {
436 if (rdma_create_qp(rd->child_cm_id, rd->pd, &init_attr) != 0) {
437 log_err("fio: rdma_create_qp failed\n");
440 rd->qp = rd->child_cm_id->qp;
442 if (rdma_create_qp(rd->cm_id, rd->pd, &init_attr) != 0) {
443 log_err("fio: rdma_create_qp failed\n");
446 rd->qp = rd->cm_id->qp;
452 ibv_destroy_cq(rd->cq);
454 ibv_destroy_comp_channel(rd->channel);
456 ibv_dealloc_pd(rd->pd);
461 static int fio_rdmaio_setup_control_msg_buffers(struct thread_data *td)
463 struct rdmaio_data *rd = td->io_ops->data;
465 rd->recv_mr = ibv_reg_mr(rd->pd, &rd->recv_buf, sizeof(rd->recv_buf),
466 IBV_ACCESS_LOCAL_WRITE);
467 if (rd->recv_mr == NULL) {
468 log_err("fio: recv_buf reg_mr failed\n");
472 rd->send_mr = ibv_reg_mr(rd->pd, &rd->send_buf, sizeof(rd->send_buf),
474 if (rd->send_mr == NULL) {
475 log_err("fio: send_buf reg_mr failed\n");
476 ibv_dereg_mr(rd->recv_mr);
480 /* setup work request */
482 rd->recv_sgl.addr = (uint64_t) (unsigned long)&rd->recv_buf;
483 rd->recv_sgl.length = sizeof(rd->recv_buf);
484 rd->recv_sgl.lkey = rd->recv_mr->lkey;
485 rd->rq_wr.sg_list = &rd->recv_sgl;
486 rd->rq_wr.num_sge = 1;
487 rd->rq_wr.wr_id = FIO_RDMA_MAX_IO_DEPTH;
490 rd->send_sgl.addr = (uint64_t) (unsigned long)&rd->send_buf;
491 rd->send_sgl.length = sizeof(rd->send_buf);
492 rd->send_sgl.lkey = rd->send_mr->lkey;
494 rd->sq_wr.opcode = IBV_WR_SEND;
495 rd->sq_wr.send_flags = IBV_SEND_SIGNALED;
496 rd->sq_wr.sg_list = &rd->send_sgl;
497 rd->sq_wr.num_sge = 1;
498 rd->sq_wr.wr_id = FIO_RDMA_MAX_IO_DEPTH;
503 static int get_next_channel_event(struct thread_data *td,
504 struct rdma_event_channel *channel,
505 enum rdma_cm_event_type wait_event)
507 struct rdmaio_data *rd = td->io_ops->data;
508 struct rdma_cm_event *event;
511 ret = rdma_get_cm_event(channel, &event);
513 log_err("fio: rdma_get_cm_event: %d\n", ret);
517 if (event->event != wait_event) {
518 log_err("fio: event is %s instead of %s\n",
519 rdma_event_str(event->event),
520 rdma_event_str(wait_event));
524 switch (event->event) {
525 case RDMA_CM_EVENT_CONNECT_REQUEST:
526 rd->child_cm_id = event->id;
532 rdma_ack_cm_event(event);
537 static int fio_rdmaio_prep(struct thread_data *td, struct io_u *io_u)
539 struct rdmaio_data *rd = td->io_ops->data;
540 struct rdma_io_u_data *r_io_u_d;
542 r_io_u_d = io_u->engine_data;
544 switch (rd->rdma_protocol) {
545 case FIO_RDMA_MEM_WRITE:
546 case FIO_RDMA_MEM_READ:
547 r_io_u_d->rdma_sgl.addr = (uint64_t) (unsigned long)io_u->buf;
548 r_io_u_d->rdma_sgl.lkey = io_u->mr->lkey;
549 r_io_u_d->sq_wr.wr_id = r_io_u_d->wr_id;
550 r_io_u_d->sq_wr.send_flags = IBV_SEND_SIGNALED;
551 r_io_u_d->sq_wr.sg_list = &r_io_u_d->rdma_sgl;
552 r_io_u_d->sq_wr.num_sge = 1;
554 case FIO_RDMA_CHA_SEND:
555 r_io_u_d->rdma_sgl.addr = (uint64_t) (unsigned long)io_u->buf;
556 r_io_u_d->rdma_sgl.lkey = io_u->mr->lkey;
557 r_io_u_d->rdma_sgl.length = io_u->buflen;
558 r_io_u_d->sq_wr.wr_id = r_io_u_d->wr_id;
559 r_io_u_d->sq_wr.opcode = IBV_WR_SEND;
560 r_io_u_d->sq_wr.send_flags = IBV_SEND_SIGNALED;
561 r_io_u_d->sq_wr.sg_list = &r_io_u_d->rdma_sgl;
562 r_io_u_d->sq_wr.num_sge = 1;
564 case FIO_RDMA_CHA_RECV:
565 r_io_u_d->rdma_sgl.addr = (uint64_t) (unsigned long)io_u->buf;
566 r_io_u_d->rdma_sgl.lkey = io_u->mr->lkey;
567 r_io_u_d->rdma_sgl.length = io_u->buflen;
568 r_io_u_d->rq_wr.wr_id = r_io_u_d->wr_id;
569 r_io_u_d->rq_wr.sg_list = &r_io_u_d->rdma_sgl;
570 r_io_u_d->rq_wr.num_sge = 1;
573 log_err("fio: unknown rdma protocol - %d\n", rd->rdma_protocol);
580 static struct io_u *fio_rdmaio_event(struct thread_data *td, int event)
582 struct rdmaio_data *rd = td->io_ops->data;
586 io_u = rd->io_us_completed[0];
587 for (i = 0; i < rd->io_u_completed_nr - 1; i++)
588 rd->io_us_completed[i] = rd->io_us_completed[i + 1];
590 rd->io_u_completed_nr--;
592 dprint_io_u(io_u, "fio_rdmaio_event");
597 static int fio_rdmaio_getevents(struct thread_data *td, unsigned int min,
598 unsigned int max, const struct timespec *t)
600 struct rdmaio_data *rd = td->io_ops->data;
601 enum ibv_wc_opcode comp_opcode;
602 struct ibv_cq *ev_cq;
605 comp_opcode = IBV_WC_RDMA_WRITE;
607 switch (rd->rdma_protocol) {
608 case FIO_RDMA_MEM_WRITE:
609 comp_opcode = IBV_WC_RDMA_WRITE;
611 case FIO_RDMA_MEM_READ:
612 comp_opcode = IBV_WC_RDMA_READ;
614 case FIO_RDMA_CHA_SEND:
615 comp_opcode = IBV_WC_SEND;
617 case FIO_RDMA_CHA_RECV:
618 comp_opcode = IBV_WC_RECV;
621 log_err("fio: unknown rdma protocol - %d\n", rd->rdma_protocol);
625 if (rd->cq_event_num > 0) { /* previous left */
631 if (ibv_get_cq_event(rd->channel, &ev_cq, &ev_ctx) != 0) {
632 log_err("fio: Failed to get cq event!\n");
635 if (ev_cq != rd->cq) {
636 log_err("fio: Unknown CQ!\n");
639 if (ibv_req_notify_cq(rd->cq, 0) != 0) {
640 log_err("fio: Failed to set notify!\n");
644 ret = cq_event_handler(td, comp_opcode);
648 ibv_ack_cq_events(rd->cq, ret);
654 rd->cq_event_num -= r;
659 static int fio_rdmaio_send(struct thread_data *td, struct io_u **io_us,
662 struct rdmaio_data *rd = td->io_ops->data;
663 struct ibv_send_wr *bad_wr;
665 enum ibv_wc_opcode comp_opcode;
666 comp_opcode = IBV_WC_RDMA_WRITE;
670 struct rdma_io_u_data *r_io_u_d;
674 for (i = 0; i < nr; i++) {
675 /* RDMA_WRITE or RDMA_READ */
676 switch (rd->rdma_protocol) {
677 case FIO_RDMA_MEM_WRITE:
678 /* compose work request */
679 r_io_u_d = io_us[i]->engine_data;
680 index = __rand(&rd->rand_state) % rd->rmt_nr;
681 r_io_u_d->sq_wr.opcode = IBV_WR_RDMA_WRITE;
682 r_io_u_d->sq_wr.wr.rdma.rkey = rd->rmt_us[index].rkey;
683 r_io_u_d->sq_wr.wr.rdma.remote_addr = \
684 rd->rmt_us[index].buf;
685 r_io_u_d->sq_wr.sg_list->length = io_us[i]->buflen;
687 case FIO_RDMA_MEM_READ:
688 /* compose work request */
689 r_io_u_d = io_us[i]->engine_data;
690 index = __rand(&rd->rand_state) % rd->rmt_nr;
691 r_io_u_d->sq_wr.opcode = IBV_WR_RDMA_READ;
692 r_io_u_d->sq_wr.wr.rdma.rkey = rd->rmt_us[index].rkey;
693 r_io_u_d->sq_wr.wr.rdma.remote_addr = \
694 rd->rmt_us[index].buf;
695 r_io_u_d->sq_wr.sg_list->length = io_us[i]->buflen;
697 case FIO_RDMA_CHA_SEND:
698 r_io_u_d = io_us[i]->engine_data;
699 r_io_u_d->sq_wr.opcode = IBV_WR_SEND;
700 r_io_u_d->sq_wr.send_flags = IBV_SEND_SIGNALED;
703 log_err("fio: unknown rdma protocol - %d\n",
708 if (ibv_post_send(rd->qp, &r_io_u_d->sq_wr, &bad_wr) != 0) {
709 log_err("fio: ibv_post_send fail\n");
713 dprint_io_u(io_us[i], "fio_rdmaio_send");
716 /* wait for completion
717 rdma_poll_wait(td, comp_opcode); */
722 static int fio_rdmaio_recv(struct thread_data *td, struct io_u **io_us,
725 struct rdmaio_data *rd = td->io_ops->data;
726 struct ibv_recv_wr *bad_wr;
727 struct rdma_io_u_data *r_io_u_d;
731 if (rd->rdma_protocol == FIO_RDMA_CHA_RECV) {
732 /* post io_u into recv queue */
733 for (i = 0; i < nr; i++) {
734 r_io_u_d = io_us[i]->engine_data;
735 if (ibv_post_recv(rd->qp, &r_io_u_d->rq_wr, &bad_wr) !=
737 log_err("fio: ibv_post_recv fail\n");
741 } else if ((rd->rdma_protocol == FIO_RDMA_MEM_READ)
742 || (rd->rdma_protocol == FIO_RDMA_MEM_WRITE)) {
743 /* re-post the rq_wr */
744 if (ibv_post_recv(rd->qp, &rd->rq_wr, &bad_wr) != 0) {
745 log_err("fio: ibv_post_recv fail\n");
749 rdma_poll_wait(td, IBV_WC_RECV);
751 dprint(FD_IO, "fio: recv FINISH message\n");
759 static int fio_rdmaio_queue(struct thread_data *td, struct io_u *io_u)
761 struct rdmaio_data *rd = td->io_ops->data;
763 fio_ro_check(td, io_u);
765 if (rd->io_u_queued_nr == (int)td->o.iodepth)
768 rd->io_us_queued[rd->io_u_queued_nr] = io_u;
769 rd->io_u_queued_nr++;
771 dprint_io_u(io_u, "fio_rdmaio_queue");
776 static void fio_rdmaio_queued(struct thread_data *td, struct io_u **io_us,
779 struct rdmaio_data *rd = td->io_ops->data;
783 if (!fio_fill_issue_time(td))
786 fio_gettime(&now, NULL);
788 for (i = 0; i < nr; i++) {
789 struct io_u *io_u = io_us[i];
791 /* queued -> flight */
792 rd->io_us_flight[rd->io_u_flight_nr] = io_u;
793 rd->io_u_flight_nr++;
795 memcpy(&io_u->issue_time, &now, sizeof(now));
796 io_u_queued(td, io_u);
800 static int fio_rdmaio_commit(struct thread_data *td)
802 struct rdmaio_data *rd = td->io_ops->data;
806 if (!rd->io_us_queued)
809 io_us = rd->io_us_queued;
811 /* RDMA_WRITE or RDMA_READ */
813 ret = fio_rdmaio_send(td, io_us, rd->io_u_queued_nr);
814 else if (!rd->is_client)
815 ret = fio_rdmaio_recv(td, io_us, rd->io_u_queued_nr);
817 ret = 0; /* must be a SYNC */
820 fio_rdmaio_queued(td, io_us, ret);
821 io_u_mark_submit(td, ret);
822 rd->io_u_queued_nr -= ret;
827 } while (rd->io_u_queued_nr);
832 static int fio_rdmaio_connect(struct thread_data *td, struct fio_file *f)
834 struct rdmaio_data *rd = td->io_ops->data;
835 struct rdma_conn_param conn_param;
836 struct ibv_send_wr *bad_wr;
838 memset(&conn_param, 0, sizeof(conn_param));
839 conn_param.responder_resources = 1;
840 conn_param.initiator_depth = 1;
841 conn_param.retry_count = 10;
843 if (rdma_connect(rd->cm_id, &conn_param) != 0) {
844 log_err("fio: rdma_connect fail\n");
848 if (get_next_channel_event
849 (td, rd->cm_channel, RDMA_CM_EVENT_ESTABLISHED) != 0) {
850 log_err("fio: wait for RDMA_CM_EVENT_ESTABLISHED\n");
854 /* send task request */
855 rd->send_buf.mode = htonl(rd->rdma_protocol);
856 rd->send_buf.nr = htonl(td->o.iodepth);
858 if (ibv_post_send(rd->qp, &rd->sq_wr, &bad_wr) != 0) {
859 log_err("fio: ibv_post_send fail");
863 rdma_poll_wait(td, IBV_WC_SEND);
865 /* wait for remote MR info from server side */
866 rdma_poll_wait(td, IBV_WC_RECV);
868 /* In SEND/RECV test, it's a good practice to setup the iodepth of
869 * of the RECV side deeper than that of the SEND side to
870 * avoid RNR (receiver not ready) error. The
871 * SEND side may send so many unsolicited message before
872 * RECV side commits sufficient recv buffers into recv queue.
873 * This may lead to RNR error. Here, SEND side pauses for a while
874 * during which RECV side commits sufficient recv buffers.
881 static int fio_rdmaio_accept(struct thread_data *td, struct fio_file *f)
883 struct rdmaio_data *rd = td->io_ops->data;
884 struct rdma_conn_param conn_param;
885 struct ibv_send_wr *bad_wr;
887 /* rdma_accept() - then wait for accept success */
888 memset(&conn_param, 0, sizeof(conn_param));
889 conn_param.responder_resources = 1;
890 conn_param.initiator_depth = 1;
892 if (rdma_accept(rd->child_cm_id, &conn_param) != 0) {
893 log_err("fio: rdma_accept\n");
897 if (get_next_channel_event
898 (td, rd->cm_channel, RDMA_CM_EVENT_ESTABLISHED) != 0) {
899 log_err("fio: wait for RDMA_CM_EVENT_ESTABLISHED\n");
903 /* wait for request */
904 rdma_poll_wait(td, IBV_WC_RECV);
906 if (ibv_post_send(rd->qp, &rd->sq_wr, &bad_wr) != 0) {
907 log_err("fio: ibv_post_send fail");
911 rdma_poll_wait(td, IBV_WC_SEND);
916 static int fio_rdmaio_open_file(struct thread_data *td, struct fio_file *f)
919 return fio_rdmaio_accept(td, f);
921 return fio_rdmaio_connect(td, f);
924 static int fio_rdmaio_close_file(struct thread_data *td, struct fio_file *f)
926 struct rdmaio_data *rd = td->io_ops->data;
927 struct ibv_send_wr *bad_wr;
929 /* unregister rdma buffer */
932 * Client sends notification to the server side
934 /* refer to: http://linux.die.net/man/7/rdma_cm */
935 if ((rd->is_client == 1) && ((rd->rdma_protocol == FIO_RDMA_MEM_WRITE)
936 || (rd->rdma_protocol ==
937 FIO_RDMA_MEM_READ))) {
938 if (ibv_post_send(rd->qp, &rd->sq_wr, &bad_wr) != 0) {
939 log_err("fio: ibv_post_send fail");
943 dprint(FD_IO, "fio: close information sent success\n");
944 rdma_poll_wait(td, IBV_WC_SEND);
947 if (rd->is_client == 1)
948 rdma_disconnect(rd->cm_id);
950 rdma_disconnect(rd->child_cm_id);
952 rdma_disconnect(rd->cm_id);
957 if (get_next_channel_event(td, rd->cm_channel, RDMA_CM_EVENT_DISCONNECTED) != 0) {
958 log_err("fio: wait for RDMA_CM_EVENT_DISCONNECTED\n");
963 ibv_destroy_cq(rd->cq);
964 ibv_destroy_qp(rd->qp);
966 if (rd->is_client == 1)
967 rdma_destroy_id(rd->cm_id);
969 rdma_destroy_id(rd->child_cm_id);
970 rdma_destroy_id(rd->cm_id);
973 ibv_destroy_comp_channel(rd->channel);
974 ibv_dealloc_pd(rd->pd);
979 static int fio_rdmaio_setup_connect(struct thread_data *td, const char *host,
982 struct rdmaio_data *rd = td->io_ops->data;
983 struct ibv_recv_wr *bad_wr;
986 rd->addr.sin_family = AF_INET;
987 rd->addr.sin_port = htons(port);
989 if (inet_aton(host, &rd->addr.sin_addr) != 1) {
990 struct hostent *hent;
992 hent = gethostbyname(host);
994 td_verror(td, errno, "gethostbyname");
998 memcpy(&rd->addr.sin_addr, hent->h_addr, 4);
1002 err = rdma_resolve_addr(rd->cm_id, NULL, (struct sockaddr *)&rd->addr, 2000);
1004 log_err("fio: rdma_resolve_addr: %d\n", err);
1008 err = get_next_channel_event(td, rd->cm_channel, RDMA_CM_EVENT_ADDR_RESOLVED);
1010 log_err("fio: get_next_channel_event: %d\n", err);
1015 err = rdma_resolve_route(rd->cm_id, 2000);
1017 log_err("fio: rdma_resolve_route: %d\n", err);
1021 err = get_next_channel_event(td, rd->cm_channel, RDMA_CM_EVENT_ROUTE_RESOLVED);
1023 log_err("fio: get_next_channel_event: %d\n", err);
1027 /* create qp and buffer */
1028 if (fio_rdmaio_setup_qp(td) != 0)
1031 if (fio_rdmaio_setup_control_msg_buffers(td) != 0)
1035 err = ibv_post_recv(rd->qp, &rd->rq_wr, &bad_wr);
1037 log_err("fio: ibv_post_recv fail: %d\n", err);
1044 static int fio_rdmaio_setup_listen(struct thread_data *td, short port)
1046 struct rdmaio_data *rd = td->io_ops->data;
1047 struct ibv_recv_wr *bad_wr;
1049 rd->addr.sin_family = AF_INET;
1050 rd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
1051 rd->addr.sin_port = htons(port);
1054 if (rdma_bind_addr(rd->cm_id, (struct sockaddr *)&rd->addr) != 0) {
1055 log_err("fio: rdma_bind_addr fail\n");
1059 if (rdma_listen(rd->cm_id, 3) != 0) {
1060 log_err("fio: rdma_listen fail\n");
1064 /* wait for CONNECT_REQUEST */
1065 if (get_next_channel_event
1066 (td, rd->cm_channel, RDMA_CM_EVENT_CONNECT_REQUEST) != 0) {
1067 log_err("fio: wait for RDMA_CM_EVENT_CONNECT_REQUEST\n");
1071 if (fio_rdmaio_setup_qp(td) != 0)
1074 if (fio_rdmaio_setup_control_msg_buffers(td) != 0)
1078 if (ibv_post_recv(rd->qp, &rd->rq_wr, &bad_wr) != 0) {
1079 log_err("fio: ibv_post_recv fail\n");
1086 static int check_set_rlimits(struct thread_data *td)
1088 #ifdef CONFIG_RLIMIT_MEMLOCK
1091 /* check RLIMIT_MEMLOCK */
1092 if (getrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
1093 log_err("fio: getrlimit fail: %d(%s)\n",
1094 errno, strerror(errno));
1099 if ((rl.rlim_cur != RLIM_INFINITY)
1100 && (rl.rlim_cur < td->orig_buffer_size)) {
1101 log_err("fio: soft RLIMIT_MEMLOCK is: %" PRId64 "\n",
1103 log_err("fio: total block size is: %zd\n",
1104 td->orig_buffer_size);
1105 /* try to set larger RLIMIT_MEMLOCK */
1106 rl.rlim_cur = rl.rlim_max;
1107 if (setrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
1108 log_err("fio: setrlimit fail: %d(%s)\n",
1109 errno, strerror(errno));
1110 log_err("fio: you may try enlarge MEMLOCK by root\n");
1111 log_err("# ulimit -l unlimited\n");
1120 static int compat_options(struct thread_data *td)
1122 // The original RDMA engine had an ugly / seperator
1123 // on the filename for it's options. This function
1124 // retains backwards compatibility with it.100
1126 struct rdmaio_options *o = td->eo;
1127 char *modep, *portp;
1128 char *filename = td->o.filename;
1133 portp = strchr(filename, '/');
1140 o->port = strtol(portp, NULL, 10);
1141 if (!o->port || o->port > 65535)
1144 modep = strchr(portp, '/');
1145 if (modep != NULL) {
1151 if (!strncmp("rdma_write", modep, strlen(modep)) ||
1152 !strncmp("RDMA_WRITE", modep, strlen(modep)))
1153 o->verb = FIO_RDMA_MEM_WRITE;
1154 else if (!strncmp("rdma_read", modep, strlen(modep)) ||
1155 !strncmp("RDMA_READ", modep, strlen(modep)))
1156 o->verb = FIO_RDMA_MEM_READ;
1157 else if (!strncmp("send", modep, strlen(modep)) ||
1158 !strncmp("SEND", modep, strlen(modep)))
1159 o->verb = FIO_RDMA_CHA_SEND;
1163 o->verb = FIO_RDMA_MEM_WRITE;
1169 log_err("fio: bad rdma host/port/protocol: %s\n", td->o.filename);
1173 static int fio_rdmaio_init(struct thread_data *td)
1175 struct rdmaio_data *rd = td->io_ops->data;
1176 struct rdmaio_options *o = td->eo;
1177 unsigned int max_bs;
1181 log_err("fio: rdma connections must be read OR write\n");
1184 if (td_random(td)) {
1185 log_err("fio: RDMA network IO can't be random\n");
1189 if (compat_options(td))
1193 log_err("fio: no port has been specified which is required "
1194 "for the rdma engine\n");
1198 if (check_set_rlimits(td))
1201 rd->rdma_protocol = o->verb;
1202 rd->cq_event_num = 0;
1204 rd->cm_channel = rdma_create_event_channel();
1205 if (!rd->cm_channel) {
1206 log_err("fio: rdma_create_event_channel fail\n");
1210 ret = rdma_create_id(rd->cm_channel, &rd->cm_id, rd, RDMA_PS_TCP);
1212 log_err("fio: rdma_create_id fail\n");
1216 if ((rd->rdma_protocol == FIO_RDMA_MEM_WRITE) ||
1217 (rd->rdma_protocol == FIO_RDMA_MEM_READ)) {
1219 malloc(FIO_RDMA_MAX_IO_DEPTH * sizeof(struct remote_u));
1220 memset(rd->rmt_us, 0,
1221 FIO_RDMA_MAX_IO_DEPTH * sizeof(struct remote_u));
1225 rd->io_us_queued = malloc(td->o.iodepth * sizeof(struct io_u *));
1226 memset(rd->io_us_queued, 0, td->o.iodepth * sizeof(struct io_u *));
1227 rd->io_u_queued_nr = 0;
1229 rd->io_us_flight = malloc(td->o.iodepth * sizeof(struct io_u *));
1230 memset(rd->io_us_flight, 0, td->o.iodepth * sizeof(struct io_u *));
1231 rd->io_u_flight_nr = 0;
1233 rd->io_us_completed = malloc(td->o.iodepth * sizeof(struct io_u *));
1234 memset(rd->io_us_completed, 0, td->o.iodepth * sizeof(struct io_u *));
1235 rd->io_u_completed_nr = 0;
1237 if (td_read(td)) { /* READ as the server */
1239 /* server rd->rdma_buf_len will be setup after got request */
1240 ret = fio_rdmaio_setup_listen(td, o->port);
1241 } else { /* WRITE as the client */
1243 ret = fio_rdmaio_setup_connect(td, td->o.filename, o->port);
1246 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
1247 /* register each io_u in the free list */
1248 for (i = 0; i < td->io_u_freelist.nr; i++) {
1249 struct io_u *io_u = td->io_u_freelist.io_us[i];
1251 io_u->engine_data = malloc(sizeof(struct rdma_io_u_data));
1252 memset(io_u->engine_data, 0, sizeof(struct rdma_io_u_data));
1253 ((struct rdma_io_u_data *)io_u->engine_data)->wr_id = i;
1255 io_u->mr = ibv_reg_mr(rd->pd, io_u->buf, max_bs,
1256 IBV_ACCESS_LOCAL_WRITE |
1257 IBV_ACCESS_REMOTE_READ |
1258 IBV_ACCESS_REMOTE_WRITE);
1259 if (io_u->mr == NULL) {
1260 log_err("fio: ibv_reg_mr io_u failed\n");
1264 rd->send_buf.rmt_us[i].buf =
1265 htonll((uint64_t) (unsigned long)io_u->buf);
1266 rd->send_buf.rmt_us[i].rkey = htonl(io_u->mr->rkey);
1267 rd->send_buf.rmt_us[i].size = htonl(max_bs);
1270 log_info("fio: Send rkey %x addr %" PRIx64 " len %d to client\n", io_u->mr->rkey, io_u->buf, max_bs); */
1274 rd->send_buf.nr = htonl(i);
1279 static void fio_rdmaio_cleanup(struct thread_data *td)
1281 struct rdmaio_data *rd = td->io_ops->data;
1287 static int fio_rdmaio_setup(struct thread_data *td)
1289 struct rdmaio_data *rd;
1291 if (!td->files_index) {
1292 add_file(td, td->o.filename ?: "rdma", 0, 0);
1293 td->o.nr_files = td->o.nr_files ?: 1;
1297 if (!td->io_ops->data) {
1298 rd = malloc(sizeof(*rd));
1300 memset(rd, 0, sizeof(*rd));
1301 init_rand_seed(&rd->rand_state, (unsigned int) GOLDEN_RATIO_PRIME, 0);
1302 td->io_ops->data = rd;
1308 static struct ioengine_ops ioengine_rw = {
1310 .version = FIO_IOOPS_VERSION,
1311 .setup = fio_rdmaio_setup,
1312 .init = fio_rdmaio_init,
1313 .prep = fio_rdmaio_prep,
1314 .queue = fio_rdmaio_queue,
1315 .commit = fio_rdmaio_commit,
1316 .getevents = fio_rdmaio_getevents,
1317 .event = fio_rdmaio_event,
1318 .cleanup = fio_rdmaio_cleanup,
1319 .open_file = fio_rdmaio_open_file,
1320 .close_file = fio_rdmaio_close_file,
1321 .flags = FIO_DISKLESSIO | FIO_UNIDIR | FIO_PIPEIO,
1323 .option_struct_size = sizeof(struct rdmaio_options),
1326 static void fio_init fio_rdmaio_register(void)
1328 register_ioengine(&ioengine_rw);
1331 static void fio_exit fio_rdmaio_unregister(void)
1333 unregister_ioengine(&ioengine_rw);