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