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
/
example-02-read-to-volatile.c
401 lines (307 loc) · 12.5 KB
/
example-02-read-to-volatile.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
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright 2020-2021, Intel Corporation */
/*
* example-02-read-to-volatile.c -- 'read to volatile' integration tests
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <rdma/rdma_cma.h>
#include "cmocka_headers.h"
#include "librpma.h"
#include "mocks.h"
#define MOCK_READ_ADDR (&msg)
#define MOCK_READ_LEN (strlen(msg) + 1)
#define MOCK_READ_SRC_OFFSET 0
static const char msg[] = "Hello client!";
int client_main(int argc, char *argv[]);
int server_main(int argc, char *argv[]);
/* tests */
/*
* test_client__success -- 'read to volatile' integration test (client)
*/
void
test_client__success(void **unused)
{
/* configure mocks for rpma_utils_get_ibv_context() */
struct rdma_addrinfo res1 = {0};
will_return(rdma_getaddrinfo, MOCK_OK);
will_return(rdma_getaddrinfo, &res1);
will_return_always(__wrap__test_malloc, MOCK_OK);
will_return(rdma_create_id, &Cm_id);
expect_value(rdma_resolve_addr, id, &Cm_id);
expect_value(rdma_resolve_addr, src_addr, NULL);
expect_value(rdma_resolve_addr, dst_addr, NULL);
expect_value(rdma_resolve_addr, timeout_ms, MOCK_TIMEOUT);
will_return(rdma_resolve_addr, MOCK_VERBS);
will_return(rdma_resolve_addr, MOCK_OK);
expect_value(rdma_destroy_id, id, &Cm_id);
will_return(rdma_destroy_id, MOCK_OK);
expect_value(rdma_freeaddrinfo, res, &res1);
/* configure mocks for rpma_peer_new() */
#ifdef ON_DEMAND_PAGING_SUPPORTED
will_return(ibv_query_device_ex_mock, &Ibv_odp_capable_caps);
#endif
expect_value(ibv_alloc_pd, ibv_ctx, MOCK_VERBS);
will_return(ibv_alloc_pd, MOCK_IBV_PD);
/* configure mocks for ibv_reg_mr() */
struct posix_memalign_args allocated = {0};
will_return(__wrap_posix_memalign, &allocated);
expect_value(ibv_reg_mr, pd, MOCK_IBV_PD);
expect_value(ibv_reg_mr, length, MOCK_SIZE);
expect_value(ibv_reg_mr, access, IBV_ACCESS_LOCAL_WRITE);
will_return(ibv_reg_mr, &allocated.ptr);
will_return(ibv_reg_mr, MOCK_MR);
/* configure mocks for rpma_conn_req_new() */
struct rdma_addrinfo res2 = {0};
will_return(rdma_getaddrinfo, MOCK_OK);
will_return(rdma_getaddrinfo, &res2);
will_return(rdma_getaddrinfo, MOCK_SRC_ADDR);
will_return(rdma_getaddrinfo, MOCK_DST_ADDR);
will_return(rdma_create_id, &Cm_id);
expect_value(rdma_resolve_addr, id, &Cm_id);
expect_value(rdma_resolve_addr, src_addr, MOCK_SRC_ADDR);
expect_value(rdma_resolve_addr, dst_addr, MOCK_DST_ADDR);
expect_value(rdma_resolve_addr, timeout_ms, MOCK_TIMEOUT);
will_return(rdma_resolve_addr, MOCK_VERBS);
will_return(rdma_resolve_addr, MOCK_OK);
expect_value(rdma_resolve_route, id, &Cm_id);
will_return(rdma_resolve_route, MOCK_OK);
will_return(ibv_create_comp_channel, &Ibv_comp_channel);
will_return(ibv_create_cq, &Ibv_cq);
will_return(ibv_req_notify_cq_mock, MOCK_OK);
expect_value(rdma_create_qp, id, &Cm_id);
expect_value(rdma_create_qp, pd, MOCK_IBV_PD);
will_return(rdma_create_qp, MOCK_OK);
will_return(rdma_create_qp, &Ibv_qp);
expect_value(rdma_freeaddrinfo, res, &res2);
/* configure mocks for rpma_conn_req_connect() */
will_return(rdma_create_event_channel, MOCK_EVCH);
expect_value(rdma_migrate_id, id, &Cm_id);
expect_value(rdma_migrate_id, channel, MOCK_EVCH);
will_return(rdma_migrate_id, MOCK_OK);
struct mmap_args allocated_raw = {0};
will_return(__wrap_mmap, &allocated_raw);
will_return(__wrap_mmap, &allocated_raw);
expect_value(ibv_reg_mr, pd, MOCK_IBV_PD);
expect_value(ibv_reg_mr, length, MOCK_RAW_SIZE);
expect_value(ibv_reg_mr, access, IBV_ACCESS_LOCAL_WRITE);
will_return(ibv_reg_mr, &allocated_raw.ptr);
will_return(ibv_reg_mr, MOCK_MR_RAW);
expect_value(rdma_connect, id, &Cm_id);
will_return(rdma_connect, MOCK_OK);
/* configure mocks for rpma_conn_next_event() */
struct common_data data;
data.mr_desc_size = DESCRIPTORS_MAX_SIZE;
create_descriptor(&data.descriptors[0],
(uintptr_t)MOCK_READ_ADDR,
MOCK_READ_LEN,
MOCK_RKEY,
RPMA_MR_USAGE_READ_DST);
struct rdma_cm_event f_event = {0};
f_event.event = RDMA_CM_EVENT_ESTABLISHED;
f_event.param.conn.private_data = &data;
f_event.param.conn.private_data_len = sizeof(data);
expect_value(rdma_get_cm_event, channel, MOCK_EVCH);
will_return(rdma_get_cm_event, &f_event);
expect_value(rdma_ack_cm_event, event, &f_event);
will_return(rdma_ack_cm_event, MOCK_OK);
/* configure mocks for rpma_read() */
expect_value(ibv_post_send_mock, qp, &Ibv_qp);
expect_value(ibv_post_send_mock, wr->opcode, IBV_WR_RDMA_READ);
expect_value(ibv_post_send_mock, wr->send_flags, IBV_SEND_SIGNALED);
expect_value(ibv_post_send_mock, wr->wr_id, 0 /* op_context */);
expect_value(ibv_post_send_mock, wr->num_sge, 1);
expect_value(ibv_post_send_mock, wr->sg_list->length, MOCK_READ_LEN);
expect_value(ibv_post_send_mock, wr->wr.rdma.remote_addr,
MOCK_READ_ADDR);
will_return(ibv_post_send_mock, (uint64_t *)&allocated.ptr);
will_return(ibv_post_send_mock, MOCK_READ_SRC_OFFSET);
will_return(ibv_post_send_mock, MOCK_OK);
/* configure mocks for rpma_conn_completion_wait() */
expect_value(ibv_get_cq_event, channel, MOCK_COMP_CHANNEL);
will_return(ibv_get_cq_event, MOCK_OK);
expect_value(ibv_ack_cq_events, cq, MOCK_CQ);
will_return(ibv_req_notify_cq_mock, MOCK_OK);
/* configure mock for rpma_conn_completion_get() */
struct ibv_wc wc = {0};
wc.opcode = IBV_WC_RDMA_READ;
expect_value(ibv_poll_cq_mock, cq, &Ibv_cq);
will_return(ibv_poll_cq_mock, 1);
will_return(ibv_poll_cq_mock, &wc);
/* configure mocks for rpma_conn_disconnect() */
expect_value(rdma_disconnect, id, &Cm_id);
will_return(rdma_disconnect, MOCK_OK);
/* configure mocks for rpma_conn_next_event() */
struct rdma_cm_event s_event = {0};
s_event.event = RDMA_CM_EVENT_DISCONNECTED;
expect_value(rdma_get_cm_event, channel, MOCK_EVCH);
will_return(rdma_get_cm_event, &s_event);
expect_value(rdma_ack_cm_event, event, &s_event);
will_return(rdma_ack_cm_event, MOCK_OK);
/* configure mocks for rpma_conn_delete() */
expect_value(ibv_dereg_mr, mr, MOCK_MR_RAW);
will_return(ibv_dereg_mr, MOCK_OK);
expect_value(rdma_destroy_qp, id, &Cm_id);
expect_value(ibv_destroy_cq, cq, &Ibv_cq);
will_return(ibv_destroy_cq, MOCK_OK);
will_return(ibv_destroy_comp_channel, MOCK_OK);
expect_value(rdma_destroy_id, id, &Cm_id);
will_return(rdma_destroy_id, MOCK_OK);
expect_value(rdma_destroy_event_channel, channel, MOCK_EVCH);
expect_value(ibv_dereg_mr, mr, MOCK_MR);
will_return(ibv_dereg_mr, MOCK_OK);
/* configure mocks for rpma_peer_delete() */
expect_value(ibv_dealloc_pd, pd, MOCK_IBV_PD);
will_return(ibv_dealloc_pd, MOCK_OK);
/* run test */
char *argv[] = {"client", MOCK_ADDR, MOCK_PORT};
int ret = client_main(3, argv);
assert_int_equal(ret, MOCK_OK);
}
/*
* test_server__success -- 'read to volatile' integration test (server)
*/
void
test_server__success(void **unused)
{
/* configure mocks for rpma_utils_get_ibv_context() */
struct rdma_addrinfo res1 = {0};
will_return(rdma_getaddrinfo, MOCK_OK);
will_return(rdma_getaddrinfo, &res1);
will_return_always(__wrap__test_malloc, MOCK_OK);
struct rdma_cm_id Cm_id = {0};
will_return(rdma_create_id, &Cm_id);
expect_value(rdma_bind_addr, id, &Cm_id);
expect_value(rdma_bind_addr, addr, NULL);
will_return(rdma_bind_addr, MOCK_VERBS);
will_return(rdma_bind_addr, MOCK_OK);
expect_value(rdma_destroy_id, id, &Cm_id);
will_return(rdma_destroy_id, MOCK_OK);
expect_value(rdma_freeaddrinfo, res, &res1);
/* configure mocks for rpma_peer_new() */
#ifdef ON_DEMAND_PAGING_SUPPORTED
will_return(ibv_query_device_ex_mock, &Ibv_odp_capable_caps);
#endif
expect_value(ibv_alloc_pd, ibv_ctx, MOCK_VERBS);
will_return(ibv_alloc_pd, MOCK_IBV_PD);
/* configure mocks for rpma_ep_listen() */
will_return(rdma_create_event_channel, MOCK_EVCH);
will_return(rdma_create_id, &Cm_id);
struct rdma_addrinfo res2 = {0};
will_return(rdma_getaddrinfo, MOCK_OK);
will_return(rdma_getaddrinfo, &res2);
will_return(rdma_getaddrinfo, MOCK_SRC_ADDR);
will_return(rdma_getaddrinfo, MOCK_DST_ADDR);
expect_value(rdma_bind_addr, id, &Cm_id);
expect_value(rdma_bind_addr, addr, MOCK_SRC_ADDR);
will_return(rdma_bind_addr, MOCK_VERBS);
will_return(rdma_bind_addr, MOCK_OK);
expect_value(rdma_listen, id, &Cm_id);
will_return(rdma_listen, MOCK_OK);
expect_value(rdma_freeaddrinfo, res, &res2);
/* configure mocks for ibv_reg_mr() */
struct posix_memalign_args allocated = {0};
will_return(__wrap_posix_memalign, &allocated);
expect_value(ibv_reg_mr, pd, MOCK_IBV_PD);
expect_value(ibv_reg_mr, length, MOCK_READ_LEN);
expect_value(ibv_reg_mr, access, IBV_ACCESS_REMOTE_READ);
will_return(ibv_reg_mr, &allocated.ptr);
will_return(ibv_reg_mr, MOCK_MR);
/* configure mocks for rpma_ep_next_conn_req() */
struct rdma_cm_event f_event = {0};
const char *msg = "Hello client!";
f_event.param.conn.private_data = msg;
f_event.param.conn.private_data_len = MOCK_READ_LEN * sizeof(char);
f_event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
f_event.id = &Cm_id;
expect_value(rdma_get_cm_event, channel, MOCK_EVCH);
will_return(rdma_get_cm_event, &f_event);
will_return(ibv_create_comp_channel, &Ibv_comp_channel);
will_return(ibv_create_cq, &Ibv_cq);
will_return(ibv_req_notify_cq_mock, MOCK_OK);
expect_value(rdma_create_qp, id, &Cm_id);
expect_value(rdma_create_qp, pd, MOCK_IBV_PD);
will_return(rdma_create_qp, MOCK_OK);
will_return(rdma_create_qp, &Ibv_qp);
/* configure mocks for rpma_conn_req_connect() */
expect_value(rdma_accept, id, &Cm_id);
will_return(rdma_accept, MOCK_OK);
expect_value(rdma_ack_cm_event, event, &f_event);
will_return(rdma_ack_cm_event, MOCK_OK);
will_return(rdma_create_event_channel, MOCK_EVCH);
expect_value(rdma_migrate_id, id, &Cm_id);
expect_value(rdma_migrate_id, channel, MOCK_EVCH);
will_return(rdma_migrate_id, MOCK_OK);
struct mmap_args allocated_raw = {0};
will_return(__wrap_mmap, &allocated_raw);
will_return(__wrap_mmap, &allocated_raw);
expect_value(ibv_reg_mr, pd, MOCK_IBV_PD);
expect_value(ibv_reg_mr, length, MOCK_RAW_SIZE);
expect_value(ibv_reg_mr, access, IBV_ACCESS_LOCAL_WRITE);
will_return(ibv_reg_mr, &allocated_raw.ptr);
will_return(ibv_reg_mr, MOCK_MR_RAW);
/* configure mocks for rpma_conn_next_event() */
struct rdma_cm_event s_event = {0};
const char *s_msg = "Hello server!";
s_event.event = RDMA_CM_EVENT_ESTABLISHED;
s_event.param.conn.private_data = (void *)s_msg;
s_event.param.conn.private_data_len =
(strlen(s_msg) + 1) * sizeof(char);
expect_value(rdma_get_cm_event, channel, MOCK_EVCH);
will_return(rdma_get_cm_event, &s_event);
expect_value(rdma_ack_cm_event, event, &s_event);
will_return(rdma_ack_cm_event, MOCK_OK);
/* configure mocks for rpma_conn_next_event() */
struct rdma_cm_event t_event = {0};
t_event.event = RDMA_CM_EVENT_DISCONNECTED;
expect_value(rdma_get_cm_event, channel, MOCK_EVCH);
will_return(rdma_get_cm_event, &t_event);
expect_value(rdma_ack_cm_event, event, &t_event);
will_return(rdma_ack_cm_event, MOCK_OK);
/* configure mocks for rpma_conn_disconnect() */
expect_value(rdma_disconnect, id, &Cm_id);
will_return(rdma_disconnect, MOCK_OK);
/* configure mocks for rpma_conn_delete() */
expect_value(ibv_dereg_mr, mr, MOCK_MR_RAW);
will_return(ibv_dereg_mr, MOCK_OK);
expect_value(rdma_destroy_qp, id, &Cm_id);
expect_value(ibv_destroy_cq, cq, &Ibv_cq);
will_return(ibv_destroy_cq, MOCK_OK);
will_return(ibv_destroy_comp_channel, MOCK_OK);
expect_value(rdma_destroy_id, id, &Cm_id);
will_return(rdma_destroy_id, MOCK_OK);
expect_value(rdma_destroy_event_channel, channel, MOCK_EVCH);
expect_value(ibv_dereg_mr, mr, MOCK_MR);
will_return(ibv_dereg_mr, MOCK_OK);
/* configure mocks for rpma_ep_shutdown() */
expect_value(rdma_destroy_id, id, &Cm_id);
will_return(rdma_destroy_id, MOCK_OK);
expect_value(rdma_destroy_event_channel, channel, MOCK_EVCH);
/* configure mocks for rpma_peer_delete() */
expect_value(ibv_dealloc_pd, pd, MOCK_IBV_PD);
will_return(ibv_dealloc_pd, MOCK_OK);
/* run test */
char *argv[] = {"server", MOCK_ADDR, MOCK_PORT};
int ret = server_main(3, argv);
assert_int_equal(ret, MOCK_OK);
}
int
main(int argc, char *argv[])
{
MOCK_VERBS->abi_compat = __VERBS_ABI_IS_EXTENDED;
#ifdef ON_DEMAND_PAGING_SUPPORTED
Verbs_context.query_device_ex = ibv_query_device_ex_mock;
#endif
Verbs_context.sz = sizeof(struct verbs_context);
MOCK_VERBS->ops.post_send = ibv_post_send_mock;
MOCK_VERBS->ops.poll_cq = ibv_poll_cq_mock;
MOCK_VERBS->ops.req_notify_cq = ibv_req_notify_cq_mock;
Ibv_cq.context = MOCK_VERBS;
Ibv_qp.context = MOCK_VERBS;
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_client__success),
cmocka_unit_test(test_server__success),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}