-
Notifications
You must be signed in to change notification settings - Fork 265
/
perftest_communication.c
executable file
·2551 lines (2108 loc) · 71.2 KB
/
perftest_communication.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <unistd.h>
#include <getopt.h>
#if !defined(__FreeBSD__)
#include <malloc.h>
#include <byteswap.h>
#endif
#include <errno.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include "perftest_communication.h"
#if defined(__FreeBSD__)
#include <ctype.h>
#endif
#if defined(__FreeBSD__)
#define s6_addr32 __u6_addr.__u6_addr32
/* states: S_N: normal, S_I: comparing integral part, S_F: comparing
fractional parts, S_Z: idem but with leading Zeroes only */
#define S_N 0x0
#define S_I 0x4
#define S_F 0x8
#define S_Z 0xC
/* result_type: CMP: return diff; LEN: compare using len_diff/diff */
#define CMP 2
#define LEN 3
/* Compare S1 and S2 as strings holding indices/version numbers,
returning less than, equal to or greater than zero if S1 is less than,
equal to or greater than S2 (for more info, see the Glibc texinfo doc). */
int
strverscmp (const char *s1, const char *s2)
{
const unsigned char *p1 = (const unsigned char *) s1;
const unsigned char *p2 = (const unsigned char *) s2;
unsigned char c1, c2;
int state;
int diff;
/* Symbol(s) 0 [1-9] others (padding)
Transition (10) 0 (01) d (00) x (11) - */
static const unsigned int next_state[] =
{
/* state x d 0 - */
/* S_N */ S_N, S_I, S_Z, S_N,
/* S_I */ S_N, S_I, S_I, S_I,
/* S_F */ S_N, S_F, S_F, S_F,
/* S_Z */ S_N, S_F, S_Z, S_Z
};
static const int result_type[] =
{
/* state x/x x/d x/0 x/- d/x d/d d/0 d/-
0/x 0/d 0/0 0/- -/x -/d -/0 -/- */
/* S_N */ CMP, CMP, CMP, CMP, CMP, LEN, CMP, CMP,
CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP,
/* S_I */ CMP, -1, -1, CMP, +1, LEN, LEN, CMP,
+1, LEN, LEN, CMP, CMP, CMP, CMP, CMP,
/* S_F */ CMP, CMP, CMP, CMP, CMP, LEN, CMP, CMP,
CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP,
/* S_Z */ CMP, +1, +1, CMP, -1, CMP, CMP, CMP,
-1, CMP, CMP, CMP
};
if (p1 == p2)
return 0;
c1 = *p1++;
c2 = *p2++;
/* Hint: '0' is a digit too. */
state = S_N | ((c1 == '0') + (isdigit (c1) != 0));
while ((diff = c1 - c2) == 0 && c1 != '\0')
{
state = next_state[state];
c1 = *p1++;
c2 = *p2++;
state |= (c1 == '0') + (isdigit (c1) != 0);
}
state = result_type[state << 2 | (((c2 == '0') + (isdigit (c2) != 0)))];
switch (state)
{
case CMP:
return diff;
case LEN:
while (isdigit (*p1++))
if (!isdigit (*p2++))
return 1;
return isdigit (*p2) ? -1 : diff;
default:
return state;
}
}
#endif
/******************************************************************************
*
******************************************************************************/
static const char *sideArray[] = {"local", "remote"};
static const char *gidArray[] = {"GID" , "MGID"};
static inline int valid_mtu_size(int mtu_size)
{
return !(mtu_size < IBV_MTU_256 || mtu_size > IBV_MTU_4096);
}
static inline int ipv6_addr_v4mapped(const struct in6_addr *a)
{
return ((a->s6_addr32[0] | a->s6_addr32[1]) |
(a->s6_addr32[2] ^ htonl(0x0000ffff))) == 0UL ||
/* IPv4 encoded multicast addresses */
(a->s6_addr32[0] == htonl(0xff0e0000) &&
((a->s6_addr32[1] |
(a->s6_addr32[2] ^ htonl(0x0000ffff))) == 0UL));
}
/******************************************************************************
*
******************************************************************************/
double bswap_double(double x)
{
union {
double ddata;
uint64_t u64data;
} d1, d2;
d1.ddata = x;
d2.u64data = bswap_64(d1.u64data);
return d2.ddata;
}
/******************************************************************************
*
******************************************************************************/
static int post_one_recv_wqe(struct pingpong_context *ctx)
{
struct ibv_recv_wr wr;
struct ibv_recv_wr *bad_wr;
struct ibv_sge list;
list.addr = (uintptr_t)ctx->buf[0];
list.length = sizeof(struct pingpong_dest);
list.lkey = ctx->mr[0]->lkey;
wr.next = NULL;
wr.wr_id = SYNC_SPEC_ID;
wr.sg_list = &list;
wr.num_sge = 1;
if (ibv_post_recv(ctx->qp[0],&wr,&bad_wr)) {
fprintf(stderr, "Function ibv_post_recv failed for RDMA_CM QP\n");
return FAILURE;
}
return SUCCESS;
}
/******************************************************************************
*
******************************************************************************/
static int post_recv_to_get_ah(struct pingpong_context *ctx)
{
struct ibv_recv_wr wr;
struct ibv_recv_wr *bad_wr;
struct ibv_sge list;
list.addr = (uintptr_t)ctx->buf[0];
list.length = UD_ADDITION + sizeof(uint32_t);
list.lkey = ctx->mr[0]->lkey;
wr.next = NULL;
wr.wr_id = 0;
wr.sg_list = &list;
wr.num_sge = 1;
if (ibv_post_recv(ctx->qp[0],&wr,&bad_wr)) {
fprintf(stderr, "Function ibv_post_recv failed for RDMA_CM QP\n");
return FAILURE;
}
return SUCCESS;
}
/******************************************************************************
*
******************************************************************************/
static int send_qp_num_for_ah(struct pingpong_context *ctx,
struct perftest_parameters *user_param)
{
struct ibv_send_wr wr;
struct ibv_send_wr *bad_wr;
struct ibv_sge list;
struct ibv_wc wc;
int ne;
memcpy(ctx->buf[0], &ctx->qp[0]->qp_num, sizeof(uint32_t));
list.addr = (uintptr_t)ctx->buf[0];
list.length = sizeof(uint32_t);
list.lkey = ctx->mr[0]->lkey;
wr.wr_id = 0;
wr.sg_list = &list;
wr.num_sge = 1;
wr.opcode = IBV_WR_SEND_WITH_IMM;
wr.send_flags = IBV_SEND_SIGNALED;
wr.next = NULL;
wr.imm_data = htonl(ctx->qp[0]->qp_num);
wr.wr.ud.ah = ctx->ah[0];
wr.wr.ud.remote_qpn = user_param->rem_ud_qpn;
wr.wr.ud.remote_qkey = user_param->rem_ud_qkey;
if (ibv_post_send(ctx->qp[0],&wr,&bad_wr)) {
fprintf(stderr, "Function ibv_post_send failed\n");
return 1;
}
do {
ne = ibv_poll_cq(ctx->send_cq, 1,&wc);
} while (ne == 0);
if (wc.status || wc.opcode != IBV_WC_SEND || wc.wr_id != 0) {
fprintf(stderr, " Couldn't post send my QP number %d\n",(int)wc.status);
return 1;
}
return 0;
}
/******************************************************************************
*
******************************************************************************/
static int create_ah_from_wc_recv(struct pingpong_context *ctx,
struct perftest_parameters *user_param)
{
struct ibv_qp_attr attr;
struct ibv_qp_init_attr init_attr;
struct ibv_wc wc;
int ne;
do {
ne = ibv_poll_cq(ctx->recv_cq,1,&wc);
} while (ne == 0);
if (wc.status || !(wc.opcode & IBV_WC_RECV) || wc.wr_id != 0) {
fprintf(stderr, "Bad wc status when trying to create AH -- %d -- %d \n",(int)wc.status,(int)wc.wr_id);
return 1;
}
ctx->ah[0] = ibv_create_ah_from_wc(ctx->pd, &wc, (struct ibv_grh*)ctx->buf[0], ctx->cm_id->port_num);
user_param->rem_ud_qpn = ntohl(wc.imm_data);
ibv_query_qp(ctx->qp[0],&attr, IBV_QP_QKEY,&init_attr);
user_param->rem_ud_qkey = attr.qkey;
return 0;
}
/******************************************************************************
*
******************************************************************************/
static int ethernet_write_keys(struct pingpong_dest *my_dest,
struct perftest_comm *comm)
{
if (my_dest->gid_index == -1) {
char msg[KEY_MSG_SIZE];
sprintf(msg,KEY_PRINT_FMT,my_dest->lid,my_dest->out_reads,
my_dest->qpn,my_dest->psn, my_dest->rkey, my_dest->vaddr, my_dest->srqn);
if (write(comm->rdma_params->sockfd,msg,sizeof msg) != sizeof msg) {
perror("client write");
fprintf(stderr, "Couldn't send local address\n");
return 1;
}
} else {
char msg[KEY_MSG_SIZE_GID];
sprintf(msg,KEY_PRINT_FMT_GID, my_dest->lid,my_dest->out_reads,
my_dest->qpn,my_dest->psn, my_dest->rkey, my_dest->vaddr,
my_dest->gid.raw[0],my_dest->gid.raw[1],
my_dest->gid.raw[2],my_dest->gid.raw[3],
my_dest->gid.raw[4],my_dest->gid.raw[5],
my_dest->gid.raw[6],my_dest->gid.raw[7],
my_dest->gid.raw[8],my_dest->gid.raw[9],
my_dest->gid.raw[10],my_dest->gid.raw[11],
my_dest->gid.raw[12],my_dest->gid.raw[13],
my_dest->gid.raw[14],my_dest->gid.raw[15],
my_dest->srqn);
if (write(comm->rdma_params->sockfd, msg, sizeof msg) != sizeof msg) {
perror("client write");
fprintf(stderr, "Couldn't send local address\n");
return 1;
}
}
return 0;
}
/******************************************************************************
*
******************************************************************************/
static int ethernet_read_keys(struct pingpong_dest *rem_dest,
struct perftest_comm *comm)
{
if (rem_dest->gid_index == -1) {
int parsed;
char msg[KEY_MSG_SIZE];
if (read(comm->rdma_params->sockfd, msg, sizeof msg) != sizeof msg) {
fprintf(stderr, "ethernet_read_keys: Couldn't read remote address\n");
return 1;
}
parsed = sscanf(msg,KEY_PRINT_FMT,(unsigned int*)&rem_dest->lid,
&rem_dest->out_reads,&rem_dest->qpn,
&rem_dest->psn, &rem_dest->rkey,&rem_dest->vaddr,&rem_dest->srqn);
if (parsed != 7) {
fprintf(stderr, "Couldn't parse line <%.*s>\n",(int)sizeof msg, msg);
return 1;
}
} else {
char msg[KEY_MSG_SIZE_GID];
char *pstr = msg, *term;
char tmp[120];
int i;
if (read(comm->rdma_params->sockfd, msg, sizeof msg) != sizeof msg) {
fprintf(stderr, "ethernet_read_keys: Couldn't read remote address\n");
return 1;
}
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->out_reads = (int)strtol(tmp, NULL, 16); /*OUT_READS*/
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)strtoul(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->gid.raw[i] = (unsigned char)strtoll(tmp, NULL, 16);
}
pstr += term - pstr + 1;
strcpy(tmp, pstr);
rem_dest->gid.raw[15] = (unsigned char)strtoll(tmp, NULL, 16);
pstr += term - pstr + 4;
term = strpbrk(pstr, ":");
memcpy(tmp, pstr, term - pstr);
tmp[term - pstr] = 0;
rem_dest->srqn = (unsigned)strtoul(tmp, NULL, 16); /*SRQN*/
}
return 0;
}
/******************************************************************************
*
******************************************************************************/
static int rdma_write_keys(struct pingpong_dest *my_dest,
struct perftest_comm *comm)
{
struct ibv_send_wr wr;
struct ibv_send_wr *bad_wr;
struct ibv_sge list;
struct ibv_wc wc;
int ne;
#ifdef HAVE_ENDIAN
int i;
struct pingpong_dest m_my_dest;
m_my_dest.lid = htobe32(my_dest->lid);
m_my_dest.out_reads = htobe32(my_dest->out_reads);
m_my_dest.qpn = htobe32(my_dest->qpn);
m_my_dest.psn = htobe32(my_dest->psn);
m_my_dest.rkey = htobe32(my_dest->rkey);
m_my_dest.srqn = htobe32(my_dest->srqn);
m_my_dest.gid_index = htobe32(my_dest->gid_index);
m_my_dest.vaddr = htobe64(my_dest->vaddr);
for(i=0; i<16; i++) {
m_my_dest.gid.raw[i] = my_dest->gid.raw[i];
}
memcpy(comm->rdma_ctx->buf[0], &m_my_dest, sizeof(struct pingpong_dest));
#else
memcpy(comm->rdma_ctx->buf[0], &my_dest, sizeof(struct pingpong_dest));
#endif
list.addr = (uintptr_t)comm->rdma_ctx->buf[0];
list.length = sizeof(struct pingpong_dest);
list.lkey = comm->rdma_ctx->mr[0]->lkey;
wr.wr_id = SYNC_SPEC_ID;
wr.sg_list = &list;
wr.num_sge = 1;
wr.opcode = IBV_WR_SEND;
wr.send_flags = IBV_SEND_SIGNALED;
wr.next = NULL;
if (ibv_post_send(comm->rdma_ctx->qp[0],&wr,&bad_wr)) {
fprintf(stderr, "Function ibv_post_send failed\n");
return 1;
}
do {
ne = ibv_poll_cq(comm->rdma_ctx->send_cq, 1,&wc);
} while (ne == 0);
if (wc.status || wc.opcode != IBV_WC_SEND || wc.wr_id != SYNC_SPEC_ID) {
fprintf(stderr, " Bad wc status %d\n",(int)wc.status);
return 1;
}
return 0;
}
/******************************************************************************
*
******************************************************************************/
static int rdma_read_keys(struct pingpong_dest *rem_dest,
struct perftest_comm *comm)
{
#ifdef HAVE_ENDIAN
struct pingpong_dest a_rem_dest;
#endif
struct ibv_wc wc;
int ne;
do {
ne = ibv_poll_cq(comm->rdma_ctx->recv_cq,1,&wc);
} while (ne == 0);
if (wc.status || !(wc.opcode & IBV_WC_RECV) || wc.wr_id != SYNC_SPEC_ID) {
fprintf(stderr, "Bad wc status -- %d -- %d \n",(int)wc.status,(int)wc.wr_id);
return 1;
}
#ifdef HAVE_ENDIAN
memcpy(&a_rem_dest,comm->rdma_ctx->buf[0],sizeof(struct pingpong_dest));
rem_dest->lid = ntohl(a_rem_dest.lid);
rem_dest->out_reads = ntohl(a_rem_dest.out_reads);
rem_dest->qpn = ntohl(a_rem_dest.qpn);
rem_dest->psn = ntohl(a_rem_dest.psn);
rem_dest->rkey = ntohl(a_rem_dest.rkey);
rem_dest->vaddr = be64toh(a_rem_dest.vaddr);
memcpy(rem_dest->gid.raw, &(a_rem_dest.gid), 16*sizeof(uint8_t));
#else
memcpy(&rem_dest,comm->rdma_ctx->buf[0],sizeof(struct pingpong_dest));
#endif
if (post_one_recv_wqe(comm->rdma_ctx)) {
fprintf(stderr, "Couldn't post send \n");
return 1;
}
return 0;
}
#ifdef HAVE_GID_ATTR
enum who_is_better {LEFT_IS_BETTER, EQUAL, RIGHT_IS_BETTER};
struct roce_version_sorted_enum {
enum ibv_exp_roce_gid_type type;
int rate;
};
/* This struct defines which RoCE version is more important for default usage */
struct roce_version_sorted_enum roce_versions_sorted [] = {
{IBV_EXP_IB_ROCE_V1_GID_TYPE, 1},
{IBV_EXP_ROCE_V2_GID_TYPE, 2},
{IBV_EXP_ROCE_V1_5_GID_TYPE, 3}
};
int find_roce_version_rate (int roce_ver)
{
int i;
int arr_len = GET_ARRAY_SIZE(roce_versions_sorted);
for (i = 0; i < arr_len; i++) {
if (roce_versions_sorted[i].type == roce_ver)
return roce_versions_sorted[i].rate;
}
return -1;
}
/* RoCE V1.5 > V2 > V1
* other RoCE versions will be ignored until added to roce_versions_sorted array */
static int check_better_roce_version (int roce_ver, int roce_ver_rival)
{
int roce_ver_rate = find_roce_version_rate(roce_ver);
int roce_ver_rate_rival = find_roce_version_rate(roce_ver_rival);
if (roce_ver_rate < roce_ver_rate_rival)
return RIGHT_IS_BETTER;
else if (roce_ver_rate > roce_ver_rate_rival)
return LEFT_IS_BETTER;
else
return EQUAL;
}
#endif
static int get_best_gid_index (struct pingpong_context *ctx,
struct perftest_parameters *user_param,
struct ibv_port_attr *attr, int port)
{
int gid_index = 0, i;
union ibv_gid temp_gid, temp_gid_rival;
int is_ipv4, is_ipv4_rival;
for (i = 1; i < attr->gid_tbl_len; i++) {
if (ibv_query_gid(ctx->context, port, gid_index, &temp_gid)) {
return -1;
}
if (ibv_query_gid(ctx->context, port, i, &temp_gid_rival)) {
return -1;
}
is_ipv4 = ipv6_addr_v4mapped((struct in6_addr *)temp_gid.raw);
is_ipv4_rival = ipv6_addr_v4mapped((struct in6_addr *)temp_gid_rival.raw);
if (is_ipv4_rival && !is_ipv4 && !user_param->ipv6)
gid_index = i;
else if (!is_ipv4_rival && is_ipv4 && user_param->ipv6)
gid_index = i;
#ifdef HAVE_GID_ATTR
else {
int roce_version, roce_version_rival;
struct ibv_exp_gid_attr gid_attr;
gid_attr.comp_mask = IBV_EXP_QUERY_GID_ATTR_TYPE;
if (ibv_exp_query_gid_attr(ctx->context, port, gid_index, &gid_attr))
continue;
roce_version = gid_attr.type;
if (ibv_exp_query_gid_attr(ctx->context, port, i, &gid_attr))
continue;
roce_version_rival = gid_attr.type;
if (check_better_roce_version(roce_version, roce_version_rival) == RIGHT_IS_BETTER)
gid_index = i;
}
#endif
}
return gid_index;
}
/******************************************************************************
*
******************************************************************************/
static int ethernet_client_connect(struct perftest_comm *comm)
{
struct addrinfo *res, *t;
struct addrinfo hints;
char *service;
int sockfd = -1;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
if (check_add_port(&service,comm->rdma_params->port,comm->rdma_params->servername,&hints,&res)) {
fprintf(stderr, "Problem in resolving basic address and port\n");
return 1;
}
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",comm->rdma_params->servername,comm->rdma_params->port);
return 1;
}
comm->rdma_params->sockfd = sockfd;
return 0;
}
/******************************************************************************
*
******************************************************************************/
static int ethernet_server_connect(struct perftest_comm *comm)
{
struct addrinfo *res, *t;
struct addrinfo hints;
char *service;
int n;
int sockfd = -1, connfd;
memset(&hints, 0, sizeof hints);
hints.ai_flags = AI_PASSIVE;
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
if (check_add_port(&service,comm->rdma_params->port,NULL,&hints,&res)) {
fprintf(stderr, "Problem in resolving basic address and port\n");
return 1;
}
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", comm->rdma_params->port);
return 1;
}
listen(sockfd, 1);
connfd = accept(sockfd, NULL, 0);
if (connfd < 0) {
perror("server accept");
fprintf(stderr, "accept() failed\n");
close(sockfd);
return 1;
}
close(sockfd);
comm->rdma_params->sockfd = connfd;
return 0;
}
/******************************************************************************
*
******************************************************************************/
int set_up_connection(struct pingpong_context *ctx,
struct perftest_parameters *user_param,
struct pingpong_dest *my_dest)
{
int num_of_qps = user_param->num_of_qps;
int num_of_qps_per_port = user_param->num_of_qps / 2;
int i;
union ibv_gid temp_gid;
union ibv_gid temp_gid2;
struct ibv_port_attr attr;
srand48(getpid() * time(NULL));
/*in xrc with bidirectional,
there are send qps and recv qps. the actual number of send/recv qps
is num_of_qps / 2.
*/
if ( (user_param->connection_type == DC || user_param->use_xrc) && (user_param->duplex || user_param->tst == LAT)) {
num_of_qps /= 2;
num_of_qps_per_port = num_of_qps / 2;
}
if (user_param->gid_index != -1) {
if (ibv_query_port(ctx->context, user_param->ib_port, &attr))
return 0;
if (user_param->use_gid_user) {
if (ibv_query_gid(ctx->context, user_param->ib_port, user_param->gid_index, &temp_gid))
return -1;
} else {
user_param->gid_index = get_best_gid_index(ctx, user_param, &attr, user_param->ib_port);
if (user_param->gid_index < 0)
return -1;
if (ibv_query_gid(ctx->context, user_param->ib_port, user_param->gid_index, &temp_gid))
return -1;
}
}
if (user_param->dualport == ON) {
if (user_param->gid_index2 != -1) {
if (ibv_query_port(ctx->context, user_param->ib_port2, &attr))
return 0;
if (user_param->use_gid_user) {
if (ibv_query_gid(ctx->context, user_param->ib_port2, user_param->gid_index, &temp_gid2))
return -1;
} else {
user_param->gid_index2 = get_best_gid_index(ctx, user_param, &attr, user_param->ib_port2);
if (user_param->gid_index2 < 0)
return -1;
if (ibv_query_gid(ctx->context, user_param->ib_port2, user_param->gid_index2, &temp_gid2))
return -1;
}
}
}
for (i = 0; i < user_param->num_of_qps; i++) {
if (user_param->dualport == ON) {
/*first half of qps are for ib_port and second half are for ib_port2
in xrc with bidirectional, the first half of qps are xrc_send qps and
the second half are xrc_recv qps. the first half of the send/recv qps
are for ib_port1 and the second half are for ib_port2
*/
if (i % num_of_qps < num_of_qps_per_port) {
my_dest[i].lid = ctx_get_local_lid(ctx->context,user_param->ib_port);
my_dest[i].gid_index = user_param->gid_index;
} else {
my_dest[i].lid = ctx_get_local_lid(ctx->context,user_param->ib_port2);
my_dest[i].gid_index = user_param->gid_index2;
}
/*single-port case*/
} else {
my_dest[i].lid = ctx_get_local_lid(ctx->context,user_param->ib_port);
my_dest[i].gid_index = user_param->gid_index;
}
my_dest[i].qpn = ctx->qp[i]->qp_num;
my_dest[i].psn = lrand48() & 0xffffff;
my_dest[i].rkey = ctx->mr[i]->rkey;
/* Each qp gives his receive buffer address.*/
my_dest[i].out_reads = user_param->out_reads;
if (user_param->mr_per_qp)
my_dest[i].vaddr = (uintptr_t)ctx->buf[i] + BUFF_SIZE(ctx->size,ctx->cycle_buffer);
else
my_dest[i].vaddr = (uintptr_t)ctx->buf[0] + (user_param->num_of_qps + i)*BUFF_SIZE(ctx->size,ctx->cycle_buffer);
if (user_param->dualport==ON) {
if (i % num_of_qps < num_of_qps_per_port)
memcpy(my_dest[i].gid.raw,temp_gid.raw ,16);
else
memcpy(my_dest[i].gid.raw,temp_gid2.raw ,16);
} else {
memcpy(my_dest[i].gid.raw,temp_gid.raw ,16);
}
/*
We do not fail test upon lid above RoCE.
if ( (user_param->gid_index < 0) || ((user_param->gid_index2 < 0) && (user_param->dualport == ON)) ){
if (!my_dest[i].lid) {
fprintf(stderr," Local lid 0x0 detected. Is an SM running? \n");
return -1;
}
}
*/
}
#ifdef HAVE_XRCD
if (user_param->use_xrc) {
for (i=0; i < user_param->num_of_qps; i++) {
if (ibv_get_srq_num(ctx->srq,&(my_dest[i].srqn))) {
fprintf(stderr, "Couldn't get SRQ number\n");
return 1;
}
}
}
#endif
#ifdef HAVE_DC
if(user_param->machine == SERVER || user_param->duplex || user_param->tst == LAT) {
if (user_param->connection_type == DC) {
for (i=0; i < user_param->num_of_qps; i++) {
if (ibv_get_srq_num(ctx->srq, &(my_dest[i].srqn))) {
fprintf(stderr, "Couldn't get SRQ number\n");
return 1;
}
}
}
}
#endif
return 0;
}
/******************************************************************************
*
******************************************************************************/
int rdma_client_connect(struct pingpong_context *ctx,struct perftest_parameters *user_param)
{
char *service;
int temp,num_of_retry= NUM_OF_RETRIES;
struct sockaddr_in sin;
struct addrinfo *res;
struct rdma_cm_event *event;
struct rdma_conn_param conn_param;
struct addrinfo hints;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
if (check_add_port(&service,user_param->port,user_param->servername,&hints,&res)) {
fprintf(stderr, "Problem in resolving basic address and port\n");
return FAILURE;
}
if (res->ai_family != PF_INET) {
return FAILURE;
}
memcpy(&sin, res->ai_addr, sizeof(sin));
sin.sin_port = htons((unsigned short)user_param->port);
while (1) {
if (num_of_retry == 0) {
fprintf(stderr, "Received %d times ADDR_ERROR\n",NUM_OF_RETRIES);
return FAILURE;
}
if (rdma_resolve_addr(ctx->cm_id, NULL,(struct sockaddr *)&sin,2000)) {
fprintf(stderr, "rdma_resolve_addr failed\n");
return FAILURE;
}
if (rdma_get_cm_event(ctx->cm_channel,&event)) {
fprintf(stderr, "rdma_get_cm_events failed\n");
return FAILURE;
}
if (event->event == RDMA_CM_EVENT_ADDR_ERROR) {
num_of_retry--;
rdma_ack_cm_event(event);
continue;
}
if (event->event != RDMA_CM_EVENT_ADDR_RESOLVED) {
fprintf(stderr, "unexpected CM event %d\n",event->event);
rdma_ack_cm_event(event);
return FAILURE;
}
rdma_ack_cm_event(event);
break;
}
if (user_param->tos != DEF_TOS) {
if (rdma_set_option(ctx->cm_id,RDMA_OPTION_ID,RDMA_OPTION_ID_TOS,&user_param->tos,sizeof(uint8_t))) {
fprintf(stderr, " Set TOS option failed: %d\n",event->event);
return FAILURE;
}
}
while (1) {
if (num_of_retry <= 0) {
fprintf(stderr, "Received %d times ADDR_ERROR - aborting\n",NUM_OF_RETRIES);
return FAILURE;
}
if (rdma_resolve_route(ctx->cm_id,2000)) {
fprintf(stderr, "rdma_resolve_route failed\n");
return FAILURE;
}
if (rdma_get_cm_event(ctx->cm_channel,&event)) {
fprintf(stderr, "rdma_get_cm_events failed\n");
return FAILURE;
}
if (event->event == RDMA_CM_EVENT_ROUTE_ERROR) {
num_of_retry--;
rdma_ack_cm_event(event);
continue;
}
if (event->event != RDMA_CM_EVENT_ROUTE_RESOLVED) {
fprintf(stderr, "unexpected CM event %d\n",event->event);
rdma_ack_cm_event(event);
return FAILURE;
}
rdma_ack_cm_event(event);
break;
}
ctx->context = ctx->cm_id->verbs;
temp = user_param->work_rdma_cm;
user_param->work_rdma_cm = ON;
if (ctx_init(ctx, user_param)) {
fprintf(stderr," Unable to create the resources needed by comm struct\n");
return FAILURE;
}
memset(&conn_param, 0, sizeof conn_param);
if (user_param->verb == READ || user_param->verb == ATOMIC) {
conn_param.responder_resources = user_param->out_reads;
conn_param.initiator_depth = user_param->out_reads;
}
user_param->work_rdma_cm = temp;
conn_param.retry_count = user_param->retry_count;
conn_param.rnr_retry_count = 7;
if (user_param->work_rdma_cm == OFF) {
if (post_one_recv_wqe(ctx)) {
fprintf(stderr, "Couldn't post send \n");
return 1;
}
}
if (rdma_connect(ctx->cm_id,&conn_param)) {
fprintf(stderr, "Function rdma_connect failed\n");
return FAILURE;
}
if (rdma_get_cm_event(ctx->cm_channel,&event)) {
fprintf(stderr, "rdma_get_cm_events failed\n");
return FAILURE;
}
if (event->event != RDMA_CM_EVENT_ESTABLISHED) {
fprintf(stderr, "Unexpected CM event bl blka %d\n", event->event);
rdma_ack_cm_event(event);
return FAILURE;
}