Spelling and grammar fixes
[fio.git] / engines / rdma.c
CommitLineData
21b8aee8 1/*
85286c5c 2 * RDMA I/O engine
21b8aee8 3 *
85286c5c
BVA
4 * RDMA I/O engine based on the IB verbs and RDMA/CM user space libraries.
5 * Supports both RDMA memory semantics and channel semantics
6 * for the InfiniBand, RoCE and iWARP protocols.
21b8aee8 7 *
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 }
835}
836
837static int fio_rdmaio_commit(struct thread_data *td)
838{
565e784d 839 struct rdmaio_data *rd = td->io_ops_data;
21b8aee8 840 struct io_u **io_us;
841 int ret;
842
843 if (!rd->io_us_queued)
844 return 0;
845
846 io_us = rd->io_us_queued;
847 do {
848 /* RDMA_WRITE or RDMA_READ */
222757cc 849 if (rd->is_client)
21b8aee8 850 ret = fio_rdmaio_send(td, io_us, rd->io_u_queued_nr);
222757cc 851 else if (!rd->is_client)
21b8aee8 852 ret = fio_rdmaio_recv(td, io_us, rd->io_u_queued_nr);
222757cc 853 else
21b8aee8 854 ret = 0; /* must be a SYNC */
855
856 if (ret > 0) {
857 fio_rdmaio_queued(td, io_us, ret);
858 io_u_mark_submit(td, ret);
859 rd->io_u_queued_nr -= ret;
860 io_us += ret;
861 ret = 0;
862 } else
863 break;
864 } while (rd->io_u_queued_nr);
865
866 return ret;
867}
868
869static int fio_rdmaio_connect(struct thread_data *td, struct fio_file *f)
870{
565e784d 871 struct rdmaio_data *rd = td->io_ops_data;
21b8aee8 872 struct rdma_conn_param conn_param;
873 struct ibv_send_wr *bad_wr;
874
222757cc 875 memset(&conn_param, 0, sizeof(conn_param));
21b8aee8 876 conn_param.responder_resources = 1;
877 conn_param.initiator_depth = 1;
878 conn_param.retry_count = 10;
879
880 if (rdma_connect(rd->cm_id, &conn_param) != 0) {
2a442a30 881 log_err("fio: rdma_connect fail: %m\n");
21b8aee8 882 return 1;
883 }
884
885 if (get_next_channel_event
886 (td, rd->cm_channel, RDMA_CM_EVENT_ESTABLISHED) != 0) {
887 log_err("fio: wait for RDMA_CM_EVENT_ESTABLISHED\n");
888 return 1;
889 }
890
891 /* send task request */
892 rd->send_buf.mode = htonl(rd->rdma_protocol);
893 rd->send_buf.nr = htonl(td->o.iodepth);
894
895 if (ibv_post_send(rd->qp, &rd->sq_wr, &bad_wr) != 0) {
c5839011 896 log_err("fio: ibv_post_send fail: %m\n");
21b8aee8 897 return 1;
898 }
899
c4421263
LG
900 if (rdma_poll_wait(td, IBV_WC_SEND) < 0)
901 return 1;
21b8aee8 902
903 /* wait for remote MR info from server side */
c4421263
LG
904 if (rdma_poll_wait(td, IBV_WC_RECV) < 0)
905 return 1;
21b8aee8 906
7d7803fa
YR
907 /* In SEND/RECV test, it's a good practice to setup the iodepth of
908 * of the RECV side deeper than that of the SEND side to
909 * avoid RNR (receiver not ready) error. The
222757cc 910 * SEND side may send so many unsolicited message before
7d7803fa
YR
911 * RECV side commits sufficient recv buffers into recv queue.
912 * This may lead to RNR error. Here, SEND side pauses for a while
913 * during which RECV side commits sufficient recv buffers.
914 */
915 usleep(500000);
916
21b8aee8 917 return 0;
918}
919
920static int fio_rdmaio_accept(struct thread_data *td, struct fio_file *f)
921{
565e784d 922 struct rdmaio_data *rd = td->io_ops_data;
21b8aee8 923 struct rdma_conn_param conn_param;
924 struct ibv_send_wr *bad_wr;
c4421263 925 int ret = 0;
21b8aee8 926
927 /* rdma_accept() - then wait for accept success */
222757cc 928 memset(&conn_param, 0, sizeof(conn_param));
21b8aee8 929 conn_param.responder_resources = 1;
930 conn_param.initiator_depth = 1;
931
932 if (rdma_accept(rd->child_cm_id, &conn_param) != 0) {
2a442a30 933 log_err("fio: rdma_accept: %m\n");
21b8aee8 934 return 1;
935 }
936
937 if (get_next_channel_event
938 (td, rd->cm_channel, RDMA_CM_EVENT_ESTABLISHED) != 0) {
939 log_err("fio: wait for RDMA_CM_EVENT_ESTABLISHED\n");
940 return 1;
941 }
942
943 /* wait for request */
c4421263 944 ret = rdma_poll_wait(td, IBV_WC_RECV) < 0;
21b8aee8 945
946 if (ibv_post_send(rd->qp, &rd->sq_wr, &bad_wr) != 0) {
c5839011 947 log_err("fio: ibv_post_send fail: %m\n");
21b8aee8 948 return 1;
949 }
950
c4421263
LG
951 if (rdma_poll_wait(td, IBV_WC_SEND) < 0)
952 return 1;
21b8aee8 953
c4421263 954 return ret;
21b8aee8 955}
956
957static int fio_rdmaio_open_file(struct thread_data *td, struct fio_file *f)
958{
959 if (td_read(td))
960 return fio_rdmaio_accept(td, f);
961 else
962 return fio_rdmaio_connect(td, f);
963}
964
965static int fio_rdmaio_close_file(struct thread_data *td, struct fio_file *f)
966{
565e784d 967 struct rdmaio_data *rd = td->io_ops_data;
21b8aee8 968 struct ibv_send_wr *bad_wr;
969
970 /* unregister rdma buffer */
971
972 /*
973 * Client sends notification to the server side
974 */
975 /* refer to: http://linux.die.net/man/7/rdma_cm */
976 if ((rd->is_client == 1) && ((rd->rdma_protocol == FIO_RDMA_MEM_WRITE)
977 || (rd->rdma_protocol ==
978 FIO_RDMA_MEM_READ))) {
979 if (ibv_post_send(rd->qp, &rd->sq_wr, &bad_wr) != 0) {
c5839011 980 log_err("fio: ibv_post_send fail: %m\n");
21b8aee8 981 return 1;
982 }
983
de8f6de9 984 dprint(FD_IO, "fio: close information sent success\n");
21b8aee8 985 rdma_poll_wait(td, IBV_WC_SEND);
986 }
987
988 if (rd->is_client == 1)
989 rdma_disconnect(rd->cm_id);
990 else {
991 rdma_disconnect(rd->child_cm_id);
222757cc
JA
992#if 0
993 rdma_disconnect(rd->cm_id);
994#endif
21b8aee8 995 }
996
222757cc
JA
997#if 0
998 if (get_next_channel_event(td, rd->cm_channel, RDMA_CM_EVENT_DISCONNECTED) != 0) {
999 log_err("fio: wait for RDMA_CM_EVENT_DISCONNECTED\n");
1000 return 1;
1001 }
1002#endif
21b8aee8 1003
21b8aee8 1004 ibv_destroy_cq(rd->cq);
7d7803fa 1005 ibv_destroy_qp(rd->qp);
21b8aee8 1006
1007 if (rd->is_client == 1)
1008 rdma_destroy_id(rd->cm_id);
1009 else {
1010 rdma_destroy_id(rd->child_cm_id);
1011 rdma_destroy_id(rd->cm_id);
1012 }
1013
1014 ibv_destroy_comp_channel(rd->channel);
1015 ibv_dealloc_pd(rd->pd);
1016
1017 return 0;
1018}
1019
2f28bb35
SB
1020static int aton(struct thread_data *td, const char *host,
1021 struct sockaddr_in *addr)
1022{
1023 if (inet_aton(host, &addr->sin_addr) != 1) {
1024 struct hostent *hent;
1025
1026 hent = gethostbyname(host);
1027 if (!hent) {
1028 td_verror(td, errno, "gethostbyname");
1029 return 1;
1030 }
1031
1032 memcpy(&addr->sin_addr, hent->h_addr, 4);
1033 }
1034 return 0;
1035}
1036
21b8aee8 1037static int fio_rdmaio_setup_connect(struct thread_data *td, const char *host,
1038 unsigned short port)
1039{
565e784d 1040 struct rdmaio_data *rd = td->io_ops_data;
2f28bb35
SB
1041 struct rdmaio_options *o = td->eo;
1042 struct sockaddr_storage addrb;
21b8aee8 1043 struct ibv_recv_wr *bad_wr;
222757cc 1044 int err;
21b8aee8 1045
1046 rd->addr.sin_family = AF_INET;
1047 rd->addr.sin_port = htons(port);
1048
2f28bb35
SB
1049 err = aton(td, host, &rd->addr);
1050 if (err)
1051 return err;
21b8aee8 1052
2f28bb35 1053 /* resolve route */
de8f8675 1054 if (o->bindname && strlen(o->bindname)) {
2f28bb35
SB
1055 addrb.ss_family = AF_INET;
1056 err = aton(td, o->bindname, (struct sockaddr_in *)&addrb);
1057 if (err)
1058 return err;
1059 err = rdma_resolve_addr(rd->cm_id, (struct sockaddr *)&addrb,
1060 (struct sockaddr *)&rd->addr, 2000);
21b8aee8 1061
2f28bb35
SB
1062 } else {
1063 err = rdma_resolve_addr(rd->cm_id, NULL,
1064 (struct sockaddr *)&rd->addr, 2000);
21b8aee8 1065 }
1066
222757cc
JA
1067 if (err != 0) {
1068 log_err("fio: rdma_resolve_addr: %d\n", err);
21b8aee8 1069 return 1;
1070 }
1071
222757cc
JA
1072 err = get_next_channel_event(td, rd->cm_channel, RDMA_CM_EVENT_ADDR_RESOLVED);
1073 if (err != 0) {
1074 log_err("fio: get_next_channel_event: %d\n", err);
21b8aee8 1075 return 1;
1076 }
1077
1078 /* resolve route */
222757cc
JA
1079 err = rdma_resolve_route(rd->cm_id, 2000);
1080 if (err != 0) {
1081 log_err("fio: rdma_resolve_route: %d\n", err);
21b8aee8 1082 return 1;
1083 }
1084
222757cc
JA
1085 err = get_next_channel_event(td, rd->cm_channel, RDMA_CM_EVENT_ROUTE_RESOLVED);
1086 if (err != 0) {
1087 log_err("fio: get_next_channel_event: %d\n", err);
21b8aee8 1088 return 1;
1089 }
1090
1091 /* create qp and buffer */
1092 if (fio_rdmaio_setup_qp(td) != 0)
1093 return 1;
1094
1095 if (fio_rdmaio_setup_control_msg_buffers(td) != 0)
1096 return 1;
1097
1098 /* post recv buf */
222757cc
JA
1099 err = ibv_post_recv(rd->qp, &rd->rq_wr, &bad_wr);
1100 if (err != 0) {
1101 log_err("fio: ibv_post_recv fail: %d\n", err);
21b8aee8 1102 return 1;
1103 }
1104
1105 return 0;
1106}
1107
1108static int fio_rdmaio_setup_listen(struct thread_data *td, short port)
1109{
565e784d 1110 struct rdmaio_data *rd = td->io_ops_data;
2f28bb35 1111 struct rdmaio_options *o = td->eo;
21b8aee8 1112 struct ibv_recv_wr *bad_wr;
263775ad
LG
1113 int state = td->runstate;
1114
1115 td_set_runstate(td, TD_SETTING_UP);
21b8aee8 1116
1117 rd->addr.sin_family = AF_INET;
21b8aee8 1118 rd->addr.sin_port = htons(port);
1119
de8f8675 1120 if (!o->bindname || !strlen(o->bindname))
2f28bb35
SB
1121 rd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
1122 else
1123 rd->addr.sin_addr.s_addr = htonl(*o->bindname);
1124
21b8aee8 1125 /* rdma_listen */
1126 if (rdma_bind_addr(rd->cm_id, (struct sockaddr *)&rd->addr) != 0) {
2a442a30 1127 log_err("fio: rdma_bind_addr fail: %m\n");
21b8aee8 1128 return 1;
1129 }
1130
1131 if (rdma_listen(rd->cm_id, 3) != 0) {
2a442a30 1132 log_err("fio: rdma_listen fail: %m\n");
21b8aee8 1133 return 1;
1134 }
1135
263775ad
LG
1136 log_info("fio: waiting for connection\n");
1137
21b8aee8 1138 /* wait for CONNECT_REQUEST */
1139 if (get_next_channel_event
1140 (td, rd->cm_channel, RDMA_CM_EVENT_CONNECT_REQUEST) != 0) {
1141 log_err("fio: wait for RDMA_CM_EVENT_CONNECT_REQUEST\n");
1142 return 1;
1143 }
1144
1145 if (fio_rdmaio_setup_qp(td) != 0)
1146 return 1;
1147
1148 if (fio_rdmaio_setup_control_msg_buffers(td) != 0)
1149 return 1;
1150
1151 /* post recv buf */
1152 if (ibv_post_recv(rd->qp, &rd->rq_wr, &bad_wr) != 0) {
2a442a30 1153 log_err("fio: ibv_post_recv fail: %m\n");
21b8aee8 1154 return 1;
1155 }
1156
263775ad 1157 td_set_runstate(td, state);
21b8aee8 1158 return 0;
1159}
1160
38b9354a 1161static int check_set_rlimits(struct thread_data *td)
21b8aee8 1162{
38b9354a 1163#ifdef CONFIG_RLIMIT_MEMLOCK
21b8aee8 1164 struct rlimit rl;
1165
21b8aee8 1166 /* check RLIMIT_MEMLOCK */
1167 if (getrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
1168 log_err("fio: getrlimit fail: %d(%s)\n",
1169 errno, strerror(errno));
1170 return 1;
1171 }
1172
1173 /* soft limit */
1174 if ((rl.rlim_cur != RLIM_INFINITY)
1175 && (rl.rlim_cur < td->orig_buffer_size)) {
e07f72d3
BVA
1176 log_err("fio: soft RLIMIT_MEMLOCK is: %" PRId64 "\n",
1177 rl.rlim_cur);
1178 log_err("fio: total block size is: %zd\n",
21b8aee8 1179 td->orig_buffer_size);
1180 /* try to set larger RLIMIT_MEMLOCK */
1181 rl.rlim_cur = rl.rlim_max;
1182 if (setrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
1183 log_err("fio: setrlimit fail: %d(%s)\n",
1184 errno, strerror(errno));
1185 log_err("fio: you may try enlarge MEMLOCK by root\n");
1186 log_err("# ulimit -l unlimited\n");
1187 return 1;
1188 }
1189 }
38b9354a
JA
1190#endif
1191
1192 return 0;
1193}
1194
cdf91594 1195static int compat_options(struct thread_data *td)
38b9354a 1196{
fc002f14 1197 // The original RDMA engine had an ugly / separator
cdf91594 1198 // on the filename for it's options. This function
2f28bb35
SB
1199 // retains backwards compatibility with it. Note we do not
1200 // support setting the bindname option is this legacy mode.
38b9354a 1201
cdf91594
LG
1202 struct rdmaio_options *o = td->eo;
1203 char *modep, *portp;
1204 char *filename = td->o.filename;
38b9354a 1205
cdf91594
LG
1206 if (!filename)
1207 return 0;
21b8aee8 1208
cdf91594
LG
1209 portp = strchr(filename, '/');
1210 if (portp == NULL)
1211 return 0;
21b8aee8 1212
cdf91594
LG
1213 *portp = '\0';
1214 portp++;
21b8aee8 1215
cdf91594
LG
1216 o->port = strtol(portp, NULL, 10);
1217 if (!o->port || o->port > 65535)
21b8aee8 1218 goto bad_host;
1219
cdf91594
LG
1220 modep = strchr(portp, '/');
1221 if (modep != NULL) {
1222 *modep = '\0';
1223 modep++;
21b8aee8 1224 }
1225
21b8aee8 1226 if (modep) {
1227 if (!strncmp("rdma_write", modep, strlen(modep)) ||
1228 !strncmp("RDMA_WRITE", modep, strlen(modep)))
cdf91594 1229 o->verb = FIO_RDMA_MEM_WRITE;
21b8aee8 1230 else if (!strncmp("rdma_read", modep, strlen(modep)) ||
1231 !strncmp("RDMA_READ", modep, strlen(modep)))
cdf91594 1232 o->verb = FIO_RDMA_MEM_READ;
21b8aee8 1233 else if (!strncmp("send", modep, strlen(modep)) ||
1234 !strncmp("SEND", modep, strlen(modep)))
cdf91594 1235 o->verb = FIO_RDMA_CHA_SEND;
21b8aee8 1236 else
1237 goto bad_host;
1238 } else
cdf91594
LG
1239 o->verb = FIO_RDMA_MEM_WRITE;
1240
1241
1242 return 0;
21b8aee8 1243
cdf91594
LG
1244bad_host:
1245 log_err("fio: bad rdma host/port/protocol: %s\n", td->o.filename);
1246 return 1;
1247}
1248
1249static int fio_rdmaio_init(struct thread_data *td)
1250{
565e784d 1251 struct rdmaio_data *rd = td->io_ops_data;
cdf91594 1252 struct rdmaio_options *o = td->eo;
1480ce7d 1253 int ret;
cdf91594
LG
1254
1255 if (td_rw(td)) {
1256 log_err("fio: rdma connections must be read OR write\n");
1257 return 1;
1258 }
1259 if (td_random(td)) {
1260 log_err("fio: RDMA network IO can't be random\n");
1261 return 1;
1262 }
1263
1264 if (compat_options(td))
1265 return 1;
1266
1267 if (!o->port) {
1268 log_err("fio: no port has been specified which is required "
1269 "for the rdma engine\n");
1270 return 1;
1271 }
1272
1273 if (check_set_rlimits(td))
1274 return 1;
1275
1276 rd->rdma_protocol = o->verb;
21b8aee8 1277 rd->cq_event_num = 0;
1278
1279 rd->cm_channel = rdma_create_event_channel();
1280 if (!rd->cm_channel) {
2a442a30 1281 log_err("fio: rdma_create_event_channel fail: %m\n");
21b8aee8 1282 return 1;
1283 }
1284
1285 ret = rdma_create_id(rd->cm_channel, &rd->cm_id, rd, RDMA_PS_TCP);
1286 if (ret) {
2a442a30 1287 log_err("fio: rdma_create_id fail: %m\n");
21b8aee8 1288 return 1;
1289 }
1290
1291 if ((rd->rdma_protocol == FIO_RDMA_MEM_WRITE) ||
1292 (rd->rdma_protocol == FIO_RDMA_MEM_READ)) {
1293 rd->rmt_us =
76cc5224 1294 malloc(FIO_RDMA_MAX_IO_DEPTH * sizeof(struct remote_u));
21b8aee8 1295 memset(rd->rmt_us, 0,
76cc5224 1296 FIO_RDMA_MAX_IO_DEPTH * sizeof(struct remote_u));
21b8aee8 1297 rd->rmt_nr = 0;
1298 }
1299
1300 rd->io_us_queued = malloc(td->o.iodepth * sizeof(struct io_u *));
1301 memset(rd->io_us_queued, 0, td->o.iodepth * sizeof(struct io_u *));
1302 rd->io_u_queued_nr = 0;
1303
1304 rd->io_us_flight = malloc(td->o.iodepth * sizeof(struct io_u *));
1305 memset(rd->io_us_flight, 0, td->o.iodepth * sizeof(struct io_u *));
1306 rd->io_u_flight_nr = 0;
1307
1308 rd->io_us_completed = malloc(td->o.iodepth * sizeof(struct io_u *));
1309 memset(rd->io_us_completed, 0, td->o.iodepth * sizeof(struct io_u *));
1310 rd->io_u_completed_nr = 0;
1311
1312 if (td_read(td)) { /* READ as the server */
1313 rd->is_client = 0;
263775ad 1314 td->flags |= TD_F_NO_PROGRESS;
21b8aee8 1315 /* server rd->rdma_buf_len will be setup after got request */
cdf91594 1316 ret = fio_rdmaio_setup_listen(td, o->port);
21b8aee8 1317 } else { /* WRITE as the client */
1318 rd->is_client = 1;
cdf91594 1319 ret = fio_rdmaio_setup_connect(td, td->o.filename, o->port);
21b8aee8 1320 }
1480ce7d
DM
1321 return ret;
1322}
1323static int fio_rdmaio_post_init(struct thread_data *td)
1324{
1325 unsigned int max_bs;
1326 int i;
1327 struct rdmaio_data *rd = td->io_ops_data;
21b8aee8 1328
21b8aee8 1329 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
c4421263
LG
1330 rd->send_buf.max_bs = htonl(max_bs);
1331
21b8aee8 1332 /* register each io_u in the free list */
954cd73a
YR
1333 for (i = 0; i < td->io_u_freelist.nr; i++) {
1334 struct io_u *io_u = td->io_u_freelist.io_us[i];
21b8aee8 1335
1336 io_u->engine_data = malloc(sizeof(struct rdma_io_u_data));
1337 memset(io_u->engine_data, 0, sizeof(struct rdma_io_u_data));
1338 ((struct rdma_io_u_data *)io_u->engine_data)->wr_id = i;
1339
1340 io_u->mr = ibv_reg_mr(rd->pd, io_u->buf, max_bs,
1341 IBV_ACCESS_LOCAL_WRITE |
1342 IBV_ACCESS_REMOTE_READ |
1343 IBV_ACCESS_REMOTE_WRITE);
1344 if (io_u->mr == NULL) {
2a442a30 1345 log_err("fio: ibv_reg_mr io_u failed: %m\n");
21b8aee8 1346 return 1;
1347 }
1348
1349 rd->send_buf.rmt_us[i].buf =
b747af64 1350 cpu_to_be64((uint64_t) (unsigned long)io_u->buf);
21b8aee8 1351 rd->send_buf.rmt_us[i].rkey = htonl(io_u->mr->rkey);
1352 rd->send_buf.rmt_us[i].size = htonl(max_bs);
1353
222757cc
JA
1354#if 0
1355 log_info("fio: Send rkey %x addr %" PRIx64 " len %d to client\n", io_u->mr->rkey, io_u->buf, max_bs); */
1356#endif
21b8aee8 1357 }
1358
1359 rd->send_buf.nr = htonl(i);
1360
1480ce7d 1361 return 0;
21b8aee8 1362}
1363
1364static void fio_rdmaio_cleanup(struct thread_data *td)
1365{
565e784d 1366 struct rdmaio_data *rd = td->io_ops_data;
21b8aee8 1367
222757cc 1368 if (rd)
21b8aee8 1369 free(rd);
21b8aee8 1370}
1371
1372static int fio_rdmaio_setup(struct thread_data *td)
1373{
1374 struct rdmaio_data *rd;
1375
cdf91594
LG
1376 if (!td->files_index) {
1377 add_file(td, td->o.filename ?: "rdma", 0, 0);
1378 td->o.nr_files = td->o.nr_files ?: 1;
1379 td->o.open_files++;
1380 }
1381
565e784d 1382 if (!td->io_ops_data) {
222757cc 1383 rd = malloc(sizeof(*rd));
21b8aee8 1384
1385 memset(rd, 0, sizeof(*rd));
ec4dde59 1386 init_rand_seed(&rd->rand_state, (unsigned int) GOLDEN_RATIO_PRIME, 0);
565e784d 1387 td->io_ops_data = rd;
21b8aee8 1388 }
1389
1390 return 0;
1391}
1392
5a8a6a03 1393FIO_STATIC struct ioengine_ops ioengine = {
cdf91594
LG
1394 .name = "rdma",
1395 .version = FIO_IOOPS_VERSION,
1396 .setup = fio_rdmaio_setup,
1397 .init = fio_rdmaio_init,
1480ce7d 1398 .post_init = fio_rdmaio_post_init,
cdf91594
LG
1399 .prep = fio_rdmaio_prep,
1400 .queue = fio_rdmaio_queue,
1401 .commit = fio_rdmaio_commit,
1402 .getevents = fio_rdmaio_getevents,
1403 .event = fio_rdmaio_event,
1404 .cleanup = fio_rdmaio_cleanup,
1405 .open_file = fio_rdmaio_open_file,
1406 .close_file = fio_rdmaio_close_file,
1407 .flags = FIO_DISKLESSIO | FIO_UNIDIR | FIO_PIPEIO,
1408 .options = options,
1409 .option_struct_size = sizeof(struct rdmaio_options),
21b8aee8 1410};
1411
21b8aee8 1412static void fio_init fio_rdmaio_register(void)
1413{
5a8a6a03 1414 register_ioengine(&ioengine);
21b8aee8 1415}
1416
1417static void fio_exit fio_rdmaio_unregister(void)
1418{
5a8a6a03 1419 unregister_ioengine(&ioengine);
21b8aee8 1420}