-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathresource_lat.c
1808 lines (1572 loc) · 40.4 KB
/
resource_lat.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
/*
* rdma_mr lat -- simple memory registration latency measuring tool
*
* Copyright (c) 2017, Mellanox Technologies. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef VERSION
#define VERSION "0.1"
#endif
#define _GNU_SOURCE
#include <sys/param.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <getopt.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <time.h>
#include <limits.h>
#include <unistd.h>
#include <hugetlbfs.h>
#include <sys/time.h>
#include <malloc.h>
#include <inttypes.h>
#include <sys/resource.h>
#include <sys/mman.h>
#include <sys/capability.h>
#include <sys/types.h>
#include <infiniband/verbs.h>
#include <infiniband/mlx5dv.h>
#include <rdma/rdma_cma.h>
#include "options.h"
#include "ioctl.h"
#include "ib_user_ioctl_cmds.h"
#include "ts.h"
static pthread_barrier_t run_barrier;
struct rdma_connection {
struct rdma_cm_id *cm_id; /* client id or server child id */
};
struct cm_ctx {
struct rdma_event_channel *event_channel;
pthread_t event_thread;
struct rdma_cm_event *event;
};
struct thread_ctx {
uint8_t *buf;
union {
struct ibv_mr **mr_list;
struct ibv_pd **pd_list;
struct ibv_context **uctx_list;
struct ibv_mw **mw_list;
struct ibv_xrcd **xrcd_list;
struct ibv_ah **ah_list;
} u;
struct ibv_comp_channel *cq_channel;
struct ibv_cq **cq_list;
struct mlx5dv_devx_obj **devx_cq_list;
uint32_t *devx_cq_handle;
struct ibv_qp **qp_list;
struct ibv_wq **wq_list;
struct ibv_rwq_ind_table **rq_ind_tbl_list;
struct ibv_flow **flow_list;
struct rdma_connection *connections;
struct time_stats alloc_stats;
struct time_stats free_stats;
long long issued;
};
struct run_ctx {
struct ibv_device *device;
struct ibv_context *context;
struct ibv_pd *pd;
struct cm_ctx cm_ctx;
union ibv_gid local_gid;
uint64_t size;
uint64_t mr_size; /* mr_size and size are same if all MR
* register the same pages.
* Otherwise size = num_mrs * mr_size.
*/
uint64_t min_mr_size; /*
* this is the min_mr size to test
* when testing with different sizes.
* starting from PAGE_SIZE to
* mr_size.
*/
uint64_t max_mr_size; /* Do MR tests from min_mr_size to
* max_mr_size with doubling MR size
* on each iteration.
*/
uint64_t page_size;
uint64_t align;
uint64_t rlimit;
uint64_t rlimit_set;
int access_flags;
int huge;
int mmap;
int odp;
int lock_memory;
int dedicated_pages; /* Each MR gets dedicated pages */
int read_fault;
int count; /* resource/operation count */
int iter; /* iteration - how many times to operate */
int threads;
int write_pattern;
int drop_ipc_lock_cap;
int ioctl_destroy;
int segfault;
int wait;
int report_interval;
int devx;
char pattern;
char *ibdev_name;
char *resource_type;
uint64_t step_size; /* every new MR will be at this
* step_size from base address.
*/
};
struct thread_start_info {
pthread_t thread;
struct run_ctx *ctx;
struct thread_ctx t_ctx;
int created;
};
#define HUGE_PAGE_KPATH "/proc/sys/vm/nr_hugepages"
static void usage(const char *argv0)
{
printf("Usage:\n");
printf("%s\n", argv0);
printf("Options:\n");
printf(" -d, --ibdev=<ibdev> use IB device <dev> (default first device found)\n");
printf(" -s --size=<size> size of mr in bytes (default 4096)\n");
printf(" -l --align=<align_size> align memory allocation to this size\n");
printf(" -c --count=<count> number of resources (i.e. MR, PD etc) to alloc/register\n");
printf(" -r --rlimit=<bytes> memory resource hard limit in bytes\n");
printf(" -i --iter=iteration how many times to iterarate the operation\n");
printf(" -u --huge use huge pages\n");
printf(" -o --odp use ODP registration\n");
printf(" -t --threads=<threads> Number of parallel threads\n");
printf(" -a --assign use dedicated pages for each MR\n");
printf(" -A --all use all MR sizes starting from PAGE_SIZE to size\n");
printf(" -m --mmap use mmap for allocation for huge pages\n");
printf(" -I --interval seconds between periodic reports\n");
printf(" -L --lock lock memory before registration\n");
printf(" -f --fault read page fault memory before registration\n");
printf(" -D --drop_ipc_lock drop ipc lock capability before registration\n");
printf(" -O --ioctl destroy resource using ioctl method\n");
printf(" -R --resource resource type (pd, mr, uctx, mw, cq, qp, xrcd, wq, rqit, ah, flow, cmid)\n");
printf(" -F --segfault seg fault after registration\n");
printf(" -x --devx devx resource creation mode\n");
printf(" -W --wait Wait for user signal before resource creation and destroy\n");
printf(" -h display this help message\n");
printf(" -v display program version\n");
}
void version(const char *argv0)
{
printf("%s %s\n", argv0, VERSION);
}
static void parse_options(struct run_ctx *ctx, int argc, char **argv)
{
static struct option long_options[] = {
{ .name = "ibdev", .has_arg = 1, .val = 'd' },
{ .name = "size", .has_arg = 1, .val = 's' },
{ .name = "align", .has_arg = 1, .val = 'l' },
{ .name = "iter", .has_arg = 1, .val = 'i' },
{ .name = "pattern", .has_arg = 1, .val = 'p' },
{ .name = "threads", .has_arg = 1, .val = 't' },
{ .name = "rlimit", .has_arg = 1, .val = 'r' },
{ .name = "count", .has_arg = 1, .val = 'c' },
{ .name = "interval", .has_arg = 1, .val = 'I' },
{ .name = "resource", .has_arg = 1, .val = 'R' },
{ .name = "huge", .has_arg = 0, .val = 'u' },
{ .name = "odp", .has_arg = 0, .val = 'o' },
{ .name = "assign", .has_arg = 0, .val = 'a' },
{ .name = "all", .has_arg = 0, .val = 'A' },
{ .name = "lock", .has_arg = 0, .val = 'L' },
{ .name = "fault", .has_arg = 0, .val = 'f' },
{ .name = "drop_ipc", .has_arg = 0, .val = 'D' },
{ .name = "ioctl", .has_arg = 0, .val = 'O' },
{ .name = "segfault", .has_arg = 0, .val = 'F' },
{ .name = "devx", .has_arg = 0, .val = 'x' },
{ .name = "wait", .has_arg = 0, .val = 'W' },
{ .name = "mmap", .has_arg = 0, .val = 'm' },
{ .name = NULL }
};
int opt;
if (argc < 2) {
usage(argv[0]);
exit(1);
}
while ((opt = getopt_long(argc, argv, "hv:d:I:R:p:r:s:t:i:c:l:uoLfDOFWmaAx",
long_options, NULL)) != -1) {
switch (opt) {
case 'v':
version(argv[0]);
exit(0);
case 'h':
usage(argv[0]);
exit(0);
case 'a':
ctx->dedicated_pages = 1;
break;
case 'd':
ctx->ibdev_name = malloc(strlen(optarg));
if (!ctx->ibdev_name) {
fprintf(stderr, "Couldn't allocate mem.\n");
exit(1);
}
strcpy(ctx->ibdev_name, optarg);
break;
case 'R':
ctx->resource_type = malloc(strlen(optarg));
if (!ctx->resource_type) {
fprintf(stderr, "Couldn't allocate mem.\n");
exit(1);
}
strcpy(ctx->resource_type, optarg);
break;
case 's':
ctx->size = parse_size(optarg);
break;
case 'A':
ctx->min_mr_size = sysconf(_SC_PAGESIZE);
break;
case 'I':
ctx->report_interval = parse_int(optarg);
break;
case 'l':
ctx->align = parse_size(optarg);
break;
case 'm':
ctx->mmap = 1;
break;
case 'c':
ctx->count = parse_int(optarg);
break;
case 'i':
ctx->iter = parse_int(optarg);
break;
case 'r':
ctx->rlimit = parse_size(optarg);
ctx->rlimit_set = 1;
break;
case 'p':
ctx->write_pattern = 1;
ctx->pattern = *((char*)optarg);
break;
case 't':
ctx->threads = parse_int(optarg);
break;
case 'u':
ctx->huge = 1;
break;
case 'o':
ctx->odp = 1;
break;
case 'L':
ctx->lock_memory = 1;
break;
case 'f':
ctx->read_fault = 1;
break;
case 'D':
ctx->drop_ipc_lock_cap = 1;
break;
case 'O':
ctx->ioctl_destroy = 1;
break;
case 'F':
ctx->segfault = 1;
break;
case 'x':
ctx->devx = 1;
break;
case 'W':
ctx->wait = 1;
break;
}
}
}
static void normalize_sizes(struct run_ctx *ctx)
{
if ((strcmp(ctx->resource_type, "mr") == 0) ||
(strcmp(ctx->resource_type, "mw") == 0)) {
ctx->mr_size = ctx->size;
if (ctx->dedicated_pages) {
ctx->step_size = ctx->size;
ctx->size = ctx->size * ctx->count;
} else {
ctx->step_size = 0;
}
}
}
static int config_hugetlb_pages(uint64_t num_hpages)
{
char hpages_str[128] = {0};
size_t s;
int err = 0;
int fd;
fd = open(HUGE_PAGE_KPATH, O_RDWR, 0);
if (fd < 0)
return fd;
sprintf(hpages_str, "%ld", num_hpages);
s = write(fd, hpages_str, strlen(hpages_str));
if (s != strlen(hpages_str))
err = -EINVAL;
close(fd);
return err;
}
static void reset_huge_tlb_pages(uint64_t num_pages)
{
config_hugetlb_pages(num_pages);
}
static int config_hugetlb_kernel(const struct run_ctx *ctx)
{
long hpage_size = gethugepagesize();
uint64_t num_hpages;
if (hpage_size == 0)
return -EINVAL;
num_hpages = (ctx->size * ctx->threads) / hpage_size;
if (num_hpages == 0)
num_hpages = 1;
return config_hugetlb_pages(num_hpages);
}
static int alloc_hugepage_mem(const struct run_ctx *ctx, struct thread_ctx *t_ctx)
{
int mmap_flags = MAP_ANONYMOUS | MAP_PRIVATE;
if (ctx->mmap) {
mmap_flags |= MAP_HUGETLB;
t_ctx->buf = mmap(0, ctx->size, PROT_WRITE | PROT_READ,
mmap_flags, 0, 0);
if (t_ctx->buf == MAP_FAILED) {
perror("mmap");
return -ENOMEM;
}
} else {
t_ctx->buf = get_hugepage_region(ctx->size, GHR_STRICT | GHR_COLOR);
if (!t_ctx->buf) {
perror("mmap");
return -ENOMEM;
}
}
return 0;
}
static int alloc_mem(const struct run_ctx *ctx, struct thread_ctx *t_ctx)
{
int err = 0;
if (ctx->huge) {
err = config_hugetlb_kernel(ctx);
if (err) {
printf("fail to configure hugetlb\n");
err = -EINVAL;
return err;
}
err = alloc_hugepage_mem(ctx, t_ctx);
if (err)
return err;
} else {
t_ctx->buf = memalign(ctx->align, ctx->size);
if (!t_ctx->buf) {
fprintf(stderr, "Couldn't allocate work buf.\n");
err = -ENOMEM;
return err;
}
}
return err;
}
static void free_mem(const struct run_ctx *ctx, struct thread_ctx *t_ctx)
{
if (ctx->huge) {
if (t_ctx->buf)
free_hugepage_region(t_ctx->buf);
reset_huge_tlb_pages(0);
} else {
if (t_ctx->buf)
free(t_ctx->buf);
}
}
static int set_rlimit(struct run_ctx *ctx)
{
struct rlimit rlim, after_rlim;
int ret;
ret = getrlimit(RLIMIT_MEMLOCK, &rlim);
if (ret)
return ret;
if (ctx->rlimit_set) {
rlim.rlim_cur = ctx->rlimit;
rlim.rlim_max = ctx->rlimit;
ret = setrlimit(RLIMIT_MEMLOCK, &rlim);
if (ret)
return ret;
ret = getrlimit(RLIMIT_MEMLOCK, &after_rlim);
if (after_rlim.rlim_max != ctx->rlimit) {
fprintf(stderr, "Set rlimit %ld, Got %ld\n",
ctx->rlimit, after_rlim.rlim_max);
return -EINVAL;
}
}
return ret;
}
static int lock_mem(const struct run_ctx *ctx, struct thread_ctx *t_ctx)
{
int ret;
if (ctx->lock_memory)
ret = mlock(t_ctx->buf, ctx->size);
return ret;
}
static void read_fault(const struct run_ctx *ctx, struct thread_ctx *t_ctx)
{
uint8_t dummy_buf[4096];
uint8_t *read_ptr = t_ctx->buf;
uint64_t read_size = ctx->size;
if (!ctx->read_fault)
return;
while (read_size) {
memcpy(&dummy_buf[0], read_ptr,
MIN(sizeof(dummy_buf), read_size));
read_ptr += MIN(sizeof(dummy_buf), read_size);
read_size -= MIN(sizeof(dummy_buf), read_size);
}
}
static int drop_ipc_lock_cap(void)
{
cap_value_t capList[1];
cap_t caps;
int ret;
/* Retrieve caller's current capabilities */
caps = cap_get_proc();
if (caps == NULL)
return -EINVAL;
/* Change setting of 'capability' in the effective set of 'caps'. The
* third argument, 1, is the number of items in the array 'capList'.
*/
capList[0] = CAP_IPC_LOCK;
ret = cap_set_flag(caps, CAP_EFFECTIVE, 1, capList, CAP_CLEAR);
if (ret)
goto err;
ret = cap_set_proc(caps);
if (ret)
goto err;
ret = cap_set_flag(caps, CAP_PERMITTED, 1, capList, CAP_CLEAR);
if (ret)
goto err;
ret = cap_set_proc(caps);
err:
cap_free(caps);
return ret;
}
static int setup_ipc_lock_cap(struct run_ctx *ctx)
{
int ret = 0;
if (ctx->drop_ipc_lock_cap)
ret = drop_ipc_lock_cap();
return ret;
}
enum resource_id {
RTYPE_PD,
RTYPE_MR,
RTYPE_UCTX,
RTYPE_MW,
RTYPE_CQ,
RTYPE_QP,
RTYPE_XRCD,
RTYPE_WQ,
RTYPE_RQ_IND_TBL,
RTYPE_AH,
RTYPE_FLOW,
RTYPE_CM_ID,
RTYPE_MAX
};
struct resource_info {
enum resource_id id;
const char *name;
};
static const struct resource_info resource_types[] = {
{ RTYPE_PD, "pd", },
{ RTYPE_MR, "mr", },
{ RTYPE_UCTX, "uctx", },
{ RTYPE_MW, "mw", },
{ RTYPE_QP, "qp", },
{ RTYPE_CQ, "cq", },
{ RTYPE_XRCD, "xrcd", },
{ RTYPE_WQ, "wq", },
{ RTYPE_RQ_IND_TBL, "rqit", },
{ RTYPE_AH, "ah", },
{ RTYPE_FLOW, "flow", },
{ RTYPE_CM_ID, "cmid", },
{ -1, NULL, },
};
static int check_resource_type(char *type)
{
int err = -EINVAL;
int i = 0;
while (resource_types[i].name) {
if (strcmp(type, resource_types[i].name)) {
i++;
continue;
}
err = resource_types[i].id;
break;
}
return err;
}
static int alloc_resource_holder(const struct run_ctx *ctx,
struct thread_ctx *t)
{
int type;
type = check_resource_type(ctx->resource_type);
if (type == RTYPE_CQ || type == RTYPE_QP ||
type == RTYPE_WQ || type == RTYPE_RQ_IND_TBL ||
type == RTYPE_FLOW || type == RTYPE_CM_ID) {
if (ctx->devx) {
t->devx_cq_list = calloc(ctx->count, sizeof(struct mlx5dv_devx_obj *));
if (!t->devx_cq_list) {
fprintf(stderr, "Couldn't allocate devx cq list memory\n");
return -ENOMEM;
}
t->devx_cq_handle = calloc(ctx->count, sizeof(uint32_t));
} else {
t->cq_list = calloc(ctx->count, sizeof(struct ibv_cq*));
if (!t->cq_list) {
fprintf(stderr, "Couldn't allocate cq list memory\n");
return -ENOMEM;
}
}
}
if (type == RTYPE_QP || type == RTYPE_FLOW) {
t->qp_list = calloc(ctx->count, sizeof(struct ibv_qp*));
if (!t->qp_list) {
fprintf(stderr, "Couldn't allocate qp list memory\n");
return -ENOMEM;
}
if (type == RTYPE_FLOW) {
t->flow_list = calloc(ctx->count, sizeof(struct ibv_flow*));
if (!t->flow_list) {
fprintf(stderr, "Couldn't allocate flow list memory\n");
return -ENOMEM;
}
}
} else if (type == RTYPE_WQ || type == RTYPE_RQ_IND_TBL) {
t->wq_list = calloc(ctx->count, sizeof(struct ibv_wq*));
if (!t->wq_list) {
fprintf(stderr, "Couldn't allocate wq list memory\n");
return -ENOMEM;
}
if (type == RTYPE_RQ_IND_TBL) {
t->rq_ind_tbl_list =
calloc(ctx->count,
sizeof(struct ibv_rwq_ind_table*));
if (!t->rq_ind_tbl_list) {
fprintf(stderr, "Couldn't allocate rq indir tbl\n");
return -ENOMEM;
}
return 0;
}
return 0;
} else if (type == RTYPE_CM_ID) {
t->connections = calloc(ctx->count, sizeof(struct rdma_connection));
if (!t->connections) {
fprintf(stderr, "Couldn't allocate connections\n");
return -ENOMEM;
}
}
t->u.mr_list = calloc(ctx->count, sizeof(struct ibv_mr*));
if (!t->u.mr_list) {
fprintf(stderr, "Couldn't allocate list memory\n");
return -ENOMEM;
}
return 0;
}
static int alloc_uctx(const struct run_ctx *ctx, struct thread_ctx *t, int i)
{
int err = 0;
t->u.uctx_list[i] = ibv_open_device(ctx->device);
if (!t->u.uctx_list[i]) {
fprintf(stderr, "alloc pd count = %d\n", i);
err = -ENOMEM;
}
return err;
}
static int alloc_pd(const struct run_ctx *ctx, struct thread_ctx *t, int i)
{
int err = 0;
t->u.pd_list[i] = ibv_alloc_pd(ctx->context);
if (!t->u.pd_list[i]) {
fprintf(stderr, "alloc pd count = %d\n", i);
err = -ENOMEM;
}
return err;
}
static int alloc_mr(const struct run_ctx *ctx, struct thread_ctx *t, int i)
{
int err = 0;
t->u.mr_list[i] =
ibv_reg_mr(ctx->pd, t->buf + (i * ctx->step_size),
ctx->mr_size, ctx->access_flags);
if (!t->u.mr_list[i]) {
fprintf(stderr, "Registered MR count = %d\n", i);
err = -ENOMEM;
}
return err;
}
static int alloc_mw(const struct run_ctx *ctx, struct thread_ctx *t, int i)
{
int err = 0;
t->u.mw_list[i] = ibv_alloc_mw(ctx->pd, IBV_MW_TYPE_2);
if (!t->u.mw_list[i]) {
fprintf(stderr, "Registered MW count = %d\n", i);
err = -ENOMEM;
}
return err;
}
struct mlx5_ifc_cqc_bits {
uint8_t status[0x4];
uint8_t as_notify[0x1];
uint8_t initiator_src_dct[0x1];
uint8_t dbr_umem_valid[0x1];
uint8_t additional_element[0x1];
uint8_t cqe_sz[0x3];
uint8_t cc[0x1];
uint8_t reserved_at_c[0x1];
uint8_t scqe_break_moderation_en[0x1];
uint8_t oi[0x1];
uint8_t cq_period_mode[0x2];
uint8_t cqe_comp_en[0x1];
uint8_t mini_cqe_res_format[0x2];
uint8_t st[0x4];
uint8_t always_armed_cq[0x1];
uint8_t element_type[0x3];
uint8_t reserved_at_1c[0x2];
uint8_t cqe_compression_layout[0x2];
uint8_t dbr_umem_id[0x20];
uint8_t reserved_at_40[0x14];
uint8_t page_offset[0x6];
uint8_t reserved_at_5a[0x2];
uint8_t mini_cqe_res_format_3_2[0x2];
uint8_t cq_time_stamp_format[0x2];
uint8_t reserved_at_60[0x3];
uint8_t log_cq_size[0x5];
uint8_t uar_page[0x18];
uint8_t reserved_at_80[0x4];
uint8_t cq_period[0xc];
uint8_t cq_max_count[0x10];
uint8_t c_eqn_or_add_element[0x20];
uint8_t reserved_at_c0[0x3];
uint8_t log_page_size[0x5];
uint8_t reserved_at_c8[0x18];
uint8_t reserved_at_e0[0x20];
uint8_t reserved_at_100[0x8];
uint8_t last_notified_index[0x18];
uint8_t reserved_at_120[0x8];
uint8_t last_solicit_index[0x18];
uint8_t reserved_at_140[0x8];
uint8_t consumer_counter[0x18];
uint8_t reserved_at_160[0x8];
uint8_t producer_counter[0x18];
uint8_t as_notify_params[0x40];
uint8_t dbr_addr[0x40];
};
struct mlx5_ifc_create_cq_out_bits {
uint8_t status[0x8];
uint8_t reserved_at_8[0x18];
uint8_t syndrome[0x20];
uint8_t reserved_at_40[0x8];
uint8_t cqn[0x18];
uint8_t reserved_at_60[0x20];
};
struct mlx5_ifc_create_cq_in_bits {
uint8_t opcode[0x10];
uint8_t uid[0x10];
uint8_t reserved_at_20[0x10];
uint8_t op_mod[0x10];
uint8_t reserved_at_40[0x8];
uint8_t input_cqn[0x18];
uint8_t reserved_at_60[0x20];
struct mlx5_ifc_cqc_bits cq_context;
uint8_t e_mtt_pointer_or_cq_umem_offset[0x40];
uint8_t cq_umem_id[0x20];
uint8_t cq_umem_valid[0x1];
uint8_t reserved_at_2e1[0x1f];
uint8_t reserved_at_300[0x580];
uint8_t pas[];
};
enum {
MLX5_CMD_OPCODE_CREATE_CQ = 0x400,
};
static int devx_alloc_cq(const struct run_ctx *ctx, struct thread_ctx *t, int i)
{
struct mlx5dv_devx_umem *cq_dbr_umem;
struct mlx5dv_devx_umem *cq_umem;
struct mlx5dv_devx_uar *uar;
struct mlx5dv_devx devx = {0};
struct mlx5dv_obj obj = {0};
uint32_t *cq_dbr; /* array of 2 be32 values */
void *cq_ring;
void *cqc;
uint32_t out[DEVX_ST_SZ_DW(create_cq_out)] = {0};
uint32_t in[DEVX_ST_SZ_DW(create_cq_in)] = {0};
uint32_t eq_num;
int err;
uar = mlx5dv_devx_alloc_uar(ctx->context, MLX5DV_UAR_ALLOC_TYPE_NC);
/* Initialize CQ */
cq_ring = memalign(getpagesize(), 4096);
cq_umem = mlx5dv_devx_umem_reg(ctx->context, cq_ring,
4096, IBV_ACCESS_LOCAL_WRITE);
if (cq_umem == NULL) {
printf("Failed register CQ ring memory\n");
err = errno;
goto err_reg_ring;
}
cq_dbr = memalign(64, sizeof(uint64_t));
memset(cq_dbr, 0, sizeof(uint64_t));
cq_dbr_umem = mlx5dv_devx_umem_reg(ctx->context, cq_dbr, 64,
IBV_ACCESS_LOCAL_WRITE);
if (cq_dbr_umem == NULL) {
printf("Failed to register host CQ DBR memory\n");
err = errno;
goto err_reg_dbr;
}
/* Get an EQ number */
err = mlx5dv_devx_query_eqn(ctx->context, 0, &eq_num);
if (err)
return err;
DEVX_SET(create_cq_in, in, opcode, MLX5_CMD_OPCODE_CREATE_CQ);
DEVX_SET64(create_cq_in, in, e_mtt_pointer_or_cq_umem_offset, 0);
DEVX_SET(create_cq_in, in, cq_umem_id, cq_umem->umem_id);
DEVX_SET(create_cq_in, in, cq_umem_valid, 1);
cqc = DEVX_ADDR_OF(create_cq_in, in, cq_context);
DEVX_SET(cqc, cqc, dbr_umem_id, cq_dbr_umem->umem_id);
DEVX_SET(cqc, cqc, log_cq_size, 5);
DEVX_SET(cqc, cqc, uar_page, uar->page_id);
DEVX_SET(cqc, cqc, c_eqn_or_add_element, eq_num);
DEVX_SET64(cqc, cqc, dbr_addr, (uintptr_t)cq_dbr);
DEVX_SET64(cqc, cqc, dbr_addr, (uintptr_t)0);
t->devx_cq_list[i] =
mlx5dv_devx_obj_create(ctx->context,
in, sizeof(in), out, sizeof(out));
if (!t->devx_cq_list[i]) {
printf("fail to create devx cq errno = %d, %s\n",
errno, strerror(errno));
return errno;
}
obj.devx.in = t->devx_cq_list[i];
obj.devx.out = &devx;
err = mlx5dv_init_obj(&obj, MLX5DV_OBJ_DEVX);
if (err)
return err;
t->devx_cq_handle[i] = devx.handle;
return 0;
err_reg_dbr:
err_reg_ring:
free(cq_ring);
return -ENOMEM;
}
static int ibv_alloc_cq(const struct run_ctx *ctx, struct thread_ctx *t, int i)
{
if (!t->cq_channel) {
t->cq_channel = ibv_create_comp_channel(ctx->context);
if (!t->cq_channel) {
printf("%s fail to create cq channel\n", __func__);
return -ENOMEM;
}
}
t->cq_list[i] = ibv_create_cq(ctx->context, 4, NULL,
t->cq_channel, 0);
if (!t->cq_list[i]) {
printf("%s fail to create cq\n", __func__);
return -ENOMEM;
}
return 0;
}
static int alloc_cq(const struct run_ctx *ctx, struct thread_ctx *t, int i)
{
if (ctx->devx)
return devx_alloc_cq(ctx, t, i);
else
return ibv_alloc_cq(ctx, t, i);
}
static int alloc_qp(const struct run_ctx *ctx, struct thread_ctx *t,
int i, int type)
{
struct ibv_qp_init_attr qp_attr = { 0 };
int err = 0;
qp_attr.send_cq = qp_attr.recv_cq = t->cq_list[i];
qp_attr.cap.max_send_wr = 4;
qp_attr.cap.max_recv_wr = 4;
qp_attr.cap.max_send_sge = 1;
qp_attr.cap.max_recv_sge = 1;
qp_attr.sq_sig_all = 1;
if (type == RTYPE_QP)
qp_attr.qp_type = IBV_QPT_RC;
else
qp_attr.qp_type = IBV_QPT_RAW_PACKET;
t->qp_list[i] = ibv_create_qp(ctx->pd, &qp_attr);
if (!t->qp_list[i]) {
fprintf(stderr, "Registered QP count = %d\n", i);
err = -ENOMEM;
}
return err;
}
static int alloc_xrcd(const struct run_ctx *ctx, struct thread_ctx *t, int i)
{
struct ibv_xrcd_init_attr xrcd_attr = { 0 };
int err = 0;
xrcd_attr.oflags = O_CREAT;
xrcd_attr.fd = -1; /* we don't care for fd in resource test */
xrcd_attr.comp_mask = IBV_XRCD_INIT_ATTR_OFLAGS | IBV_XRCD_INIT_ATTR_FD;
t->u.xrcd_list[i] = ibv_open_xrcd(ctx->context, &xrcd_attr);
if (!t->u.xrcd_list[i]) {
fprintf(stderr, "Registered xrcd count = %d\n", i);
err = -ENOMEM;
}
return err;
}
static int alloc_wq(const struct run_ctx *ctx, struct thread_ctx *t, int i)
{
struct ibv_wq_init_attr wq_attr = { 0 };
int err = 0;
wq_attr.wq_context = NULL;
wq_attr.wq_type = IBV_WQT_RQ;
wq_attr.max_wr = 1;
wq_attr.max_sge = 1;
wq_attr.pd = ctx->pd;
wq_attr.cq = t->cq_list[i];
wq_attr.comp_mask = 0;
wq_attr.create_flags = IBV_WQ_INIT_ATTR_FLAGS;
t->wq_list[i] = ibv_create_wq(ctx->context, &wq_attr);
if (!t->wq_list[i]) {
fprintf(stderr, "Registered WQ count = %d\n", i);
err = -ENOMEM;
}
return err;
}
static int alloc_rq_ind_tbl(const struct run_ctx *ctx, struct thread_ctx *t, int i)
{
struct ibv_rwq_ind_table_init_attr attr = { 0 };
int err = 0;
attr.log_ind_tbl_size = 0;
attr.ind_tbl = &t->wq_list[i];
attr.comp_mask = 0;
t->rq_ind_tbl_list[i] = ibv_create_rwq_ind_table(ctx->context, &attr);
if (!t->rq_ind_tbl_list[i]) {
fprintf(stderr, "Registered table count = %d\n", i);
err = -ENOMEM;
}
return err;
}
static int alloc_ah(const struct run_ctx *ctx, struct thread_ctx *t, int i)
{
struct ibv_ah_attr ah_attr;
int err = 0;
memset(&ah_attr, 0, sizeof(ah_attr));
ah_attr.port_num = 1;
ah_attr.is_global = 1;
ah_attr.grh.dgid = ctx->local_gid;
t->u.ah_list[i] = ibv_create_ah(ctx->pd, &ah_attr);
if (!t->u.ah_list[i]) {
fprintf(stderr, "created ah count = %d\n", i);
err = -ENOMEM;
}
return err;