-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwrite_bw.c
executable file
·1463 lines (1323 loc) · 43.9 KB
/
write_bw.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
/*
* Copyright (c) 2005 Topspin Communications. All rights reserved.
* Copyright (c) 2005 Mellanox Technologies Ltd. All rights reserved.
* Copyright (c) 2009 HNR Consulting. All rights reserved.
*
* This software is available to you under a choice of one of two
* licenses. You may choose to be licensed under the terms of the GNU
* General Public License (GPL) Version 2, available from the file
* COPYING in the main directory of this source tree, or the
* OpenIB.org BSD license below:
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* $Id$
*/
#if HAVE_CONFIG_H
# include <config.h>
#endif /* HAVE_CONFIG_H */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netdb.h>
#include <malloc.h>
#include <getopt.h>
#include <arpa/inet.h>
#include <byteswap.h>
#include <time.h>
#include <infiniband/verbs.h>
#include "get_clock.h"
#define PINGPONG_RDMA_WRID 3
#define VERSION 2.0
#define ALL 1
#define MAX_INLINE 928
#define RC 0
#define UC 1
struct user_parameters {
const char *servername;
int connection_type;
int mtu;
int all; /* run all msg size */
long iters;
int tx_depth;
int use_event;
int numofqps;
int maxpostsofqpiniteration;
int inline_size;
int qp_timeout;
int gid_index; /* if value not negative, we use gid AND gid_index=value */
};
struct extended_qp {
struct ibv_qp *qp;
int scnt, ccnt ;
};
static int sl = 0;
static int page_size;
cycles_t *tposted;
cycles_t *tcompleted;
int Optype;
struct pingpong_context {
struct ibv_context *context;
struct ibv_comp_channel *channel;
struct ibv_pd *pd;
struct ibv_mr *mr;
struct ibv_cq *cq;
struct ibv_qp **qp;
void *buf;
//unsigned size;
long long size;
int tx_depth;
struct ibv_sge list;
struct ibv_sge recv_list;
struct ibv_send_wr wr;
struct ibv_recv_wr rwr;
int *scnt;
int *ccnt;
union ibv_gid dgid;
};
struct pingpong_dest {
int lid;
int qpn;
int psn;
unsigned rkey;
unsigned long long vaddr;
union ibv_gid dgid;
};
static uint16_t pp_get_local_lid(struct pingpong_context *ctx, int port)
{
struct ibv_port_attr attr;
if (ibv_query_port(ctx->context, port, &attr))
return 0;
return attr.lid;
}
static int pp_client_connect(const char *servername, int port)
{
struct addrinfo *res, *t;
struct addrinfo hints = {
.ai_family = AF_UNSPEC,
.ai_socktype = SOCK_STREAM
};
char *service;
int n;
int sockfd = -1;
if (asprintf(&service, "%d", port) < 0)
return -1;
n = getaddrinfo(servername, service, &hints, &res);
if (n < 0) {
fprintf(stderr, "%s for %s:%d\n", gai_strerror(n), servername, port);
return n;
}
for (t = res; t; t = t->ai_next) {
sockfd = socket(t->ai_family, t->ai_socktype, t->ai_protocol);
if (sockfd >= 0) {
if (!connect(sockfd, t->ai_addr, t->ai_addrlen))
break;
close(sockfd);
sockfd = -1;
}
}
freeaddrinfo(res);
if (sockfd < 0) {
fprintf(stderr, "Couldn't connect to %s:%d\n", servername, port);
return sockfd;
}
return sockfd;
}
struct pingpong_dest * pp_client_exch_dest(int sockfd,
const struct pingpong_dest *my_dest, struct user_parameters *user_parm)
{
struct pingpong_dest *rem_dest = NULL;
char msg[sizeof "0000:000000:000000:00000000:0000000000000000:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00"];
int parsed;
sprintf(msg, "%04x:%06x:%06x:%08x:%016Lx:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
my_dest->lid, my_dest->qpn, my_dest->psn,my_dest->rkey,my_dest->vaddr,
my_dest->dgid.raw[0], my_dest->dgid.raw[1], my_dest->dgid.raw[2],
my_dest->dgid.raw[3], my_dest->dgid.raw[4], my_dest->dgid.raw[5],
my_dest->dgid.raw[6], my_dest->dgid.raw[7], my_dest->dgid.raw[8],
my_dest->dgid.raw[9], my_dest->dgid.raw[10], my_dest->dgid.raw[11],
my_dest->dgid.raw[12], my_dest->dgid.raw[13], my_dest->dgid.raw[14],
my_dest->dgid.raw[15]);
if (write(sockfd, msg, sizeof msg) != sizeof msg) {
perror("client write");
fprintf(stderr, "Couldn't send local address\n");
goto out;
}
//printf("sizeof(msg) = %ld\n", sizeof msg);
if (read(sockfd, msg, sizeof msg) != sizeof msg) {
perror("client read");
fprintf(stderr, "Couldn't read remote address\n");
goto out;
}
rem_dest = malloc(sizeof *rem_dest);
if (!rem_dest)
goto out;
if (user_parm->gid_index < 0) {
parsed = sscanf(msg, "%x:%x:%x:%x:%Lx", &rem_dest->lid, &rem_dest->qpn,
&rem_dest->psn, &rem_dest->rkey, &rem_dest->vaddr);
if (parsed != 5) {
fprintf(stderr, "Couldn't parse line <%.*s>\n",(int)sizeof msg, msg);
free(rem_dest);
rem_dest = NULL;
goto out;
}
}else{
char *pstr = msg, *term;
char tmp[20];
int i;
term = strpbrk(pstr, ":");
memcpy(tmp, pstr, term - pstr);
tmp[term - pstr] = 0;
rem_dest->lid = (int)strtol(tmp, NULL, 16); // LID
pstr += term - pstr + 1;
term = strpbrk(pstr, ":");
memcpy(tmp, pstr, term - pstr);
tmp[term - pstr] = 0;
rem_dest->qpn = (int)strtol(tmp, NULL, 16); // QPN
pstr += term - pstr + 1;
term = strpbrk(pstr, ":");
memcpy(tmp, pstr, term - pstr);
tmp[term - pstr] = 0;
rem_dest->psn = (int)strtol(tmp, NULL, 16); // PSN
pstr += term - pstr + 1;
term = strpbrk(pstr, ":");
memcpy(tmp, pstr, term - pstr);
tmp[term - pstr] = 0;
rem_dest->rkey = (unsigned)strtol(tmp, NULL, 16); // RKEY
pstr += term - pstr + 1;
term = strpbrk(pstr, ":");
memcpy(tmp, pstr, term - pstr);
tmp[term - pstr] = 0;
rem_dest->vaddr = strtoull(tmp, NULL, 16); // VA
for (i = 0; i < 15; ++i) {
pstr += term - pstr + 1;
term = strpbrk(pstr, ":");
memcpy(tmp, pstr, term - pstr);
tmp[term - pstr] = 0;
rem_dest->dgid.raw[i] = (unsigned char)strtoll(tmp, NULL, 16);
}
pstr += term - pstr + 1;
strcpy(tmp, pstr);
rem_dest->dgid.raw[15] = (unsigned char)strtoll(tmp, NULL, 16);
}
out:
return rem_dest;
}
int pp_server_connect(int port)
{
struct addrinfo *res, *t;
struct addrinfo hints = {
.ai_flags = AI_PASSIVE,
.ai_family = AF_UNSPEC,
.ai_socktype = SOCK_STREAM
};
char *service;
int sockfd = -1, connfd;
int n;
if (asprintf(&service, "%d", port) < 0)
return -1;
n = getaddrinfo(NULL, service, &hints, &res);
if (n < 0) {
fprintf(stderr, "%s for port %d\n", gai_strerror(n), port);
return n;
}
for (t = res; t; t = t->ai_next) {
sockfd = socket(t->ai_family, t->ai_socktype, t->ai_protocol);
if (sockfd >= 0) {
n = 1;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n);
if (!bind(sockfd, t->ai_addr, t->ai_addrlen))
break;
close(sockfd);
sockfd = -1;
}
}
freeaddrinfo(res);
if (sockfd < 0) {
fprintf(stderr, "Couldn't listen to port %d\n", port);
return sockfd;
}
listen(sockfd, 1);
connfd = accept(sockfd, NULL, 0);
if (connfd < 0) {
perror("server accept");
fprintf(stderr, "accept() failed\n");
close(sockfd);
return connfd;
}
close(sockfd);
return connfd;
}
static struct pingpong_dest *pp_server_exch_dest(int connfd, const struct pingpong_dest *my_dest, struct user_parameters *user_parm)
{
char msg[sizeof "0000:000000:000000:00000000:0000000000000000:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00"];
struct pingpong_dest *rem_dest = NULL;
int parsed;
int n;
n = read(connfd, msg, sizeof msg);
//printf("n = %ld, sizeof(msg) = %ld\n", n, sizeof msg);
if (n != sizeof msg) {
perror("server read");
fprintf(stderr, "%d/%d: Couldn't read remote address\n", n, (int) sizeof msg);
goto out;
}
rem_dest = malloc(sizeof *rem_dest);
if (!rem_dest)
goto out;
if (user_parm->gid_index < 0) {
parsed = sscanf(msg, "%x:%x:%x:%x:%Lx", &rem_dest->lid, &rem_dest->qpn,
&rem_dest->psn, &rem_dest->rkey, &rem_dest->vaddr);
if (parsed != 5) {
fprintf(stderr, "Couldn't parse line <%.*s>\n",(int)sizeof msg, msg);
free(rem_dest);
rem_dest = NULL;
goto out;
}
}else{
char *pstr = msg, *term;
char tmp[20];
int i;
term = strpbrk(pstr, ":");
memcpy(tmp, pstr, term - pstr);
tmp[term - pstr] = 0;
rem_dest->lid = (int)strtol(tmp, NULL, 16); // LID
pstr += term - pstr + 1;
term = strpbrk(pstr, ":");
memcpy(tmp, pstr, term - pstr);
tmp[term - pstr] = 0;
rem_dest->qpn = (int)strtol(tmp, NULL, 16); // QPN
pstr += term - pstr + 1;
term = strpbrk(pstr, ":");
memcpy(tmp, pstr, term - pstr);
tmp[term - pstr] = 0;
rem_dest->psn = (int)strtol(tmp, NULL, 16); // PSN
pstr += term - pstr + 1;
term = strpbrk(pstr, ":");
memcpy(tmp, pstr, term - pstr);
tmp[term - pstr] = 0;
rem_dest->rkey = (unsigned)strtol(tmp, NULL, 16); // RKEY
pstr += term - pstr + 1;
term = strpbrk(pstr, ":");
memcpy(tmp, pstr, term - pstr);
tmp[term - pstr] = 0;
rem_dest->vaddr = strtoull(tmp, NULL, 16); // VA
for (i = 0; i < 15; ++i) {
pstr += term - pstr + 1;
term = strpbrk(pstr, ":");
memcpy(tmp, pstr, term - pstr);
tmp[term - pstr] = 0;
rem_dest->dgid.raw[i] = (unsigned char)strtoll(tmp, NULL, 16);
}
pstr += term - pstr + 1;
strcpy(tmp, pstr);
rem_dest->dgid.raw[15] = (unsigned char)strtoll(tmp, NULL, 16);
}
sprintf(msg, "%04x:%06x:%06x:%08x:%016Lx:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
my_dest->lid, my_dest->qpn, my_dest->psn, my_dest->rkey, my_dest->vaddr,
my_dest->dgid.raw[0], my_dest->dgid.raw[1], my_dest->dgid.raw[2],
my_dest->dgid.raw[3], my_dest->dgid.raw[4], my_dest->dgid.raw[5],
my_dest->dgid.raw[6], my_dest->dgid.raw[7], my_dest->dgid.raw[8],
my_dest->dgid.raw[9], my_dest->dgid.raw[10], my_dest->dgid.raw[11],
my_dest->dgid.raw[12], my_dest->dgid.raw[13], my_dest->dgid.raw[14],
my_dest->dgid.raw[15]);
if (write(connfd, msg, sizeof msg) != sizeof msg) {
perror("server write");
fprintf(stderr, "Couldn't send local address\n");
free(rem_dest);
rem_dest = NULL;
goto out;
}
out:
return rem_dest;
}
static struct pingpong_context *pp_init_ctx(struct ibv_device *ib_dev,
long long size,
int tx_depth, int port, struct user_parameters *user_parm)
{
struct pingpong_context *ctx;
struct ibv_device_attr device_attr;
int counter;
ctx = malloc(sizeof *ctx);
if (!ctx)
return NULL;
ctx->qp = malloc(sizeof (struct ibv_qp*) * user_parm->numofqps );
ctx->size = size;
ctx->tx_depth = tx_depth;
ctx->scnt = malloc(user_parm->numofqps * sizeof (int));
if (!ctx->scnt) {
perror("malloc");
return NULL;
}
ctx->ccnt = malloc(user_parm->numofqps * sizeof (int));
if (!ctx->ccnt) {
perror("malloc");
return NULL;
}
memset(ctx->scnt, 0, user_parm->numofqps * sizeof (int));
memset(ctx->ccnt, 0, user_parm->numofqps * sizeof (int));
ctx->buf = memalign(page_size, size * 2 * user_parm->numofqps );
if (!ctx->buf) {
fprintf(stderr, "Couldn't allocate work buf.\n");
return NULL;
}
memset(ctx->buf, 0, size * 2 * user_parm->numofqps);
ctx->context = ibv_open_device(ib_dev);
if (!ctx->context) {
fprintf(stderr, "Couldn't get context for %s\n",
ibv_get_device_name(ib_dev));
return NULL;
}
if (user_parm->mtu == 0) {/*user did not ask for specific mtu */
if (ibv_query_device(ctx->context, &device_attr)) {
fprintf(stderr, "Failed to query device props");
return NULL;
}
if (device_attr.vendor_part_id == 23108 || user_parm->gid_index > -1) {
user_parm->mtu = 1024;
} else {
user_parm->mtu = 2048;
}
}
if (user_parm->use_event) {
ctx->channel = ibv_create_comp_channel(ctx->context);
if (!ctx->channel) {
fprintf(stderr, "Couldn't create completion channel\n");
return NULL;
}
} else {
ctx->channel = NULL;
}
ctx->pd = ibv_alloc_pd(ctx->context);
if (!ctx->pd) {
fprintf(stderr, "Couldn't allocate PD\n");
return NULL;
}
/* We dont really want IBV_ACCESS_LOCAL_WRITE, but IB spec says:
* The Consumer is not allowed to assign Remote Write or Remote Atomic to
* a Memory Region that has not been assigned Local Write. */
ctx->mr = ibv_reg_mr(ctx->pd, ctx->buf, size * 2 * user_parm->numofqps,
IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE | IBV_ACCESS_REMOTE_READ);
if (!ctx->mr) {
fprintf(stderr, "Couldn't allocate MR\n");
return NULL;
}
ctx->cq = ibv_create_cq(ctx->context, tx_depth * user_parm->numofqps , NULL, ctx->channel, 0);
if (!ctx->cq) {
fprintf(stderr, "Couldn't create CQ\n");
return NULL;
}
for (counter =0 ; counter < user_parm->numofqps ; counter++)
{
struct ibv_qp_init_attr initattr;
struct ibv_qp_attr attr;
memset(&initattr, 0, sizeof(struct ibv_qp_init_attr));
initattr.send_cq = ctx->cq;
initattr.recv_cq = ctx->cq;
initattr.cap.max_send_wr = tx_depth;
/* Work around: driver doesnt support
* recv_wr = 0 */
initattr.cap.max_recv_wr = 1;
initattr.cap.max_send_sge = 1;
initattr.cap.max_recv_sge = 1;
initattr.cap.max_inline_data = user_parm->inline_size;
if (user_parm->connection_type == 1) {
initattr.qp_type = IBV_QPT_UC;
} else {
initattr.qp_type = IBV_QPT_RC;
}
ctx->qp[counter] = ibv_create_qp(ctx->pd, &initattr);
if (!ctx->qp[counter]) {
fprintf(stderr, "Couldn't create QP\n");
return NULL;
}
printf("ibv_create_qp success, counter = %d\n", counter);
attr.qp_state = IBV_QPS_INIT;
attr.pkey_index = 0;
attr.port_num = port;
attr.qp_access_flags = IBV_ACCESS_REMOTE_WRITE | IBV_ACCESS_REMOTE_READ;
if (ibv_modify_qp(ctx->qp[counter], &attr,
IBV_QP_STATE |
IBV_QP_PKEY_INDEX |
IBV_QP_PORT |
IBV_QP_ACCESS_FLAGS)) {
fprintf(stderr, "Failed to modify QP to INIT\n");
return NULL;
}
}
return ctx;
}
static int pp_connect_ctx(struct pingpong_context *ctx, int port, int my_psn,
struct pingpong_dest *dest, struct user_parameters *user_parm, int qpindex)
{
struct ibv_qp_attr attr;
memset(&attr, 0, sizeof attr);
attr.qp_state = IBV_QPS_RTR;
switch (user_parm->mtu) {
case 256 :
attr.path_mtu = IBV_MTU_256;
break;
case 512 :
attr.path_mtu = IBV_MTU_512;
break;
case 1024 :
attr.path_mtu = IBV_MTU_1024;
break;
case 2048 :
attr.path_mtu = IBV_MTU_2048;
break;
case 4096 :
attr.path_mtu = IBV_MTU_4096;
break;
}
printf("Mtu : %d\n", user_parm->mtu);
attr.dest_qp_num = dest->qpn;
attr.rq_psn = dest->psn;
if (user_parm->connection_type==RC) {
attr.max_dest_rd_atomic = 1;
attr.min_rnr_timer = 12;
}
if (user_parm->gid_index<0) {
attr.ah_attr.is_global = 0;
attr.ah_attr.dlid = dest->lid;
attr.ah_attr.sl = sl;
} else {
attr.ah_attr.is_global = 1;
attr.ah_attr.grh.dgid = dest->dgid;
attr.ah_attr.grh.hop_limit = 1;
attr.ah_attr.sl = 0;
}
attr.ah_attr.src_path_bits = 0;
attr.ah_attr.port_num = port;
if (user_parm->connection_type == RC) {
if (ibv_modify_qp(ctx->qp[qpindex], &attr,
IBV_QP_STATE |
IBV_QP_AV |
IBV_QP_PATH_MTU |
IBV_QP_DEST_QPN |
IBV_QP_RQ_PSN |
IBV_QP_MIN_RNR_TIMER |
IBV_QP_MAX_DEST_RD_ATOMIC)) {
fprintf(stderr, "Failed to modify RC QP to RTR\n");
return 1;
}
attr.timeout = user_parm->qp_timeout;
attr.retry_cnt = 7;
attr.rnr_retry = 7;
} else {
if (ibv_modify_qp(ctx->qp[qpindex], &attr,
IBV_QP_STATE |
IBV_QP_AV |
IBV_QP_PATH_MTU |
IBV_QP_DEST_QPN |
IBV_QP_RQ_PSN)) {
fprintf(stderr, "Failed to modify UC QP to RTR\n");
return 1;
}
}
attr.qp_state = IBV_QPS_RTS;
attr.sq_psn = my_psn;
attr.max_rd_atomic = 1;
if (user_parm->connection_type == 0) {
attr.max_rd_atomic = 1;
if (ibv_modify_qp(ctx->qp[qpindex], &attr,
IBV_QP_STATE |
IBV_QP_SQ_PSN |
IBV_QP_TIMEOUT |
IBV_QP_RETRY_CNT |
IBV_QP_RNR_RETRY |
IBV_QP_MAX_QP_RD_ATOMIC)) {
fprintf(stderr, "Failed to modify RC QP to RTS\n");
return 1;
}
} else {
if (ibv_modify_qp(ctx->qp[qpindex], &attr,
IBV_QP_STATE |
IBV_QP_SQ_PSN)) {
fprintf(stderr, "Failed to modify UC QP to RTS\n");
return 1;
}
}
/*
if (!user_parm->servername) { // server
int i;
struct ibv_recv_wr *bad_wr_recv;
//struct ibv_recv_wr wr, *bad_wr_recv = NULL;
//struct ibv_sge sge;
//recieve
ctx->rwr.wr_id = 0;
ctx->rwr.sg_list = &ctx->recv_list;
ctx->rwr.num_sge = 1;
ctx->rwr.next = NULL;
//wr.wr_id = 0;
//wr.next = NULL;
//wr.sg_list = &sge;
//wr.num_sge = 1;
//sge.addr = (uintptr_t) ctx->buf;
//sge.length = ctx->size;
//sge.lkey = ctx->mr->lkey;
ctx->recv_list.addr = (uintptr_t) ctx->buf;
ctx->recv_list.length = ctx->size;
ctx->recv_list.lkey = ctx->mr->lkey;
printf("***DEBUG: Prepost receive\n");
//if (ibv_post_recv(ctx->qp[0], &wr, &bad_wr_recv)) {
if (ibv_post_recv(ctx->qp[0], &ctx->rwr, &bad_wr_recv)) {
fprintf(stderr, "Couldn't post recv: counter=%d\n", i);
return 14;
}
}
printf("***DDDD\n");
*/
return 0;
}
static void usage(const char *argv0)
{
printf("Usage:\n");
printf(" %s start a server and wait for connection\n", argv0);
printf(" %s <host> connect to server at <host>\n", argv0);
printf("\n");
printf("Options:\n");
printf(" -o --output output file dir for latency logging (used for sender)\n");
printf(" -O --Optype=<int> RDMA OP Type(W:0, R:1, WIMM:2, SEND:3) Default=WRITE\n");
printf(" -p, --port=<port> listen on/connect to port <port> (default 18515)\n");
printf(" -d, --ib-dev=<dev> use IB device <dev> (default first device found)\n");
printf(" -i, --ib-port=<port> use port <port> of IB device (default 1)\n");
printf(" -c, --connection=<RC/UC> connection type RC/UC (default RC)\n");
printf(" -m, --mtu=<mtu> mtu size (256 - 4096. default for hermon is 2048)\n");
printf(" -g, --post=<num of posts> number of posts for each qp in the chain (default tx_depth)\n");
printf(" -q, --qp=<num of qp's> Num of qp's(default 1)\n");
printf(" -s, --size=<size> size of message to exchange (default 65536)\n");
printf(" -a, --all Run sizes from 2 till 2^23\n");
printf(" -t, --tx-depth=<dep> size of tx queue (default 100)\n");
printf(" -n, --iters=<iters> number of exchanges (at least 2, default 5000)\n");
printf(" -I, --inline_size=<size> max size of message to be sent in inline mode (default 400)\n");
printf(" -u, --qp-timeout=<timeout> QP timeout, timeout value is 4 usec * 2 ^(timeout), default 14\n");
printf(" -S, --sl=<sl> SL (default 0)\n");
printf(" -x, --gid-index=<index> test uses GID with GID index taken from command line (for RDMAoE index should be 0)\n");
printf(" -b, --bidirectional measure bidirectional bandwidth (default unidirectional)\n");
printf(" -V, --version display version number\n");
printf(" -N, --no peak-bw cancel peak-bw calculation (default with peak-bw)\n");
printf(" -F, --CPU-freq do not fail even if cpufreq_ondemand module is loaded\n");
}
static void print_report(long iters, long size, int duplex,
cycles_t *tposted, cycles_t *tcompleted, struct user_parameters *user_param,
int noPeak, int no_cpu_freq_fail, const char *filename, cycles_t START_cycle)
{
//printf("From print_report: iters = %d\n", iters);
double cycles_to_units;
unsigned long tsize; /* Transferred size, in megabytes */
long i;
int opt_posted = 0, opt_completed = 0;
cycles_t opt_delta;
//cycles_t t;
opt_delta = tcompleted[opt_posted] - tposted[opt_completed];
//long cnt = 0;
// When num iters are large, find the peak takes FOREVER
/*
if (!noPeak) {
// Find the peak bandwidth unless asked not to in command line
for (i = 0; i < iters * user_param->numofqps; ++i)
for (j = i; j < iters * user_param->numofqps; ++j) {
t = (tcompleted[j] - tposted[i]) / (j - i + 1);
if (t < opt_delta) {
opt_delta = t;
opt_posted = i;
opt_completed = j;
}
cnt++;
printf("@@@cnt: %ld\n", cnt);
}
}
*/
cycles_to_units = get_cpu_mhz(no_cpu_freq_fail) * 1000000;
tsize = duplex ? 2 : 1;
tsize = tsize * size;
printf("%7d %d %7.2f %7.2f\n",
size,iters,!(noPeak) * tsize * cycles_to_units / opt_delta / 0x100000,
tsize * iters * user_param->numofqps * cycles_to_units /(tcompleted[(iters* user_param->numofqps) - 1] - tposted[0]) / 0x100000);
FILE *f = fopen(filename, "w");
fprintf(f, "Task_cnt\tTime(us)\t\tLatency\n");
double cpu_mhz = get_cpu_mhz(no_cpu_freq_fail);
double curr_time_us, lat_us;
for (i = 0; i < iters * user_param->numofqps; ++i) {
curr_time_us = (double)(tcompleted[i] - START_cycle) / cpu_mhz;
lat_us = (double)(tcompleted[i] - tposted[i]) / cpu_mhz;
fprintf(f, "%ld\t\t%.2f\t\t%.2f\n", i + 1, curr_time_us, lat_us);
}
fclose(f);
}
int run_iter(struct pingpong_context *ctx, struct user_parameters *user_param,
struct pingpong_dest **rem_dest, int size)
{
//printf("BOTH entered?\n");
struct ibv_qp *qp;
long totscnt, totccnt ;
int index;//warmindex;
int inline_size;
struct ibv_send_wr *bad_wr;
struct ibv_wc wc;
int ne;
ctx->list.addr = (uintptr_t) ctx->buf;
ctx->list.length = size;
ctx->list.lkey = ctx->mr->lkey;
ctx->wr.sg_list = &ctx->list;
ctx->wr.num_sge = 1;
if (Optype == 0) {
printf("PICK WRITE\n");
ctx->wr.opcode = IBV_WR_RDMA_WRITE;
} else if (Optype == 1) {
printf("PICK READ\n");
ctx->wr.opcode = IBV_WR_RDMA_READ;
} else if (Optype == 2) {
printf("PICK WRITE IMM");
ctx->wr.opcode = IBV_WR_RDMA_WRITE_WITH_IMM;
} else if (Optype == 3) {
printf("PICK_SEND\n");
ctx->wr.opcode = IBV_WR_SEND;
} else {
printf("PICK WRITE\n");
ctx->wr.opcode = IBV_WR_RDMA_WRITE;
}
inline_size = user_param->inline_size;
if (size > inline_size) {/* complaince to perf_main */
ctx->wr.send_flags = IBV_SEND_SIGNALED;
} else {
if (Optype == 0 || Optype == 3) {
ctx->wr.send_flags = IBV_SEND_SIGNALED | IBV_SEND_INLINE;
//ctx->wr.send_flags = IBV_SEND_SIGNALED;
} else if (Optype == 1 || Optype == 2) {
ctx->wr.send_flags = IBV_SEND_SIGNALED;
} else {
printf("Unhandled Optype when setting INLINE flag. Check the actual code pls.\n");
exit(EXIT_FAILURE);
}
}
ctx->wr.next = NULL;
// recv wr
ctx->rwr.wr_id = 0;
ctx->rwr.sg_list = &ctx->recv_list;
ctx->rwr.num_sge = 1;
ctx->rwr.next = NULL;
ctx->recv_list.addr = (uintptr_t) ctx->buf;
ctx->recv_list.lkey = ctx->mr->lkey;
ctx->recv_list.length = ctx->size;
totscnt = 0;
totccnt = 0;
/*clear the scnt ccnt counters for each iteration*/
for (index =0 ; index < user_param->numofqps ; index++) {
ctx->scnt[index] = 0;
ctx->ccnt[index] = 0;
}
index = 0;
/* Done with setup. Start the test.
warm up posting of total 100 wq's per qp
1 for each qp till all qps have 100 */
/*
for (warmindex =0 ;warmindex < user_param->maxpostsofqpiniteration ;warmindex ++ ) {
for (index =0 ; index < user_param->numofqps ; index++) {
ctx->wr.wr.rdma.remote_addr = rem_dest[index]->vaddr;
ctx->wr.wr.rdma.rkey = rem_dest[index]->rkey;
qp = ctx->qp[index];
ctx->wr.wr_id = index ;
tposted[totscnt] = get_cycles();
if (ibv_post_send(qp, &ctx->wr, &bad_wr)) {
fprintf(stderr, "Couldn't post warmup send: qp index = %d qp scnt=%d total scnt %d\n",
index,ctx->scnt[index],totscnt);
return 1;
}
ctx->scnt[index]= ctx->scnt[index]+1;
++totscnt;
}
}
*/
/* main loop for posting */
//printf("before main loop, totscnt = %d\n", totscnt);
/*
while (totscnt < (user_param->iters * user_param->numofqps) || totccnt < (user_param->iters * user_param->numofqps) ) {
//int CNT = 0;
// main loop to run over all the qps and post each time n messages
for (index =0 ; index < user_param->numofqps ; index++) {
ctx->wr.wr.rdma.remote_addr = rem_dest[index]->vaddr;
ctx->wr.wr.rdma.rkey = rem_dest[index]->rkey;
qp = ctx->qp[index];
ctx->wr.wr_id = index ;
while (ctx->scnt[index] < user_param->iters && (ctx->scnt[index] - ctx->ccnt[index]) < user_param->maxpostsofqpiniteration) {
//CNT++;
tposted[totscnt] = get_cycles();
if (ibv_post_send(qp, &ctx->wr, &bad_wr)) {
fprintf(stderr, "Couldn't post send: qp index = %d qp scnt=%d total scnt %d\n",
index,ctx->scnt[index],totscnt);
return 1;
}
ctx->scnt[index]= ctx->scnt[index]+1;
++totscnt;
//printf("<1>totscnt: %d, totccnt: %d\n", totscnt, totccnt);
}
}
//printf("CNT: %d\n", CNT);
// finished posting now polling
if (totccnt < (user_param->iters * user_param->numofqps) ) {
int ne;
do {
ne = ibv_poll_cq(ctx->cq, 1, &wc);
} while (ne == 0);
tcompleted[totccnt] = get_cycles();
if (ne < 0) {
fprintf(stderr, "poll CQ failed %d\n", ne);
return 1;
}
if (wc.status != IBV_WC_SUCCESS) {
fprintf(stderr, "Completion wth error at %s:\n",
user_param->servername ? "client" : "server");
fprintf(stderr, "Failed status %d: wr_id %d\n",
wc.status, (int) wc.wr_id);
fprintf(stderr, "qp index %d ,qp scnt=%d, qp ccnt=%d total scnt %d total ccnt %d\n",
(int)wc.wr_id, ctx->scnt[(int)wc.wr_id], ctx->ccnt[(int)wc.wr_id], totscnt, totccnt);
return 1;
}
//here the id is the index to the qp num
ctx->ccnt[(int)wc.wr_id] = ctx->ccnt[(int)wc.wr_id]+1;
totccnt += 1;
}
//printf("<2>totscnt: %d, totccnt: %d\n", totscnt, totccnt);
}
*/
if (!user_param->servername) { // server
printf("Greetings. I'm server.\n");
int ne;
int rcnt = 0;
struct ibv_wc wc;
struct ibv_recv_wr *bad_wr_recv;
while (rcnt < user_param->iters * user_param->numofqps) {
++rcnt;
if (ibv_post_recv(ctx->qp[0], &ctx->rwr, &bad_wr_recv)) {
fprintf(stderr, "Couldn't post recv: rcnt=%d\n",
rcnt);
return 15;
}
if (user_param->use_event) {
struct ibv_cq *ev_cq;
void *ev_ctx;
if (ibv_get_cq_event(ctx->channel, &ev_cq, &ev_ctx)) {
fprintf(stderr, "Failed to get cq_event\n");
return 1;
}
if (ev_cq != ctx->cq) {
fprintf(stderr, "CQ event for unknown CQ %p\n", ev_cq);
return 1;
}
if (ibv_req_notify_cq(ctx->cq, 0)) {
fprintf(stderr, "Couldn't request CQ notification\n");
return 1;
}
}
do {
ne = ibv_poll_cq(ctx->cq, 1, &wc);
if (ne) {
if (wc.status != IBV_WC_SUCCESS) {
fprintf(stderr, "Completion wth error at %s:\n",
user_param->servername ? "client" : "server");
printf("error wc status: %s\n", ibv_wc_status_str(wc.status));
fprintf(stderr, "Failed status %d: wr_id %d syndrom 0x%x\n",
wc.status, (int) wc.wr_id, wc.vendor_err);
fprintf(stderr, "rcnt=%d\n", rcnt);
return 1;
}
}
} while (ne == 0 );
}
if (ne < 0) {
fprintf(stderr, "Poll Recieve CQ failed %d\n", ne);
return 12;
}
} else { // client
int index = 0;
ctx->wr.wr.rdma.remote_addr = rem_dest[index]->vaddr;
ctx->wr.wr.rdma.rkey = rem_dest[index]->rkey;
qp = ctx->qp[index];
ctx->wr.wr_id = index ;
while (totccnt < (user_param->iters * user_param->numofqps)) {
//while (totscnt < (user_param->iters * user_param->numofqps) || totccnt < (user_param->iters * user_param->numofqps) ) {
//int CNT = 0;
// main loop to run over all the qps and post each time n messages
//for (index =0 ; index < user_param->numofqps ; index++) {
/*
ctx->wr.wr.rdma.remote_addr = rem_dest[index]->vaddr;
ctx->wr.wr.rdma.rkey = rem_dest[index]->rkey;
qp = ctx->qp[index];
ctx->wr.wr_id = index ;
*/
tposted[totscnt] = get_cycles();
if (ibv_post_send(qp, &ctx->wr, &bad_wr)) {
fprintf(stderr, "Couldn't post send: qp index = %d qp scnt=%d total scnt %d\n",
index,ctx->scnt[index],totscnt);
return 1;
}
//ctx->scnt[index]= ctx->scnt[index]+1;
++totscnt;
//printf("<1>totscnt: %d, totccnt: %d\n", totscnt, totccnt);
//}
//printf("CNT: %d\n", CNT);
// finished posting now polling
//int ne;
if (user_param->use_event) {
struct ibv_cq *ev_cq;
void *ev_ctx;
if (ibv_get_cq_event(ctx->channel, &ev_cq, &ev_ctx)) {
fprintf(stderr, "Failed to get cq_event\n");
return 1;
}
if (ev_cq != ctx->cq) {
fprintf(stderr, "CQ event for unknown CQ %p\n", ev_cq);
return 1;
}
if (ibv_req_notify_cq(ctx->cq, 0)) {
fprintf(stderr, "Couldn't request CQ notification\n");
return 1;
}
}
do {
ne = ibv_poll_cq(ctx->cq, 1, &wc);
} while (ne == 0);
tcompleted[totccnt] = get_cycles();
if (ne < 0) {
fprintf(stderr, "poll CQ failed %d\n", ne);