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/
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
28 #include <sys/types.h>
29 #include <sys/socket.h>
31 #include <sys/resource.h>
41 #include <rdma/rdma_cma.h>
42 #include <infiniband/arch.h>
44 #define FIO_RDMA_MAX_IO_DEPTH 128
60 struct rdma_info_blk {
61 uint32_t mode; /* channel semantic or memory semantic */
62 uint32_t nr; /* client: io depth
63 server: number of records for memory semantic
65 struct remote_u rmt_us[FIO_RDMA_MAX_IO_DEPTH];
68 struct rdma_io_u_data {
70 struct ibv_send_wr sq_wr;
71 struct ibv_recv_wr rq_wr;
72 struct ibv_sge rdma_sgl;
77 enum rdma_io_mode rdma_protocol;
79 struct sockaddr_in addr;
81 struct ibv_recv_wr rq_wr;
82 struct ibv_sge recv_sgl;
83 struct rdma_info_blk recv_buf;
84 struct ibv_mr *recv_mr;
86 struct ibv_send_wr sq_wr;
87 struct ibv_sge send_sgl;
88 struct rdma_info_blk send_buf;
89 struct ibv_mr *send_mr;
91 struct ibv_comp_channel *channel;
97 struct rdma_event_channel *cm_channel;
98 struct rdma_cm_id *cm_id;
99 struct rdma_cm_id *child_cm_id;
103 struct remote_u *rmt_us;
105 struct io_u **io_us_queued;
107 struct io_u **io_us_flight;
109 struct io_u **io_us_completed;
110 int io_u_completed_nr;
113 static int client_recv(struct thread_data *td, struct ibv_wc *wc)
115 struct rdmaio_data *rd = td->io_ops->data;
117 if (wc->byte_len != sizeof(rd->recv_buf)) {
118 log_err("Received bogus data, size %d\n", wc->byte_len);
122 /* store mr info for MEMORY semantic */
123 if ((rd->rdma_protocol == FIO_RDMA_MEM_WRITE) ||
124 (rd->rdma_protocol == FIO_RDMA_MEM_READ)) {
125 /* struct flist_head *entry; */
128 rd->rmt_nr = ntohl(rd->recv_buf.nr);
130 for (i = 0; i < rd->rmt_nr; i++) {
131 rd->rmt_us[i].buf = ntohll(rd->recv_buf.rmt_us[i].buf);
132 rd->rmt_us[i].rkey = ntohl(rd->recv_buf.rmt_us[i].rkey);
133 rd->rmt_us[i].size = ntohl(rd->recv_buf.rmt_us[i].size);
136 "fio: Received rkey %x addr %" PRIx64
137 " len %d from peer\n", rd->rmt_us[i].rkey,
138 rd->rmt_us[i].buf, rd->rmt_us[i].size);
145 static int server_recv(struct thread_data *td, struct ibv_wc *wc)
147 struct rdmaio_data *rd = td->io_ops->data;
149 if (wc->wr_id == FIO_RDMA_MAX_IO_DEPTH) {
150 rd->rdma_protocol = ntohl(rd->recv_buf.mode);
152 /* CHANNEL semantic, do nothing */
153 if (rd->rdma_protocol == FIO_RDMA_CHA_SEND)
154 rd->rdma_protocol = FIO_RDMA_CHA_RECV;
160 static int cq_event_handler(struct thread_data *td, enum ibv_wc_opcode opcode)
162 struct rdmaio_data *rd = td->io_ops->data;
164 struct rdma_io_u_data *r_io_u_d;
169 while ((ret = ibv_poll_cq(rd->cq, 1, &wc)) == 1) {
174 log_err("fio: cq completion status %d(%s)\n",
175 wc.status, ibv_wc_status_str(wc.status));
182 if (rd->is_client == 1)
183 client_recv(td, &wc);
185 server_recv(td, &wc);
187 if (wc.wr_id == FIO_RDMA_MAX_IO_DEPTH)
190 for (i = 0; i < rd->io_u_flight_nr; i++) {
191 r_io_u_d = rd->io_us_flight[i]->engine_data;
193 if (wc.wr_id == r_io_u_d->rq_wr.wr_id) {
194 rd->io_us_flight[i]->resid =
195 rd->io_us_flight[i]->buflen
198 rd->io_us_flight[i]->error = 0;
200 rd->io_us_completed[rd->
202 = rd->io_us_flight[i];
203 rd->io_u_completed_nr++;
207 if (i == rd->io_u_flight_nr)
208 log_err("fio: recv wr %" PRId64 " not found\n",
211 /* put the last one into middle of the list */
212 rd->io_us_flight[i] =
213 rd->io_us_flight[rd->io_u_flight_nr - 1];
214 rd->io_u_flight_nr--;
220 case IBV_WC_RDMA_WRITE:
221 case IBV_WC_RDMA_READ:
222 if (wc.wr_id == FIO_RDMA_MAX_IO_DEPTH)
225 for (i = 0; i < rd->io_u_flight_nr; i++) {
226 r_io_u_d = rd->io_us_flight[i]->engine_data;
228 if (wc.wr_id == r_io_u_d->sq_wr.wr_id) {
229 rd->io_us_completed[rd->
231 = rd->io_us_flight[i];
232 rd->io_u_completed_nr++;
236 if (i == rd->io_u_flight_nr)
237 log_err("fio: send wr %" PRId64 " not found\n",
240 /* put the last one into middle of the list */
241 rd->io_us_flight[i] =
242 rd->io_us_flight[rd->io_u_flight_nr - 1];
243 rd->io_u_flight_nr--;
249 log_info("fio: unknown completion event %d\n",
256 log_err("fio: poll error %d\n", ret);
264 * Return -1 for error and 'nr events' for a positive number
267 static int rdma_poll_wait(struct thread_data *td, enum ibv_wc_opcode opcode)
269 struct rdmaio_data *rd = td->io_ops->data;
270 struct ibv_cq *ev_cq;
274 if (rd->cq_event_num > 0) { /* previous left */
280 if (ibv_get_cq_event(rd->channel, &ev_cq, &ev_ctx) != 0) {
281 log_err("fio: Failed to get cq event!\n");
284 if (ev_cq != rd->cq) {
285 log_err("fio: Unknown CQ!\n");
288 if (ibv_req_notify_cq(rd->cq, 0) != 0) {
289 log_err("fio: Failed to set notify!\n");
293 ret = cq_event_handler(td, opcode);
297 ibv_ack_cq_events(rd->cq, ret);
304 static int fio_rdmaio_setup_qp(struct thread_data *td)
306 struct rdmaio_data *rd = td->io_ops->data;
307 struct ibv_qp_init_attr init_attr;
308 int qp_depth = td->o.iodepth * 2; /* 2 times of io depth */
310 if (rd->is_client == 0)
311 rd->pd = ibv_alloc_pd(rd->child_cm_id->verbs);
313 rd->pd = ibv_alloc_pd(rd->cm_id->verbs);
314 if (rd->pd == NULL) {
315 log_err("fio: ibv_alloc_pd fail\n");
319 if (rd->is_client == 0)
320 rd->channel = ibv_create_comp_channel(rd->child_cm_id->verbs);
322 rd->channel = ibv_create_comp_channel(rd->cm_id->verbs);
323 if (rd->channel == NULL) {
324 log_err("fio: ibv_create_comp_channel fail\n");
331 if (rd->is_client == 0)
332 rd->cq = ibv_create_cq(rd->child_cm_id->verbs,
333 qp_depth, rd, rd->channel, 0);
335 rd->cq = ibv_create_cq(rd->cm_id->verbs,
336 qp_depth, rd, rd->channel, 0);
337 if (rd->cq == NULL) {
338 log_err("fio: ibv_create_cq failed\n");
342 if (ibv_req_notify_cq(rd->cq, 0) != 0) {
343 log_err("fio: ibv_create_cq failed\n");
347 /* create queue pair */
348 memset(&init_attr, 0, sizeof(init_attr));
349 init_attr.cap.max_send_wr = qp_depth;
350 init_attr.cap.max_recv_wr = qp_depth;
351 init_attr.cap.max_recv_sge = 1;
352 init_attr.cap.max_send_sge = 1;
353 init_attr.qp_type = IBV_QPT_RC;
354 init_attr.send_cq = rd->cq;
355 init_attr.recv_cq = rd->cq;
357 if (rd->is_client == 0) {
358 if (rdma_create_qp(rd->child_cm_id, rd->pd, &init_attr) != 0) {
359 log_err("fio: rdma_create_qp failed\n");
362 rd->qp = rd->child_cm_id->qp;
364 if (rdma_create_qp(rd->cm_id, rd->pd, &init_attr) != 0) {
365 log_err("fio: rdma_create_qp failed\n");
368 rd->qp = rd->cm_id->qp;
374 ibv_destroy_cq(rd->cq);
376 ibv_destroy_comp_channel(rd->channel);
378 ibv_dealloc_pd(rd->pd);
383 static int fio_rdmaio_setup_control_msg_buffers(struct thread_data *td)
385 struct rdmaio_data *rd = td->io_ops->data;
387 rd->recv_mr = ibv_reg_mr(rd->pd, &rd->recv_buf, sizeof(rd->recv_buf),
388 IBV_ACCESS_LOCAL_WRITE);
389 if (rd->recv_mr == NULL) {
390 log_err("fio: recv_buf reg_mr failed\n");
394 rd->send_mr = ibv_reg_mr(rd->pd, &rd->send_buf, sizeof(rd->send_buf),
396 if (rd->send_mr == NULL) {
397 log_err("fio: send_buf reg_mr failed\n");
398 ibv_dereg_mr(rd->recv_mr);
402 /* setup work request */
404 rd->recv_sgl.addr = (uint64_t) (unsigned long)&rd->recv_buf;
405 rd->recv_sgl.length = sizeof rd->recv_buf;
406 rd->recv_sgl.lkey = rd->recv_mr->lkey;
407 rd->rq_wr.sg_list = &rd->recv_sgl;
408 rd->rq_wr.num_sge = 1;
409 rd->rq_wr.wr_id = FIO_RDMA_MAX_IO_DEPTH;
412 rd->send_sgl.addr = (uint64_t) (unsigned long)&rd->send_buf;
413 rd->send_sgl.length = sizeof rd->send_buf;
414 rd->send_sgl.lkey = rd->send_mr->lkey;
416 rd->sq_wr.opcode = IBV_WR_SEND;
417 rd->sq_wr.send_flags = IBV_SEND_SIGNALED;
418 rd->sq_wr.sg_list = &rd->send_sgl;
419 rd->sq_wr.num_sge = 1;
420 rd->sq_wr.wr_id = FIO_RDMA_MAX_IO_DEPTH;
425 static int get_next_channel_event(struct thread_data *td,
426 struct rdma_event_channel *channel,
427 enum rdma_cm_event_type wait_event)
429 struct rdmaio_data *rd = td->io_ops->data;
432 struct rdma_cm_event *event;
434 ret = rdma_get_cm_event(channel, &event);
436 log_err("fio: rdma_get_cm_event");
440 if (event->event != wait_event) {
441 log_err("fio: event is %s instead of %s\n",
442 rdma_event_str(event->event),
443 rdma_event_str(wait_event));
447 switch (event->event) {
448 case RDMA_CM_EVENT_CONNECT_REQUEST:
449 rd->child_cm_id = event->id;
455 rdma_ack_cm_event(event);
460 static int fio_rdmaio_prep(struct thread_data *td, struct io_u *io_u)
462 struct rdmaio_data *rd = td->io_ops->data;
463 struct rdma_io_u_data *r_io_u_d;
465 r_io_u_d = io_u->engine_data;
467 switch (rd->rdma_protocol) {
468 case FIO_RDMA_MEM_WRITE:
469 case FIO_RDMA_MEM_READ:
470 r_io_u_d->rdma_sgl.addr = (uint64_t) (unsigned long)io_u->buf;
471 r_io_u_d->rdma_sgl.lkey = io_u->mr->lkey;
472 r_io_u_d->sq_wr.wr_id = r_io_u_d->wr_id;
473 r_io_u_d->sq_wr.send_flags = IBV_SEND_SIGNALED;
474 r_io_u_d->sq_wr.sg_list = &r_io_u_d->rdma_sgl;
475 r_io_u_d->sq_wr.num_sge = 1;
477 case FIO_RDMA_CHA_SEND:
478 r_io_u_d->rdma_sgl.addr = (uint64_t) (unsigned long)io_u->buf;
479 r_io_u_d->rdma_sgl.lkey = io_u->mr->lkey;
480 r_io_u_d->rdma_sgl.length = io_u->buflen;
481 r_io_u_d->sq_wr.wr_id = r_io_u_d->wr_id;
482 r_io_u_d->sq_wr.opcode = IBV_WR_SEND;
483 r_io_u_d->sq_wr.send_flags = IBV_SEND_SIGNALED;
484 r_io_u_d->sq_wr.sg_list = &r_io_u_d->rdma_sgl;
485 r_io_u_d->sq_wr.num_sge = 1;
487 case FIO_RDMA_CHA_RECV:
488 r_io_u_d->rdma_sgl.addr = (uint64_t) (unsigned long)io_u->buf;
489 r_io_u_d->rdma_sgl.lkey = io_u->mr->lkey;
490 r_io_u_d->rdma_sgl.length = io_u->buflen;
491 r_io_u_d->rq_wr.wr_id = r_io_u_d->wr_id;
492 r_io_u_d->rq_wr.sg_list = &r_io_u_d->rdma_sgl;
493 r_io_u_d->rq_wr.num_sge = 1;
496 log_err("fio: unknown rdma protocol - %d\n", rd->rdma_protocol);
503 static struct io_u *fio_rdmaio_event(struct thread_data *td, int event)
505 struct rdmaio_data *rd = td->io_ops->data;
509 io_u = rd->io_us_completed[0];
510 for (i = 0; i < rd->io_u_completed_nr - 1; i++) {
511 rd->io_us_completed[i] = rd->io_us_completed[i + 1];
513 rd->io_u_completed_nr--;
515 dprint_io_u(io_u, "fio_rdmaio_event");
520 static int fio_rdmaio_getevents(struct thread_data *td, unsigned int min,
521 unsigned int max, struct timespec *t)
523 struct rdmaio_data *rd = td->io_ops->data;
525 enum ibv_wc_opcode comp_opcode;
526 comp_opcode = IBV_WC_RDMA_WRITE;
527 struct ibv_cq *ev_cq;
533 switch (rd->rdma_protocol) {
534 case FIO_RDMA_MEM_WRITE:
535 comp_opcode = IBV_WC_RDMA_WRITE;
537 case FIO_RDMA_MEM_READ:
538 comp_opcode = IBV_WC_RDMA_READ;
540 case FIO_RDMA_CHA_SEND:
541 comp_opcode = IBV_WC_SEND;
543 case FIO_RDMA_CHA_RECV:
544 comp_opcode = IBV_WC_RECV;
547 log_err("fio: unknown rdma protocol - %d\n", rd->rdma_protocol);
551 if (rd->cq_event_num > 0) { /* previous left */
557 if (ibv_get_cq_event(rd->channel, &ev_cq, &ev_ctx) != 0) {
558 log_err("fio: Failed to get cq event!\n");
561 if (ev_cq != rd->cq) {
562 log_err("fio: Unknown CQ!\n");
565 if (ibv_req_notify_cq(rd->cq, 0) != 0) {
566 log_err("fio: Failed to set notify!\n");
570 ret = cq_event_handler(td, comp_opcode);
574 ibv_ack_cq_events(rd->cq, ret);
580 rd->cq_event_num -= r;
585 static int fio_rdmaio_send(struct thread_data *td, struct io_u **io_us,
588 struct rdmaio_data *rd = td->io_ops->data;
589 struct ibv_send_wr *bad_wr;
591 enum ibv_wc_opcode comp_opcode;
592 comp_opcode = IBV_WC_RDMA_WRITE;
595 struct rdma_io_u_data *r_io_u_d;
599 for (i = 0; i < nr; i++) {
600 /* RDMA_WRITE or RDMA_READ */
601 switch (rd->rdma_protocol) {
602 case FIO_RDMA_MEM_WRITE:
603 /* compose work request */
604 r_io_u_d = io_us[i]->engine_data;
605 index = rand() % rd->rmt_nr;
606 r_io_u_d->sq_wr.opcode = IBV_WR_RDMA_WRITE;
607 r_io_u_d->sq_wr.wr.rdma.rkey = rd->rmt_us[index].rkey;
608 r_io_u_d->sq_wr.wr.rdma.remote_addr = \
609 rd->rmt_us[index].buf;
610 r_io_u_d->sq_wr.sg_list->length = io_us[i]->buflen;
612 case FIO_RDMA_MEM_READ:
613 /* compose work request */
614 r_io_u_d = io_us[i]->engine_data;
615 index = rand() % rd->rmt_nr;
616 r_io_u_d->sq_wr.opcode = IBV_WR_RDMA_READ;
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_CHA_SEND:
623 r_io_u_d = io_us[i]->engine_data;
624 r_io_u_d->sq_wr.opcode = IBV_WR_SEND;
625 r_io_u_d->sq_wr.send_flags = IBV_SEND_SIGNALED;
628 log_err("fio: unknown rdma protocol - %d\n",
633 if (ibv_post_send(rd->qp, &r_io_u_d->sq_wr, &bad_wr) != 0) {
634 log_err("fio: ibv_post_send fail\n");
638 dprint_io_u(io_us[i], "fio_rdmaio_send");
641 /* wait for completion
642 rdma_poll_wait(td, comp_opcode); */
647 static int fio_rdmaio_recv(struct thread_data *td, struct io_u **io_us,
650 struct rdmaio_data *rd = td->io_ops->data;
651 struct ibv_recv_wr *bad_wr;
652 struct rdma_io_u_data *r_io_u_d;
656 if (rd->rdma_protocol == FIO_RDMA_CHA_RECV) {
657 /* post io_u into recv queue */
658 for (i = 0; i < nr; i++) {
659 r_io_u_d = io_us[i]->engine_data;
660 if (ibv_post_recv(rd->qp, &r_io_u_d->rq_wr, &bad_wr) !=
662 log_err("fio: ibv_post_recv fail\n");
666 } else if ((rd->rdma_protocol == FIO_RDMA_MEM_READ)
667 || (rd->rdma_protocol == FIO_RDMA_MEM_WRITE)) {
668 /* re-post the rq_wr */
669 if (ibv_post_recv(rd->qp, &rd->rq_wr, &bad_wr) != 0) {
670 log_err("fio: ibv_post_recv fail\n");
674 rdma_poll_wait(td, IBV_WC_RECV);
676 dprint(FD_IO, "fio: recv FINISH message\n");
684 static int fio_rdmaio_queue(struct thread_data *td, struct io_u *io_u)
686 struct rdmaio_data *rd = td->io_ops->data;
688 fio_ro_check(td, io_u);
690 if (rd->io_u_queued_nr == (int)td->o.iodepth)
693 rd->io_us_queued[rd->io_u_queued_nr] = io_u;
694 rd->io_u_queued_nr++;
696 dprint_io_u(io_u, "fio_rdmaio_queue");
701 static void fio_rdmaio_queued(struct thread_data *td, struct io_u **io_us,
704 struct rdmaio_data *rd = td->io_ops->data;
708 if (!fio_fill_issue_time(td))
711 fio_gettime(&now, NULL);
713 for (i = 0; i < nr; i++) {
714 struct io_u *io_u = io_us[i];
716 /* queued -> flight */
717 rd->io_us_flight[rd->io_u_flight_nr] = io_u;
718 rd->io_u_flight_nr++;
720 memcpy(&io_u->issue_time, &now, sizeof(now));
721 io_u_queued(td, io_u);
725 static int fio_rdmaio_commit(struct thread_data *td)
727 struct rdmaio_data *rd = td->io_ops->data;
731 if (!rd->io_us_queued)
734 io_us = rd->io_us_queued;
736 /* RDMA_WRITE or RDMA_READ */
738 ret = fio_rdmaio_send(td, io_us, rd->io_u_queued_nr);
739 } else if (!rd->is_client) {
740 ret = fio_rdmaio_recv(td, io_us, rd->io_u_queued_nr);
742 ret = 0; /* must be a SYNC */
745 fio_rdmaio_queued(td, io_us, ret);
746 io_u_mark_submit(td, ret);
747 rd->io_u_queued_nr -= ret;
752 } while (rd->io_u_queued_nr);
757 static int fio_rdmaio_connect(struct thread_data *td, struct fio_file *f)
759 struct rdmaio_data *rd = td->io_ops->data;
760 struct rdma_conn_param conn_param;
761 struct ibv_send_wr *bad_wr;
763 memset(&conn_param, 0, sizeof conn_param);
764 conn_param.responder_resources = 1;
765 conn_param.initiator_depth = 1;
766 conn_param.retry_count = 10;
768 if (rdma_connect(rd->cm_id, &conn_param) != 0) {
769 log_err("fio: rdma_connect fail\n");
773 if (get_next_channel_event
774 (td, rd->cm_channel, RDMA_CM_EVENT_ESTABLISHED) != 0) {
775 log_err("fio: wait for RDMA_CM_EVENT_ESTABLISHED\n");
779 /* send task request */
780 rd->send_buf.mode = htonl(rd->rdma_protocol);
781 rd->send_buf.nr = htonl(td->o.iodepth);
783 if (ibv_post_send(rd->qp, &rd->sq_wr, &bad_wr) != 0) {
784 log_err("fio: ibv_post_send fail");
788 rdma_poll_wait(td, IBV_WC_SEND);
790 /* wait for remote MR info from server side */
791 rdma_poll_wait(td, IBV_WC_RECV);
796 static int fio_rdmaio_accept(struct thread_data *td, struct fio_file *f)
798 struct rdmaio_data *rd = td->io_ops->data;
799 struct rdma_conn_param conn_param;
800 struct ibv_send_wr *bad_wr;
802 /* rdma_accept() - then wait for accept success */
803 memset(&conn_param, 0, sizeof conn_param);
804 conn_param.responder_resources = 1;
805 conn_param.initiator_depth = 1;
807 if (rdma_accept(rd->child_cm_id, &conn_param) != 0) {
808 log_err("fio: rdma_accept\n");
812 if (get_next_channel_event
813 (td, rd->cm_channel, RDMA_CM_EVENT_ESTABLISHED) != 0) {
814 log_err("fio: wait for RDMA_CM_EVENT_ESTABLISHED\n");
818 /* wait for request */
819 rdma_poll_wait(td, IBV_WC_RECV);
821 if (ibv_post_send(rd->qp, &rd->sq_wr, &bad_wr) != 0) {
822 log_err("fio: ibv_post_send fail");
826 rdma_poll_wait(td, IBV_WC_SEND);
831 static int fio_rdmaio_open_file(struct thread_data *td, struct fio_file *f)
834 return fio_rdmaio_accept(td, f);
836 return fio_rdmaio_connect(td, f);
839 static int fio_rdmaio_close_file(struct thread_data *td, struct fio_file *f)
841 struct rdmaio_data *rd = td->io_ops->data;
842 struct ibv_send_wr *bad_wr;
844 /* unregister rdma buffer */
847 * Client sends notification to the server side
849 /* refer to: http://linux.die.net/man/7/rdma_cm */
850 if ((rd->is_client == 1) && ((rd->rdma_protocol == FIO_RDMA_MEM_WRITE)
851 || (rd->rdma_protocol ==
852 FIO_RDMA_MEM_READ))) {
853 if (ibv_post_send(rd->qp, &rd->sq_wr, &bad_wr) != 0) {
854 log_err("fio: ibv_post_send fail");
858 dprint(FD_IO, "fio: close infomation sent success\n");
859 rdma_poll_wait(td, IBV_WC_SEND);
862 if (rd->is_client == 1)
863 rdma_disconnect(rd->cm_id);
865 rdma_disconnect(rd->child_cm_id);
866 /* rdma_disconnect(rd->cm_id); */
869 /* if (get_next_channel_event(td, rd->cm_channel, RDMA_CM_EVENT_DISCONNECTED) != 0)
871 log_err("fio: wait for RDMA_CM_EVENT_DISCONNECTED\n");
875 ibv_destroy_qp(rd->qp);
876 ibv_destroy_cq(rd->cq);
878 if (rd->is_client == 1)
879 rdma_destroy_id(rd->cm_id);
881 rdma_destroy_id(rd->child_cm_id);
882 rdma_destroy_id(rd->cm_id);
885 ibv_destroy_comp_channel(rd->channel);
886 ibv_dealloc_pd(rd->pd);
891 static int fio_rdmaio_setup_connect(struct thread_data *td, const char *host,
894 struct rdmaio_data *rd = td->io_ops->data;
895 struct ibv_recv_wr *bad_wr;
897 rd->addr.sin_family = AF_INET;
898 rd->addr.sin_port = htons(port);
900 if (inet_aton(host, &rd->addr.sin_addr) != 1) {
901 struct hostent *hent;
903 hent = gethostbyname(host);
905 td_verror(td, errno, "gethostbyname");
909 memcpy(&rd->addr.sin_addr, hent->h_addr, 4);
913 if (rdma_resolve_addr(rd->cm_id, NULL,
914 (struct sockaddr *)&rd->addr, 2000) != 0) {
915 log_err("fio: rdma_resolve_addr");
919 if (get_next_channel_event
920 (td, rd->cm_channel, RDMA_CM_EVENT_ADDR_RESOLVED)
922 log_err("fio: get_next_channel_event");
927 if (rdma_resolve_route(rd->cm_id, 2000) != 0) {
928 log_err("fio: rdma_resolve_route");
932 if (get_next_channel_event
933 (td, rd->cm_channel, RDMA_CM_EVENT_ROUTE_RESOLVED) != 0) {
934 log_err("fio: get_next_channel_event");
938 /* create qp and buffer */
939 if (fio_rdmaio_setup_qp(td) != 0)
942 if (fio_rdmaio_setup_control_msg_buffers(td) != 0)
946 if (ibv_post_recv(rd->qp, &rd->rq_wr, &bad_wr) != 0) {
947 log_err("fio: ibv_post_recv fail\n");
954 static int fio_rdmaio_setup_listen(struct thread_data *td, short port)
956 struct rdmaio_data *rd = td->io_ops->data;
957 struct ibv_recv_wr *bad_wr;
959 rd->addr.sin_family = AF_INET;
960 rd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
961 rd->addr.sin_port = htons(port);
964 if (rdma_bind_addr(rd->cm_id, (struct sockaddr *)&rd->addr) != 0) {
965 log_err("fio: rdma_bind_addr fail\n");
969 if (rdma_listen(rd->cm_id, 3) != 0) {
970 log_err("fio: rdma_listen fail\n");
974 /* wait for CONNECT_REQUEST */
975 if (get_next_channel_event
976 (td, rd->cm_channel, RDMA_CM_EVENT_CONNECT_REQUEST) != 0) {
977 log_err("fio: wait for RDMA_CM_EVENT_CONNECT_REQUEST\n");
981 if (fio_rdmaio_setup_qp(td) != 0)
984 if (fio_rdmaio_setup_control_msg_buffers(td) != 0)
988 if (ibv_post_recv(rd->qp, &rd->rq_wr, &bad_wr) != 0) {
989 log_err("fio: ibv_post_recv fail\n");
996 static int fio_rdmaio_init(struct thread_data *td)
998 struct rdmaio_data *rd = td->io_ops->data;
1000 char host[64], buf[128];
1001 char *sep, *portp, *modep;
1006 log_err("fio: rdma connections must be read OR write\n");
1009 if (td_random(td)) {
1010 log_err("fio: RDMA network IO can't be random\n");
1014 /* check RLIMIT_MEMLOCK */
1015 if (getrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
1016 log_err("fio: getrlimit fail: %d(%s)\n",
1017 errno, strerror(errno));
1022 if ((rl.rlim_cur != RLIM_INFINITY)
1023 && (rl.rlim_cur < td->orig_buffer_size)) {
1024 log_err("fio: soft RLIMIT_MEMLOCK is: %" PRId64 "\n",
1026 log_err("fio: total block size is: %zd\n",
1027 td->orig_buffer_size);
1028 /* try to set larger RLIMIT_MEMLOCK */
1029 rl.rlim_cur = rl.rlim_max;
1030 if (setrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
1031 log_err("fio: setrlimit fail: %d(%s)\n",
1032 errno, strerror(errno));
1033 log_err("fio: you may try enlarge MEMLOCK by root\n");
1034 log_err("# ulimit -l unlimited\n");
1039 strcpy(buf, td->o.filename);
1041 sep = strchr(buf, '/');
1053 sep = strchr(portp, '/');
1059 port = strtol(portp, NULL, 10);
1060 if (!port || port > 65535)
1064 if (!strncmp("rdma_write", modep, strlen(modep)) ||
1065 !strncmp("RDMA_WRITE", modep, strlen(modep)))
1066 rd->rdma_protocol = FIO_RDMA_MEM_WRITE;
1067 else if (!strncmp("rdma_read", modep, strlen(modep)) ||
1068 !strncmp("RDMA_READ", modep, strlen(modep)))
1069 rd->rdma_protocol = FIO_RDMA_MEM_READ;
1070 else if (!strncmp("send", modep, strlen(modep)) ||
1071 !strncmp("SEND", modep, strlen(modep)))
1072 rd->rdma_protocol = FIO_RDMA_CHA_SEND;
1076 rd->rdma_protocol = FIO_RDMA_MEM_WRITE;
1078 rd->cq_event_num = 0;
1080 rd->cm_channel = rdma_create_event_channel();
1081 if (!rd->cm_channel) {
1082 log_err("fio: rdma_create_event_channel fail\n");
1086 ret = rdma_create_id(rd->cm_channel, &rd->cm_id, rd, RDMA_PS_TCP);
1088 log_err("fio: rdma_create_id fail\n");
1092 if ((rd->rdma_protocol == FIO_RDMA_MEM_WRITE) ||
1093 (rd->rdma_protocol == FIO_RDMA_MEM_READ)) {
1095 malloc(FIO_RDMA_MAX_IO_DEPTH * sizeof(struct remote_u));
1096 memset(rd->rmt_us, 0,
1097 FIO_RDMA_MAX_IO_DEPTH * sizeof(struct remote_u));
1101 rd->io_us_queued = malloc(td->o.iodepth * sizeof(struct io_u *));
1102 memset(rd->io_us_queued, 0, td->o.iodepth * sizeof(struct io_u *));
1103 rd->io_u_queued_nr = 0;
1105 rd->io_us_flight = malloc(td->o.iodepth * sizeof(struct io_u *));
1106 memset(rd->io_us_flight, 0, td->o.iodepth * sizeof(struct io_u *));
1107 rd->io_u_flight_nr = 0;
1109 rd->io_us_completed = malloc(td->o.iodepth * sizeof(struct io_u *));
1110 memset(rd->io_us_completed, 0, td->o.iodepth * sizeof(struct io_u *));
1111 rd->io_u_completed_nr = 0;
1113 if (td_read(td)) { /* READ as the server */
1115 /* server rd->rdma_buf_len will be setup after got request */
1116 ret = fio_rdmaio_setup_listen(td, port);
1117 } else { /* WRITE as the client */
1119 ret = fio_rdmaio_setup_connect(td, host, port);
1122 struct flist_head *entry;
1123 unsigned int max_bs;
1124 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
1125 /* register each io_u in the free list */
1127 flist_for_each(entry, &td->io_u_freelist) {
1128 struct io_u *io_u = flist_entry(entry, struct io_u, list);
1130 io_u->engine_data = malloc(sizeof(struct rdma_io_u_data));
1131 memset(io_u->engine_data, 0, sizeof(struct rdma_io_u_data));
1132 ((struct rdma_io_u_data *)io_u->engine_data)->wr_id = i;
1134 io_u->mr = ibv_reg_mr(rd->pd, io_u->buf, max_bs,
1135 IBV_ACCESS_LOCAL_WRITE |
1136 IBV_ACCESS_REMOTE_READ |
1137 IBV_ACCESS_REMOTE_WRITE);
1138 if (io_u->mr == NULL) {
1139 log_err("fio: ibv_reg_mr io_u failed\n");
1143 rd->send_buf.rmt_us[i].buf =
1144 htonll((uint64_t) (unsigned long)io_u->buf);
1145 rd->send_buf.rmt_us[i].rkey = htonl(io_u->mr->rkey);
1146 rd->send_buf.rmt_us[i].size = htonl(max_bs);
1148 /* log_info("fio: Send rkey %x addr %" PRIx64 " len %d to client\n",
1149 io_u->mr->rkey, io_u->buf, max_bs); */
1153 rd->send_buf.nr = htonl(i);
1157 log_err("fio: bad rdma host/port/protocol: %s\n", td->o.filename);
1161 static void fio_rdmaio_cleanup(struct thread_data *td)
1163 struct rdmaio_data *rd = td->io_ops->data;
1166 /* if (nd->listenfd != -1)
1167 close(nd->listenfd);
1168 if (nd->pipes[0] != -1)
1169 close(nd->pipes[0]);
1170 if (nd->pipes[1] != -1)
1171 close(nd->pipes[1]);
1177 static int fio_rdmaio_setup(struct thread_data *td)
1179 struct rdmaio_data *rd;
1181 if (!td->io_ops->data) {
1182 rd = malloc(sizeof(*rd));;
1184 memset(rd, 0, sizeof(*rd));
1185 td->io_ops->data = rd;
1191 static struct ioengine_ops ioengine_rw = {
1193 .version = FIO_IOOPS_VERSION,
1194 .setup = fio_rdmaio_setup,
1195 .init = fio_rdmaio_init,
1196 .prep = fio_rdmaio_prep,
1197 .queue = fio_rdmaio_queue,
1198 .commit = fio_rdmaio_commit,
1199 .getevents = fio_rdmaio_getevents,
1200 .event = fio_rdmaio_event,
1201 .cleanup = fio_rdmaio_cleanup,
1202 .open_file = fio_rdmaio_open_file,
1203 .close_file = fio_rdmaio_close_file,
1204 .flags = FIO_DISKLESSIO | FIO_UNIDIR | FIO_PIPEIO,
1207 #else /* FIO_HAVE_RDMA */
1209 static int fio_rdmaio_open_file(struct thread_data *td, struct fio_file *f)
1214 static int fio_rdmaio_close_file(struct thread_data *td, struct fio_file *f)
1219 static int fio_rdmaio_queue(struct thread_data *td, struct io_u *io_u)
1221 return FIO_Q_COMPLETED;
1224 static int fio_rdmaio_init(struct thread_data fio_unused * td)
1226 log_err("fio: rdma(librdmacm libibverbs) not available\n");
1227 log_err(" You haven't compiled rdma ioengine into fio.\n");
1228 log_err(" If you want to try rdma ioengine,\n");
1229 log_err(" make sure OFED is installed,\n");
1230 log_err(" $ ofed_info\n");
1231 log_err(" then try to make fio as follows:\n");
1232 log_err(" $ export EXTFLAGS=\"-DFIO_HAVE_RDMA\"\n");
1233 log_err(" $ export EXTLIBS=\"-libverbs -lrdmacm\"\n");
1234 log_err(" $ make clean && make\n");
1238 static struct ioengine_ops ioengine_rw = {
1240 .version = FIO_IOOPS_VERSION,
1241 .init = fio_rdmaio_init,
1242 .queue = fio_rdmaio_queue,
1243 .open_file = fio_rdmaio_open_file,
1244 .close_file = fio_rdmaio_close_file,
1245 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR | FIO_PIPEIO,
1250 static void fio_init fio_rdmaio_register(void)
1252 register_ioengine(&ioengine_rw);
1255 static void fio_exit fio_rdmaio_unregister(void)
1257 unregister_ioengine(&ioengine_rw);