rdma: fixup compile issue
[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.
21 * 2. server side parses test mode, and sends back confirmation
22 * to client side. In RDMA WRITE/READ test, this confirmation
23 * includes memory information, such as rkey, address.
24 * 3. client side initiates test loop.
25 * 4. In RDMA WRITE/READ test, client side sends a completion
26 * notification to server side. Server side updates its
27 * td->done as true.
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);
328 if (rd->pd == NULL) {
329 log_err("fio: ibv_alloc_pd fail\n");
330 return 1;
331 }
332
333 if (rd->is_client == 0)
334 rd->channel = ibv_create_comp_channel(rd->child_cm_id->verbs);
335 else
336 rd->channel = ibv_create_comp_channel(rd->cm_id->verbs);
337 if (rd->channel == NULL) {
338 log_err("fio: ibv_create_comp_channel fail\n");
339 goto err1;
340 }
341
342 if (qp_depth < 16)
343 qp_depth = 16;
344
345 if (rd->is_client == 0)
346 rd->cq = ibv_create_cq(rd->child_cm_id->verbs,
347 qp_depth, rd, rd->channel, 0);
348 else
349 rd->cq = ibv_create_cq(rd->cm_id->verbs,
350 qp_depth, rd, rd->channel, 0);
351 if (rd->cq == NULL) {
352 log_err("fio: ibv_create_cq failed\n");
353 goto err2;
354 }
355
356 if (ibv_req_notify_cq(rd->cq, 0) != 0) {
357 log_err("fio: ibv_create_cq failed\n");
358 goto err3;
359 }
360
361 /* create queue pair */
362 memset(&init_attr, 0, sizeof(init_attr));
363 init_attr.cap.max_send_wr = qp_depth;
364 init_attr.cap.max_recv_wr = qp_depth;
365 init_attr.cap.max_recv_sge = 1;
366 init_attr.cap.max_send_sge = 1;
367 init_attr.qp_type = IBV_QPT_RC;
368 init_attr.send_cq = rd->cq;
369 init_attr.recv_cq = rd->cq;
370
371 if (rd->is_client == 0) {
372 if (rdma_create_qp(rd->child_cm_id, rd->pd, &init_attr) != 0) {
373 log_err("fio: rdma_create_qp failed\n");
374 goto err3;
375 }
376 rd->qp = rd->child_cm_id->qp;
377 } else {
378 if (rdma_create_qp(rd->cm_id, rd->pd, &init_attr) != 0) {
379 log_err("fio: rdma_create_qp failed\n");
380 goto err3;
381 }
382 rd->qp = rd->cm_id->qp;
383 }
384
385 return 0;
386
387err3:
388 ibv_destroy_cq(rd->cq);
389err2:
390 ibv_destroy_comp_channel(rd->channel);
391err1:
392 ibv_dealloc_pd(rd->pd);
393
394 return 1;
395}
396
397static int fio_rdmaio_setup_control_msg_buffers(struct thread_data *td)
398{
399 struct rdmaio_data *rd = td->io_ops->data;
400
401 rd->recv_mr = ibv_reg_mr(rd->pd, &rd->recv_buf, sizeof(rd->recv_buf),
402 IBV_ACCESS_LOCAL_WRITE);
403 if (rd->recv_mr == NULL) {
404 log_err("fio: recv_buf reg_mr failed\n");
405 return 1;
406 }
407
408 rd->send_mr = ibv_reg_mr(rd->pd, &rd->send_buf, sizeof(rd->send_buf),
409 0);
410 if (rd->send_mr == NULL) {
411 log_err("fio: send_buf reg_mr failed\n");
412 ibv_dereg_mr(rd->recv_mr);
413 return 1;
414 }
415
416 /* setup work request */
417 /* recv wq */
418 rd->recv_sgl.addr = (uint64_t) (unsigned long)&rd->recv_buf;
419 rd->recv_sgl.length = sizeof rd->recv_buf;
420 rd->recv_sgl.lkey = rd->recv_mr->lkey;
421 rd->rq_wr.sg_list = &rd->recv_sgl;
422 rd->rq_wr.num_sge = 1;
76cc5224 423 rd->rq_wr.wr_id = FIO_RDMA_MAX_IO_DEPTH;
21b8aee8 424
425 /* send wq */
426 rd->send_sgl.addr = (uint64_t) (unsigned long)&rd->send_buf;
427 rd->send_sgl.length = sizeof rd->send_buf;
428 rd->send_sgl.lkey = rd->send_mr->lkey;
429
430 rd->sq_wr.opcode = IBV_WR_SEND;
431 rd->sq_wr.send_flags = IBV_SEND_SIGNALED;
432 rd->sq_wr.sg_list = &rd->send_sgl;
433 rd->sq_wr.num_sge = 1;
76cc5224 434 rd->sq_wr.wr_id = FIO_RDMA_MAX_IO_DEPTH;
21b8aee8 435
436 return 0;
437}
438
439static int get_next_channel_event(struct thread_data *td,
440 struct rdma_event_channel *channel,
441 enum rdma_cm_event_type wait_event)
442{
443 struct rdmaio_data *rd = td->io_ops->data;
444
445 int ret;
446 struct rdma_cm_event *event;
447
448 ret = rdma_get_cm_event(channel, &event);
449 if (ret) {
450 log_err("fio: rdma_get_cm_event");
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];
524 for (i = 0; i < rd->io_u_completed_nr - 1; i++) {
525 rd->io_us_completed[i] = rd->io_us_completed[i + 1];
526 }
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;
538 int r;
539 enum ibv_wc_opcode comp_opcode;
540 comp_opcode = IBV_WC_RDMA_WRITE;
541 struct ibv_cq *ev_cq;
542 void *ev_ctx;
543 int ret;
544
545 r = 0;
546
547 switch (rd->rdma_protocol) {
548 case FIO_RDMA_MEM_WRITE:
549 comp_opcode = IBV_WC_RDMA_WRITE;
550 break;
551 case FIO_RDMA_MEM_READ:
552 comp_opcode = IBV_WC_RDMA_READ;
553 break;
554 case FIO_RDMA_CHA_SEND:
555 comp_opcode = IBV_WC_SEND;
556 break;
557 case FIO_RDMA_CHA_RECV:
558 comp_opcode = IBV_WC_RECV;
559 break;
560 default:
561 log_err("fio: unknown rdma protocol - %d\n", rd->rdma_protocol);
562 break;
563 }
564
565 if (rd->cq_event_num > 0) { /* previous left */
566 rd->cq_event_num--;
567 return 0;
568 }
569
570again:
571 if (ibv_get_cq_event(rd->channel, &ev_cq, &ev_ctx) != 0) {
572 log_err("fio: Failed to get cq event!\n");
573 return -1;
574 }
575 if (ev_cq != rd->cq) {
576 log_err("fio: Unknown CQ!\n");
577 return -1;
578 }
579 if (ibv_req_notify_cq(rd->cq, 0) != 0) {
580 log_err("fio: Failed to set notify!\n");
581 return -1;
582 }
583
584 ret = cq_event_handler(td, comp_opcode);
585 if (ret < 1)
586 goto again;
587
588 ibv_ack_cq_events(rd->cq, ret);
589
590 r += ret;
591 if (r < min)
592 goto again;
593
594 rd->cq_event_num -= r;
595
596 return r;
597}
598
599static int fio_rdmaio_send(struct thread_data *td, struct io_u **io_us,
600 unsigned int nr)
601{
602 struct rdmaio_data *rd = td->io_ops->data;
603 struct ibv_send_wr *bad_wr;
e07f72d3 604#if 0
21b8aee8 605 enum ibv_wc_opcode comp_opcode;
606 comp_opcode = IBV_WC_RDMA_WRITE;
e07f72d3 607#endif
7d7803fa
YR
608 int i;
609 long index;
21b8aee8 610 struct rdma_io_u_data *r_io_u_d;
611
612 r_io_u_d = NULL;
613
614 for (i = 0; i < nr; i++) {
615 /* RDMA_WRITE or RDMA_READ */
616 switch (rd->rdma_protocol) {
617 case FIO_RDMA_MEM_WRITE:
618 /* compose work request */
619 r_io_u_d = io_us[i]->engine_data;
7d7803fa
YR
620 if (td->o.use_os_rand)
621 index = os_random_long(&td->random_state) % rd->rmt_nr;
622 else
ea6209ad 623 index = __rand(&rd->rand_state) % rd->rmt_nr;
21b8aee8 624 r_io_u_d->sq_wr.opcode = IBV_WR_RDMA_WRITE;
625 r_io_u_d->sq_wr.wr.rdma.rkey = rd->rmt_us[index].rkey;
b6cf38f0
YR
626 r_io_u_d->sq_wr.wr.rdma.remote_addr = \
627 rd->rmt_us[index].buf;
21b8aee8 628 r_io_u_d->sq_wr.sg_list->length = io_us[i]->buflen;
629 break;
630 case FIO_RDMA_MEM_READ:
631 /* compose work request */
632 r_io_u_d = io_us[i]->engine_data;
7d7803fa
YR
633 if (td->o.use_os_rand)
634 index = os_random_long(&td->random_state) % rd->rmt_nr;
635 else
ea6209ad 636 index = __rand(&rd->rand_state) % rd->rmt_nr;
21b8aee8 637 r_io_u_d->sq_wr.opcode = IBV_WR_RDMA_READ;
638 r_io_u_d->sq_wr.wr.rdma.rkey = rd->rmt_us[index].rkey;
b6cf38f0
YR
639 r_io_u_d->sq_wr.wr.rdma.remote_addr = \
640 rd->rmt_us[index].buf;
21b8aee8 641 r_io_u_d->sq_wr.sg_list->length = io_us[i]->buflen;
642 break;
643 case FIO_RDMA_CHA_SEND:
644 r_io_u_d = io_us[i]->engine_data;
645 r_io_u_d->sq_wr.opcode = IBV_WR_SEND;
646 r_io_u_d->sq_wr.send_flags = IBV_SEND_SIGNALED;
647 break;
648 default:
649 log_err("fio: unknown rdma protocol - %d\n",
650 rd->rdma_protocol);
651 break;
652 }
653
654 if (ibv_post_send(rd->qp, &r_io_u_d->sq_wr, &bad_wr) != 0) {
655 log_err("fio: ibv_post_send fail\n");
656 return -1;
657 }
658
659 dprint_io_u(io_us[i], "fio_rdmaio_send");
660 }
661
662 /* wait for completion
663 rdma_poll_wait(td, comp_opcode); */
664
665 return i;
666}
667
668static int fio_rdmaio_recv(struct thread_data *td, struct io_u **io_us,
669 unsigned int nr)
670{
671 struct rdmaio_data *rd = td->io_ops->data;
672 struct ibv_recv_wr *bad_wr;
673 struct rdma_io_u_data *r_io_u_d;
674 int i;
675
676 i = 0;
677 if (rd->rdma_protocol == FIO_RDMA_CHA_RECV) {
678 /* post io_u into recv queue */
679 for (i = 0; i < nr; i++) {
680 r_io_u_d = io_us[i]->engine_data;
681 if (ibv_post_recv(rd->qp, &r_io_u_d->rq_wr, &bad_wr) !=
682 0) {
683 log_err("fio: ibv_post_recv fail\n");
684 return 1;
685 }
686 }
687 } else if ((rd->rdma_protocol == FIO_RDMA_MEM_READ)
688 || (rd->rdma_protocol == FIO_RDMA_MEM_WRITE)) {
689 /* re-post the rq_wr */
690 if (ibv_post_recv(rd->qp, &rd->rq_wr, &bad_wr) != 0) {
691 log_err("fio: ibv_post_recv fail\n");
692 return 1;
693 }
694
695 rdma_poll_wait(td, IBV_WC_RECV);
696
697 dprint(FD_IO, "fio: recv FINISH message\n");
a05d62b2
YR
698 td->done = 1;
699 return 0;
21b8aee8 700 }
701
702 return i;
703}
704
705static int fio_rdmaio_queue(struct thread_data *td, struct io_u *io_u)
706{
707 struct rdmaio_data *rd = td->io_ops->data;
708
709 fio_ro_check(td, io_u);
710
711 if (rd->io_u_queued_nr == (int)td->o.iodepth)
712 return FIO_Q_BUSY;
713
714 rd->io_us_queued[rd->io_u_queued_nr] = io_u;
715 rd->io_u_queued_nr++;
716
717 dprint_io_u(io_u, "fio_rdmaio_queue");
718
719 return FIO_Q_QUEUED;
720}
721
722static void fio_rdmaio_queued(struct thread_data *td, struct io_u **io_us,
723 unsigned int nr)
724{
725 struct rdmaio_data *rd = td->io_ops->data;
726 struct timeval now;
727 unsigned int i;
728
729 if (!fio_fill_issue_time(td))
730 return;
731
732 fio_gettime(&now, NULL);
733
734 for (i = 0; i < nr; i++) {
735 struct io_u *io_u = io_us[i];
736
737 /* queued -> flight */
738 rd->io_us_flight[rd->io_u_flight_nr] = io_u;
739 rd->io_u_flight_nr++;
740
741 memcpy(&io_u->issue_time, &now, sizeof(now));
742 io_u_queued(td, io_u);
743 }
744}
745
746static int fio_rdmaio_commit(struct thread_data *td)
747{
748 struct rdmaio_data *rd = td->io_ops->data;
749 struct io_u **io_us;
750 int ret;
751
752 if (!rd->io_us_queued)
753 return 0;
754
755 io_us = rd->io_us_queued;
756 do {
757 /* RDMA_WRITE or RDMA_READ */
758 if (rd->is_client) {
759 ret = fio_rdmaio_send(td, io_us, rd->io_u_queued_nr);
760 } else if (!rd->is_client) {
761 ret = fio_rdmaio_recv(td, io_us, rd->io_u_queued_nr);
762 } else
763 ret = 0; /* must be a SYNC */
764
765 if (ret > 0) {
766 fio_rdmaio_queued(td, io_us, ret);
767 io_u_mark_submit(td, ret);
768 rd->io_u_queued_nr -= ret;
769 io_us += ret;
770 ret = 0;
771 } else
772 break;
773 } while (rd->io_u_queued_nr);
774
775 return ret;
776}
777
778static int fio_rdmaio_connect(struct thread_data *td, struct fio_file *f)
779{
780 struct rdmaio_data *rd = td->io_ops->data;
781 struct rdma_conn_param conn_param;
782 struct ibv_send_wr *bad_wr;
783
784 memset(&conn_param, 0, sizeof conn_param);
785 conn_param.responder_resources = 1;
786 conn_param.initiator_depth = 1;
787 conn_param.retry_count = 10;
788
789 if (rdma_connect(rd->cm_id, &conn_param) != 0) {
790 log_err("fio: rdma_connect fail\n");
791 return 1;
792 }
793
794 if (get_next_channel_event
795 (td, rd->cm_channel, RDMA_CM_EVENT_ESTABLISHED) != 0) {
796 log_err("fio: wait for RDMA_CM_EVENT_ESTABLISHED\n");
797 return 1;
798 }
799
800 /* send task request */
801 rd->send_buf.mode = htonl(rd->rdma_protocol);
802 rd->send_buf.nr = htonl(td->o.iodepth);
803
804 if (ibv_post_send(rd->qp, &rd->sq_wr, &bad_wr) != 0) {
805 log_err("fio: ibv_post_send fail");
806 return 1;
807 }
808
809 rdma_poll_wait(td, IBV_WC_SEND);
810
811 /* wait for remote MR info from server side */
812 rdma_poll_wait(td, IBV_WC_RECV);
813
7d7803fa
YR
814 /* In SEND/RECV test, it's a good practice to setup the iodepth of
815 * of the RECV side deeper than that of the SEND side to
816 * avoid RNR (receiver not ready) error. The
817 * SEND side may send so many unsolicited message before
818 * RECV side commits sufficient recv buffers into recv queue.
819 * This may lead to RNR error. Here, SEND side pauses for a while
820 * during which RECV side commits sufficient recv buffers.
821 */
822 usleep(500000);
823
21b8aee8 824 return 0;
825}
826
827static int fio_rdmaio_accept(struct thread_data *td, struct fio_file *f)
828{
829 struct rdmaio_data *rd = td->io_ops->data;
830 struct rdma_conn_param conn_param;
831 struct ibv_send_wr *bad_wr;
832
833 /* rdma_accept() - then wait for accept success */
834 memset(&conn_param, 0, sizeof conn_param);
835 conn_param.responder_resources = 1;
836 conn_param.initiator_depth = 1;
837
838 if (rdma_accept(rd->child_cm_id, &conn_param) != 0) {
839 log_err("fio: rdma_accept\n");
840 return 1;
841 }
842
843 if (get_next_channel_event
844 (td, rd->cm_channel, RDMA_CM_EVENT_ESTABLISHED) != 0) {
845 log_err("fio: wait for RDMA_CM_EVENT_ESTABLISHED\n");
846 return 1;
847 }
848
849 /* wait for request */
850 rdma_poll_wait(td, IBV_WC_RECV);
851
852 if (ibv_post_send(rd->qp, &rd->sq_wr, &bad_wr) != 0) {
853 log_err("fio: ibv_post_send fail");
854 return 1;
855 }
856
857 rdma_poll_wait(td, IBV_WC_SEND);
858
859 return 0;
860}
861
862static int fio_rdmaio_open_file(struct thread_data *td, struct fio_file *f)
863{
864 if (td_read(td))
865 return fio_rdmaio_accept(td, f);
866 else
867 return fio_rdmaio_connect(td, f);
868}
869
870static int fio_rdmaio_close_file(struct thread_data *td, struct fio_file *f)
871{
872 struct rdmaio_data *rd = td->io_ops->data;
873 struct ibv_send_wr *bad_wr;
874
875 /* unregister rdma buffer */
876
877 /*
878 * Client sends notification to the server side
879 */
880 /* refer to: http://linux.die.net/man/7/rdma_cm */
881 if ((rd->is_client == 1) && ((rd->rdma_protocol == FIO_RDMA_MEM_WRITE)
882 || (rd->rdma_protocol ==
883 FIO_RDMA_MEM_READ))) {
884 if (ibv_post_send(rd->qp, &rd->sq_wr, &bad_wr) != 0) {
885 log_err("fio: ibv_post_send fail");
886 return 1;
887 }
888
889 dprint(FD_IO, "fio: close infomation sent success\n");
890 rdma_poll_wait(td, IBV_WC_SEND);
891 }
892
893 if (rd->is_client == 1)
894 rdma_disconnect(rd->cm_id);
895 else {
896 rdma_disconnect(rd->child_cm_id);
897/* rdma_disconnect(rd->cm_id); */
898 }
899
900/* if (get_next_channel_event(td, rd->cm_channel, RDMA_CM_EVENT_DISCONNECTED) != 0)
901 {
902 log_err("fio: wait for RDMA_CM_EVENT_DISCONNECTED\n");
903 return 1;
904 }*/
905
21b8aee8 906 ibv_destroy_cq(rd->cq);
7d7803fa 907 ibv_destroy_qp(rd->qp);
21b8aee8 908
909 if (rd->is_client == 1)
910 rdma_destroy_id(rd->cm_id);
911 else {
912 rdma_destroy_id(rd->child_cm_id);
913 rdma_destroy_id(rd->cm_id);
914 }
915
916 ibv_destroy_comp_channel(rd->channel);
917 ibv_dealloc_pd(rd->pd);
918
919 return 0;
920}
921
922static int fio_rdmaio_setup_connect(struct thread_data *td, const char *host,
923 unsigned short port)
924{
925 struct rdmaio_data *rd = td->io_ops->data;
926 struct ibv_recv_wr *bad_wr;
927
928 rd->addr.sin_family = AF_INET;
929 rd->addr.sin_port = htons(port);
930
931 if (inet_aton(host, &rd->addr.sin_addr) != 1) {
932 struct hostent *hent;
933
934 hent = gethostbyname(host);
935 if (!hent) {
936 td_verror(td, errno, "gethostbyname");
937 return 1;
938 }
939
940 memcpy(&rd->addr.sin_addr, hent->h_addr, 4);
941 }
942
943 /* resolve route */
944 if (rdma_resolve_addr(rd->cm_id, NULL,
945 (struct sockaddr *)&rd->addr, 2000) != 0) {
946 log_err("fio: rdma_resolve_addr");
947 return 1;
948 }
949
950 if (get_next_channel_event
951 (td, rd->cm_channel, RDMA_CM_EVENT_ADDR_RESOLVED)
952 != 0) {
953 log_err("fio: get_next_channel_event");
954 return 1;
955 }
956
957 /* resolve route */
958 if (rdma_resolve_route(rd->cm_id, 2000) != 0) {
959 log_err("fio: rdma_resolve_route");
960 return 1;
961 }
962
963 if (get_next_channel_event
964 (td, rd->cm_channel, RDMA_CM_EVENT_ROUTE_RESOLVED) != 0) {
965 log_err("fio: get_next_channel_event");
966 return 1;
967 }
968
969 /* create qp and buffer */
970 if (fio_rdmaio_setup_qp(td) != 0)
971 return 1;
972
973 if (fio_rdmaio_setup_control_msg_buffers(td) != 0)
974 return 1;
975
976 /* post recv buf */
977 if (ibv_post_recv(rd->qp, &rd->rq_wr, &bad_wr) != 0) {
978 log_err("fio: ibv_post_recv fail\n");
979 return 1;
980 }
981
982 return 0;
983}
984
985static int fio_rdmaio_setup_listen(struct thread_data *td, short port)
986{
987 struct rdmaio_data *rd = td->io_ops->data;
988 struct ibv_recv_wr *bad_wr;
989
990 rd->addr.sin_family = AF_INET;
991 rd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
992 rd->addr.sin_port = htons(port);
993
994 /* rdma_listen */
995 if (rdma_bind_addr(rd->cm_id, (struct sockaddr *)&rd->addr) != 0) {
996 log_err("fio: rdma_bind_addr fail\n");
997 return 1;
998 }
999
1000 if (rdma_listen(rd->cm_id, 3) != 0) {
1001 log_err("fio: rdma_listen fail\n");
1002 return 1;
1003 }
1004
1005 /* wait for CONNECT_REQUEST */
1006 if (get_next_channel_event
1007 (td, rd->cm_channel, RDMA_CM_EVENT_CONNECT_REQUEST) != 0) {
1008 log_err("fio: wait for RDMA_CM_EVENT_CONNECT_REQUEST\n");
1009 return 1;
1010 }
1011
1012 if (fio_rdmaio_setup_qp(td) != 0)
1013 return 1;
1014
1015 if (fio_rdmaio_setup_control_msg_buffers(td) != 0)
1016 return 1;
1017
1018 /* post recv buf */
1019 if (ibv_post_recv(rd->qp, &rd->rq_wr, &bad_wr) != 0) {
1020 log_err("fio: ibv_post_recv fail\n");
1021 return 1;
1022 }
1023
1024 return 0;
1025}
1026
1027static int fio_rdmaio_init(struct thread_data *td)
1028{
1029 struct rdmaio_data *rd = td->io_ops->data;
1030 unsigned int port;
1031 char host[64], buf[128];
1032 char *sep, *portp, *modep;
1033 int ret;
1034 struct rlimit rl;
1035
1036 if (td_rw(td)) {
1037 log_err("fio: rdma connections must be read OR write\n");
1038 return 1;
1039 }
1040 if (td_random(td)) {
1041 log_err("fio: RDMA network IO can't be random\n");
1042 return 1;
1043 }
1044
1045 /* check RLIMIT_MEMLOCK */
1046 if (getrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
1047 log_err("fio: getrlimit fail: %d(%s)\n",
1048 errno, strerror(errno));
1049 return 1;
1050 }
1051
1052 /* soft limit */
1053 if ((rl.rlim_cur != RLIM_INFINITY)
1054 && (rl.rlim_cur < td->orig_buffer_size)) {
e07f72d3
BVA
1055 log_err("fio: soft RLIMIT_MEMLOCK is: %" PRId64 "\n",
1056 rl.rlim_cur);
1057 log_err("fio: total block size is: %zd\n",
21b8aee8 1058 td->orig_buffer_size);
1059 /* try to set larger RLIMIT_MEMLOCK */
1060 rl.rlim_cur = rl.rlim_max;
1061 if (setrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
1062 log_err("fio: setrlimit fail: %d(%s)\n",
1063 errno, strerror(errno));
1064 log_err("fio: you may try enlarge MEMLOCK by root\n");
1065 log_err("# ulimit -l unlimited\n");
1066 return 1;
1067 }
1068 }
1069
1070 strcpy(buf, td->o.filename);
1071
1072 sep = strchr(buf, '/');
1073 if (!sep)
1074 goto bad_host;
1075
1076 *sep = '\0';
1077 sep++;
1078 strcpy(host, buf);
1079 if (!strlen(host))
1080 goto bad_host;
1081
1082 modep = NULL;
1083 portp = sep;
1084 sep = strchr(portp, '/');
1085 if (sep) {
1086 *sep = '\0';
1087 modep = sep + 1;
1088 }
1089
1090 port = strtol(portp, NULL, 10);
1091 if (!port || port > 65535)
1092 goto bad_host;
1093
1094 if (modep) {
1095 if (!strncmp("rdma_write", modep, strlen(modep)) ||
1096 !strncmp("RDMA_WRITE", modep, strlen(modep)))
1097 rd->rdma_protocol = FIO_RDMA_MEM_WRITE;
1098 else if (!strncmp("rdma_read", modep, strlen(modep)) ||
1099 !strncmp("RDMA_READ", modep, strlen(modep)))
1100 rd->rdma_protocol = FIO_RDMA_MEM_READ;
1101 else if (!strncmp("send", modep, strlen(modep)) ||
1102 !strncmp("SEND", modep, strlen(modep)))
1103 rd->rdma_protocol = FIO_RDMA_CHA_SEND;
1104 else
1105 goto bad_host;
1106 } else
1107 rd->rdma_protocol = FIO_RDMA_MEM_WRITE;
1108
1109 rd->cq_event_num = 0;
1110
1111 rd->cm_channel = rdma_create_event_channel();
1112 if (!rd->cm_channel) {
1113 log_err("fio: rdma_create_event_channel fail\n");
1114 return 1;
1115 }
1116
1117 ret = rdma_create_id(rd->cm_channel, &rd->cm_id, rd, RDMA_PS_TCP);
1118 if (ret) {
1119 log_err("fio: rdma_create_id fail\n");
1120 return 1;
1121 }
1122
1123 if ((rd->rdma_protocol == FIO_RDMA_MEM_WRITE) ||
1124 (rd->rdma_protocol == FIO_RDMA_MEM_READ)) {
1125 rd->rmt_us =
76cc5224 1126 malloc(FIO_RDMA_MAX_IO_DEPTH * sizeof(struct remote_u));
21b8aee8 1127 memset(rd->rmt_us, 0,
76cc5224 1128 FIO_RDMA_MAX_IO_DEPTH * sizeof(struct remote_u));
21b8aee8 1129 rd->rmt_nr = 0;
1130 }
1131
1132 rd->io_us_queued = malloc(td->o.iodepth * sizeof(struct io_u *));
1133 memset(rd->io_us_queued, 0, td->o.iodepth * sizeof(struct io_u *));
1134 rd->io_u_queued_nr = 0;
1135
1136 rd->io_us_flight = malloc(td->o.iodepth * sizeof(struct io_u *));
1137 memset(rd->io_us_flight, 0, td->o.iodepth * sizeof(struct io_u *));
1138 rd->io_u_flight_nr = 0;
1139
1140 rd->io_us_completed = malloc(td->o.iodepth * sizeof(struct io_u *));
1141 memset(rd->io_us_completed, 0, td->o.iodepth * sizeof(struct io_u *));
1142 rd->io_u_completed_nr = 0;
1143
1144 if (td_read(td)) { /* READ as the server */
1145 rd->is_client = 0;
1146 /* server rd->rdma_buf_len will be setup after got request */
1147 ret = fio_rdmaio_setup_listen(td, port);
1148 } else { /* WRITE as the client */
1149 rd->is_client = 1;
1150 ret = fio_rdmaio_setup_connect(td, host, port);
1151 }
1152
1153 struct flist_head *entry;
1154 unsigned int max_bs;
1155 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
1156 /* register each io_u in the free list */
1157 int i = 0;
1158 flist_for_each(entry, &td->io_u_freelist) {
1159 struct io_u *io_u = flist_entry(entry, struct io_u, list);
1160
1161 io_u->engine_data = malloc(sizeof(struct rdma_io_u_data));
1162 memset(io_u->engine_data, 0, sizeof(struct rdma_io_u_data));
1163 ((struct rdma_io_u_data *)io_u->engine_data)->wr_id = i;
1164
1165 io_u->mr = ibv_reg_mr(rd->pd, io_u->buf, max_bs,
1166 IBV_ACCESS_LOCAL_WRITE |
1167 IBV_ACCESS_REMOTE_READ |
1168 IBV_ACCESS_REMOTE_WRITE);
1169 if (io_u->mr == NULL) {
1170 log_err("fio: ibv_reg_mr io_u failed\n");
1171 return 1;
1172 }
1173
1174 rd->send_buf.rmt_us[i].buf =
1175 htonll((uint64_t) (unsigned long)io_u->buf);
1176 rd->send_buf.rmt_us[i].rkey = htonl(io_u->mr->rkey);
1177 rd->send_buf.rmt_us[i].size = htonl(max_bs);
1178
1179/* log_info("fio: Send rkey %x addr %" PRIx64 " len %d to client\n",
1180 io_u->mr->rkey, io_u->buf, max_bs); */
1181 i++;
1182 }
1183
1184 rd->send_buf.nr = htonl(i);
1185
1186 return ret;
1187bad_host:
1188 log_err("fio: bad rdma host/port/protocol: %s\n", td->o.filename);
1189 return 1;
1190}
1191
1192static void fio_rdmaio_cleanup(struct thread_data *td)
1193{
1194 struct rdmaio_data *rd = td->io_ops->data;
1195
1196 if (rd) {
1197/* if (nd->listenfd != -1)
1198 close(nd->listenfd);
1199 if (nd->pipes[0] != -1)
1200 close(nd->pipes[0]);
1201 if (nd->pipes[1] != -1)
1202 close(nd->pipes[1]);
1203*/
1204 free(rd);
1205 }
1206}
1207
1208static int fio_rdmaio_setup(struct thread_data *td)
1209{
1210 struct rdmaio_data *rd;
1211
1212 if (!td->io_ops->data) {
1213 rd = malloc(sizeof(*rd));;
1214
1215 memset(rd, 0, sizeof(*rd));
ee88d056 1216 init_rand_seed(&rd->rand_state, (unsigned int) GOLDEN_RATIO_PRIME);
21b8aee8 1217 td->io_ops->data = rd;
1218 }
1219
1220 return 0;
1221}
1222
1223static struct ioengine_ops ioengine_rw = {
b6cf38f0
YR
1224 .name = "rdma",
1225 .version = FIO_IOOPS_VERSION,
1226 .setup = fio_rdmaio_setup,
1227 .init = fio_rdmaio_init,
1228 .prep = fio_rdmaio_prep,
1229 .queue = fio_rdmaio_queue,
1230 .commit = fio_rdmaio_commit,
1231 .getevents = fio_rdmaio_getevents,
1232 .event = fio_rdmaio_event,
1233 .cleanup = fio_rdmaio_cleanup,
1234 .open_file = fio_rdmaio_open_file,
1235 .close_file = fio_rdmaio_close_file,
1236 .flags = FIO_DISKLESSIO | FIO_UNIDIR | FIO_PIPEIO,
21b8aee8 1237};
1238
1239#else /* FIO_HAVE_RDMA */
1240
1241static int fio_rdmaio_open_file(struct thread_data *td, struct fio_file *f)
1242{
1243 return 0;
1244}
1245
1246static int fio_rdmaio_close_file(struct thread_data *td, struct fio_file *f)
1247{
1248 return 0;
1249}
1250
1251static int fio_rdmaio_queue(struct thread_data *td, struct io_u *io_u)
1252{
1253 return FIO_Q_COMPLETED;
1254}
1255
1256static int fio_rdmaio_init(struct thread_data fio_unused * td)
1257{
1258 log_err("fio: rdma(librdmacm libibverbs) not available\n");
1259 log_err(" You haven't compiled rdma ioengine into fio.\n");
1260 log_err(" If you want to try rdma ioengine,\n");
1261 log_err(" make sure OFED is installed,\n");
1262 log_err(" $ ofed_info\n");
1263 log_err(" then try to make fio as follows:\n");
7d7803fa
YR
1264 log_err(" $ export EXTFLAGS+=\" -DFIO_HAVE_RDMA \"\n");
1265 log_err(" $ export EXTLIBS+=\" -libverbs -lrdmacm \"\n");
21b8aee8 1266 log_err(" $ make clean && make\n");
1267 return 1;
1268}
1269
1270static struct ioengine_ops ioengine_rw = {
b6cf38f0
YR
1271 .name = "rdma",
1272 .version = FIO_IOOPS_VERSION,
1273 .init = fio_rdmaio_init,
1274 .queue = fio_rdmaio_queue,
1275 .open_file = fio_rdmaio_open_file,
1276 .close_file = fio_rdmaio_close_file,
1277 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR | FIO_PIPEIO,
21b8aee8 1278};
1279
1280#endif
1281
1282static void fio_init fio_rdmaio_register(void)
1283{
1284 register_ioengine(&ioengine_rw);
1285}
1286
1287static void fio_exit fio_rdmaio_unregister(void)
1288{
1289 unregister_ioengine(&ioengine_rw);
1290}