This repository has been archived by the owner on Sep 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
/
mocks.c
796 lines (666 loc) · 15.5 KB
/
mocks.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
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright 2020-2021, Intel Corporation */
/*
* mocks.c -- common mocks for integration tests
*/
#include <string.h>
#include <librpma.h>
#include <sys/types.h>
#include <sys/mman.h>
#include "cmocka_headers.h"
#include "conn_cfg.h"
#include "mocks.h"
struct verbs_context Verbs_context;
struct ibv_comp_channel Ibv_comp_channel; /* mock IBV completion channel */
struct ibv_cq Ibv_cq; /* mock IBV CQ */
struct ibv_mr Ibv_mr; /* mock IBV MR */
struct ibv_mr Ibv_mr_raw; /* mock IBV MR RAW */
struct ibv_mr Ibv_mr_flush; /* mock IBV MR FLUSH */
struct rdma_cm_id Cm_id; /* mock CM ID */
struct ibv_qp Ibv_qp; /* mock IBV QP */
#ifdef ON_DEMAND_PAGING_SUPPORTED
/* predefined IBV On-demand Paging caps */
struct ibv_odp_caps Ibv_odp_capable_caps = {
.general_caps = IBV_ODP_SUPPORT,
.per_transport_caps = {
IBV_ODP_SUPPORT_WRITE | IBV_ODP_SUPPORT_READ,
0,
0
},
};
#endif
/*
* ibv_query_device -- ibv_query_device() mock
*/
int
ibv_query_device(struct ibv_context *context,
struct ibv_device_attr *device_attr)
{
assert_ptr_equal(context, MOCK_VERBS);
assert_non_null(device_attr);
int ret = mock_type(int);
if (ret)
return ret;
memset(device_attr, 0, sizeof(struct ibv_device_attr));
return 0;
}
#ifdef ON_DEMAND_PAGING_SUPPORTED
/*
* ibv_query_device_ex_mock -- ibv_query_device_ex() mock
*/
int
ibv_query_device_ex_mock(struct ibv_context *context,
const struct ibv_query_device_ex_input *input,
struct ibv_device_attr_ex *attr,
size_t attr_size)
{
assert_ptr_equal(context, MOCK_VERBS);
assert_null(input);
assert_non_null(attr);
/* attr_size is provided by ibverbs - no validation needed */
struct ibv_odp_caps *caps = mock_type(struct ibv_odp_caps *);
if (caps == NULL)
return mock_type(int);
memcpy(&attr->odp_caps, caps, sizeof(struct ibv_odp_caps));
return 0;
}
#endif
/*
* ibv_req_notify_cq_mock -- ibv_req_notify_cq() mock
*/
int
ibv_req_notify_cq_mock(struct ibv_cq *cq, int solicited_only)
{
assert_ptr_equal(cq, MOCK_CQ);
assert_int_equal(solicited_only, 0);
return mock_type(int);
}
/*
* ibv_get_cq_event -- ibv_get_cq_event() mock
*/
int
ibv_get_cq_event(struct ibv_comp_channel *channel, struct ibv_cq **cq,
void **cq_context)
{
check_expected_ptr(channel);
assert_non_null(cq);
assert_non_null(cq_context);
errno = mock_type(int);
if (!errno) {
*cq = MOCK_CQ;
*cq_context = NULL;
return 0;
}
return -1;
}
/*
* ibv_ack_cq_events -- ibv_ack_cq_events() mock
*/
void
ibv_ack_cq_events(struct ibv_cq *cq, unsigned nevents)
{
check_expected_ptr(cq);
assert_int_equal(nevents, 1);
}
/*
* ibv_post_send_mock -- mock of ibv_post_send()
*/
int
ibv_post_send_mock(struct ibv_qp *qp, struct ibv_send_wr *wr,
struct ibv_send_wr **bad_wr)
{
assert_non_null(qp);
assert_non_null(wr);
assert_null(wr->next);
assert_non_null(wr->sg_list);
assert_non_null(wr->sg_list->addr);
assert_non_null(wr->wr.rdma.remote_addr);
assert_non_null(bad_wr);
check_expected_ptr(qp);
check_expected(wr->opcode);
check_expected(wr->send_flags);
check_expected(wr->wr_id);
check_expected(wr->num_sge);
check_expected(wr->sg_list->length);
check_expected(wr->wr.rdma.remote_addr);
uint64_t *pdst_addr = mock_type(uint64_t *);
uint64_t src_offset = mock_type(uint64_t);
assert_int_equal(wr->sg_list->addr, *pdst_addr + src_offset);
memcpy((void *)wr->sg_list->addr,
(void *)wr->wr.rdma.remote_addr, wr->sg_list->length);
return mock_type(int);
}
/*
* ibv_poll_cq_mock -- ibv_poll_cq() mock
*/
int
ibv_poll_cq_mock(struct ibv_cq *cq, int num_entries, struct ibv_wc *wc)
{
check_expected_ptr(cq);
assert_int_equal(num_entries, 1);
assert_non_null(wc);
int result = mock_type(int);
if (result != 1)
return result;
struct ibv_wc *wc_ret = mock_type(struct ibv_wc *);
memcpy(wc, wc_ret, sizeof(struct ibv_wc));
return 1;
}
/*
* ibv_dereg_mr -- a mock of ibv_dereg_mr()
*/
int
ibv_dereg_mr(struct ibv_mr *mr)
{
check_expected_ptr(mr);
return mock_type(int); /* errno */
}
/*
* rdma_create_id -- rdma_create_id() mock
*/
int
rdma_create_id(struct rdma_event_channel *channel,
struct rdma_cm_id **id, void *context,
enum rdma_port_space ps)
{
assert_non_null(id);
assert_null(context);
assert_int_equal(ps, RDMA_PS_TCP);
/* allocate (struct rdma_cm_id *) */
*id = mock_type(struct rdma_cm_id *);
if (*id == NULL) {
errno = mock_type(int);
return -1;
}
memset(*id, 0, sizeof(struct rdma_cm_id));
return 0;
}
/*
* rdma_destroy_id -- rdma_destroy_id() mock
*/
int
rdma_destroy_id(struct rdma_cm_id *id)
{
check_expected(id);
errno = mock_type(int);
if (errno)
return -1;
return 0;
}
/*
* ibv_alloc_pd -- ibv_alloc_pd() mock
*/
struct ibv_pd *
ibv_alloc_pd(struct ibv_context *ibv_ctx)
{
check_expected_ptr(ibv_ctx);
struct ibv_pd *pd = mock_type(struct ibv_pd *);
if (pd != NULL)
return pd;
/*
* The ibv_alloc_pd(3) manual page does not document that this function
* returns any error via errno but seemingly it is. For the usability
* sake, in librpma we try to deduce what really happened using
* the errno value.
*/
errno = mock_type(int);
return NULL;
}
/*
* ibv_dealloc_pd -- ibv_dealloc_pd() mock
*/
int
ibv_dealloc_pd(struct ibv_pd *pd)
{
check_expected_ptr(pd);
return mock_type(int);
}
/*
* ibv_create_cq -- ibv_create_cq() mock
*/
struct ibv_cq *
ibv_create_cq(struct ibv_context *context, int cqe, void *cq_context,
struct ibv_comp_channel *channel, int comp_vector)
{
int cqe_default;
(void) rpma_conn_cfg_get_cqe(rpma_conn_cfg_default(), &cqe_default);
assert_ptr_equal(context, MOCK_VERBS);
assert_int_equal(cqe, cqe_default);
assert_ptr_equal(channel, MOCK_COMP_CHANNEL);
assert_int_equal(comp_vector, 0);
struct ibv_cq *cq = mock_type(struct ibv_cq *);
if (!cq) {
errno = mock_type(int);
return NULL;
}
cq->channel = channel;
return cq;
}
/*
* ibv_destroy_cq -- ibv_destroy_cq() mock
*/
int
ibv_destroy_cq(struct ibv_cq *cq)
{
check_expected_ptr(cq);
return mock_type(int);
}
/*
* ibv_create_comp_channel -- ibv_create_comp_channel() mock
*/
struct ibv_comp_channel *
ibv_create_comp_channel(struct ibv_context *context)
{
assert_ptr_equal(context, MOCK_VERBS);
struct ibv_comp_channel *channel = mock_type(struct ibv_comp_channel *);
if (!channel) {
errno = mock_type(int);
return NULL;
}
return channel;
}
/*
* ibv_destroy_comp_channel -- ibv_destroy_comp_channel() mock
*/
int
ibv_destroy_comp_channel(struct ibv_comp_channel *channel)
{
assert_ptr_equal(channel, MOCK_COMP_CHANNEL);
return mock_type(int);
}
/*
* rdma_accept -- rdma_accept() mock
*/
int
rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
{
check_expected(id);
assert_non_null(conn_param);
assert_int_equal(conn_param->responder_resources, RDMA_MAX_RESP_RES);
assert_int_equal(conn_param->initiator_depth, RDMA_MAX_INIT_DEPTH);
assert_int_equal(conn_param->flow_control, 1);
assert_int_equal(conn_param->retry_count, 7); /* max 3-bit value */
assert_int_equal(conn_param->rnr_retry_count, 7); /* max 3-bit value */
errno = mock_type(int);
if (errno)
return -1;
return 0;
}
/*
* rdma_connect -- rdma_connect() mock
*/
int
rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
{
check_expected(id);
assert_non_null(conn_param);
assert_int_equal(conn_param->responder_resources, RDMA_MAX_RESP_RES);
assert_int_equal(conn_param->initiator_depth, RDMA_MAX_INIT_DEPTH);
assert_int_equal(conn_param->flow_control, 1);
assert_int_equal(conn_param->retry_count, 7); /* max 3-bit value */
assert_int_equal(conn_param->rnr_retry_count, 7); /* max 3-bit value */
errno = mock_type(int);
if (errno)
return -1;
return 0;
}
/*
* rdma_disconnect -- rdma_disconnect() mock
*/
int
rdma_disconnect(struct rdma_cm_id *id)
{
check_expected_ptr(id);
errno = mock_type(int);
if (errno)
return -1;
return 0;
}
/*
* rdma_resolve_route -- rdma_resolve_route() mock
*/
int
rdma_resolve_route(struct rdma_cm_id *id, int timeout_ms)
{
check_expected(id);
assert_int_equal(timeout_ms, MOCK_TIMEOUT);
errno = mock_type(int);
if (errno)
return -1;
return 0;
}
/*
* rdma_get_cm_event -- rdma_get_cm_event() mock
*/
int
rdma_get_cm_event(struct rdma_event_channel *channel,
struct rdma_cm_event **event_ptr)
{
check_expected_ptr(channel);
assert_non_null(event_ptr);
struct rdma_cm_event *event = mock_type(struct rdma_cm_event *);
if (!event) {
errno = mock_type(int);
return -1;
}
*event_ptr = event;
return 0;
}
/*
* rdma_ack_cm_event -- rdma_ack_cm_event() mock
*/
int
rdma_ack_cm_event(struct rdma_cm_event *event)
{
check_expected_ptr(event);
errno = mock_type(int);
if (errno)
return -1;
return 0;
}
/*
* rdma_create_event_channel -- rdma_create_event_channel() mock
*/
struct rdma_event_channel *
rdma_create_event_channel(void)
{
struct rdma_event_channel *evch =
mock_type(struct rdma_event_channel *);
if (!evch) {
errno = mock_type(int);
return NULL;
}
return evch;
}
/*
* rdma_destroy_event_channel -- rdma_destroy_event_channel() mock
*/
void
rdma_destroy_event_channel(struct rdma_event_channel *channel)
{
check_expected_ptr(channel);
}
/*
* rdma_create_qp -- rdma_create_qp() mock
*/
int
rdma_create_qp(struct rdma_cm_id *id, struct ibv_pd *pd,
struct ibv_qp_init_attr *qp_init_attr)
{
check_expected_ptr(id);
check_expected_ptr(pd);
assert_non_null(qp_init_attr);
assert_int_equal(qp_init_attr->qp_context, NULL);
assert_int_equal(qp_init_attr->send_cq, MOCK_CQ);
assert_int_equal(qp_init_attr->recv_cq, MOCK_CQ);
assert_int_equal(qp_init_attr->srq, NULL);
assert_int_equal(qp_init_attr->cap.max_send_wr, MOCK_DEFAULT_Q_SIZE);
assert_int_equal(qp_init_attr->cap.max_recv_wr, MOCK_DEFAULT_Q_SIZE);
assert_int_equal(qp_init_attr->cap.max_send_sge, MOCK_MAX_SGE);
assert_int_equal(qp_init_attr->cap.max_recv_sge, MOCK_MAX_SGE);
assert_int_equal(qp_init_attr->cap.max_inline_data,
MOCK_MAX_INLINE_DATA);
assert_int_equal(qp_init_attr->qp_type, IBV_QPT_RC);
assert_int_equal(qp_init_attr->sq_sig_all, 0);
errno = mock_type(int);
if (errno)
return -1;
id->qp = mock_type(struct ibv_qp *);
return 0;
}
/*
* rdma_destroy_qp -- rdma_destroy_qp() mock
*/
void
rdma_destroy_qp(struct rdma_cm_id *id)
{
check_expected_ptr(id);
}
#if defined(ibv_reg_mr)
/*
* Since rdma-core v27.0-105-g5a750676
* ibv_reg_mr() has been defined as a macro
* in <infiniband/verbs.h>:
*
* https://github.com/linux-rdma/rdma-core/commit/5a750676e8312715100900c6336bbc98577e082b
*
* In order to mock the ibv_reg_mr() function
* the `ibv_reg_mr` symbol has to be undefined first
* and the additional ibv_reg_mr_iova2() function
* has to be mocked, because it is called
* by the 'ibv_reg_mr' macro.
*/
#undef ibv_reg_mr
/*
* ibv_reg_mr_iova2 -- ibv_reg_mr_iova2() mock
*/
struct ibv_mr *
ibv_reg_mr_iova2(struct ibv_pd *pd, void *addr, size_t length,
uint64_t iova, unsigned access)
{
return ibv_reg_mr(pd, addr, length, (int)access);
}
#endif
/*
* ibv_reg_mr -- ibv_reg_mr() mock
*/
struct ibv_mr *
ibv_reg_mr(struct ibv_pd *pd, void *addr, size_t length, int access)
{
check_expected_ptr(pd);
check_expected(length);
check_expected(access);
void **paddr = mock_type(void **);
assert_ptr_equal(addr, *paddr);
struct ibv_mr *mr = mock_type(struct ibv_mr *);
if (mr == NULL) {
errno = mock_type(int);
return NULL;
}
mr->addr = addr;
mr->length = length;
return mr;
}
/*
* ibv_wc_status_str -- ibv_wc_status_str() mock
*/
const char *
ibv_wc_status_str(enum ibv_wc_status status)
{
return "";
}
/*
* rdma_migrate_id -- rdma_migrate_id() mock
*/
int
rdma_migrate_id(struct rdma_cm_id *id, struct rdma_event_channel *channel)
{
check_expected_ptr(id);
check_expected_ptr(channel);
errno = mock_type(int);
if (errno)
return -1;
return 0;
}
/*
* rdma_reject -- rdma_reject() mock
*/
int
rdma_reject(struct rdma_cm_id *id, const void *private_data,
uint8_t private_data_len)
{
assert_true(0);
return 0;
}
/*
* rdma_listen -- rdma_listen() mock
*/
int
rdma_listen(struct rdma_cm_id *id, int backlog)
{
check_expected_ptr(id);
assert_int_equal(backlog, 0);
errno = mock_type(int);
if (errno)
return -1;
return 0;
}
/*
* rdma_getaddrinfo -- rdma_getaddrinfo() mock
*/
#ifdef RDMA_GETADDRINFO_OLD_SIGNATURE
int
rdma_getaddrinfo(char *node, char *port,
struct rdma_addrinfo *hints, struct rdma_addrinfo **res)
#else
int
rdma_getaddrinfo(const char *node, const char *port,
const struct rdma_addrinfo *hints, struct rdma_addrinfo **res)
#endif
{
assert_string_equal(node, MOCK_ADDR);
errno = mock_type(int);
if (errno)
return -1;
struct rdma_addrinfo *buf = mock_type(struct rdma_addrinfo *);
if (port == NULL) {
buf->ai_src_addr = NULL;
buf->ai_dst_addr = NULL;
} else {
assert_string_equal(port, MOCK_PORT);
buf->ai_src_addr = mock_type(struct sockaddr *);
buf->ai_dst_addr = mock_type(struct sockaddr *);
}
*res = buf;
return 0;
}
/*
* rdma_freeaddrinfo -- rdma_freeaddrinfo() mock
*/
void
rdma_freeaddrinfo(struct rdma_addrinfo *res)
{
check_expected_ptr(res);
}
/*
* rdma_resolve_addr -- rdma_resolve_addr() mock
*/
int
rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
struct sockaddr *dst_addr, int timeout_ms)
{
check_expected_ptr(id);
check_expected_ptr(src_addr);
check_expected_ptr(dst_addr);
check_expected(timeout_ms);
id->verbs = mock_type(struct ibv_context *);
errno = mock_type(int);
if (errno)
return -1;
return 0;
}
/*
* rdma_bind_addr -- rdma_bind_addr() mock
* Note: CM ID is not modified.
*/
int
rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr)
{
check_expected_ptr(id);
check_expected_ptr(addr);
id->verbs = mock_type(struct ibv_context *);
errno = mock_type(int);
if (errno)
return -1;
return 0;
}
/*
* rdma_event_str -- rdma_event_str() mock
*/
const char *
rdma_event_str(enum rdma_cm_event_type event)
{
return "";
}
/*
* __wrap__test_malloc -- malloc() mock
*/
void *
__wrap__test_malloc(size_t size)
{
int err = mock_type(int);
if (err) {
errno = err;
return NULL;
}
return __real__test_malloc(size);
}
/*
* __wrap_posix_memalign -- posix_memalign() mock
*/
int
__wrap_posix_memalign(void **memptr, size_t alignment, size_t size)
{
struct posix_memalign_args *args =
mock_type(struct posix_memalign_args *);
*memptr = __real__test_malloc(size);
/* save the address of the allocated memory to verify it later */
args->ptr = *memptr;
if (*memptr == NULL)
return ENOMEM;
return 0;
}
/*
* fprintf -- fprintf() mock
*/
int
__wrap_fprintf(FILE *__restrict __stream, const char *__restrict __format, ...)
{
return 0;
}
/*
* create_descriptor -- create a descriptor from the given values
*/
int
create_descriptor(void *desc,
uint64_t raddr, uint64_t size, uint32_t rkey, uint8_t usage)
{
char *buff = (char *)desc;
uint64_t addr = htole64(raddr);
memcpy(buff, &addr, sizeof(uint64_t));
buff += sizeof(uint64_t);
uint64_t length = htole64(size);
memcpy(buff, &length, sizeof(uint64_t));
buff += sizeof(uint64_t);
uint32_t key = htole32(rkey);
memcpy(buff, &key, sizeof(uint32_t));
buff += sizeof(uint32_t);
*((uint8_t *)buff) = usage;
return 0;
}
/*
* __wrap_mmap -- mmap() mock
*/
void *
__wrap_mmap(void *__addr, size_t __len, int __prot,
int __flags, int __fd, off_t __offset)
{
void *err = mock_type(void *);
if (err == MAP_FAILED)
return MAP_FAILED;
struct mmap_args *args = mock_type(struct mmap_args *);
void *memptr = __real__test_malloc(__len);
/* save the address of the allocated memory to verify it later */
args->ptr = memptr;
return memptr;
}
/*
* __wrap_munmap -- munmap() mock
*/
int
__wrap_munmap(void *__addr, size_t __len)
{
(void) __len; /* unsused */
test_free(__addr);
return 0;
}