-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrdmaio.c
1774 lines (1514 loc) · 39 KB
/
rdmaio.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* rdmaio -- rdma io generation tool
*
* Copyright (c) 2017, Mellanox Technologies. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef VERSION
#define VERSION "0.1 alpha"
#endif
#define _GNU_SOURCE
#include <endian.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <getopt.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <time.h>
#include <limits.h>
#include <unistd.h>
#include <semaphore.h>
#include <hugetlbfs.h>
#include <sys/time.h>
#include <malloc.h>
#include <inttypes.h>
#include <infiniband/verbs.h>
#include <infiniband/sa.h>
#include <rdma/rdma_cma.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <sys/resource.h>
#include <sys/capability.h>
#include<signal.h>
#include<unistd.h>
#include "options.h"
#include "ts.h"
#define RDMAIO_Q_DEPTH 1024
enum rdmaio_msg_opc {
RDMA_IO_OPC_REG_MR_REQ = 0x1,
RDMA_IO_OPC_REG_MR_RSP = 0x81
};
enum rdmaio_cmd_rsp_type {
RDMAIO_CMD = 1,
RDMAIO_RSP = 2,
};
struct rdmaio_alloc_mr_cmd {
uint32_t mr_len;
uint32_t rsvd;
};
struct rdmaio_alloc_mr_rsp {
uint32_t mr_len;
uint32_t mr_key;
uint64_t addr;
};
struct rdmaio_msg {
uint8_t cmd_or_rsp; /* enum rdmaio_cmd_rsp_type */
uint8_t opc; /* enum rdmaio_msg_opc */
uint16_t rsvd2;
uint32_t rsvd3;
uint32_t io_handle;
union {
struct rdmaio_alloc_mr_cmd alloc_mr_cmd;
struct rdmaio_alloc_mr_rsp alloc_mr_rsp;
uint8_t cmd[56];
} u;
};
struct rdmaio_rx_wr {
struct rdmaio_msg *cmd;
struct ibv_recv_wr wr;
struct ibv_sge sge;
};
struct rdmaio_tx_wr {
struct rdmaio_msg *cmd;
struct ibv_send_wr rdma_wr;
struct ibv_send_wr send_wr;
struct ibv_sge sge;
};
enum rdmaio_cm_state {
RDMAIO_CM_STATE_ADDR_RESOLVED,
RDMAIO_CM_STATE_ROUTE_RESOLVED,
RDMAIO_CM_CONNECTED,
RDMAIO_CM_DISCONNECTED,
RDMAIO_CM_FATAL_ERROR,
};
struct run_ctx;
struct rdma_connection;
enum rdmaio_si_state {
RDMAIO_CLIENT_STATE_START_IO,
RDMAIO_CLIENT_STATE_MR_REQ_SENT,
RDMAIO_CLIENT_STATE_MR_RSP_RECV,
RDMAIO_CLIENT_STATE_SI_SENT,
RDMAIO_CLIENT_STATE_RW_SENT = RDMAIO_CLIENT_STATE_SI_SENT
};
struct rdma_rw_ctx {
struct ibv_mr *local_mr;
uint64_t length; /* length of the IO can be lot smaller than
* the MR length.
*/
/* address and rkey advertized by server */
uint64_t raddr;
uint32_t rkey;
};
struct rdma_connection {
struct ibv_comp_channel *cq_channel;
struct ibv_cq *cq;
struct ibv_pd *pd;
long msg_count;
struct {
struct rdmaio_msg *cmds;
struct rdmaio_rx_wr *wrs;
struct ibv_mr *cmds_mr;
uint64_t rqe_post_cnt;
struct ibv_mr *io_mr;
} rx;
struct {
struct rdmaio_msg *cmds;
struct rdmaio_tx_wr *wrs;
struct ibv_mr *cmds_mr;
uint64_t send_cnt;
struct time_stats slat;
struct time_stats clat;
struct ts_time clat_ts;
enum rdmaio_si_state si_state;
} tx;
pthread_t io_thread;
int (*io_handler)(struct rdma_connection *q);
int id;
enum rdmaio_cm_state state;
struct rdma_cm_id *cm_id; /* client id or server child id */
struct rdmacm_client_ctx *c_ctx;
struct run_ctx *run_ctx;
};
struct rdmacm_client_ctx {
pthread_t cm_thread;
sem_t sem;
struct rdma_connection *q;
struct rdma_rw_ctx rw_ctx;
};
struct rdmacm_run_ctx {
struct rdma_event_channel *channel;
struct rdma_cm_event *event;
struct sockaddr_storage local_addr;
struct {
struct rdma_cm_id *listen_cm_id;
int next_free_client_index;
struct rdma_connection clients[128];
} s_ctx;
struct rdmacm_client_ctx c_ctx;
};
struct run_ctx {
struct ibv_context *context;
char *ibdev_name;
uint64_t size;
uint64_t page_size;
uint64_t align;
uint64_t count;
uint64_t offset;
void *buf;
int access_flags;
int huge;
int odp;
int si; /* Send with invalidate test */
int read; /* Read test, mutually exclusive with SI test */
int write; /* Write test, mutually exclusive with SI test */
int write_pattern;
char pattern;
int server;
char *host;
uint16_t port; /* network byte order */
int connections;
int skip_route_resolve;
long long wait_time;
int drop_ipc_lock_cap;
uint64_t rlimit;
uint64_t rlimit_set;
struct rdmacm_run_ctx r_ctx;
struct sockaddr_storage sockaddr;
struct sockaddr_storage src_sockaddr;
};
#define HUGE_PAGE_KPATH "/proc/sys/vm/nr_hugepages"
static int parse_address(struct sockaddr_storage *sock_addr,
const char *input_addr)
{
struct addrinfo *info;
int ret;
ret = getaddrinfo(input_addr, NULL, NULL, &info);
if (ret) {
printf("err (%s) - invalid hostname or IP address\n",
gai_strerror(ret));
return ret;
}
if (info->ai_family == PF_INET)
memcpy(sock_addr, info->ai_addr, sizeof(struct sockaddr_in));
else if (info->ai_family == PF_INET6)
memcpy(sock_addr, info->ai_addr, sizeof(struct sockaddr_in6));
else
ret = -1;
freeaddrinfo(info);
return ret;
}
static void usage(const char *argv0)
{
printf("Usage:\n");
printf("%s\n", argv0);
printf("Options:\n");
printf(" -a --address=<ip_address> ip address to connect to\n");
printf(" -S --size=<size> size of mr in bytes (default 4096)\n");
printf(" -l --align=<align_size> align memory allocation to this size\n");
printf(" -n --count=<count> number of wr operations\n");
printf(" -u --huge use huge pages\n");
printf(" -j --skip Skip route lookup for subsequent connections\n");
printf(" -s --server run in server mode\n");
printf(" -c --client run in client mode, connecting to <host>\n");
printf(" -C --connections number of connections\n");
printf(" -B --bind bind src addr from which to originate traffic\n");
printf(" -o --odp use ODP registration\n");
printf(" -I --sendinvalidate send invalidate operation test\n");
printf(" -p --port server port to listen on/connect to\n");
printf(" -f --offset use offset in registered MR for data transfer\n");
printf(" -w --wait wait time before starting the IOs such as 1hour or 2min\n");
printf(" -h display this help message\n");
printf(" -r --rlimit=<bytes> memory resource hard limit in bytes\n");
printf(" -v display program version\n");
}
void version(const char *argv0)
{
printf("%s %s\n", argv0, VERSION);
}
void parse_options(struct run_ctx *ctx, int argc, char **argv)
{
int opt;
int ret = 0;
static struct option long_options[] = {
{ .name = "address", .has_arg = 1, .val = 'a' },
{ .name = "bind", .has_arg = 1, .val = 'B' },
{ .name = "size", .has_arg = 1, .val = 'S' },
{ .name = "align", .has_arg = 1, .val = 'l' },
{ .name = "pattern", .has_arg = 1, .val = 'P' },
{ .name = "count", .has_arg = 1, .val = 'n' },
{ .name = "server", .has_arg = 0, .val = 's' },
{ .name = "sendinvalidate", .has_arg = 0, .val = 'I' },
{ .name = "rdmaread", .has_arg = 0, .val = 'R' },
{ .name = "rdmawrite", .has_arg = 0, .val = 'W' },
{ .name = "client", .has_arg = 1, .val = '0' },
{ .name = "connections", .has_arg = 1, .val = 'C' },
{ .name = "huge", .has_arg = 0, .val = 'u' },
{ .name = "port", .has_arg = 1, .val = 'p' },
{ .name = "skip", .has_arg = 0, .val = 'j' },
{ .name = "odp", .has_arg = 0, .val = 'o' },
{ .name = "wait", .has_arg = 1, .val = 'w' },
{ .name = "offset", .has_arg = 1, .val = 'f' },
{ .name = "rlimit", .has_arg = 1, .val = 'r' },
{ .name = NULL }
};
if (argc < 2) {
usage(argv[0]);
exit(1);
}
while ((opt = getopt_long(argc, argv, "hv:d:P:S:a:B:C:p:r:w:n:f:l:juoscIRWD", long_options, NULL)) != -1) {
switch (opt) {
case 'v':
version(argv[0]);
exit(0);
case 'h':
usage(argv[0]);
exit(0);
case 'a':
ret = parse_address(&ctx->sockaddr, optarg);
if (ret)
goto err;
break;
case 'S':
ctx->size = parse_size(optarg);
break;
case 'l':
ctx->align = parse_size(optarg);
break;
case 'n':
ctx->count = parse_size(optarg);
break;
case 'f':
ctx->offset = parse_size(optarg);
break;
case 'C':
ctx->connections = parse_size(optarg);
break;
case 'B':
ret = parse_address(&ctx->src_sockaddr, optarg);
if (ret)
goto err;
break;
case 'P':
ctx->write_pattern = 1;
ctx->pattern = *((char*)optarg);
break;
case 'p':
ctx->port = atoi(optarg);
break;
case 'u':
ctx->huge = 1;
break;
case 'o':
ctx->odp = 1;
break;
case 'I':
ctx->si = 1;
break;
case 'R':
ctx->read = 1;
break;
case 'W':
ctx->write = 1;
break;
case 'j':
ctx->skip_route_resolve = 1;
break;
case 'r':
ctx->rlimit = parse_size(optarg);
ctx->rlimit_set = 1;
break;
case 's':
ctx->server = 1;
break;
case 'c':
ctx->host = (char*)optarg;
break;
case 'w':
ctx->wait_time = parse_time_seconds(optarg);
printf("Wait time = %lld\n", ctx->wait_time);
break;
case 'D':
ctx->drop_ipc_lock_cap = 1;
break;
}
}
return;
err:
exit(1);
}
struct statistics {
long long start, finish, load_time;
long long min, max;
};
static int config_hugetlb_pages(uint64_t num_hpages)
{
char hpages_str[128] = {0};
size_t s;
int err = 0;
int fd;
fd = open(HUGE_PAGE_KPATH, O_RDWR, 0);
if (fd < 0)
return fd;
sprintf(hpages_str, "%ld", num_hpages);
s = write(fd, hpages_str, strlen(hpages_str));
if (s != strlen(hpages_str))
err = -EINVAL;
close(fd);
return err;
}
static void reset_huge_tlb_pages(uint64_t num_pages)
{
config_hugetlb_pages(num_pages);
}
static int config_hugetlb_kernel(struct run_ctx *ctx)
{
long hpage_size = gethugepagesize();
uint64_t num_hpages;
if (hpage_size == 0)
return -EINVAL;
num_hpages = ctx->size / hpage_size;
if (num_hpages == 0)
num_hpages = 1;
return config_hugetlb_pages(num_hpages);
}
static int alloc_mem(struct run_ctx *ctx)
{
int err = 0;
if (ctx->huge) {
err = config_hugetlb_kernel(ctx);
if (err) {
fprintf(stderr,"fail to configure hugetlb\n");
err = -EINVAL;
return err;
}
ctx->buf = get_hugepage_region(ctx->size, GHR_STRICT | GHR_COLOR);
if (!ctx->buf) {
perror("mmap");
err = -ENOMEM;
return err;
}
} else {
ctx->buf = memalign(ctx->align, ctx->size);
if (!ctx->buf) {
fprintf(stderr, "Couldn't allocate work buf.\n");
err = -ENOMEM;
return err;
}
}
return err;
}
static void free_mem(struct run_ctx *ctx)
{
if (ctx->huge) {
free_hugepage_region(ctx->buf);
reset_huge_tlb_pages(0);
} else {
free(ctx->buf);
}
}
static struct rdma_cm_event *wait_for_event(struct run_ctx *ctx)
{
int ret;
ret = rdma_get_cm_event(ctx->r_ctx.channel, &ctx->r_ctx.event);
if (ret) {
fprintf(stderr,"%s null event.\n", __func__);
return NULL;
}
fprintf(stderr,"rdmacm event: %s status = %d id = %p\n",
rdma_event_str(ctx->r_ctx.event->event),
ctx->r_ctx.event->status, ctx->r_ctx.event->id);
if (ctx->r_ctx.event->event == RDMA_CM_EVENT_CONNECT_REQUEST ||
ctx->r_ctx.event->event == RDMA_CM_EVENT_ESTABLISHED) {
fprintf(stderr,"listen_id = %p, id = %p\n",
ctx->r_ctx.event->listen_id, ctx->r_ctx.event->id);
}
return ctx->r_ctx.event;
}
static int
rdma_ack_event(struct run_ctx *ctx)
{
int ret;
ret = rdma_ack_cm_event(ctx->r_ctx.event);
return ret;
}
static int set_rlimit(struct run_ctx *ctx)
{
struct rlimit rlim, after_rlim;
int ret;
ret = getrlimit(RLIMIT_MEMLOCK, &rlim);
if (ret)
return ret;
if (ctx->rlimit_set) {
rlim.rlim_cur = ctx->rlimit;
rlim.rlim_max = ctx->rlimit;
ret = setrlimit(RLIMIT_MEMLOCK, &rlim);
if (ret)
return ret;
ret = getrlimit(RLIMIT_MEMLOCK, &after_rlim);
if (after_rlim.rlim_max != ctx->rlimit) {
fprintf(stderr, "Set rlimit %ld, Got %ld\n",
ctx->rlimit, after_rlim.rlim_max);
return -EINVAL;
}
}
return ret;
}
static int drop_ipc_lock_cap(void)
{
cap_value_t capList[1];
cap_t caps;
int ret;
/* Retrieve caller's current capabilities */
caps = cap_get_proc();
if (caps == NULL)
return -EINVAL;
/* Change setting of 'capability' in the effective set of 'caps'. The
* third argument, 1, is the number of items in the array 'capList'.
*/
capList[0] = CAP_IPC_LOCK;
ret = cap_set_flag(caps, CAP_EFFECTIVE, 1, capList, CAP_CLEAR);
if (ret)
goto err;
ret = cap_set_proc(caps);
if (ret)
goto err;
ret = cap_set_flag(caps, CAP_PERMITTED, 1, capList, CAP_CLEAR);
if (ret)
goto err;
ret = cap_set_proc(caps);
err:
cap_free(caps);
return ret;
}
static int setup_ipc_lock_cap(struct run_ctx *ctx)
{
int ret = 0;
if (ctx->drop_ipc_lock_cap)
ret = drop_ipc_lock_cap();
return ret;
}
static int create_q(struct rdma_connection *q)
{
struct ibv_qp_init_attr qp_attr;
int ret;
memset(&qp_attr, 0, sizeof(qp_attr));
q->pd = ibv_alloc_pd(q->cm_id->verbs);
if (!q->pd) {
fprintf(stderr,"%s fail to create pd\n", __func__);
return -ENOMEM;
}
q->cq_channel = ibv_create_comp_channel(q->cm_id->verbs);
if (!q->cq_channel) {
fprintf(stderr,"%s fail to create cq channel\n", __func__);
return -ENOMEM;
}
q->cq = ibv_create_cq(q->cm_id->verbs,
RDMAIO_Q_DEPTH * 2, q,
q->cq_channel,
0);
if (!q->cq) {
fprintf(stderr,"%s fail to create cq\n", __func__);
return -ENOMEM;
}
ret = ibv_req_notify_cq(q->cq, 0);
if (ret)
return ret;
qp_attr.qp_context = q;
qp_attr.send_cq = q->cq;
qp_attr.recv_cq = q->cq;
qp_attr.qp_type = IBV_QPT_RC;
qp_attr.sq_sig_all = 1;
qp_attr.cap.max_send_wr = RDMAIO_Q_DEPTH;
qp_attr.cap.max_send_sge = 1;
qp_attr.cap.max_recv_wr = RDMAIO_Q_DEPTH;
qp_attr.cap.max_recv_sge = 1;
ret = rdma_create_qp(q->cm_id, q->pd, &qp_attr);
if (ret) {
fprintf(stderr,"%s fail to create qp ret = %d\n", __func__, ret);
goto err;
}
return ret;
err:
ibv_destroy_cq(q->cq);
ibv_destroy_comp_channel(q->cq_channel);
ibv_dealloc_pd(q->pd);
return ret;
}
static void init_rx_cmd_wrs(struct rdma_connection *q,
int rx_msg_size, int rx_msg_count)
{
int i;
fprintf(stderr,"%s rx_msg size = %d, count = %d\n", __func__,
rx_msg_size, rx_msg_count);
for (i = 0; i < rx_msg_count; i++) {
q->rx.wrs[i].cmd = &q->rx.cmds[i];
q->rx.wrs[i].sge.addr = (uintptr_t)&q->rx.cmds[i];
q->rx.wrs[i].sge.length = rx_msg_size;
q->rx.wrs[i].sge.lkey = q->rx.cmds_mr->lkey;
q->rx.wrs[i].wr.wr_id = (uintptr_t)q->rx.wrs[i].cmd;
q->rx.wrs[i].wr.sg_list = &q->rx.wrs[i].sge;
q->rx.wrs[i].wr.num_sge = 1;
}
}
static int post_recv_wr(struct rdma_connection *q, int index)
{
struct ibv_recv_wr *bad_wr;
int ret;
ret = ibv_post_recv(q->cm_id->qp, &q->rx.wrs[index].wr, &bad_wr);
if (!ret)
q->rx.rqe_post_cnt++;
return ret;
}
static int
setup_rx_cmds_buffers(struct run_ctx *ctx, struct rdma_connection *q,
int msg_size, int msg_count)
{
int ret;
int i;
q->rx.cmds = calloc(msg_count, msg_size);
if (!q->rx.cmds)
return -ENOMEM;
memset(q->rx.cmds, 0, msg_size * msg_count);
q->rx.cmds_mr = ibv_reg_mr(q->pd, q->rx.cmds,
msg_size * msg_count,
ctx->access_flags);
if (!q->rx.cmds_mr)
return -ENOMEM;
fprintf(stdout,"%s rx cmds mr lkey = 0x%x\n", __func__, q->rx.cmds_mr->lkey);
q->rx.wrs = calloc(msg_count, sizeof(*q->rx.wrs));
if (!q->rx.wrs)
return -ENOMEM;
init_rx_cmd_wrs(q, msg_size, msg_count);
for (i = 0; i < msg_count; i++) {
ret = post_recv_wr(q, i);
if (ret)
break;
}
fprintf(stdout,"%s i= %d ret = %d\n", __func__, i, ret);
return ret;
}
static int
setup_tx_cmds_buffers(struct run_ctx *ctx, struct rdma_connection *q,
int msg_size, int msg_count)
{
q->tx.cmds = calloc(msg_count, msg_size);
if (!q->tx.cmds)
return -ENOMEM;
memset(q->tx.cmds, 0, msg_size * msg_count);
q->tx.cmds_mr = ibv_reg_mr(q->pd, q->tx.cmds,
msg_size * msg_count,
ctx->access_flags);
if (!q->tx.cmds_mr)
return -ENOMEM;
fprintf(stdout,"%s tx cmds mr key = 0x%x\n", __func__, q->tx.cmds_mr->lkey);
q->tx.wrs = calloc(msg_count, sizeof(*q->tx.wrs));
if (!q->tx.wrs)
return -ENOMEM;
return 0;
}
static void init_tx_cmd_wrs(struct rdma_connection *q,
int msg_size, int msg_count)
{
int i;
fprintf(stdout,"%s tx msg_size = %d, count = %d\n", __func__,
msg_size, msg_count);
for (i = 0; i < msg_count; i++) {
q->tx.wrs[i].cmd = &q->tx.cmds[i];
q->tx.wrs[i].sge.addr = (uintptr_t)&q->tx.cmds[i];
q->tx.wrs[i].sge.length = msg_size;
q->tx.wrs[i].sge.lkey = q->tx.cmds_mr->lkey;
q->tx.wrs[i].send_wr.wr_id = (uintptr_t)q->tx.wrs[i].cmd;
q->tx.wrs[i].send_wr.sg_list = &q->tx.wrs[i].sge;
q->tx.wrs[i].send_wr.num_sge = 1;
q->tx.wrs[i].send_wr.send_flags = IBV_SEND_SIGNALED;
}
}
static int setup_msg_buffers(struct run_ctx *ctx, struct rdma_connection *q)
{
int ret;
ret = setup_rx_cmds_buffers(ctx, q, sizeof(struct rdmaio_msg),
RDMAIO_Q_DEPTH);
if (ret)
return ret;
ret = setup_tx_cmds_buffers(ctx, q, sizeof(struct rdmaio_msg),
RDMAIO_Q_DEPTH);
if (ret)
return ret;
init_tx_cmd_wrs(q, sizeof(struct rdmaio_msg), RDMAIO_Q_DEPTH);
return 0;
}
static int get_tx_index(const struct rdma_connection *q)
{
return q->tx.send_cnt % RDMAIO_Q_DEPTH;
}
static struct rdmaio_msg *get_tx_msg(const struct rdma_connection *q)
{
int index = get_tx_index(q);
return q->tx.wrs[index].cmd;
}
static struct ibv_send_wr *get_tx_wr(const struct rdma_connection *q)
{
int index = get_tx_index(q);
return &q->tx.wrs[index].send_wr;
}
static int get_rx_index(struct rdma_connection *q)
{
return q->rx.rqe_post_cnt % RDMAIO_Q_DEPTH;
}
static int post_sq_wr(struct rdma_connection *q, struct ibv_send_wr *sq_wr)
{
struct ts_time sstat = { 0 };
struct ibv_send_wr *bad_wr;
int ret;
ts_log_start_time(&sstat);
ret = ibv_post_send(q->cm_id->qp, sq_wr, &bad_wr);
if (!ret)
q->tx.send_cnt++;
else
fprintf(stderr,"%s sq opcode = %d, ret = %d\n", __func__,
sq_wr->opcode, ret);
ts_log_end_time(&sstat);
ts_update_time_stats(&sstat, &q->tx.slat);
ts_log_start_time(&q->tx.clat_ts);
return ret;
}
static int post_rdma_wr(struct rdma_connection *q)
{
struct ibv_send_wr *rdma_wr = get_tx_wr(q);
struct ibv_sge sge;
memset(rdma_wr, 0, sizeof(*rdma_wr));
memset(&sge, 0, sizeof(sge));
if (q->run_ctx->read)
rdma_wr->opcode = IBV_WR_RDMA_READ;
else if (q->run_ctx->write)
rdma_wr->opcode = IBV_WR_RDMA_WRITE;
if (q->run_ctx->r_ctx.c_ctx.rw_ctx.length) {
rdma_wr->wr.rdma.rkey = q->run_ctx->r_ctx.c_ctx.rw_ctx.rkey;
rdma_wr->wr.rdma.remote_addr = q->run_ctx->r_ctx.c_ctx.rw_ctx.raddr;
rdma_wr->num_sge = 1;
rdma_wr->sg_list = &sge;
}
rdma_wr->send_flags = IBV_SEND_SIGNALED;
sge.addr = (uintptr_t)q->run_ctx->r_ctx.c_ctx.rw_ctx.local_mr->addr;
sge.length = q->run_ctx->r_ctx.c_ctx.rw_ctx.length;
sge.lkey = q->run_ctx->r_ctx.c_ctx.rw_ctx.local_mr->lkey;
return post_sq_wr(q, rdma_wr);
}
/*
* client-server ladder diagram:
* client: server:
* Send MR alloc req:
* Recv MR alloc req:
* allocate MR
* Send MR allocated resp.
* Recv MR alloc resp:
* Do send_with_invalidate:
* dealloc_MR:
* Reset state to for new IO.
*/
static int prepare_send_alloc_mr_req(struct rdma_connection *q)
{
struct ibv_send_wr *tx_wr = get_tx_wr(q);
struct rdmaio_msg *msg = get_tx_msg(q);
msg->cmd_or_rsp = RDMAIO_CMD;
msg->opc = RDMA_IO_OPC_REG_MR_REQ;
msg->io_handle = (uintptr_t)msg;
msg->u.alloc_mr_cmd.mr_len = q->run_ctx->size;
tx_wr->opcode = IBV_WR_SEND;
return post_sq_wr(q, tx_wr);
}
static int client_si_ops_handler(struct rdma_connection *q)
{
int ret = 0;
restart_state:
switch (q->tx.si_state) {
case RDMAIO_CLIENT_STATE_START_IO:
ret = prepare_send_alloc_mr_req(q);
q->tx.si_state = RDMAIO_CLIENT_STATE_MR_REQ_SENT;
break;
case RDMAIO_CLIENT_STATE_MR_REQ_SENT:
/* np for mr req send completion */
break;
case RDMAIO_CLIENT_STATE_SI_SENT:
/* Send with invalidate completion arrived,
* Restart the sequence.
*/
q->tx.si_state = RDMAIO_CLIENT_STATE_START_IO;
goto restart_state;
break;
default:
break;
};
return ret;
}
static void client_send_handler(struct rdma_connection *q)
{
struct run_ctx *ctx = q->run_ctx;
struct ibv_send_wr *tx_wr = get_tx_wr(q);
int ret = 0;
ts_log_end_time(&q->tx.clat_ts);
ts_update_time_stats(&q->tx.clat_ts, &q->tx.clat);
if (ctx->si) {
ret = client_si_ops_handler(q);
} else if (ctx->read || ctx->write) {
/* nop */
} else {
tx_wr->opcode = IBV_WR_SEND;
ret = post_sq_wr(q, tx_wr);
}
if (ret)
fprintf(stderr,"%s ret = %d\n", __func__, ret);
}
static void client_rw_handler(struct rdma_connection *q)
{
ts_log_end_time(&q->tx.clat_ts);
ts_update_time_stats(&q->tx.clat_ts, &q->tx.clat);
post_rdma_wr(q);
}
static int client_setup_rw_buffer(struct rdma_connection *q)
{
struct run_ctx *ctx = q->run_ctx;
size_t length = q->run_ctx->size;
struct ibv_mr *mr;
void *buffer;
if (!ctx->read && !ctx->write)
return 0;
buffer = malloc(length);
if (!buffer)
return -ENOMEM;
mr = ibv_reg_mr(q->pd, buffer, length,
IBV_ACCESS_LOCAL_WRITE |
IBV_ACCESS_REMOTE_READ |
IBV_ACCESS_REMOTE_WRITE);
if (!mr)
return -ENOMEM;
ctx->r_ctx.c_ctx.rw_ctx.local_mr = mr;
ctx->r_ctx.c_ctx.rw_ctx.length = 0;
return 0;
}
static int client_io_warmup(struct rdma_connection *q)
{
struct run_ctx *ctx = q->run_ctx;
struct ibv_send_wr *tx_wr = get_tx_wr(q);
int ret;
if (ctx->si)
ret = client_si_ops_handler(q);
else if (ctx->read || ctx->write) {
ret = prepare_send_alloc_mr_req(q);
q->tx.si_state = RDMAIO_CLIENT_STATE_MR_REQ_SENT;
} else {
tx_wr->opcode = IBV_WR_SEND;
ret = post_sq_wr(q, tx_wr);
}
if (ret)
fprintf(stderr,"ret = %d\n", ret);
return ret;
}
static void
client_recv_resp_handler(struct rdma_connection *q,
const struct rdmaio_msg *msg)
{
struct run_ctx *ctx = q->run_ctx;
struct ibv_send_wr *tx_wr = get_tx_wr(q);
int rx_index;
int ret = 0;
rx_index = get_rx_index(q);
if (ctx->si) {
tx_wr->invalidate_rkey = msg->u.alloc_mr_rsp.mr_key;
tx_wr->opcode = IBV_WR_SEND_WITH_INV;
ret = post_sq_wr(q, tx_wr);
q->tx.si_state = RDMAIO_CLIENT_STATE_SI_SENT;
fprintf(stdout,"%s mr len=%d, key=0x%x\n", __func__,
msg->u.alloc_mr_rsp.mr_len, msg->u.alloc_mr_rsp.mr_key);
} else if (ctx->read || ctx->write) {
fprintf(stdout,"%s mr len=%d, key=0x%x\n", __func__,
msg->u.alloc_mr_rsp.mr_len, msg->u.alloc_mr_rsp.mr_key);
q->run_ctx->r_ctx.c_ctx.rw_ctx.raddr = msg->u.alloc_mr_rsp.addr;
q->run_ctx->r_ctx.c_ctx.rw_ctx.rkey = msg->u.alloc_mr_rsp.mr_key;
ret = post_rdma_wr(q);
q->tx.si_state = RDMAIO_CLIENT_STATE_RW_SENT;
} else {
tx_wr->opcode = IBV_WR_SEND;
ret = post_sq_wr(q, tx_wr);
}
if (ret)
fprintf(stderr,"%s ret = %d\n", __func__, ret);
post_recv_wr(q, rx_index);
}
static void
client_recv_wc_handler(struct rdma_connection *q, struct ibv_wc *wc)
{