rdma: cleanup and fixes
[fio.git] / engines / rdma.c
CommitLineData
21b8aee8 1/*
85286c5c 2 * RDMA I/O engine
21b8aee8 3 *
85286c5c
BVA
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.
21b8aee8 7 *
85286c5c 8 * This I/O engine is disabled by default. To enable it, execute:
21b8aee8 9 *
7d7803fa
YR
10 * $ export EXTFLAGS+=" -DFIO_HAVE_RDMA "
11 * $ export EXTLIBS+=" -libverbs -lrdmacm "
21b8aee8 12 *
85286c5c
BVA
13 * before running make. You will need the Linux RDMA software as well, either
14 * from your Linux distributor or directly from openfabrics.org:
21b8aee8 15 *
16 * http://www.openfabrics.org/downloads/OFED/
17 *
7d7803fa
YR
18 * Exchanging steps of RDMA ioengine control messages:
19 * 1. client side sends test mode (RDMA_WRITE/RDMA_READ/SEND)
20 * to server side.
222757cc 21 * 2. server side parses test mode, and sends back confirmation
7d7803fa 22 * to client side. In RDMA WRITE/READ test, this confirmation
222757cc 23 * includes memory information, such as rkey, address.
7d7803fa 24 * 3. client side initiates test loop.
222757cc 25 * 4. In RDMA WRITE/READ test, client side sends a completion
7d7803fa 26 * notification to server side. Server side updates its
222757cc 27 * td->done as true.
7d7803fa 28 *
21b8aee8 29 */
30#include <stdio.h>
31#include <stdlib.h>
32#include <unistd.h>
33#include <errno.h>
34#include <assert.h>
35#include <netinet/in.h>
36#include <arpa/inet.h>
37#include <netdb.h>
38#include <sys/poll.h>
39#include <sys/types.h>
40#include <sys/socket.h>
41#include <sys/time.h>
42#include <sys/resource.h>
43
44#include <byteswap.h>
45#include <pthread.h>
46#include <inttypes.h>
47
48#include "../fio.h"
ee88d056 49#include "../hash.h"
21b8aee8 50
51#ifdef FIO_HAVE_RDMA
52
53#include <rdma/rdma_cma.h>
54#include <infiniband/arch.h>
55
7d7803fa 56#define FIO_RDMA_MAX_IO_DEPTH 512
21b8aee8 57
58enum rdma_io_mode {
59 FIO_RDMA_UNKNOWN = 0,
60 FIO_RDMA_MEM_WRITE,
61 FIO_RDMA_MEM_READ,
62 FIO_RDMA_CHA_SEND,
63 FIO_RDMA_CHA_RECV
64};
65
66struct remote_u {
67 uint64_t buf;
68 uint32_t rkey;
69 uint32_t size;
70};
71
72struct rdma_info_blk {
73 uint32_t mode; /* channel semantic or memory semantic */
74 uint32_t nr; /* client: io depth
75 server: number of records for memory semantic
76 */
76cc5224 77 struct remote_u rmt_us[FIO_RDMA_MAX_IO_DEPTH];
21b8aee8 78};
79
80struct rdma_io_u_data {
81 uint64_t wr_id;
82 struct ibv_send_wr sq_wr;
83 struct ibv_recv_wr rq_wr;
84 struct ibv_sge rdma_sgl;
85};
86
87struct rdmaio_data {
88 int is_client;
89 enum rdma_io_mode rdma_protocol;
90 char host[64];
91 struct sockaddr_in addr;
92
93 struct ibv_recv_wr rq_wr;
94 struct ibv_sge recv_sgl;
95 struct rdma_info_blk recv_buf;
96 struct ibv_mr *recv_mr;
97
98 struct ibv_send_wr sq_wr;
99 struct ibv_sge send_sgl;
100 struct rdma_info_blk send_buf;
101 struct ibv_mr *send_mr;
102
103 struct ibv_comp_channel *channel;
104 struct ibv_cq *cq;
105 struct ibv_pd *pd;
106 struct ibv_qp *qp;
107
108 pthread_t cmthread;
109 struct rdma_event_channel *cm_channel;
110 struct rdma_cm_id *cm_id;
111 struct rdma_cm_id *child_cm_id;
112
113 int cq_event_num;
114
115 struct remote_u *rmt_us;
116 int rmt_nr;
117 struct io_u **io_us_queued;
118 int io_u_queued_nr;
119 struct io_u **io_us_flight;
120 int io_u_flight_nr;
121 struct io_u **io_us_completed;
122 int io_u_completed_nr;
ea6209ad
JA
123
124 struct frand_state rand_state;
21b8aee8 125};
126
127static int client_recv(struct thread_data *td, struct ibv_wc *wc)
128{
129 struct rdmaio_data *rd = td->io_ops->data;
130
131 if (wc->byte_len != sizeof(rd->recv_buf)) {
b6cf38f0 132 log_err("Received bogus data, size %d\n", wc->byte_len);
21b8aee8 133 return 1;
134 }
135
136 /* store mr info for MEMORY semantic */
137 if ((rd->rdma_protocol == FIO_RDMA_MEM_WRITE) ||
138 (rd->rdma_protocol == FIO_RDMA_MEM_READ)) {
139 /* struct flist_head *entry; */
140 int i = 0;
141
142 rd->rmt_nr = ntohl(rd->recv_buf.nr);
143
144 for (i = 0; i < rd->rmt_nr; i++) {
145 rd->rmt_us[i].buf = ntohll(rd->recv_buf.rmt_us[i].buf);
146 rd->rmt_us[i].rkey = ntohl(rd->recv_buf.rmt_us[i].rkey);
147 rd->rmt_us[i].size = ntohl(rd->recv_buf.rmt_us[i].size);
148
149 dprint(FD_IO,
150 "fio: Received rkey %x addr %" PRIx64
151 " len %d from peer\n", rd->rmt_us[i].rkey,
152 rd->rmt_us[i].buf, rd->rmt_us[i].size);
153 }
154 }
155
156 return 0;
157}
158
159static int server_recv(struct thread_data *td, struct ibv_wc *wc)
160{
161 struct rdmaio_data *rd = td->io_ops->data;
162
76cc5224 163 if (wc->wr_id == FIO_RDMA_MAX_IO_DEPTH) {
21b8aee8 164 rd->rdma_protocol = ntohl(rd->recv_buf.mode);
165
166 /* CHANNEL semantic, do nothing */
167 if (rd->rdma_protocol == FIO_RDMA_CHA_SEND)
168 rd->rdma_protocol = FIO_RDMA_CHA_RECV;
169 }
170
171 return 0;
172}
173
174static int cq_event_handler(struct thread_data *td, enum ibv_wc_opcode opcode)
175{
176 struct rdmaio_data *rd = td->io_ops->data;
177 struct ibv_wc wc;
178 struct rdma_io_u_data *r_io_u_d;
179 int ret;
180 int compevnum = 0;
181 int i;
182
183 while ((ret = ibv_poll_cq(rd->cq, 1, &wc)) == 1) {
184 ret = 0;
185 compevnum++;
186
187 if (wc.status) {
188 log_err("fio: cq completion status %d(%s)\n",
189 wc.status, ibv_wc_status_str(wc.status));
190 return -1;
191 }
192
193 switch (wc.opcode) {
194
195 case IBV_WC_RECV:
196 if (rd->is_client == 1)
197 client_recv(td, &wc);
198 else
199 server_recv(td, &wc);
200
76cc5224 201 if (wc.wr_id == FIO_RDMA_MAX_IO_DEPTH)
21b8aee8 202 break;
203
204 for (i = 0; i < rd->io_u_flight_nr; i++) {
205 r_io_u_d = rd->io_us_flight[i]->engine_data;
206
207 if (wc.wr_id == r_io_u_d->rq_wr.wr_id) {
208 rd->io_us_flight[i]->resid =
209 rd->io_us_flight[i]->buflen
210 - wc.byte_len;
211
212 rd->io_us_flight[i]->error = 0;
213
214 rd->io_us_completed[rd->
215 io_u_completed_nr]
216 = rd->io_us_flight[i];
217 rd->io_u_completed_nr++;
218 break;
219 }
220 }
221 if (i == rd->io_u_flight_nr)
e07f72d3 222 log_err("fio: recv wr %" PRId64 " not found\n",
21b8aee8 223 wc.wr_id);
224 else {
225 /* put the last one into middle of the list */
226 rd->io_us_flight[i] =
227 rd->io_us_flight[rd->io_u_flight_nr - 1];
228 rd->io_u_flight_nr--;
229 }
230
231 break;
232
233 case IBV_WC_SEND:
234 case IBV_WC_RDMA_WRITE:
235 case IBV_WC_RDMA_READ:
76cc5224 236 if (wc.wr_id == FIO_RDMA_MAX_IO_DEPTH)
21b8aee8 237 break;
238
239 for (i = 0; i < rd->io_u_flight_nr; i++) {
240 r_io_u_d = rd->io_us_flight[i]->engine_data;
241
242 if (wc.wr_id == r_io_u_d->sq_wr.wr_id) {
243 rd->io_us_completed[rd->
244 io_u_completed_nr]
245 = rd->io_us_flight[i];
246 rd->io_u_completed_nr++;
247 break;
248 }
249 }
250 if (i == rd->io_u_flight_nr)
e07f72d3 251 log_err("fio: send wr %" PRId64 " not found\n",
21b8aee8 252 wc.wr_id);
253 else {
254 /* put the last one into middle of the list */
255 rd->io_us_flight[i] =
256 rd->io_us_flight[rd->io_u_flight_nr - 1];
257 rd->io_u_flight_nr--;
258 }
259
260 break;
261
262 default:
263 log_info("fio: unknown completion event %d\n",
264 wc.opcode);
265 return -1;
266 }
267 rd->cq_event_num++;
268 }
269 if (ret) {
270 log_err("fio: poll error %d\n", ret);
271 return 1;
272 }
273
274 return compevnum;
275}
276
277/*
278 * Return -1 for error and 'nr events' for a positive number
279 * of events
280 */
281static int rdma_poll_wait(struct thread_data *td, enum ibv_wc_opcode opcode)
282{
283 struct rdmaio_data *rd = td->io_ops->data;
284 struct ibv_cq *ev_cq;
285 void *ev_ctx;
286 int ret;
287
288 if (rd->cq_event_num > 0) { /* previous left */
289 rd->cq_event_num--;
290 return 0;
291 }
292
293again:
294 if (ibv_get_cq_event(rd->channel, &ev_cq, &ev_ctx) != 0) {
295 log_err("fio: Failed to get cq event!\n");
296 return -1;
297 }
298 if (ev_cq != rd->cq) {
299 log_err("fio: Unknown CQ!\n");
300 return -1;
301 }
302 if (ibv_req_notify_cq(rd->cq, 0) != 0) {
303 log_err("fio: Failed to set notify!\n");
304 return -1;
305 }
306
307 ret = cq_event_handler(td, opcode);
308 if (ret < 1)
309 goto again;
310
311 ibv_ack_cq_events(rd->cq, ret);
312
313 rd->cq_event_num--;
314
315 return ret;
316}
317
318static int fio_rdmaio_setup_qp(struct thread_data *td)
319{
320 struct rdmaio_data *rd = td->io_ops->data;
321 struct ibv_qp_init_attr init_attr;
322 int qp_depth = td->o.iodepth * 2; /* 2 times of io depth */
323
324 if (rd->is_client == 0)
325 rd->pd = ibv_alloc_pd(rd->child_cm_id->verbs);
326 else
327 rd->pd = ibv_alloc_pd(rd->cm_id->verbs);
222757cc 328
21b8aee8 329 if (rd->pd == NULL) {
330 log_err("fio: ibv_alloc_pd fail\n");
331 return 1;
332 }
333
334 if (rd->is_client == 0)
335 rd->channel = ibv_create_comp_channel(rd->child_cm_id->verbs);
336 else
337 rd->channel = ibv_create_comp_channel(rd->cm_id->verbs);
338 if (rd->channel == NULL) {
339 log_err("fio: ibv_create_comp_channel fail\n");
340 goto err1;
341 }
342
343 if (qp_depth < 16)
344 qp_depth = 16;
345
346 if (rd->is_client == 0)
347 rd->cq = ibv_create_cq(rd->child_cm_id->verbs,
348 qp_depth, rd, rd->channel, 0);
349 else
350 rd->cq = ibv_create_cq(rd->cm_id->verbs,
351 qp_depth, rd, rd->channel, 0);
352 if (rd->cq == NULL) {
353 log_err("fio: ibv_create_cq failed\n");
354 goto err2;
355 }
356
357 if (ibv_req_notify_cq(rd->cq, 0) != 0) {
358 log_err("fio: ibv_create_cq failed\n");
359 goto err3;
360 }
361
362 /* create queue pair */
363 memset(&init_attr, 0, sizeof(init_attr));
364 init_attr.cap.max_send_wr = qp_depth;
365 init_attr.cap.max_recv_wr = qp_depth;
366 init_attr.cap.max_recv_sge = 1;
367 init_attr.cap.max_send_sge = 1;
368 init_attr.qp_type = IBV_QPT_RC;
369 init_attr.send_cq = rd->cq;
370 init_attr.recv_cq = rd->cq;
371
372 if (rd->is_client == 0) {
373 if (rdma_create_qp(rd->child_cm_id, rd->pd, &init_attr) != 0) {
374 log_err("fio: rdma_create_qp failed\n");
375 goto err3;
376 }
377 rd->qp = rd->child_cm_id->qp;
378 } else {
379 if (rdma_create_qp(rd->cm_id, rd->pd, &init_attr) != 0) {
380 log_err("fio: rdma_create_qp failed\n");
381 goto err3;
382 }
383 rd->qp = rd->cm_id->qp;
384 }
385
386 return 0;
387
388err3:
389 ibv_destroy_cq(rd->cq);
390err2:
391 ibv_destroy_comp_channel(rd->channel);
392err1:
393 ibv_dealloc_pd(rd->pd);
394
395 return 1;
396}
397
398static int fio_rdmaio_setup_control_msg_buffers(struct thread_data *td)
399{
400 struct rdmaio_data *rd = td->io_ops->data;
401
402 rd->recv_mr = ibv_reg_mr(rd->pd, &rd->recv_buf, sizeof(rd->recv_buf),
403 IBV_ACCESS_LOCAL_WRITE);
404 if (rd->recv_mr == NULL) {
405 log_err("fio: recv_buf reg_mr failed\n");
406 return 1;
407 }
408
409 rd->send_mr = ibv_reg_mr(rd->pd, &rd->send_buf, sizeof(rd->send_buf),
410 0);
411 if (rd->send_mr == NULL) {
412 log_err("fio: send_buf reg_mr failed\n");
413 ibv_dereg_mr(rd->recv_mr);
414 return 1;
415 }
416
417 /* setup work request */
418 /* recv wq */
419 rd->recv_sgl.addr = (uint64_t) (unsigned long)&rd->recv_buf;
222757cc 420 rd->recv_sgl.length = sizeof(rd->recv_buf);
21b8aee8 421 rd->recv_sgl.lkey = rd->recv_mr->lkey;
422 rd->rq_wr.sg_list = &rd->recv_sgl;
423 rd->rq_wr.num_sge = 1;
76cc5224 424 rd->rq_wr.wr_id = FIO_RDMA_MAX_IO_DEPTH;
21b8aee8 425
426 /* send wq */
427 rd->send_sgl.addr = (uint64_t) (unsigned long)&rd->send_buf;
222757cc 428 rd->send_sgl.length = sizeof(rd->send_buf);
21b8aee8 429 rd->send_sgl.lkey = rd->send_mr->lkey;
430
431 rd->sq_wr.opcode = IBV_WR_SEND;
432 rd->sq_wr.send_flags = IBV_SEND_SIGNALED;
433 rd->sq_wr.sg_list = &rd->send_sgl;
434 rd->sq_wr.num_sge = 1;
76cc5224 435 rd->sq_wr.wr_id = FIO_RDMA_MAX_IO_DEPTH;
21b8aee8 436
437 return 0;
438}
439
440static int get_next_channel_event(struct thread_data *td,
441 struct rdma_event_channel *channel,
442 enum rdma_cm_event_type wait_event)
443{
444 struct rdmaio_data *rd = td->io_ops->data;
21b8aee8 445 struct rdma_cm_event *event;
222757cc 446 int ret;
21b8aee8 447
448 ret = rdma_get_cm_event(channel, &event);
449 if (ret) {
222757cc 450 log_err("fio: rdma_get_cm_event: %d\n", ret);
21b8aee8 451 return 1;
452 }
453
454 if (event->event != wait_event) {
455 log_err("fio: event is %s instead of %s\n",
456 rdma_event_str(event->event),
457 rdma_event_str(wait_event));
458 return 1;
459 }
460
461 switch (event->event) {
462 case RDMA_CM_EVENT_CONNECT_REQUEST:
463 rd->child_cm_id = event->id;
464 break;
465 default:
466 break;
467 }
468
469 rdma_ack_cm_event(event);
470
471 return 0;
472}
473
474static int fio_rdmaio_prep(struct thread_data *td, struct io_u *io_u)
475{
476 struct rdmaio_data *rd = td->io_ops->data;
477 struct rdma_io_u_data *r_io_u_d;
478
479 r_io_u_d = io_u->engine_data;
480
481 switch (rd->rdma_protocol) {
482 case FIO_RDMA_MEM_WRITE:
483 case FIO_RDMA_MEM_READ:
484 r_io_u_d->rdma_sgl.addr = (uint64_t) (unsigned long)io_u->buf;
485 r_io_u_d->rdma_sgl.lkey = io_u->mr->lkey;
486 r_io_u_d->sq_wr.wr_id = r_io_u_d->wr_id;
487 r_io_u_d->sq_wr.send_flags = IBV_SEND_SIGNALED;
488 r_io_u_d->sq_wr.sg_list = &r_io_u_d->rdma_sgl;
489 r_io_u_d->sq_wr.num_sge = 1;
490 break;
491 case FIO_RDMA_CHA_SEND:
492 r_io_u_d->rdma_sgl.addr = (uint64_t) (unsigned long)io_u->buf;
493 r_io_u_d->rdma_sgl.lkey = io_u->mr->lkey;
494 r_io_u_d->rdma_sgl.length = io_u->buflen;
495 r_io_u_d->sq_wr.wr_id = r_io_u_d->wr_id;
496 r_io_u_d->sq_wr.opcode = IBV_WR_SEND;
497 r_io_u_d->sq_wr.send_flags = IBV_SEND_SIGNALED;
498 r_io_u_d->sq_wr.sg_list = &r_io_u_d->rdma_sgl;
499 r_io_u_d->sq_wr.num_sge = 1;
500 break;
501 case FIO_RDMA_CHA_RECV:
502 r_io_u_d->rdma_sgl.addr = (uint64_t) (unsigned long)io_u->buf;
503 r_io_u_d->rdma_sgl.lkey = io_u->mr->lkey;
504 r_io_u_d->rdma_sgl.length = io_u->buflen;
505 r_io_u_d->rq_wr.wr_id = r_io_u_d->wr_id;
506 r_io_u_d->rq_wr.sg_list = &r_io_u_d->rdma_sgl;
507 r_io_u_d->rq_wr.num_sge = 1;
508 break;
509 default:
510 log_err("fio: unknown rdma protocol - %d\n", rd->rdma_protocol);
511 break;
512 }
513
514 return 0;
515}
516
517static struct io_u *fio_rdmaio_event(struct thread_data *td, int event)
518{
519 struct rdmaio_data *rd = td->io_ops->data;
520 struct io_u *io_u;
521 int i;
522
523 io_u = rd->io_us_completed[0];
222757cc 524 for (i = 0; i < rd->io_u_completed_nr - 1; i++)
21b8aee8 525 rd->io_us_completed[i] = rd->io_us_completed[i + 1];
222757cc 526
21b8aee8 527 rd->io_u_completed_nr--;
528
529 dprint_io_u(io_u, "fio_rdmaio_event");
530
531 return io_u;
532}
533
534static int fio_rdmaio_getevents(struct thread_data *td, unsigned int min,
535 unsigned int max, struct timespec *t)
536{
537 struct rdmaio_data *rd = td->io_ops->data;
21b8aee8 538 enum ibv_wc_opcode comp_opcode;
539 comp_opcode = IBV_WC_RDMA_WRITE;
540 struct ibv_cq *ev_cq;
541 void *ev_ctx;
222757cc 542 int ret, r = 0;
21b8aee8 543
544 switch (rd->rdma_protocol) {
545 case FIO_RDMA_MEM_WRITE:
546 comp_opcode = IBV_WC_RDMA_WRITE;
547 break;
548 case FIO_RDMA_MEM_READ:
549 comp_opcode = IBV_WC_RDMA_READ;
550 break;
551 case FIO_RDMA_CHA_SEND:
552 comp_opcode = IBV_WC_SEND;
553 break;
554 case FIO_RDMA_CHA_RECV:
555 comp_opcode = IBV_WC_RECV;
556 break;
557 default:
558 log_err("fio: unknown rdma protocol - %d\n", rd->rdma_protocol);
559 break;
560 }
561
562 if (rd->cq_event_num > 0) { /* previous left */
563 rd->cq_event_num--;
564 return 0;
565 }
566
567again:
568 if (ibv_get_cq_event(rd->channel, &ev_cq, &ev_ctx) != 0) {
569 log_err("fio: Failed to get cq event!\n");
570 return -1;
571 }
572 if (ev_cq != rd->cq) {
573 log_err("fio: Unknown CQ!\n");
574 return -1;
575 }
576 if (ibv_req_notify_cq(rd->cq, 0) != 0) {
577 log_err("fio: Failed to set notify!\n");
578 return -1;
579 }
580
581 ret = cq_event_handler(td, comp_opcode);
582 if (ret < 1)
583 goto again;
584
585 ibv_ack_cq_events(rd->cq, ret);
586
587 r += ret;
588 if (r < min)
589 goto again;
590
591 rd->cq_event_num -= r;
592
593 return r;
594}
595
596static int fio_rdmaio_send(struct thread_data *td, struct io_u **io_us,
597 unsigned int nr)
598{
599 struct rdmaio_data *rd = td->io_ops->data;
600 struct ibv_send_wr *bad_wr;
e07f72d3 601#if 0
21b8aee8 602 enum ibv_wc_opcode comp_opcode;
603 comp_opcode = IBV_WC_RDMA_WRITE;
e07f72d3 604#endif
7d7803fa
YR
605 int i;
606 long index;
21b8aee8 607 struct rdma_io_u_data *r_io_u_d;
608
609 r_io_u_d = NULL;
610
611 for (i = 0; i < nr; i++) {
612 /* RDMA_WRITE or RDMA_READ */
613 switch (rd->rdma_protocol) {
614 case FIO_RDMA_MEM_WRITE:
615 /* compose work request */
616 r_io_u_d = io_us[i]->engine_data;
222757cc 617 index = __rand(&rd->rand_state) % rd->rmt_nr;
21b8aee8 618 r_io_u_d->sq_wr.opcode = IBV_WR_RDMA_WRITE;
619 r_io_u_d->sq_wr.wr.rdma.rkey = rd->rmt_us[index].rkey;
b6cf38f0
YR
620 r_io_u_d->sq_wr.wr.rdma.remote_addr = \
621 rd->rmt_us[index].buf;
21b8aee8 622 r_io_u_d->sq_wr.sg_list->length = io_us[i]->buflen;
623 break;
624 case FIO_RDMA_MEM_READ:
625 /* compose work request */
626 r_io_u_d = io_us[i]->engine_data;
222757cc 627 index = __rand(&rd->rand_state) % rd->rmt_nr;
21b8aee8 628 r_io_u_d->sq_wr.opcode = IBV_WR_RDMA_READ;
629 r_io_u_d->sq_wr.wr.rdma.rkey = rd->rmt_us[index].rkey;
b6cf38f0
YR
630 r_io_u_d->sq_wr.wr.rdma.remote_addr = \
631 rd->rmt_us[index].buf;
21b8aee8 632 r_io_u_d->sq_wr.sg_list->length = io_us[i]->buflen;
633 break;
634 case FIO_RDMA_CHA_SEND:
635 r_io_u_d = io_us[i]->engine_data;
636 r_io_u_d->sq_wr.opcode = IBV_WR_SEND;
637 r_io_u_d->sq_wr.send_flags = IBV_SEND_SIGNALED;
638 break;
639 default:
640 log_err("fio: unknown rdma protocol - %d\n",
641 rd->rdma_protocol);
642 break;
643 }
644
645 if (ibv_post_send(rd->qp, &r_io_u_d->sq_wr, &bad_wr) != 0) {
646 log_err("fio: ibv_post_send fail\n");
647 return -1;
648 }
649
650 dprint_io_u(io_us[i], "fio_rdmaio_send");
651 }
652
653 /* wait for completion
654 rdma_poll_wait(td, comp_opcode); */
655
656 return i;
657}
658
659static int fio_rdmaio_recv(struct thread_data *td, struct io_u **io_us,
660 unsigned int nr)
661{
662 struct rdmaio_data *rd = td->io_ops->data;
663 struct ibv_recv_wr *bad_wr;
664 struct rdma_io_u_data *r_io_u_d;
665 int i;
666
667 i = 0;
668 if (rd->rdma_protocol == FIO_RDMA_CHA_RECV) {
669 /* post io_u into recv queue */
670 for (i = 0; i < nr; i++) {
671 r_io_u_d = io_us[i]->engine_data;
672 if (ibv_post_recv(rd->qp, &r_io_u_d->rq_wr, &bad_wr) !=
673 0) {
674 log_err("fio: ibv_post_recv fail\n");
675 return 1;
676 }
677 }
678 } else if ((rd->rdma_protocol == FIO_RDMA_MEM_READ)
679 || (rd->rdma_protocol == FIO_RDMA_MEM_WRITE)) {
680 /* re-post the rq_wr */
681 if (ibv_post_recv(rd->qp, &rd->rq_wr, &bad_wr) != 0) {
682 log_err("fio: ibv_post_recv fail\n");
683 return 1;
684 }
685
686 rdma_poll_wait(td, IBV_WC_RECV);
687
688 dprint(FD_IO, "fio: recv FINISH message\n");
a05d62b2
YR
689 td->done = 1;
690 return 0;
21b8aee8 691 }
692
693 return i;
694}
695
696static int fio_rdmaio_queue(struct thread_data *td, struct io_u *io_u)
697{
698 struct rdmaio_data *rd = td->io_ops->data;
699
700 fio_ro_check(td, io_u);
701
702 if (rd->io_u_queued_nr == (int)td->o.iodepth)
703 return FIO_Q_BUSY;
704
705 rd->io_us_queued[rd->io_u_queued_nr] = io_u;
706 rd->io_u_queued_nr++;
707
708 dprint_io_u(io_u, "fio_rdmaio_queue");
709
710 return FIO_Q_QUEUED;
711}
712
713static void fio_rdmaio_queued(struct thread_data *td, struct io_u **io_us,
714 unsigned int nr)
715{
716 struct rdmaio_data *rd = td->io_ops->data;
717 struct timeval now;
718 unsigned int i;
719
720 if (!fio_fill_issue_time(td))
721 return;
722
723 fio_gettime(&now, NULL);
724
725 for (i = 0; i < nr; i++) {
726 struct io_u *io_u = io_us[i];
727
728 /* queued -> flight */
729 rd->io_us_flight[rd->io_u_flight_nr] = io_u;
730 rd->io_u_flight_nr++;
731
732 memcpy(&io_u->issue_time, &now, sizeof(now));
733 io_u_queued(td, io_u);
734 }
735}
736
737static int fio_rdmaio_commit(struct thread_data *td)
738{
739 struct rdmaio_data *rd = td->io_ops->data;
740 struct io_u **io_us;
741 int ret;
742
743 if (!rd->io_us_queued)
744 return 0;
745
746 io_us = rd->io_us_queued;
747 do {
748 /* RDMA_WRITE or RDMA_READ */
222757cc 749 if (rd->is_client)
21b8aee8 750 ret = fio_rdmaio_send(td, io_us, rd->io_u_queued_nr);
222757cc 751 else if (!rd->is_client)
21b8aee8 752 ret = fio_rdmaio_recv(td, io_us, rd->io_u_queued_nr);
222757cc 753 else
21b8aee8 754 ret = 0; /* must be a SYNC */
755
756 if (ret > 0) {
757 fio_rdmaio_queued(td, io_us, ret);
758 io_u_mark_submit(td, ret);
759 rd->io_u_queued_nr -= ret;
760 io_us += ret;
761 ret = 0;
762 } else
763 break;
764 } while (rd->io_u_queued_nr);
765
766 return ret;
767}
768
769static int fio_rdmaio_connect(struct thread_data *td, struct fio_file *f)
770{
771 struct rdmaio_data *rd = td->io_ops->data;
772 struct rdma_conn_param conn_param;
773 struct ibv_send_wr *bad_wr;
774
222757cc 775 memset(&conn_param, 0, sizeof(conn_param));
21b8aee8 776 conn_param.responder_resources = 1;
777 conn_param.initiator_depth = 1;
778 conn_param.retry_count = 10;
779
780 if (rdma_connect(rd->cm_id, &conn_param) != 0) {
781 log_err("fio: rdma_connect fail\n");
782 return 1;
783 }
784
785 if (get_next_channel_event
786 (td, rd->cm_channel, RDMA_CM_EVENT_ESTABLISHED) != 0) {
787 log_err("fio: wait for RDMA_CM_EVENT_ESTABLISHED\n");
788 return 1;
789 }
790
791 /* send task request */
792 rd->send_buf.mode = htonl(rd->rdma_protocol);
793 rd->send_buf.nr = htonl(td->o.iodepth);
794
795 if (ibv_post_send(rd->qp, &rd->sq_wr, &bad_wr) != 0) {
796 log_err("fio: ibv_post_send fail");
797 return 1;
798 }
799
800 rdma_poll_wait(td, IBV_WC_SEND);
801
802 /* wait for remote MR info from server side */
803 rdma_poll_wait(td, IBV_WC_RECV);
804
7d7803fa
YR
805 /* In SEND/RECV test, it's a good practice to setup the iodepth of
806 * of the RECV side deeper than that of the SEND side to
807 * avoid RNR (receiver not ready) error. The
222757cc 808 * SEND side may send so many unsolicited message before
7d7803fa
YR
809 * RECV side commits sufficient recv buffers into recv queue.
810 * This may lead to RNR error. Here, SEND side pauses for a while
811 * during which RECV side commits sufficient recv buffers.
812 */
813 usleep(500000);
814
21b8aee8 815 return 0;
816}
817
818static int fio_rdmaio_accept(struct thread_data *td, struct fio_file *f)
819{
820 struct rdmaio_data *rd = td->io_ops->data;
821 struct rdma_conn_param conn_param;
822 struct ibv_send_wr *bad_wr;
823
824 /* rdma_accept() - then wait for accept success */
222757cc 825 memset(&conn_param, 0, sizeof(conn_param));
21b8aee8 826 conn_param.responder_resources = 1;
827 conn_param.initiator_depth = 1;
828
829 if (rdma_accept(rd->child_cm_id, &conn_param) != 0) {
830 log_err("fio: rdma_accept\n");
831 return 1;
832 }
833
834 if (get_next_channel_event
835 (td, rd->cm_channel, RDMA_CM_EVENT_ESTABLISHED) != 0) {
836 log_err("fio: wait for RDMA_CM_EVENT_ESTABLISHED\n");
837 return 1;
838 }
839
840 /* wait for request */
841 rdma_poll_wait(td, IBV_WC_RECV);
842
843 if (ibv_post_send(rd->qp, &rd->sq_wr, &bad_wr) != 0) {
844 log_err("fio: ibv_post_send fail");
845 return 1;
846 }
847
848 rdma_poll_wait(td, IBV_WC_SEND);
849
850 return 0;
851}
852
853static int fio_rdmaio_open_file(struct thread_data *td, struct fio_file *f)
854{
855 if (td_read(td))
856 return fio_rdmaio_accept(td, f);
857 else
858 return fio_rdmaio_connect(td, f);
859}
860
861static int fio_rdmaio_close_file(struct thread_data *td, struct fio_file *f)
862{
863 struct rdmaio_data *rd = td->io_ops->data;
864 struct ibv_send_wr *bad_wr;
865
866 /* unregister rdma buffer */
867
868 /*
869 * Client sends notification to the server side
870 */
871 /* refer to: http://linux.die.net/man/7/rdma_cm */
872 if ((rd->is_client == 1) && ((rd->rdma_protocol == FIO_RDMA_MEM_WRITE)
873 || (rd->rdma_protocol ==
874 FIO_RDMA_MEM_READ))) {
875 if (ibv_post_send(rd->qp, &rd->sq_wr, &bad_wr) != 0) {
876 log_err("fio: ibv_post_send fail");
877 return 1;
878 }
879
880 dprint(FD_IO, "fio: close infomation sent success\n");
881 rdma_poll_wait(td, IBV_WC_SEND);
882 }
883
884 if (rd->is_client == 1)
885 rdma_disconnect(rd->cm_id);
886 else {
887 rdma_disconnect(rd->child_cm_id);
222757cc
JA
888#if 0
889 rdma_disconnect(rd->cm_id);
890#endif
21b8aee8 891 }
892
222757cc
JA
893#if 0
894 if (get_next_channel_event(td, rd->cm_channel, RDMA_CM_EVENT_DISCONNECTED) != 0) {
895 log_err("fio: wait for RDMA_CM_EVENT_DISCONNECTED\n");
896 return 1;
897 }
898#endif
21b8aee8 899
21b8aee8 900 ibv_destroy_cq(rd->cq);
7d7803fa 901 ibv_destroy_qp(rd->qp);
21b8aee8 902
903 if (rd->is_client == 1)
904 rdma_destroy_id(rd->cm_id);
905 else {
906 rdma_destroy_id(rd->child_cm_id);
907 rdma_destroy_id(rd->cm_id);
908 }
909
910 ibv_destroy_comp_channel(rd->channel);
911 ibv_dealloc_pd(rd->pd);
912
913 return 0;
914}
915
916static int fio_rdmaio_setup_connect(struct thread_data *td, const char *host,
917 unsigned short port)
918{
919 struct rdmaio_data *rd = td->io_ops->data;
920 struct ibv_recv_wr *bad_wr;
222757cc 921 int err;
21b8aee8 922
923 rd->addr.sin_family = AF_INET;
924 rd->addr.sin_port = htons(port);
925
926 if (inet_aton(host, &rd->addr.sin_addr) != 1) {
927 struct hostent *hent;
928
929 hent = gethostbyname(host);
930 if (!hent) {
931 td_verror(td, errno, "gethostbyname");
932 return 1;
933 }
934
935 memcpy(&rd->addr.sin_addr, hent->h_addr, 4);
936 }
937
938 /* resolve route */
222757cc
JA
939 err = rdma_resolve_addr(rd->cm_id, NULL, (struct sockaddr *)&rd->addr, 2000);
940 if (err != 0) {
941 log_err("fio: rdma_resolve_addr: %d\n", err);
21b8aee8 942 return 1;
943 }
944
222757cc
JA
945 err = get_next_channel_event(td, rd->cm_channel, RDMA_CM_EVENT_ADDR_RESOLVED);
946 if (err != 0) {
947 log_err("fio: get_next_channel_event: %d\n", err);
21b8aee8 948 return 1;
949 }
950
951 /* resolve route */
222757cc
JA
952 err = rdma_resolve_route(rd->cm_id, 2000);
953 if (err != 0) {
954 log_err("fio: rdma_resolve_route: %d\n", err);
21b8aee8 955 return 1;
956 }
957
222757cc
JA
958 err = get_next_channel_event(td, rd->cm_channel, RDMA_CM_EVENT_ROUTE_RESOLVED);
959 if (err != 0) {
960 log_err("fio: get_next_channel_event: %d\n", err);
21b8aee8 961 return 1;
962 }
963
964 /* create qp and buffer */
965 if (fio_rdmaio_setup_qp(td) != 0)
966 return 1;
967
968 if (fio_rdmaio_setup_control_msg_buffers(td) != 0)
969 return 1;
970
971 /* post recv buf */
222757cc
JA
972 err = ibv_post_recv(rd->qp, &rd->rq_wr, &bad_wr);
973 if (err != 0) {
974 log_err("fio: ibv_post_recv fail: %d\n", err);
21b8aee8 975 return 1;
976 }
977
978 return 0;
979}
980
981static int fio_rdmaio_setup_listen(struct thread_data *td, short port)
982{
983 struct rdmaio_data *rd = td->io_ops->data;
984 struct ibv_recv_wr *bad_wr;
985
986 rd->addr.sin_family = AF_INET;
987 rd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
988 rd->addr.sin_port = htons(port);
989
990 /* rdma_listen */
991 if (rdma_bind_addr(rd->cm_id, (struct sockaddr *)&rd->addr) != 0) {
992 log_err("fio: rdma_bind_addr fail\n");
993 return 1;
994 }
995
996 if (rdma_listen(rd->cm_id, 3) != 0) {
997 log_err("fio: rdma_listen fail\n");
998 return 1;
999 }
1000
1001 /* wait for CONNECT_REQUEST */
1002 if (get_next_channel_event
1003 (td, rd->cm_channel, RDMA_CM_EVENT_CONNECT_REQUEST) != 0) {
1004 log_err("fio: wait for RDMA_CM_EVENT_CONNECT_REQUEST\n");
1005 return 1;
1006 }
1007
1008 if (fio_rdmaio_setup_qp(td) != 0)
1009 return 1;
1010
1011 if (fio_rdmaio_setup_control_msg_buffers(td) != 0)
1012 return 1;
1013
1014 /* post recv buf */
1015 if (ibv_post_recv(rd->qp, &rd->rq_wr, &bad_wr) != 0) {
1016 log_err("fio: ibv_post_recv fail\n");
1017 return 1;
1018 }
1019
1020 return 0;
1021}
1022
1023static int fio_rdmaio_init(struct thread_data *td)
1024{
1025 struct rdmaio_data *rd = td->io_ops->data;
222757cc
JA
1026 struct flist_head *entry;
1027 unsigned int max_bs;
21b8aee8 1028 unsigned int port;
1029 char host[64], buf[128];
1030 char *sep, *portp, *modep;
222757cc 1031 int ret, i = 0;
21b8aee8 1032 struct rlimit rl;
1033
1034 if (td_rw(td)) {
1035 log_err("fio: rdma connections must be read OR write\n");
1036 return 1;
1037 }
1038 if (td_random(td)) {
1039 log_err("fio: RDMA network IO can't be random\n");
1040 return 1;
1041 }
1042
1043 /* check RLIMIT_MEMLOCK */
1044 if (getrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
1045 log_err("fio: getrlimit fail: %d(%s)\n",
1046 errno, strerror(errno));
1047 return 1;
1048 }
1049
1050 /* soft limit */
1051 if ((rl.rlim_cur != RLIM_INFINITY)
1052 && (rl.rlim_cur < td->orig_buffer_size)) {
e07f72d3
BVA
1053 log_err("fio: soft RLIMIT_MEMLOCK is: %" PRId64 "\n",
1054 rl.rlim_cur);
1055 log_err("fio: total block size is: %zd\n",
21b8aee8 1056 td->orig_buffer_size);
1057 /* try to set larger RLIMIT_MEMLOCK */
1058 rl.rlim_cur = rl.rlim_max;
1059 if (setrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
1060 log_err("fio: setrlimit fail: %d(%s)\n",
1061 errno, strerror(errno));
1062 log_err("fio: you may try enlarge MEMLOCK by root\n");
1063 log_err("# ulimit -l unlimited\n");
1064 return 1;
1065 }
1066 }
1067
1068 strcpy(buf, td->o.filename);
1069
1070 sep = strchr(buf, '/');
1071 if (!sep)
1072 goto bad_host;
1073
1074 *sep = '\0';
1075 sep++;
1076 strcpy(host, buf);
1077 if (!strlen(host))
1078 goto bad_host;
1079
1080 modep = NULL;
1081 portp = sep;
1082 sep = strchr(portp, '/');
1083 if (sep) {
1084 *sep = '\0';
1085 modep = sep + 1;
1086 }
1087
1088 port = strtol(portp, NULL, 10);
1089 if (!port || port > 65535)
1090 goto bad_host;
1091
1092 if (modep) {
1093 if (!strncmp("rdma_write", modep, strlen(modep)) ||
1094 !strncmp("RDMA_WRITE", modep, strlen(modep)))
1095 rd->rdma_protocol = FIO_RDMA_MEM_WRITE;
1096 else if (!strncmp("rdma_read", modep, strlen(modep)) ||
1097 !strncmp("RDMA_READ", modep, strlen(modep)))
1098 rd->rdma_protocol = FIO_RDMA_MEM_READ;
1099 else if (!strncmp("send", modep, strlen(modep)) ||
1100 !strncmp("SEND", modep, strlen(modep)))
1101 rd->rdma_protocol = FIO_RDMA_CHA_SEND;
1102 else
1103 goto bad_host;
1104 } else
1105 rd->rdma_protocol = FIO_RDMA_MEM_WRITE;
1106
1107 rd->cq_event_num = 0;
1108
1109 rd->cm_channel = rdma_create_event_channel();
1110 if (!rd->cm_channel) {
1111 log_err("fio: rdma_create_event_channel fail\n");
1112 return 1;
1113 }
1114
1115 ret = rdma_create_id(rd->cm_channel, &rd->cm_id, rd, RDMA_PS_TCP);
1116 if (ret) {
1117 log_err("fio: rdma_create_id fail\n");
1118 return 1;
1119 }
1120
1121 if ((rd->rdma_protocol == FIO_RDMA_MEM_WRITE) ||
1122 (rd->rdma_protocol == FIO_RDMA_MEM_READ)) {
1123 rd->rmt_us =
76cc5224 1124 malloc(FIO_RDMA_MAX_IO_DEPTH * sizeof(struct remote_u));
21b8aee8 1125 memset(rd->rmt_us, 0,
76cc5224 1126 FIO_RDMA_MAX_IO_DEPTH * sizeof(struct remote_u));
21b8aee8 1127 rd->rmt_nr = 0;
1128 }
1129
1130 rd->io_us_queued = malloc(td->o.iodepth * sizeof(struct io_u *));
1131 memset(rd->io_us_queued, 0, td->o.iodepth * sizeof(struct io_u *));
1132 rd->io_u_queued_nr = 0;
1133
1134 rd->io_us_flight = malloc(td->o.iodepth * sizeof(struct io_u *));
1135 memset(rd->io_us_flight, 0, td->o.iodepth * sizeof(struct io_u *));
1136 rd->io_u_flight_nr = 0;
1137
1138 rd->io_us_completed = malloc(td->o.iodepth * sizeof(struct io_u *));
1139 memset(rd->io_us_completed, 0, td->o.iodepth * sizeof(struct io_u *));
1140 rd->io_u_completed_nr = 0;
1141
1142 if (td_read(td)) { /* READ as the server */
1143 rd->is_client = 0;
1144 /* server rd->rdma_buf_len will be setup after got request */
1145 ret = fio_rdmaio_setup_listen(td, port);
1146 } else { /* WRITE as the client */
1147 rd->is_client = 1;
1148 ret = fio_rdmaio_setup_connect(td, host, port);
1149 }
1150
21b8aee8 1151 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
1152 /* register each io_u in the free list */
21b8aee8 1153 flist_for_each(entry, &td->io_u_freelist) {
1154 struct io_u *io_u = flist_entry(entry, struct io_u, list);
1155
1156 io_u->engine_data = malloc(sizeof(struct rdma_io_u_data));
1157 memset(io_u->engine_data, 0, sizeof(struct rdma_io_u_data));
1158 ((struct rdma_io_u_data *)io_u->engine_data)->wr_id = i;
1159
1160 io_u->mr = ibv_reg_mr(rd->pd, io_u->buf, max_bs,
1161 IBV_ACCESS_LOCAL_WRITE |
1162 IBV_ACCESS_REMOTE_READ |
1163 IBV_ACCESS_REMOTE_WRITE);
1164 if (io_u->mr == NULL) {
1165 log_err("fio: ibv_reg_mr io_u failed\n");
1166 return 1;
1167 }
1168
1169 rd->send_buf.rmt_us[i].buf =
1170 htonll((uint64_t) (unsigned long)io_u->buf);
1171 rd->send_buf.rmt_us[i].rkey = htonl(io_u->mr->rkey);
1172 rd->send_buf.rmt_us[i].size = htonl(max_bs);
1173
222757cc
JA
1174#if 0
1175 log_info("fio: Send rkey %x addr %" PRIx64 " len %d to client\n", io_u->mr->rkey, io_u->buf, max_bs); */
1176#endif
21b8aee8 1177 i++;
1178 }
1179
1180 rd->send_buf.nr = htonl(i);
1181
1182 return ret;
1183bad_host:
1184 log_err("fio: bad rdma host/port/protocol: %s\n", td->o.filename);
1185 return 1;
1186}
1187
1188static void fio_rdmaio_cleanup(struct thread_data *td)
1189{
1190 struct rdmaio_data *rd = td->io_ops->data;
1191
222757cc 1192 if (rd)
21b8aee8 1193 free(rd);
21b8aee8 1194}
1195
1196static int fio_rdmaio_setup(struct thread_data *td)
1197{
1198 struct rdmaio_data *rd;
1199
1200 if (!td->io_ops->data) {
222757cc 1201 rd = malloc(sizeof(*rd));
21b8aee8 1202
1203 memset(rd, 0, sizeof(*rd));
ee88d056 1204 init_rand_seed(&rd->rand_state, (unsigned int) GOLDEN_RATIO_PRIME);
21b8aee8 1205 td->io_ops->data = rd;
1206 }
1207
1208 return 0;
1209}
1210
1211static struct ioengine_ops ioengine_rw = {
b6cf38f0
YR
1212 .name = "rdma",
1213 .version = FIO_IOOPS_VERSION,
1214 .setup = fio_rdmaio_setup,
1215 .init = fio_rdmaio_init,
1216 .prep = fio_rdmaio_prep,
1217 .queue = fio_rdmaio_queue,
1218 .commit = fio_rdmaio_commit,
1219 .getevents = fio_rdmaio_getevents,
1220 .event = fio_rdmaio_event,
1221 .cleanup = fio_rdmaio_cleanup,
1222 .open_file = fio_rdmaio_open_file,
1223 .close_file = fio_rdmaio_close_file,
1224 .flags = FIO_DISKLESSIO | FIO_UNIDIR | FIO_PIPEIO,
21b8aee8 1225};
1226
1227#else /* FIO_HAVE_RDMA */
1228
1229static int fio_rdmaio_open_file(struct thread_data *td, struct fio_file *f)
1230{
1231 return 0;
1232}
1233
1234static int fio_rdmaio_close_file(struct thread_data *td, struct fio_file *f)
1235{
1236 return 0;
1237}
1238
1239static int fio_rdmaio_queue(struct thread_data *td, struct io_u *io_u)
1240{
1241 return FIO_Q_COMPLETED;
1242}
1243
1244static int fio_rdmaio_init(struct thread_data fio_unused * td)
1245{
1246 log_err("fio: rdma(librdmacm libibverbs) not available\n");
1247 log_err(" You haven't compiled rdma ioengine into fio.\n");
1248 log_err(" If you want to try rdma ioengine,\n");
1249 log_err(" make sure OFED is installed,\n");
1250 log_err(" $ ofed_info\n");
1251 log_err(" then try to make fio as follows:\n");
7d7803fa
YR
1252 log_err(" $ export EXTFLAGS+=\" -DFIO_HAVE_RDMA \"\n");
1253 log_err(" $ export EXTLIBS+=\" -libverbs -lrdmacm \"\n");
21b8aee8 1254 log_err(" $ make clean && make\n");
1255 return 1;
1256}
1257
1258static struct ioengine_ops ioengine_rw = {
b6cf38f0
YR
1259 .name = "rdma",
1260 .version = FIO_IOOPS_VERSION,
1261 .init = fio_rdmaio_init,
1262 .queue = fio_rdmaio_queue,
1263 .open_file = fio_rdmaio_open_file,
1264 .close_file = fio_rdmaio_close_file,
1265 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR | FIO_PIPEIO,
21b8aee8 1266};
1267
1268#endif
1269
1270static void fio_init fio_rdmaio_register(void)
1271{
1272 register_ioengine(&ioengine_rw);
1273}
1274
1275static void fio_exit fio_rdmaio_unregister(void)
1276{
1277 unregister_ioengine(&ioengine_rw);
1278}