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 uint32_t max_bs; /* maximum block size */
141 struct remote_u rmt_us[FIO_RDMA_MAX_IO_DEPTH];
144 struct rdma_io_u_data {
146 struct ibv_send_wr sq_wr;
147 struct ibv_recv_wr rq_wr;
148 struct ibv_sge rdma_sgl;
153 enum rdma_io_mode rdma_protocol;
155 struct sockaddr_in addr;
157 struct ibv_recv_wr rq_wr;
158 struct ibv_sge recv_sgl;
159 struct rdma_info_blk recv_buf;
160 struct ibv_mr *recv_mr;
162 struct ibv_send_wr sq_wr;
163 struct ibv_sge send_sgl;
164 struct rdma_info_blk send_buf;
165 struct ibv_mr *send_mr;
167 struct ibv_comp_channel *channel;
173 struct rdma_event_channel *cm_channel;
174 struct rdma_cm_id *cm_id;
175 struct rdma_cm_id *child_cm_id;
179 struct remote_u *rmt_us;
181 struct io_u **io_us_queued;
183 struct io_u **io_us_flight;
185 struct io_u **io_us_completed;
186 int io_u_completed_nr;
188 struct frand_state rand_state;
191 static int client_recv(struct thread_data *td, struct ibv_wc *wc)
193 struct rdmaio_data *rd = td->io_ops->data;
196 if (wc->byte_len != sizeof(rd->recv_buf)) {
197 log_err("Received bogus data, size %d\n", wc->byte_len);
201 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
202 if (max_bs > ntohl(rd->recv_buf.max_bs)) {
203 log_err("fio: Server's block size (%d) must be greater than or "
204 "equal to the client's block size (%d)!\n",
205 ntohl(rd->recv_buf.max_bs), max_bs);
209 /* store mr info for MEMORY semantic */
210 if ((rd->rdma_protocol == FIO_RDMA_MEM_WRITE) ||
211 (rd->rdma_protocol == FIO_RDMA_MEM_READ)) {
212 /* struct flist_head *entry; */
215 rd->rmt_nr = ntohl(rd->recv_buf.nr);
217 for (i = 0; i < rd->rmt_nr; i++) {
218 rd->rmt_us[i].buf = ntohll(rd->recv_buf.rmt_us[i].buf);
219 rd->rmt_us[i].rkey = ntohl(rd->recv_buf.rmt_us[i].rkey);
220 rd->rmt_us[i].size = ntohl(rd->recv_buf.rmt_us[i].size);
223 "fio: Received rkey %x addr %" PRIx64
224 " len %d from peer\n", rd->rmt_us[i].rkey,
225 rd->rmt_us[i].buf, rd->rmt_us[i].size);
232 static int server_recv(struct thread_data *td, struct ibv_wc *wc)
234 struct rdmaio_data *rd = td->io_ops->data;
237 if (wc->wr_id == FIO_RDMA_MAX_IO_DEPTH) {
238 rd->rdma_protocol = ntohl(rd->recv_buf.mode);
240 /* CHANNEL semantic, do nothing */
241 if (rd->rdma_protocol == FIO_RDMA_CHA_SEND)
242 rd->rdma_protocol = FIO_RDMA_CHA_RECV;
244 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
245 if (max_bs < ntohl(rd->recv_buf.max_bs)) {
246 log_err("fio: Server's block size (%d) must be greater than or "
247 "equal to the client's block size (%d)!\n",
248 ntohl(rd->recv_buf.max_bs), max_bs);
257 static int cq_event_handler(struct thread_data *td, enum ibv_wc_opcode opcode)
259 struct rdmaio_data *rd = td->io_ops->data;
261 struct rdma_io_u_data *r_io_u_d;
266 while ((ret = ibv_poll_cq(rd->cq, 1, &wc)) == 1) {
271 log_err("fio: cq completion status %d(%s)\n",
272 wc.status, ibv_wc_status_str(wc.status));
279 if (rd->is_client == 1)
280 ret = client_recv(td, &wc);
282 ret = server_recv(td, &wc);
287 if (wc.wr_id == FIO_RDMA_MAX_IO_DEPTH)
290 for (i = 0; i < rd->io_u_flight_nr; i++) {
291 r_io_u_d = rd->io_us_flight[i]->engine_data;
293 if (wc.wr_id == r_io_u_d->rq_wr.wr_id) {
294 rd->io_us_flight[i]->resid =
295 rd->io_us_flight[i]->buflen
298 rd->io_us_flight[i]->error = 0;
300 rd->io_us_completed[rd->
302 = rd->io_us_flight[i];
303 rd->io_u_completed_nr++;
307 if (i == rd->io_u_flight_nr)
308 log_err("fio: recv wr %" PRId64 " not found\n",
311 /* put the last one into middle of the list */
312 rd->io_us_flight[i] =
313 rd->io_us_flight[rd->io_u_flight_nr - 1];
314 rd->io_u_flight_nr--;
320 case IBV_WC_RDMA_WRITE:
321 case IBV_WC_RDMA_READ:
322 if (wc.wr_id == FIO_RDMA_MAX_IO_DEPTH)
325 for (i = 0; i < rd->io_u_flight_nr; i++) {
326 r_io_u_d = rd->io_us_flight[i]->engine_data;
328 if (wc.wr_id == r_io_u_d->sq_wr.wr_id) {
329 rd->io_us_completed[rd->
331 = rd->io_us_flight[i];
332 rd->io_u_completed_nr++;
336 if (i == rd->io_u_flight_nr)
337 log_err("fio: send wr %" PRId64 " not found\n",
340 /* put the last one into middle of the list */
341 rd->io_us_flight[i] =
342 rd->io_us_flight[rd->io_u_flight_nr - 1];
343 rd->io_u_flight_nr--;
349 log_info("fio: unknown completion event %d\n",
357 log_err("fio: poll error %d\n", ret);
365 * Return -1 for error and 'nr events' for a positive number
368 static int rdma_poll_wait(struct thread_data *td, enum ibv_wc_opcode opcode)
370 struct rdmaio_data *rd = td->io_ops->data;
371 struct ibv_cq *ev_cq;
375 if (rd->cq_event_num > 0) { /* previous left */
381 if (ibv_get_cq_event(rd->channel, &ev_cq, &ev_ctx) != 0) {
382 log_err("fio: Failed to get cq event!\n");
385 if (ev_cq != rd->cq) {
386 log_err("fio: Unknown CQ!\n");
389 if (ibv_req_notify_cq(rd->cq, 0) != 0) {
390 log_err("fio: Failed to set notify!\n");
394 ret = cq_event_handler(td, opcode);
398 ibv_ack_cq_events(rd->cq, ret);
405 static int fio_rdmaio_setup_qp(struct thread_data *td)
407 struct rdmaio_data *rd = td->io_ops->data;
408 struct ibv_qp_init_attr init_attr;
409 int qp_depth = td->o.iodepth * 2; /* 2 times of io depth */
411 if (rd->is_client == 0)
412 rd->pd = ibv_alloc_pd(rd->child_cm_id->verbs);
414 rd->pd = ibv_alloc_pd(rd->cm_id->verbs);
416 if (rd->pd == NULL) {
417 log_err("fio: ibv_alloc_pd fail\n");
421 if (rd->is_client == 0)
422 rd->channel = ibv_create_comp_channel(rd->child_cm_id->verbs);
424 rd->channel = ibv_create_comp_channel(rd->cm_id->verbs);
425 if (rd->channel == NULL) {
426 log_err("fio: ibv_create_comp_channel fail\n");
433 if (rd->is_client == 0)
434 rd->cq = ibv_create_cq(rd->child_cm_id->verbs,
435 qp_depth, rd, rd->channel, 0);
437 rd->cq = ibv_create_cq(rd->cm_id->verbs,
438 qp_depth, rd, rd->channel, 0);
439 if (rd->cq == NULL) {
440 log_err("fio: ibv_create_cq failed\n");
444 if (ibv_req_notify_cq(rd->cq, 0) != 0) {
445 log_err("fio: ibv_create_cq failed\n");
449 /* create queue pair */
450 memset(&init_attr, 0, sizeof(init_attr));
451 init_attr.cap.max_send_wr = qp_depth;
452 init_attr.cap.max_recv_wr = qp_depth;
453 init_attr.cap.max_recv_sge = 1;
454 init_attr.cap.max_send_sge = 1;
455 init_attr.qp_type = IBV_QPT_RC;
456 init_attr.send_cq = rd->cq;
457 init_attr.recv_cq = rd->cq;
459 if (rd->is_client == 0) {
460 if (rdma_create_qp(rd->child_cm_id, rd->pd, &init_attr) != 0) {
461 log_err("fio: rdma_create_qp failed\n");
464 rd->qp = rd->child_cm_id->qp;
466 if (rdma_create_qp(rd->cm_id, rd->pd, &init_attr) != 0) {
467 log_err("fio: rdma_create_qp failed\n");
470 rd->qp = rd->cm_id->qp;
476 ibv_destroy_cq(rd->cq);
478 ibv_destroy_comp_channel(rd->channel);
480 ibv_dealloc_pd(rd->pd);
485 static int fio_rdmaio_setup_control_msg_buffers(struct thread_data *td)
487 struct rdmaio_data *rd = td->io_ops->data;
489 rd->recv_mr = ibv_reg_mr(rd->pd, &rd->recv_buf, sizeof(rd->recv_buf),
490 IBV_ACCESS_LOCAL_WRITE);
491 if (rd->recv_mr == NULL) {
492 log_err("fio: recv_buf reg_mr failed\n");
496 rd->send_mr = ibv_reg_mr(rd->pd, &rd->send_buf, sizeof(rd->send_buf),
498 if (rd->send_mr == NULL) {
499 log_err("fio: send_buf reg_mr failed\n");
500 ibv_dereg_mr(rd->recv_mr);
504 /* setup work request */
506 rd->recv_sgl.addr = (uint64_t) (unsigned long)&rd->recv_buf;
507 rd->recv_sgl.length = sizeof(rd->recv_buf);
508 rd->recv_sgl.lkey = rd->recv_mr->lkey;
509 rd->rq_wr.sg_list = &rd->recv_sgl;
510 rd->rq_wr.num_sge = 1;
511 rd->rq_wr.wr_id = FIO_RDMA_MAX_IO_DEPTH;
514 rd->send_sgl.addr = (uint64_t) (unsigned long)&rd->send_buf;
515 rd->send_sgl.length = sizeof(rd->send_buf);
516 rd->send_sgl.lkey = rd->send_mr->lkey;
518 rd->sq_wr.opcode = IBV_WR_SEND;
519 rd->sq_wr.send_flags = IBV_SEND_SIGNALED;
520 rd->sq_wr.sg_list = &rd->send_sgl;
521 rd->sq_wr.num_sge = 1;
522 rd->sq_wr.wr_id = FIO_RDMA_MAX_IO_DEPTH;
527 static int get_next_channel_event(struct thread_data *td,
528 struct rdma_event_channel *channel,
529 enum rdma_cm_event_type wait_event)
531 struct rdmaio_data *rd = td->io_ops->data;
532 struct rdma_cm_event *event;
535 ret = rdma_get_cm_event(channel, &event);
537 log_err("fio: rdma_get_cm_event: %d\n", ret);
541 if (event->event != wait_event) {
542 log_err("fio: event is %s instead of %s\n",
543 rdma_event_str(event->event),
544 rdma_event_str(wait_event));
548 switch (event->event) {
549 case RDMA_CM_EVENT_CONNECT_REQUEST:
550 rd->child_cm_id = event->id;
556 rdma_ack_cm_event(event);
561 static int fio_rdmaio_prep(struct thread_data *td, struct io_u *io_u)
563 struct rdmaio_data *rd = td->io_ops->data;
564 struct rdma_io_u_data *r_io_u_d;
566 r_io_u_d = io_u->engine_data;
568 switch (rd->rdma_protocol) {
569 case FIO_RDMA_MEM_WRITE:
570 case FIO_RDMA_MEM_READ:
571 r_io_u_d->rdma_sgl.addr = (uint64_t) (unsigned long)io_u->buf;
572 r_io_u_d->rdma_sgl.lkey = io_u->mr->lkey;
573 r_io_u_d->sq_wr.wr_id = r_io_u_d->wr_id;
574 r_io_u_d->sq_wr.send_flags = IBV_SEND_SIGNALED;
575 r_io_u_d->sq_wr.sg_list = &r_io_u_d->rdma_sgl;
576 r_io_u_d->sq_wr.num_sge = 1;
578 case FIO_RDMA_CHA_SEND:
579 r_io_u_d->rdma_sgl.addr = (uint64_t) (unsigned long)io_u->buf;
580 r_io_u_d->rdma_sgl.lkey = io_u->mr->lkey;
581 r_io_u_d->rdma_sgl.length = io_u->buflen;
582 r_io_u_d->sq_wr.wr_id = r_io_u_d->wr_id;
583 r_io_u_d->sq_wr.opcode = IBV_WR_SEND;
584 r_io_u_d->sq_wr.send_flags = IBV_SEND_SIGNALED;
585 r_io_u_d->sq_wr.sg_list = &r_io_u_d->rdma_sgl;
586 r_io_u_d->sq_wr.num_sge = 1;
588 case FIO_RDMA_CHA_RECV:
589 r_io_u_d->rdma_sgl.addr = (uint64_t) (unsigned long)io_u->buf;
590 r_io_u_d->rdma_sgl.lkey = io_u->mr->lkey;
591 r_io_u_d->rdma_sgl.length = io_u->buflen;
592 r_io_u_d->rq_wr.wr_id = r_io_u_d->wr_id;
593 r_io_u_d->rq_wr.sg_list = &r_io_u_d->rdma_sgl;
594 r_io_u_d->rq_wr.num_sge = 1;
597 log_err("fio: unknown rdma protocol - %d\n", rd->rdma_protocol);
604 static struct io_u *fio_rdmaio_event(struct thread_data *td, int event)
606 struct rdmaio_data *rd = td->io_ops->data;
610 io_u = rd->io_us_completed[0];
611 for (i = 0; i < rd->io_u_completed_nr - 1; i++)
612 rd->io_us_completed[i] = rd->io_us_completed[i + 1];
614 rd->io_u_completed_nr--;
616 dprint_io_u(io_u, "fio_rdmaio_event");
621 static int fio_rdmaio_getevents(struct thread_data *td, unsigned int min,
622 unsigned int max, const struct timespec *t)
624 struct rdmaio_data *rd = td->io_ops->data;
625 enum ibv_wc_opcode comp_opcode;
626 struct ibv_cq *ev_cq;
629 comp_opcode = IBV_WC_RDMA_WRITE;
631 switch (rd->rdma_protocol) {
632 case FIO_RDMA_MEM_WRITE:
633 comp_opcode = IBV_WC_RDMA_WRITE;
635 case FIO_RDMA_MEM_READ:
636 comp_opcode = IBV_WC_RDMA_READ;
638 case FIO_RDMA_CHA_SEND:
639 comp_opcode = IBV_WC_SEND;
641 case FIO_RDMA_CHA_RECV:
642 comp_opcode = IBV_WC_RECV;
645 log_err("fio: unknown rdma protocol - %d\n", rd->rdma_protocol);
649 if (rd->cq_event_num > 0) { /* previous left */
655 if (ibv_get_cq_event(rd->channel, &ev_cq, &ev_ctx) != 0) {
656 log_err("fio: Failed to get cq event!\n");
659 if (ev_cq != rd->cq) {
660 log_err("fio: Unknown CQ!\n");
663 if (ibv_req_notify_cq(rd->cq, 0) != 0) {
664 log_err("fio: Failed to set notify!\n");
668 ret = cq_event_handler(td, comp_opcode);
672 ibv_ack_cq_events(rd->cq, ret);
678 rd->cq_event_num -= r;
683 static int fio_rdmaio_send(struct thread_data *td, struct io_u **io_us,
686 struct rdmaio_data *rd = td->io_ops->data;
687 struct ibv_send_wr *bad_wr;
689 enum ibv_wc_opcode comp_opcode;
690 comp_opcode = IBV_WC_RDMA_WRITE;
694 struct rdma_io_u_data *r_io_u_d;
698 for (i = 0; i < nr; i++) {
699 /* RDMA_WRITE or RDMA_READ */
700 switch (rd->rdma_protocol) {
701 case FIO_RDMA_MEM_WRITE:
702 /* compose work request */
703 r_io_u_d = io_us[i]->engine_data;
704 index = __rand(&rd->rand_state) % rd->rmt_nr;
705 r_io_u_d->sq_wr.opcode = IBV_WR_RDMA_WRITE;
706 r_io_u_d->sq_wr.wr.rdma.rkey = rd->rmt_us[index].rkey;
707 r_io_u_d->sq_wr.wr.rdma.remote_addr = \
708 rd->rmt_us[index].buf;
709 r_io_u_d->sq_wr.sg_list->length = io_us[i]->buflen;
711 case FIO_RDMA_MEM_READ:
712 /* compose work request */
713 r_io_u_d = io_us[i]->engine_data;
714 index = __rand(&rd->rand_state) % rd->rmt_nr;
715 r_io_u_d->sq_wr.opcode = IBV_WR_RDMA_READ;
716 r_io_u_d->sq_wr.wr.rdma.rkey = rd->rmt_us[index].rkey;
717 r_io_u_d->sq_wr.wr.rdma.remote_addr = \
718 rd->rmt_us[index].buf;
719 r_io_u_d->sq_wr.sg_list->length = io_us[i]->buflen;
721 case FIO_RDMA_CHA_SEND:
722 r_io_u_d = io_us[i]->engine_data;
723 r_io_u_d->sq_wr.opcode = IBV_WR_SEND;
724 r_io_u_d->sq_wr.send_flags = IBV_SEND_SIGNALED;
727 log_err("fio: unknown rdma protocol - %d\n",
732 if (ibv_post_send(rd->qp, &r_io_u_d->sq_wr, &bad_wr) != 0) {
733 log_err("fio: ibv_post_send fail\n");
737 dprint_io_u(io_us[i], "fio_rdmaio_send");
740 /* wait for completion
741 rdma_poll_wait(td, comp_opcode); */
746 static int fio_rdmaio_recv(struct thread_data *td, struct io_u **io_us,
749 struct rdmaio_data *rd = td->io_ops->data;
750 struct ibv_recv_wr *bad_wr;
751 struct rdma_io_u_data *r_io_u_d;
755 if (rd->rdma_protocol == FIO_RDMA_CHA_RECV) {
756 /* post io_u into recv queue */
757 for (i = 0; i < nr; i++) {
758 r_io_u_d = io_us[i]->engine_data;
759 if (ibv_post_recv(rd->qp, &r_io_u_d->rq_wr, &bad_wr) !=
761 log_err("fio: ibv_post_recv fail\n");
765 } else if ((rd->rdma_protocol == FIO_RDMA_MEM_READ)
766 || (rd->rdma_protocol == FIO_RDMA_MEM_WRITE)) {
767 /* re-post the rq_wr */
768 if (ibv_post_recv(rd->qp, &rd->rq_wr, &bad_wr) != 0) {
769 log_err("fio: ibv_post_recv fail\n");
773 rdma_poll_wait(td, IBV_WC_RECV);
775 dprint(FD_IO, "fio: recv FINISH message\n");
783 static int fio_rdmaio_queue(struct thread_data *td, struct io_u *io_u)
785 struct rdmaio_data *rd = td->io_ops->data;
787 fio_ro_check(td, io_u);
789 if (rd->io_u_queued_nr == (int)td->o.iodepth)
792 rd->io_us_queued[rd->io_u_queued_nr] = io_u;
793 rd->io_u_queued_nr++;
795 dprint_io_u(io_u, "fio_rdmaio_queue");
800 static void fio_rdmaio_queued(struct thread_data *td, struct io_u **io_us,
803 struct rdmaio_data *rd = td->io_ops->data;
807 if (!fio_fill_issue_time(td))
810 fio_gettime(&now, NULL);
812 for (i = 0; i < nr; i++) {
813 struct io_u *io_u = io_us[i];
815 /* queued -> flight */
816 rd->io_us_flight[rd->io_u_flight_nr] = io_u;
817 rd->io_u_flight_nr++;
819 memcpy(&io_u->issue_time, &now, sizeof(now));
820 io_u_queued(td, io_u);
824 static int fio_rdmaio_commit(struct thread_data *td)
826 struct rdmaio_data *rd = td->io_ops->data;
830 if (!rd->io_us_queued)
833 io_us = rd->io_us_queued;
835 /* RDMA_WRITE or RDMA_READ */
837 ret = fio_rdmaio_send(td, io_us, rd->io_u_queued_nr);
838 else if (!rd->is_client)
839 ret = fio_rdmaio_recv(td, io_us, rd->io_u_queued_nr);
841 ret = 0; /* must be a SYNC */
844 fio_rdmaio_queued(td, io_us, ret);
845 io_u_mark_submit(td, ret);
846 rd->io_u_queued_nr -= ret;
851 } while (rd->io_u_queued_nr);
856 static int fio_rdmaio_connect(struct thread_data *td, struct fio_file *f)
858 struct rdmaio_data *rd = td->io_ops->data;
859 struct rdma_conn_param conn_param;
860 struct ibv_send_wr *bad_wr;
862 memset(&conn_param, 0, sizeof(conn_param));
863 conn_param.responder_resources = 1;
864 conn_param.initiator_depth = 1;
865 conn_param.retry_count = 10;
867 if (rdma_connect(rd->cm_id, &conn_param) != 0) {
868 log_err("fio: rdma_connect fail\n");
872 if (get_next_channel_event
873 (td, rd->cm_channel, RDMA_CM_EVENT_ESTABLISHED) != 0) {
874 log_err("fio: wait for RDMA_CM_EVENT_ESTABLISHED\n");
878 /* send task request */
879 rd->send_buf.mode = htonl(rd->rdma_protocol);
880 rd->send_buf.nr = htonl(td->o.iodepth);
882 if (ibv_post_send(rd->qp, &rd->sq_wr, &bad_wr) != 0) {
883 log_err("fio: ibv_post_send fail");
887 if (rdma_poll_wait(td, IBV_WC_SEND) < 0)
890 /* wait for remote MR info from server side */
891 if (rdma_poll_wait(td, IBV_WC_RECV) < 0)
894 /* In SEND/RECV test, it's a good practice to setup the iodepth of
895 * of the RECV side deeper than that of the SEND side to
896 * avoid RNR (receiver not ready) error. The
897 * SEND side may send so many unsolicited message before
898 * RECV side commits sufficient recv buffers into recv queue.
899 * This may lead to RNR error. Here, SEND side pauses for a while
900 * during which RECV side commits sufficient recv buffers.
907 static int fio_rdmaio_accept(struct thread_data *td, struct fio_file *f)
909 struct rdmaio_data *rd = td->io_ops->data;
910 struct rdma_conn_param conn_param;
911 struct ibv_send_wr *bad_wr;
914 /* rdma_accept() - then wait for accept success */
915 memset(&conn_param, 0, sizeof(conn_param));
916 conn_param.responder_resources = 1;
917 conn_param.initiator_depth = 1;
919 if (rdma_accept(rd->child_cm_id, &conn_param) != 0) {
920 log_err("fio: rdma_accept\n");
924 if (get_next_channel_event
925 (td, rd->cm_channel, RDMA_CM_EVENT_ESTABLISHED) != 0) {
926 log_err("fio: wait for RDMA_CM_EVENT_ESTABLISHED\n");
930 /* wait for request */
931 ret = rdma_poll_wait(td, IBV_WC_RECV) < 0;
933 if (ibv_post_send(rd->qp, &rd->sq_wr, &bad_wr) != 0) {
934 log_err("fio: ibv_post_send fail");
938 if (rdma_poll_wait(td, IBV_WC_SEND) < 0)
944 static int fio_rdmaio_open_file(struct thread_data *td, struct fio_file *f)
947 return fio_rdmaio_accept(td, f);
949 return fio_rdmaio_connect(td, f);
952 static int fio_rdmaio_close_file(struct thread_data *td, struct fio_file *f)
954 struct rdmaio_data *rd = td->io_ops->data;
955 struct ibv_send_wr *bad_wr;
957 /* unregister rdma buffer */
960 * Client sends notification to the server side
962 /* refer to: http://linux.die.net/man/7/rdma_cm */
963 if ((rd->is_client == 1) && ((rd->rdma_protocol == FIO_RDMA_MEM_WRITE)
964 || (rd->rdma_protocol ==
965 FIO_RDMA_MEM_READ))) {
966 if (ibv_post_send(rd->qp, &rd->sq_wr, &bad_wr) != 0) {
967 log_err("fio: ibv_post_send fail");
971 dprint(FD_IO, "fio: close information sent success\n");
972 rdma_poll_wait(td, IBV_WC_SEND);
975 if (rd->is_client == 1)
976 rdma_disconnect(rd->cm_id);
978 rdma_disconnect(rd->child_cm_id);
980 rdma_disconnect(rd->cm_id);
985 if (get_next_channel_event(td, rd->cm_channel, RDMA_CM_EVENT_DISCONNECTED) != 0) {
986 log_err("fio: wait for RDMA_CM_EVENT_DISCONNECTED\n");
991 ibv_destroy_cq(rd->cq);
992 ibv_destroy_qp(rd->qp);
994 if (rd->is_client == 1)
995 rdma_destroy_id(rd->cm_id);
997 rdma_destroy_id(rd->child_cm_id);
998 rdma_destroy_id(rd->cm_id);
1001 ibv_destroy_comp_channel(rd->channel);
1002 ibv_dealloc_pd(rd->pd);
1007 static int fio_rdmaio_setup_connect(struct thread_data *td, const char *host,
1008 unsigned short port)
1010 struct rdmaio_data *rd = td->io_ops->data;
1011 struct ibv_recv_wr *bad_wr;
1014 rd->addr.sin_family = AF_INET;
1015 rd->addr.sin_port = htons(port);
1017 if (inet_aton(host, &rd->addr.sin_addr) != 1) {
1018 struct hostent *hent;
1020 hent = gethostbyname(host);
1022 td_verror(td, errno, "gethostbyname");
1026 memcpy(&rd->addr.sin_addr, hent->h_addr, 4);
1030 err = rdma_resolve_addr(rd->cm_id, NULL, (struct sockaddr *)&rd->addr, 2000);
1032 log_err("fio: rdma_resolve_addr: %d\n", err);
1036 err = get_next_channel_event(td, rd->cm_channel, RDMA_CM_EVENT_ADDR_RESOLVED);
1038 log_err("fio: get_next_channel_event: %d\n", err);
1043 err = rdma_resolve_route(rd->cm_id, 2000);
1045 log_err("fio: rdma_resolve_route: %d\n", err);
1049 err = get_next_channel_event(td, rd->cm_channel, RDMA_CM_EVENT_ROUTE_RESOLVED);
1051 log_err("fio: get_next_channel_event: %d\n", err);
1055 /* create qp and buffer */
1056 if (fio_rdmaio_setup_qp(td) != 0)
1059 if (fio_rdmaio_setup_control_msg_buffers(td) != 0)
1063 err = ibv_post_recv(rd->qp, &rd->rq_wr, &bad_wr);
1065 log_err("fio: ibv_post_recv fail: %d\n", err);
1072 static int fio_rdmaio_setup_listen(struct thread_data *td, short port)
1074 struct rdmaio_data *rd = td->io_ops->data;
1075 struct ibv_recv_wr *bad_wr;
1076 int state = td->runstate;
1078 td_set_runstate(td, TD_SETTING_UP);
1080 rd->addr.sin_family = AF_INET;
1081 rd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
1082 rd->addr.sin_port = htons(port);
1085 if (rdma_bind_addr(rd->cm_id, (struct sockaddr *)&rd->addr) != 0) {
1086 log_err("fio: rdma_bind_addr fail\n");
1090 if (rdma_listen(rd->cm_id, 3) != 0) {
1091 log_err("fio: rdma_listen fail\n");
1095 log_info("fio: waiting for connection\n");
1097 /* wait for CONNECT_REQUEST */
1098 if (get_next_channel_event
1099 (td, rd->cm_channel, RDMA_CM_EVENT_CONNECT_REQUEST) != 0) {
1100 log_err("fio: wait for RDMA_CM_EVENT_CONNECT_REQUEST\n");
1104 if (fio_rdmaio_setup_qp(td) != 0)
1107 if (fio_rdmaio_setup_control_msg_buffers(td) != 0)
1111 if (ibv_post_recv(rd->qp, &rd->rq_wr, &bad_wr) != 0) {
1112 log_err("fio: ibv_post_recv fail\n");
1116 td_set_runstate(td, state);
1120 static int check_set_rlimits(struct thread_data *td)
1122 #ifdef CONFIG_RLIMIT_MEMLOCK
1125 /* check RLIMIT_MEMLOCK */
1126 if (getrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
1127 log_err("fio: getrlimit fail: %d(%s)\n",
1128 errno, strerror(errno));
1133 if ((rl.rlim_cur != RLIM_INFINITY)
1134 && (rl.rlim_cur < td->orig_buffer_size)) {
1135 log_err("fio: soft RLIMIT_MEMLOCK is: %" PRId64 "\n",
1137 log_err("fio: total block size is: %zd\n",
1138 td->orig_buffer_size);
1139 /* try to set larger RLIMIT_MEMLOCK */
1140 rl.rlim_cur = rl.rlim_max;
1141 if (setrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
1142 log_err("fio: setrlimit fail: %d(%s)\n",
1143 errno, strerror(errno));
1144 log_err("fio: you may try enlarge MEMLOCK by root\n");
1145 log_err("# ulimit -l unlimited\n");
1154 static int compat_options(struct thread_data *td)
1156 // The original RDMA engine had an ugly / seperator
1157 // on the filename for it's options. This function
1158 // retains backwards compatibility with it.100
1160 struct rdmaio_options *o = td->eo;
1161 char *modep, *portp;
1162 char *filename = td->o.filename;
1167 portp = strchr(filename, '/');
1174 o->port = strtol(portp, NULL, 10);
1175 if (!o->port || o->port > 65535)
1178 modep = strchr(portp, '/');
1179 if (modep != NULL) {
1185 if (!strncmp("rdma_write", modep, strlen(modep)) ||
1186 !strncmp("RDMA_WRITE", modep, strlen(modep)))
1187 o->verb = FIO_RDMA_MEM_WRITE;
1188 else if (!strncmp("rdma_read", modep, strlen(modep)) ||
1189 !strncmp("RDMA_READ", modep, strlen(modep)))
1190 o->verb = FIO_RDMA_MEM_READ;
1191 else if (!strncmp("send", modep, strlen(modep)) ||
1192 !strncmp("SEND", modep, strlen(modep)))
1193 o->verb = FIO_RDMA_CHA_SEND;
1197 o->verb = FIO_RDMA_MEM_WRITE;
1203 log_err("fio: bad rdma host/port/protocol: %s\n", td->o.filename);
1207 static int fio_rdmaio_init(struct thread_data *td)
1209 struct rdmaio_data *rd = td->io_ops->data;
1210 struct rdmaio_options *o = td->eo;
1211 unsigned int max_bs;
1215 log_err("fio: rdma connections must be read OR write\n");
1218 if (td_random(td)) {
1219 log_err("fio: RDMA network IO can't be random\n");
1223 if (compat_options(td))
1227 log_err("fio: no port has been specified which is required "
1228 "for the rdma engine\n");
1232 if (check_set_rlimits(td))
1235 rd->rdma_protocol = o->verb;
1236 rd->cq_event_num = 0;
1238 rd->cm_channel = rdma_create_event_channel();
1239 if (!rd->cm_channel) {
1240 log_err("fio: rdma_create_event_channel fail\n");
1244 ret = rdma_create_id(rd->cm_channel, &rd->cm_id, rd, RDMA_PS_TCP);
1246 log_err("fio: rdma_create_id fail\n");
1250 if ((rd->rdma_protocol == FIO_RDMA_MEM_WRITE) ||
1251 (rd->rdma_protocol == FIO_RDMA_MEM_READ)) {
1253 malloc(FIO_RDMA_MAX_IO_DEPTH * sizeof(struct remote_u));
1254 memset(rd->rmt_us, 0,
1255 FIO_RDMA_MAX_IO_DEPTH * sizeof(struct remote_u));
1259 rd->io_us_queued = malloc(td->o.iodepth * sizeof(struct io_u *));
1260 memset(rd->io_us_queued, 0, td->o.iodepth * sizeof(struct io_u *));
1261 rd->io_u_queued_nr = 0;
1263 rd->io_us_flight = malloc(td->o.iodepth * sizeof(struct io_u *));
1264 memset(rd->io_us_flight, 0, td->o.iodepth * sizeof(struct io_u *));
1265 rd->io_u_flight_nr = 0;
1267 rd->io_us_completed = malloc(td->o.iodepth * sizeof(struct io_u *));
1268 memset(rd->io_us_completed, 0, td->o.iodepth * sizeof(struct io_u *));
1269 rd->io_u_completed_nr = 0;
1271 if (td_read(td)) { /* READ as the server */
1273 td->flags |= TD_F_NO_PROGRESS;
1274 /* server rd->rdma_buf_len will be setup after got request */
1275 ret = fio_rdmaio_setup_listen(td, o->port);
1276 } else { /* WRITE as the client */
1278 ret = fio_rdmaio_setup_connect(td, td->o.filename, o->port);
1281 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
1282 rd->send_buf.max_bs = htonl(max_bs);
1284 /* register each io_u in the free list */
1285 for (i = 0; i < td->io_u_freelist.nr; i++) {
1286 struct io_u *io_u = td->io_u_freelist.io_us[i];
1288 io_u->engine_data = malloc(sizeof(struct rdma_io_u_data));
1289 memset(io_u->engine_data, 0, sizeof(struct rdma_io_u_data));
1290 ((struct rdma_io_u_data *)io_u->engine_data)->wr_id = i;
1292 io_u->mr = ibv_reg_mr(rd->pd, io_u->buf, max_bs,
1293 IBV_ACCESS_LOCAL_WRITE |
1294 IBV_ACCESS_REMOTE_READ |
1295 IBV_ACCESS_REMOTE_WRITE);
1296 if (io_u->mr == NULL) {
1297 log_err("fio: ibv_reg_mr io_u failed\n");
1301 rd->send_buf.rmt_us[i].buf =
1302 htonll((uint64_t) (unsigned long)io_u->buf);
1303 rd->send_buf.rmt_us[i].rkey = htonl(io_u->mr->rkey);
1304 rd->send_buf.rmt_us[i].size = htonl(max_bs);
1307 log_info("fio: Send rkey %x addr %" PRIx64 " len %d to client\n", io_u->mr->rkey, io_u->buf, max_bs); */
1311 rd->send_buf.nr = htonl(i);
1316 static void fio_rdmaio_cleanup(struct thread_data *td)
1318 struct rdmaio_data *rd = td->io_ops->data;
1324 static int fio_rdmaio_setup(struct thread_data *td)
1326 struct rdmaio_data *rd;
1328 if (!td->files_index) {
1329 add_file(td, td->o.filename ?: "rdma", 0, 0);
1330 td->o.nr_files = td->o.nr_files ?: 1;
1334 if (!td->io_ops->data) {
1335 rd = malloc(sizeof(*rd));
1337 memset(rd, 0, sizeof(*rd));
1338 init_rand_seed(&rd->rand_state, (unsigned int) GOLDEN_RATIO_PRIME, 0);
1339 td->io_ops->data = rd;
1345 static struct ioengine_ops ioengine_rw = {
1347 .version = FIO_IOOPS_VERSION,
1348 .setup = fio_rdmaio_setup,
1349 .init = fio_rdmaio_init,
1350 .prep = fio_rdmaio_prep,
1351 .queue = fio_rdmaio_queue,
1352 .commit = fio_rdmaio_commit,
1353 .getevents = fio_rdmaio_getevents,
1354 .event = fio_rdmaio_event,
1355 .cleanup = fio_rdmaio_cleanup,
1356 .open_file = fio_rdmaio_open_file,
1357 .close_file = fio_rdmaio_close_file,
1358 .flags = FIO_DISKLESSIO | FIO_UNIDIR | FIO_PIPEIO,
1360 .option_struct_size = sizeof(struct rdmaio_options),
1363 static void fio_init fio_rdmaio_register(void)
1365 register_ioengine(&ioengine_rw);
1368 static void fio_exit fio_rdmaio_unregister(void)
1370 unregister_ioengine(&ioengine_rw);