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