-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathgpu_direct_rdma_access.c
1548 lines (1316 loc) · 58.6 KB
/
gpu_direct_rdma_access.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) 2019 Mellanox Technologies, Inc. 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.
*/
#if HAVE_CONFIG_H
# include <config.h>
#endif /* HAVE_CONFIG_H */
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.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 <time.h>
#include <rdma/rdma_cma.h>
#include <infiniband/mlx5dv.h>
#include "khash.h"
#include "ibv_helper.h"
#include "gpu_direct_rdma_access.h"
int debug = 0;
int debug_fast_path = 0;
#define DEBUG_LOG if (debug) printf
#define DEBUG_LOG_FAST_PATH if (debug_fast_path) printf
#define FDEBUG_LOG if (debug) fprintf
#define FDEBUG_LOG_FAST_PATH if (debug_fast_path) fprintf
#define CQ_DEPTH 640
#define SEND_Q_DEPTH 640
#define DC_KEY 0xffeeddcc /*this is defined for both sides: client and server*/
#define COMP_ARRAY_SIZE 16
#define TC_PRIO 3
#define WR_ID_FLUSH_MARKER UINT64_MAX
#define mmin(a, b) a < b ? a : b
KHASH_TYPE(kh_ib_ah, struct ibv_ah_attr, struct ibv_ah*);
enum wr_id_flags {
WR_ID_FLAGS_ACTIVE = 1 << 0
};
struct wr_id_reported {
uint64_t wr_id;
uint16_t num_wrs;
uint16_t flags; /* enum wr_id_flags */
};
#ifdef PRINT_LATENCY
struct wr_latency {
uint64_t wr_start_ts;
uint64_t wr_complete_ts;
uint64_t completion_ts;
uint64_t read_comp_ts;
};
#endif /*PRINT_LATENCY*/
struct rdma_device {
struct rdma_event_channel *cm_channel;
struct rdma_cm_id *cm_id;
struct ibv_context *context;
struct ibv_pd *pd;
#ifdef PRINT_LATENCY
struct ibv_cq_ex *cq;
#else
struct ibv_cq *cq;
#endif
struct ibv_srq *srq; /* for DCT (client) only, for DCI (server) this is NULL */
struct ibv_qp *qp;
struct ibv_qp_ex *qpex; /* DCI (server) only */
struct mlx5dv_qp_ex *mqpex; /* DCI (server) only */
/* Address handler (port info) relateed fields */
int ib_port;
int is_global;
int gidx;
union ibv_gid gid;
uint16_t lid;
enum ibv_mtu mtu;
struct wr_id_reported app_wr_id[SEND_Q_DEPTH];
int app_wr_id_idx;
int qp_available_wr;
int rdma_buff_cnt;
/* AH hash */
khash_t(kh_ib_ah) ah_hash;
#ifdef PRINT_LATENCY
uint64_t hca_core_clock_kHz;
struct wr_latency latency[SEND_Q_DEPTH];
uint64_t measure_index;
uint64_t wr_complete_latency_sum; /*from wr_start_ts*/
uint64_t completion_latency_sum; /*from wr_start_ts*/
uint64_t read_comp_latency_sum; /*from completion_ts*/
uint64_t min_wr_complete_latency;
uint64_t min_completion_latency;
uint64_t min_read_comp_latency;
uint64_t max_wr_complete_latency;
uint64_t max_completion_latency;
uint64_t max_read_comp_latency;
#endif /*PRINT_LATENCY*/
};
struct rdma_buffer {
/* Buffer Related fields */
void *buf_addr; //uint64_t addr;
size_t buf_size; //uint32_t size;
/* MR Related fields */
struct ibv_mr *mr;
uint32_t rkey;
/* Linked rdma_device */
struct rdma_device *rdma_dev;
};
struct rdma_exec_params {
struct rdma_device *device;
uint64_t wr_id;
unsigned long rem_buf_rkey;
unsigned long long rem_buf_addr;
uint32_t rem_buf_size;
struct ibv_ah *ah;
unsigned long rem_dctn; /*QP number from DCT (client)*/
uint32_t local_buf_mr_lkey;
void *local_buf_addr;
struct iovec *local_buf_iovec;
int local_buf_iovcnt;
uint32_t flags; /*enum rdma_task_attr_flags*/
};
static inline
int is_server(struct rdma_device *device)
{
return device->srq == NULL;
}
/* use both gid + lid data for key generarion (lid - ib based, gid - RoCE) */
static inline
khint32_t kh_ib_ah_hash_func(struct ibv_ah_attr attr)
{
return kh_int64_hash_func(attr.grh.dgid.global.subnet_prefix ^
attr.grh.dgid.global.interface_id ^
attr.dlid);
}
static inline
int kh_ib_ah_hash_equal(struct ibv_ah_attr a, struct ibv_ah_attr b)
{
return !memcmp(&a, &b, sizeof(a));
}
KHASH_IMPL(kh_ib_ah, struct ibv_ah_attr, struct ibv_ah*, 1,
kh_ib_ah_hash_func, kh_ib_ah_hash_equal)
//============================================================================================
static struct ibv_context *open_ib_device_by_name(const char *ib_dev_name)
{
struct ibv_device **dev_list;
struct ibv_device *ib_dev;
struct ibv_context *context = NULL;
/****************************************************************************************************
* In the next block we are checking if given IB device name matches one of devices in the list.
* The result of this block is ig_dev - initialized pointer to the relevant struct ibv_device
****************************************************************************************************/
dev_list = ibv_get_device_list(NULL);
if (!dev_list) {
perror("Failed to get IB devices list");
return NULL;
}
DEBUG_LOG ("Given device name \"%s\"\n", ib_dev_name);
int i;
for (i = 0; dev_list[i]; ++i) {
char *dev_name_from_list = (char*)ibv_get_device_name(dev_list[i]);
DEBUG_LOG ("Device %d name \"%s\"\n", i, dev_name_from_list);
if (!strcmp(dev_name_from_list, ib_dev_name)) /*if found*/
break;
}
ib_dev = dev_list[i];
if (!ib_dev) {
fprintf(stderr, "IB device %s not found\n", ib_dev_name);
goto clean_device_list;
}
/****************************************************************************************************/
DEBUG_LOG ("ibv_open_device(ib_dev = %p)\n", ib_dev);
context = ibv_open_device(ib_dev);
if (!context) {
fprintf(stderr, "Couldn't get context for %s\n", ib_dev_name);
goto clean_device_list;
}
DEBUG_LOG("created ib context %p\n", context);
/* We are now done with device list, we can free it */
clean_device_list:
ibv_free_device_list(dev_list); /*dev_list is not NULL*/
return context;
}
//============================================================================================
static struct ibv_context *open_ib_device_by_addr(struct rdma_device *rdma_dev, struct sockaddr *addr)
{
int ret;
uint16_t sin_port;
char str[INET_ADDRSTRLEN];
rdma_dev->cm_channel = rdma_create_event_channel();
if (!rdma_dev->cm_channel) {
DEBUG_LOG("rdma_create_event_channel() failure");
return NULL;
}
ret = rdma_create_id(rdma_dev->cm_channel, &rdma_dev->cm_id, rdma_dev, RDMA_PS_UDP);
if (ret) {
DEBUG_LOG("rdma_create_id() failure");
goto out1;
}
ret = rdma_bind_addr(rdma_dev->cm_id, addr);
if (ret) {
DEBUG_LOG("rdma_bind_addr() failure");
goto out2;
}
if (addr->sa_family == AF_INET) {
sin_port = ((struct sockaddr_in *)addr)->sin_port;
inet_ntop(AF_INET, &(((struct sockaddr_in *)addr)->sin_addr), str, INET_ADDRSTRLEN);
}
else {
sin_port = ((struct sockaddr_in6 *)addr)->sin6_port;
inet_ntop(AF_INET6, &(((struct sockaddr_in6 *)addr)->sin6_addr), str, INET_ADDRSTRLEN);
}
if (rdma_dev->cm_id->verbs == NULL) {
DEBUG_LOG("Failed to bind to an RDMA device, exiting... <%s, %d>\n", str, ntohs(sin_port));
goto out2;
}
rdma_dev->ib_port = rdma_dev->cm_id->port_num;
rdma_dev->gidx = -1;
DEBUG_LOG("bound to RDMA device name:%s, port:%d, based on '%s'\n",
rdma_dev->cm_id->verbs->device->name, rdma_dev->cm_id->port_num, str);
return rdma_dev->cm_id->verbs;
out2:
rdma_destroy_id(rdma_dev->cm_id);
out1:
rdma_destroy_event_channel(rdma_dev->cm_channel);
return NULL;
}
static void close_ib_device(struct rdma_device *rdma_dev)
{
int ret;
if (rdma_dev->cm_channel) {
/* if we are using RDMA_CM then we just referance the cma's ibv_context */
rdma_dev->context = NULL;
if (rdma_dev->cm_id) {
DEBUG_LOG("rdma_destroy_id(%p)\n", rdma_dev->cm_id);
ret = rdma_destroy_id(rdma_dev->cm_id);
if (ret) {
fprintf(stderr, "failure in rdma_destroy_id(), error %d\n", ret);
}
}
DEBUG_LOG("rdma_destroy_event_channel(%p)\n", rdma_dev->cm_id);
rdma_destroy_event_channel(rdma_dev->cm_channel);
}
if (rdma_dev->context) {
DEBUG_LOG("ibv_close_device(%p)\n", rdma_dev->context);
ret = ibv_close_device(rdma_dev->context);
if (ret) {
fprintf(stderr, "failure in ibv_close_device(), error %d\n", ret);
}
}
}
/***********************************************************************************
* Fill portinfo structure, get lid and gid from portinfo
* Return value: 0 - success, 1 - error
****************************************************************************************/
static int rdma_set_lid_gid_from_port_info(struct rdma_device *rdma_dev)
{
struct ibv_port_attr portinfo;
int ret_val;
ret_val = ibv_query_port(rdma_dev->context, rdma_dev->ib_port, &portinfo);
if (ret_val) {
fprintf(stderr, "Couldn't get port info\n");
return 1;
}
rdma_dev->mtu = portinfo.active_mtu;
rdma_dev->lid = portinfo.lid;
if ((portinfo.link_layer != IBV_LINK_LAYER_ETHERNET) && (!portinfo.lid)) {
fprintf(stderr, "Couldn't get local LID\n");
return 1;
}
if (rdma_dev->cm_id && portinfo.link_layer == IBV_LINK_LAYER_ETHERNET) {
rdma_dev->gidx = ibv_find_sgid_type(rdma_dev->context, rdma_dev->ib_port,
IBV_GID_TYPE_ROCE_V2, rdma_dev->cm_id->route.addr.src_addr.sa_family);
}
if (rdma_dev->gidx < 0) {
if (portinfo.link_layer == IBV_LINK_LAYER_ETHERNET) {
fprintf(stderr, "Wrong GID index (%d) for ETHERNET port\n", rdma_dev->gidx);
return 1;
} else {
memset(&(rdma_dev->gid), 0, sizeof rdma_dev->gid);
}
} else /* rdma_dev->gidx >= 0*/ {
ret_val = ibv_query_gid(rdma_dev->context, rdma_dev->ib_port, rdma_dev->gidx, &(rdma_dev->gid));
if (ret_val) {
fprintf(stderr, "can't read GID of index %d, error code %d\n", rdma_dev->gidx, ret_val);
return 1;
}
DEBUG_LOG ("my gid idx: %d, value:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x\n", rdma_dev->gidx,
rdma_dev->gid.raw[0], rdma_dev->gid.raw[1], rdma_dev->gid.raw[2], rdma_dev->gid.raw[3],
rdma_dev->gid.raw[4], rdma_dev->gid.raw[5], rdma_dev->gid.raw[6], rdma_dev->gid.raw[7],
rdma_dev->gid.raw[8], rdma_dev->gid.raw[9], rdma_dev->gid.raw[10], rdma_dev->gid.raw[11],
rdma_dev->gid.raw[12], rdma_dev->gid.raw[13], rdma_dev->gid.raw[14], rdma_dev->gid.raw[15] );
}
rdma_dev->is_global = (rdma_dev->gid.global.interface_id != 0);
DEBUG_LOG ("link_layer:%s, lid:%d, is_global:%d, MTU:%d Bytes\n",
(portinfo.link_layer == IBV_LINK_LAYER_ETHERNET ? "ETH" : "IB"),
rdma_dev->lid, rdma_dev->is_global, (256<<(rdma_dev->mtu - 1)));
return 0;
}
/****************************************************************************************
* Modify target QP state to RTR (on the client side)
* Return value: 0 - success, 1 - error
****************************************************************************************/
static int modify_target_qp_to_rtr(struct rdma_device *rdma_dev)
{
struct ibv_qp_attr qp_attr;
enum ibv_qp_attr_mask attr_mask;
memset(&qp_attr, 0, sizeof qp_attr);
qp_attr.qp_state = IBV_QPS_RTR;
qp_attr.path_mtu = rdma_dev->mtu;
qp_attr.min_rnr_timer = 16;
qp_attr.ah_attr.port_num = rdma_dev->ib_port;
if (rdma_dev->gid.global.interface_id) {
qp_attr.ah_attr.is_global = 1;
qp_attr.ah_attr.grh.hop_limit = 1;
qp_attr.ah_attr.grh.sgid_index = rdma_dev->gidx;
qp_attr.ah_attr.grh.traffic_class = TC_PRIO << 5; // <<3 for dscp2prio, <<2 for ECN bits
}
attr_mask = IBV_QP_STATE |
IBV_QP_AV |
IBV_QP_PATH_MTU |
IBV_QP_MIN_RNR_TIMER; // for DCT
DEBUG_LOG("ibv_modify_qp(qp = %p, qp_attr.qp_state = %d, attr_mask = 0x%x)\n",
rdma_dev->qp, qp_attr.qp_state, attr_mask);
if (ibv_modify_qp(rdma_dev->qp, &qp_attr, attr_mask)) {
fprintf(stderr, "Failed to modify QP to RTR\n");
return 1;
}
DEBUG_LOG ("ibv_modify_qp to state %d completed: qp_num = 0x%x\n", qp_attr.qp_state, rdma_dev->qp->qp_num);
return 0;
}
/****************************************************************************************
* Modify source QP state to RTR and then to RTS (on the server side)
* Return value: 0 - success, 1 - error
****************************************************************************************/
static int modify_source_qp_to_rtr_and_rts(struct rdma_device *rdma_dev)
{
struct ibv_qp_attr qp_attr;
enum ibv_qp_attr_mask attr_mask;
memset(&qp_attr, 0, sizeof qp_attr);
/* - - - - - - - Modify QP to RTR - - - - - - - */
qp_attr.qp_state = IBV_QPS_RTR;
qp_attr.path_mtu = rdma_dev->mtu;
qp_attr.ah_attr.port_num = rdma_dev->ib_port;
if (rdma_dev->gid.global.interface_id) {
qp_attr.ah_attr.is_global = 1;
qp_attr.ah_attr.grh.hop_limit = 1;
qp_attr.ah_attr.grh.sgid_index = rdma_dev->gidx;
qp_attr.ah_attr.grh.traffic_class = TC_PRIO << 5; // <<3 for dscp2prio, <<2 for ECN bits
}
attr_mask = IBV_QP_STATE |
IBV_QP_AV |
IBV_QP_PATH_MTU ;
DEBUG_LOG("ibv_modify_qp(qp = %p, qp_attr.qp_state = %d, attr_mask = 0x%x)\n",
rdma_dev->qp, qp_attr.qp_state, attr_mask);
if (ibv_modify_qp(rdma_dev->qp, &qp_attr, attr_mask)) {
fprintf(stderr, "Failed to modify QP to RTR\n");
return 1;
}
DEBUG_LOG ("ibv_modify_qp to state %d completed: qp_num = 0x%lx\n", qp_attr.qp_state, rdma_dev->qp->qp_num);
/* - - - - - - - Modify QP to RTS - - - - - - - */
qp_attr.qp_state = IBV_QPS_RTS;
qp_attr.timeout = 16;
qp_attr.retry_cnt = 7;
qp_attr.rnr_retry = 7;
//qp_attr.sq_psn = 0;
qp_attr.max_rd_atomic = 1;
attr_mask = IBV_QP_STATE |
IBV_QP_TIMEOUT |
IBV_QP_RETRY_CNT |
IBV_QP_RNR_RETRY |
IBV_QP_SQ_PSN |
IBV_QP_MAX_QP_RD_ATOMIC ;
DEBUG_LOG("ibv_modify_qp(qp = %p, qp_attr.qp_state = %d, attr_mask = 0x%x)\n",
rdma_dev->qp, qp_attr.qp_state, attr_mask);
if (ibv_modify_qp(rdma_dev->qp, &qp_attr, attr_mask)) {
fprintf(stderr, "Failed to modify QP to RTS\n");
return 1;
}
DEBUG_LOG ("ibv_modify_qp to state %d completed: qp_num = 0x%lx\n", qp_attr.qp_state, rdma_dev->qp->qp_num);
return 0;
}
static int destroy_qp(struct ibv_qp *qp)
{
int ret;
if (qp) {
DEBUG_LOG("ibv_destroy_qp(%p)\n", qp);
ret = ibv_destroy_qp(qp);
if (ret) {
fprintf(stderr, "Couldn't destroy QP: error %d\n", ret);
}
}
return ret;
}
static int modify_source_qp_rst2rts(struct rdma_device *rdma_dev)
{
int ret_val;
/* - - - - - - - - - - Modify QP to INIT - - - - - - - - - - - - - */
struct ibv_qp_attr qp_attr = {
.qp_state = IBV_QPS_INIT,
.pkey_index = 0,
.port_num = rdma_dev->ib_port,
.qp_access_flags = IBV_ACCESS_LOCAL_WRITE
};
enum ibv_qp_attr_mask attr_mask = IBV_QP_STATE |
IBV_QP_PKEY_INDEX |
IBV_QP_PORT |
0 /*IBV_QP_ACCESS_FLAGS*/; /*we must zero this bit for DCI QP*/
DEBUG_LOG("ibv_modify_qp(qp = %p, qp_attr.qp_state = %d, attr_mask = 0x%x)\n",
rdma_dev->qp, qp_attr.qp_state, attr_mask);
ret_val = ibv_modify_qp(rdma_dev->qp, &qp_attr, attr_mask);
if (ret_val) {
fprintf(stderr, "Failed to modify QP to INIT, error %d\n", ret_val);
return 1;
}
DEBUG_LOG("ibv_modify_qp to state %d completed: qp_num = 0x%lx\n", qp_attr.qp_state, rdma_dev->qp->qp_num);
/* - - - - - - - - - - - - - Modify QP to RTS - - - - - - - - - - - - */
ret_val = modify_source_qp_to_rtr_and_rts(rdma_dev);
if (ret_val) {
return 1;
}
rdma_dev->qpex->wr_flags = IBV_SEND_SIGNALED;
return 0;
}
//============================================================================================
struct rdma_device *rdma_open_device_client(struct sockaddr *addr)
{
struct rdma_device *rdma_dev;
int ret_val;
rdma_dev = calloc(1, sizeof *rdma_dev);
if (!rdma_dev) {
fprintf(stderr, "rdma_device memory allocation failed\n");
return NULL;
}
/****************************************************************************************************
* In the next function we let rdma_cm find a IB device that matches the IP address of a the local netdev,
* if yes, we return a pointer to that ib context
* The result of this function is ib_dev - initialized pointer to the relevant struct ibv_device
****************************************************************************************************/
rdma_dev->context = open_ib_device_by_addr(rdma_dev, addr);
if (!rdma_dev->context){
goto clean_rdma_dev;
}
ret_val = rdma_set_lid_gid_from_port_info(rdma_dev);
if (ret_val) {
goto clean_device;
}
/****************************************************************************************************/
DEBUG_LOG ("ibv_alloc_pd(ibv_context = %p)\n", rdma_dev->context);
rdma_dev->pd = ibv_alloc_pd(rdma_dev->context);
if (!rdma_dev->pd) {
fprintf(stderr, "Couldn't allocate PD\n");
goto clean_device;
}
DEBUG_LOG("created pd %p\n", rdma_dev->pd);
/* ********************************** Create CQ ********************************** */
#ifdef PRINT_LATENCY
struct ibv_cq_init_attr_ex cq_attr_ex;
memset(&cq_attr_ex, 0, sizeof(cq_attr_ex));
cq_attr_ex.cqe = CQ_DEPTH;
cq_attr_ex.cq_context = rdma_dev;
cq_attr_ex.channel = NULL;
cq_attr_ex.comp_vector = 0;
cq_attr_ex.wc_flags = IBV_WC_EX_WITH_COMPLETION_TIMESTAMP;
DEBUG_LOG ("ibv_create_cq_ex(rdma_dev->context = %p, &cq_attr_ex)\n", rdma_dev->context);
rdma_dev->cq = ibv_create_cq_ex(rdma_dev->context, &cq_attr_ex);
#else /*PRINT_LATENCY*/
DEBUG_LOG ("ibv_create_cq(%p, %d, NULL, NULL, 0)\n", rdma_dev->context, CQ_DEPTH);
rdma_dev->cq = ibv_create_cq(rdma_dev->context, CQ_DEPTH, NULL, NULL /*comp. events channel*/, 0);
#endif /*PRINT_LATENCY*/
if (!rdma_dev->cq) {
fprintf(stderr, "Couldn't create CQ\n");
goto clean_pd;
}
DEBUG_LOG("created cq %p\n", rdma_dev->cq);
/* ********************************** Create SRQ ********************************** */
struct ibv_srq_init_attr srq_attr;
memset(&srq_attr, 0, sizeof(srq_attr));
srq_attr.attr.max_wr = 2;
srq_attr.attr.max_sge = 1;
DEBUG_LOG ("ibv_create_srq(%p, %d, NULL, NULL, 0)\n", rdma_dev->context, CQ_DEPTH);
rdma_dev->srq = ibv_create_srq(rdma_dev->pd, &srq_attr);
if (!rdma_dev->srq) {
fprintf(stderr, "ibv_create_srq failed\n");
goto clean_cq;
}
DEBUG_LOG("created srq %p\n", rdma_dev->srq);
/* ********************************** Create QP ********************************** */
struct ibv_qp_init_attr_ex attr_ex;
struct mlx5dv_qp_init_attr attr_dv;
memset(&attr_ex, 0, sizeof(attr_ex));
memset(&attr_dv, 0, sizeof(attr_dv));
attr_ex.qp_type = IBV_QPT_DRIVER;
#ifdef PRINT_LATENCY
attr_ex.send_cq = ibv_cq_ex_to_cq(rdma_dev->cq);
attr_ex.recv_cq = ibv_cq_ex_to_cq(rdma_dev->cq);
#else /*PRINT_LATENCY*/
attr_ex.send_cq = rdma_dev->cq;
attr_ex.recv_cq = rdma_dev->cq;
#endif /*PRINT_LATENCY*/
attr_ex.comp_mask |= IBV_QP_INIT_ATTR_PD;
attr_ex.pd = rdma_dev->pd;
attr_ex.srq = rdma_dev->srq; /* Should use SRQ for client only (DCT) */
/* create DCT */
attr_dv.comp_mask |= MLX5DV_QP_INIT_ATTR_MASK_DC;
attr_dv.dc_init_attr.dc_type = MLX5DV_DCTYPE_DCT;
attr_dv.dc_init_attr.dct_access_key = DC_KEY;
DEBUG_LOG ("mlx5dv_create_qp(%p)\n", rdma_dev->context);
rdma_dev->qp = mlx5dv_create_qp(rdma_dev->context, &attr_ex, &attr_dv);
if (!rdma_dev->qp) {
fprintf(stderr, "Couldn't create QP\n");
goto clean_srq;
}
DEBUG_LOG ("mlx5dv_create_qp %p completed: qp_num = 0x%lx\n", rdma_dev->qp, rdma_dev->qp->qp_num);
/* - - - - - - - Modify QP to INIT - - - - - - - */
struct ibv_qp_attr qp_attr = {
.qp_state = IBV_QPS_INIT,
.pkey_index = 0,
.port_num = rdma_dev->ib_port,
.qp_access_flags = IBV_ACCESS_REMOTE_READ | IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE
};
enum ibv_qp_attr_mask attr_mask = IBV_QP_STATE |
IBV_QP_PKEY_INDEX |
IBV_QP_PORT |
IBV_QP_ACCESS_FLAGS;
DEBUG_LOG ("ibv_modify_qp(qp = %p, qp_attr.qp_state = %d, attr_mask = 0x%x)\n",
rdma_dev->qp, qp_attr.qp_state, attr_mask);
ret_val = ibv_modify_qp(rdma_dev->qp, &qp_attr, attr_mask);
if (ret_val) {
fprintf(stderr, "Failed to modify QP to INIT, error %d\n", ret_val);
goto clean_qp;
}
DEBUG_LOG ("ibv_modify_qp to state %d completed: qp_num = 0x%lx\n", qp_attr.qp_state, rdma_dev->qp->qp_num);
ret_val = modify_target_qp_to_rtr(rdma_dev);
if (ret_val) {
goto clean_qp;
}
DEBUG_LOG("init AH cache\n");
kh_init_inplace(kh_ib_ah, &rdma_dev->ah_hash);
#ifdef PRINT_LATENCY
struct ibv_device_attr_ex device_attr_ex = {};
//struct ibv_query_device_ex_input query_device_ex_input = {
// .comp_masc = ...
//}
ret_val = ibv_query_device_ex(rdma_dev->context, /*struct ibv_query_device_ex_input*/NULL, &device_attr_ex);
if (ret_val) {
fprintf(stderr, "ibv_query_device_ex failed\n");
goto clean_qp;
}
if (!device_attr_ex.hca_core_clock) {
fprintf(stderr, "hca_core_clock = 0\n");
goto clean_qp;
}
rdma_dev->hca_core_clock_kHz = device_attr_ex.hca_core_clock;
DEBUG_LOG("hca_core_clock = %d kHz\n", rdma_dev->hca_core_clock_kHz);
#endif /*PRINT_LATENCY*/
return rdma_dev;
clean_qp:
destroy_qp(rdma_dev->qp);
clean_srq:
if (rdma_dev->srq) {
ibv_destroy_srq(rdma_dev->srq);
}
clean_cq:
if (rdma_dev->cq) {
#ifdef PRINT_LATENCY
ibv_destroy_cq(ibv_cq_ex_to_cq(rdma_dev->cq));
#else /*PRINT_LATENCY*/
ibv_destroy_cq(rdma_dev->cq);
#endif /*PRINT_LATENCY*/
}
clean_pd:
if (rdma_dev->pd) {
ibv_dealloc_pd(rdma_dev->pd);
}
clean_device:
close_ib_device(rdma_dev);
clean_rdma_dev:
free(rdma_dev);
return NULL;
}
//============================================================================================
struct rdma_device *rdma_open_device_server(struct sockaddr *addr)
{
struct rdma_device *rdma_dev;
int ret_val;
rdma_dev = calloc(1, sizeof *rdma_dev);
if (!rdma_dev) {
fprintf(stderr, "rdma_device memory allocation failed\n");
return NULL;
}
/****************************************************************************************************
* In the next function we let rdma_cm find a IB device that matches the IP address of a the local netdev,
* if yes, we return a pointer to that ib context
* The result of this function is ib_dev - initialized pointer to the relevant struct ibv_device
****************************************************************************************************/
rdma_dev->context = open_ib_device_by_addr(rdma_dev, addr);
if (!rdma_dev->context){
goto clean_rdma_dev;
}
ret_val = rdma_set_lid_gid_from_port_info(rdma_dev);
if (ret_val) {
goto clean_device;
}
/****************************************************************************************************/
DEBUG_LOG ("ibv_alloc_pd(ibv_context = %p)\n", rdma_dev->context);
rdma_dev->pd = ibv_alloc_pd(rdma_dev->context);
if (!rdma_dev->pd) {
fprintf(stderr, "Couldn't allocate PD\n");
goto clean_device;
}
DEBUG_LOG("created pd %p\n", rdma_dev->pd);
/* We don't create completion events channel (ibv_create_comp_channel), we prefer working in polling mode */
/* ********************************** Create CQ ********************************** */
#ifdef PRINT_LATENCY
struct ibv_cq_init_attr_ex cq_attr_ex;
memset(&cq_attr_ex, 0, sizeof(cq_attr_ex));
cq_attr_ex.cqe = CQ_DEPTH;
cq_attr_ex.cq_context = rdma_dev;
cq_attr_ex.channel = NULL;
cq_attr_ex.comp_vector = 0;
cq_attr_ex.wc_flags = IBV_WC_EX_WITH_COMPLETION_TIMESTAMP;
DEBUG_LOG ("ibv_create_cq_ex(rdma_dev->context = %p, &cq_attr_ex)\n", rdma_dev->context);
rdma_dev->cq = ibv_create_cq_ex(rdma_dev->context, &cq_attr_ex);
#else /*PRINT_LATENCY*/
DEBUG_LOG ("ibv_create_cq(%p, %d, NULL, NULL, 0)\n", rdma_dev->context, CQ_DEPTH);
rdma_dev->cq = ibv_create_cq(rdma_dev->context, CQ_DEPTH, NULL, NULL /*comp. events channel*/, 0);
#endif /*PRINT_LATENCY*/
if (!rdma_dev->cq) {
fprintf(stderr, "Couldn't create CQ\n");
goto clean_pd;
}
DEBUG_LOG("created cq %p\n", rdma_dev->cq);
/* We don't create SRQ for DCI (server) side */
/* ********************************** Create QP ********************************** */
struct ibv_qp_init_attr_ex attr_ex;
struct mlx5dv_qp_init_attr attr_dv;
memset(&attr_ex, 0, sizeof(attr_ex));
memset(&attr_dv, 0, sizeof(attr_dv));
attr_ex.qp_type = IBV_QPT_DRIVER;
#ifdef PRINT_LATENCY
attr_ex.send_cq = ibv_cq_ex_to_cq(rdma_dev->cq);
attr_ex.recv_cq = ibv_cq_ex_to_cq(rdma_dev->cq);
#else /*PRINT_LATENCY*/
attr_ex.send_cq = rdma_dev->cq;
attr_ex.recv_cq = rdma_dev->cq;
#endif /*PRINT_LATENCY*/
attr_ex.comp_mask |= IBV_QP_INIT_ATTR_PD;
attr_ex.pd = rdma_dev->pd;
/* create DCI */
attr_dv.comp_mask |= MLX5DV_QP_INIT_ATTR_MASK_DC;
attr_dv.dc_init_attr.dc_type = MLX5DV_DCTYPE_DCI;
attr_ex.cap.max_send_wr = SEND_Q_DEPTH;
attr_ex.cap.max_send_sge = MAX_SEND_SGE;
rdma_dev->qp_available_wr = SEND_Q_DEPTH;
attr_ex.comp_mask |= IBV_QP_INIT_ATTR_SEND_OPS_FLAGS;
attr_ex.send_ops_flags = IBV_QP_EX_WITH_RDMA_WRITE | IBV_QP_EX_WITH_RDMA_READ;
attr_dv.comp_mask |= MLX5DV_QP_INIT_ATTR_MASK_QP_CREATE_FLAGS;
attr_dv.create_flags |= MLX5DV_QP_CREATE_DISABLE_SCATTER_TO_CQE; /*driver doesnt support scatter2cqe data-path on DCI yet*/
DEBUG_LOG ("mlx5dv_create_qp(%p)\n", rdma_dev->context);
rdma_dev->qp = mlx5dv_create_qp(rdma_dev->context, &attr_ex, &attr_dv);
DEBUG_LOG ("mlx5dv_create_qp %p completed: qp_num = 0x%lx\n", rdma_dev->qp, rdma_dev->qp->qp_num);
if (!rdma_dev->qp) {
fprintf(stderr, "Couldn't create QP\n");
goto clean_cq;
}
rdma_dev->qpex = ibv_qp_to_qp_ex(rdma_dev->qp);
if (!rdma_dev->qpex) {
fprintf(stderr, "Couldn't create QPEX\n");
goto clean_qp;
}
rdma_dev->mqpex = mlx5dv_qp_ex_from_ibv_qp_ex(rdma_dev->qpex);
if (!rdma_dev->mqpex) {
fprintf(stderr, "Couldn't create MQPEX\n");
goto clean_qp;
}
ret_val = modify_source_qp_rst2rts(rdma_dev);
if (ret_val) {
goto clean_qp;
}
DEBUG_LOG("init AH cache\n");
kh_init_inplace(kh_ib_ah, &rdma_dev->ah_hash);
#ifdef PRINT_LATENCY
struct ibv_device_attr_ex device_attr_ex = {};
//struct ibv_query_device_ex_input query_device_ex_input = {
// .comp_masc = ...
//}
ret_val = ibv_query_device_ex(rdma_dev->context, /*struct ibv_query_device_ex_input*/NULL, &device_attr_ex);
if (ret_val) {
fprintf(stderr, "ibv_query_device_ex failed\n");
goto clean_qp;
}
if (!device_attr_ex.hca_core_clock) {
fprintf(stderr, "hca_core_clock = 0\n");
goto clean_qp;
}
rdma_dev->hca_core_clock_kHz = device_attr_ex.hca_core_clock;
DEBUG_LOG("hca_core_clock = %d kHz\n", rdma_dev->hca_core_clock_kHz);
rdma_dev->min_wr_complete_latency = 0x8FFFFFFFFFFFFFFF;
rdma_dev->min_completion_latency = 0x8FFFFFFFFFFFFFFF;
rdma_dev->min_read_comp_latency = 0x8FFFFFFFFFFFFFFF;
#endif /*PRINT_LATENCY*/
return rdma_dev;
clean_qp:
destroy_qp(rdma_dev->qp);
clean_cq:
if (rdma_dev->cq) {
#ifdef PRINT_LATENCY
ibv_destroy_cq(ibv_cq_ex_to_cq(rdma_dev->cq));
#else /*PRINT_LATENCY*/
ibv_destroy_cq(rdma_dev->cq);
#endif /*PRINT_LATENCY*/
}
clean_pd:
if (rdma_dev->pd) {
ibv_dealloc_pd(rdma_dev->pd);
}
clean_device:
close_ib_device(rdma_dev);
clean_rdma_dev:
free(rdma_dev);
return NULL;
}
//===========================================================================================
static
int rdma_exec_task(struct rdma_exec_params *exec_params)
{
int ret_val;
int required_wr = (exec_params->local_buf_iovcnt) ? (exec_params->local_buf_iovcnt + MAX_SEND_SGE - 1) / MAX_SEND_SGE : 1;
if (required_wr > exec_params->device->qp_available_wr) {
fprintf(stderr, "Required WR number %d is greater than available in QP WRs %d\n",
required_wr, exec_params->device->qp_available_wr);
return 1;
}
void (*ibv_wr_rdma_rw_post)(struct ibv_qp_ex *qp, uint32_t rkey, uint64_t remote_addr) = (exec_params->flags & RDMA_TASK_ATTR_RDMA_READ)
? ibv_wr_rdma_read // client wants to send data to the server
: ibv_wr_rdma_write; // client wants to receive data from the server
/* RDMA Read/Write for DCI connect, this will create cqe->ts_start */
DEBUG_LOG_FAST_PATH("RDMA Read/Write: ibv_wr_start: qpex = %p\n", exec_params->device->qpex);
ibv_wr_start(exec_params->device->qpex);
#ifdef PRINT_LATENCY
struct ibv_values_ex ts_values = {
.comp_mask = IBV_VALUES_MASK_RAW_CLOCK,
.raw_clock = {} /*struct timespec*/
};
ret_val = ibv_query_rt_values_ex(exec_params->device->context, &ts_values);
if (ret_val) {
fprintf(stderr, "ibv_query_rt_values_ex failed after ibv_wr_start call\n");
return 1;
}
#endif /*PRINT_LATENCY*/
// The following code should be atomic operation
int wr_id_idx = exec_params->device->app_wr_id_idx++;
if (exec_params->device->app_wr_id_idx >= SEND_Q_DEPTH) {
exec_params->device->app_wr_id_idx = 0;
}
// end of atomic operation
#ifdef PRINT_LATENCY
exec_params->device->latency[wr_id_idx].wr_start_ts = ts_values.raw_clock.tv_nsec; /*the value in hca clocks*/
#endif /*PRINT_LATENCY*/
// update internal wr_id DB
exec_params->device->qp_available_wr -= required_wr;
exec_params->device->app_wr_id[wr_id_idx].num_wrs = required_wr;
exec_params->device->app_wr_id[wr_id_idx].wr_id = exec_params->wr_id;
exec_params->device->app_wr_id[wr_id_idx].flags = WR_ID_FLAGS_ACTIVE;
exec_params->device->qpex->wr_id = (uint64_t)wr_id_idx;
if (exec_params->local_buf_iovcnt) {
int i, start_i = 0;
struct ibv_sge sg_list[MAX_SEND_SGE];
uint64_t curr_rem_addr = (uint64_t)exec_params->rem_buf_addr;
int num_sges_to_send = exec_params->local_buf_iovcnt;
while (num_sges_to_send > 0) {
int curr_iovcnt = mmin(MAX_SEND_SGE, num_sges_to_send);
exec_params->device->qpex->wr_flags = num_sges_to_send > MAX_SEND_SGE ? 0 : IBV_SEND_SIGNALED;
DEBUG_LOG_FAST_PATH("RDMA Read/Write: ibv_wr_rdma_%s: wr_id=0x%llx, qpex=%p, rkey=0x%lx, remote_buf=0x%llx\n",
exec_params->flags & RDMA_TASK_ATTR_RDMA_READ ? "read" : "write",
(long long unsigned int)exec_params->wr_id, exec_params->device->qpex, exec_params->rem_buf_rkey, (long long unsigned int)curr_rem_addr);
ibv_wr_rdma_rw_post(exec_params->device->qpex, exec_params->rem_buf_rkey, curr_rem_addr);
for (i = 0; i < curr_iovcnt; i++) {
sg_list[i].addr = (uint64_t)exec_params->local_buf_iovec[start_i + i].iov_base;
sg_list[i].length = (uint32_t)exec_params->local_buf_iovec[start_i + i].iov_len;
sg_list[i].lkey = exec_params->local_buf_mr_lkey;
curr_rem_addr += sg_list[i].length;
}
DEBUG_LOG_FAST_PATH("RDMA Read/Write: ibv_wr_set_sge_list(qpex=%p, num_sge=%lu, sg_list=%p), start_i=%d, num_sges_to_send=%d, sg[0].length=%u\n",
exec_params->device->qpex, (size_t)curr_iovcnt, (void*)sg_list, start_i, num_sges_to_send, sg_list[0].length);
ibv_wr_set_sge_list(exec_params->device->qpex, (size_t)curr_iovcnt, sg_list);
num_sges_to_send -= curr_iovcnt;
start_i += curr_iovcnt;
DEBUG_LOG_FAST_PATH("RDMA Read/Write: mlx5dv_wr_set_dc_addr: mqpex=%p, ah=%p, rem_dctn=0x%06lx\n",
exec_params->device->mqpex, exec_params->ah, exec_params->rem_dctn);
mlx5dv_wr_set_dc_addr(exec_params->device->mqpex, exec_params->ah, exec_params->rem_dctn, DC_KEY);
}
} else {
exec_params->device->qpex->wr_flags = IBV_SEND_SIGNALED;
DEBUG_LOG_FAST_PATH("RDMA Read/Write: ibv_wr_rdma_%s: wr_id=0x%llx, qpex=%p, rkey=0x%lx, remote_buf=0x%llx\n",
exec_params->flags & RDMA_TASK_ATTR_RDMA_READ ? "read" : "write",
(long long unsigned int)exec_params->wr_id, exec_params->device->qpex, exec_params->rem_buf_rkey, (unsigned long long)exec_params->rem_buf_addr);
ibv_wr_rdma_rw_post(exec_params->device->qpex, exec_params->rem_buf_rkey, exec_params->rem_buf_addr);
DEBUG_LOG_FAST_PATH("RDMA Read/Write: ibv_wr_set_sge: qpex=%p, lkey=0x%x, local_buf=0x%llx, size=%u\n",
exec_params->device->qpex, exec_params->local_buf_mr_lkey,
(unsigned long long)exec_params->local_buf_addr, exec_params->rem_buf_size);
ibv_wr_set_sge(exec_params->device->qpex, exec_params->local_buf_mr_lkey, (uintptr_t)exec_params->local_buf_addr, exec_params->rem_buf_size);
DEBUG_LOG_FAST_PATH("RDMA Read/Write: mlx5dv_wr_set_dc_addr: mqpex=%p, ah=%p, rem_dctn=0x%06lx\n",
exec_params->device->mqpex, exec_params->ah, exec_params->rem_dctn);
mlx5dv_wr_set_dc_addr(exec_params->device->mqpex, exec_params->ah, exec_params->rem_dctn, DC_KEY);
}
/* ring DB */
DEBUG_LOG_FAST_PATH("ibv_wr_complete: qpex=%p, required_wr=%d\n", exec_params->device->qpex, required_wr);
ret_val = ibv_wr_complete(exec_params->device->qpex);
if (ret_val) {
DEBUG_LOG_FAST_PATH("FAILURE: ibv_wr_complete (error=%d\n", ret_val);
return ret_val;
}
#ifdef PRINT_LATENCY
ret_val = ibv_query_rt_values_ex(exec_params->device->context, &ts_values);
if (ret_val) {
fprintf(stderr, "ibv_query_rt_values_ex failed after ibv_wr_start call\n");