Debian Codegraph @

prov/efa/test/efa_unit_test_mocks.c

raw
1 /* SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0-only */
2 /* SPDX-FileCopyrightText: Copyright Amazon.com, Inc. or its affiliates. All rights reserved. */
3
4 #define _GNU_SOURCE
5 #include <errno.h>
6 #include <stdlib.h>
7 #include <stdint.h>
8 #include <stdarg.h>
9 #include <stddef.h>
10 #include <setjmp.h>
11 #include <cmocka.h>
12 #include "efa.h"
13 #include "efa_rdm_pke_utils.h"
14 #include "efa_rdm_pke_nonreq.h"
15 #include "efa_unit_test_mocks.h"
16
17 /* mock of rdma-core functions */
18
19 /**
20 * @brief call real ibv_create_ah and mock()
21 *
22 * When combined with will_return_count(), this mock of ibv_create_ah() can be used to verify
23 * number of times ibv_create_ah() is called.
24 */
25 struct ibv_ah *efa_mock_ibv_create_ah_check_mock(struct ibv_pd *pd, struct ibv_ah_attr *attr)
26 {
27 mock();
28 return __real_ibv_create_ah(pd, attr);
29 }
30
31 int efa_mock_efadv_query_device_return_mock(struct ibv_context *ibv_ctx,
32 struct efadv_device_attr *attr,
33 uint32_t inlen)
34 {
35 return mock();
36 }
37
38
39 /**
40 * @brief a list of work requests request's WR ID
41 */
42 void *g_ibv_submitted_wr_id_vec[EFA_RDM_EP_MAX_WR_PER_IBV_POST_SEND];
43 int g_ibv_submitted_wr_id_cnt = 0;
44
45 void efa_ibv_submitted_wr_id_vec_clear()
46 {
47 memset(g_ibv_submitted_wr_id_vec, 0,
48 g_ibv_submitted_wr_id_cnt * sizeof(void *));
49 g_ibv_submitted_wr_id_cnt = 0;
50 }
51
52 void efa_mock_ibv_wr_start_no_op(struct ibv_qp_ex *qp)
53 {
54 }
55
56 /**
57 * @brief save wr_id of send request in a global array
58 *
59 * The saved work request is then be used by efa_mock_ibv_start_poll_use_send_wr()
60 * to make ibv_cq_ex to look like it indeed got a completion from device.
61 */
62 void efa_mock_ibv_wr_send_save_wr(struct ibv_qp_ex *qp)
63 {
64 g_ibv_submitted_wr_id_vec[g_ibv_submitted_wr_id_cnt] = (void *)qp->wr_id;
65 g_ibv_submitted_wr_id_cnt++;
66 }
67
68 void efa_mock_ibv_wr_send_verify_handshake_pkt_local_host_id_and_save_wr(struct ibv_qp_ex *qp)
69 {
70 struct efa_rdm_pke* pke;
71 struct efa_rdm_base_hdr *efa_rdm_base_hdr;
72 uint64_t *host_id_ptr;
73
74 pke = (struct efa_rdm_pke *)qp->wr_id;
75 efa_rdm_base_hdr = efa_rdm_pke_get_base_hdr(pke);
76
77 assert_int_equal(efa_rdm_base_hdr->type, EFA_RDM_HANDSHAKE_PKT);
78
79 if (g_efa_unit_test_mocks.local_host_id) {
80 assert_true(efa_rdm_base_hdr->flags & EFA_RDM_HANDSHAKE_HOST_ID_HDR);
81 host_id_ptr = efa_rdm_pke_get_handshake_opt_host_id_ptr(pke);
82 assert_true(*host_id_ptr == g_efa_unit_test_mocks.local_host_id);
83 } else {
84 assert_false(efa_rdm_base_hdr->flags & EFA_RDM_HANDSHAKE_HOST_ID_HDR);
85 }
86
87 function_called();
88 return efa_mock_ibv_wr_send_save_wr(qp);
89 }
90
91 void efa_mock_ibv_wr_send_imm_save_wr(struct ibv_qp_ex *qp, __be32 imm_data)
92 {
93 g_ibv_submitted_wr_id_vec[g_ibv_submitted_wr_id_cnt] = (void *)qp->wr_id;
94 g_ibv_submitted_wr_id_cnt++;
95 }
96
97 void efa_mock_ibv_wr_set_inline_data_list_no_op(struct ibv_qp_ex *qp,
98 size_t num_buf,
99 const struct ibv_data_buf *buf_list)
100 {
101 }
102
103 void efa_mock_ibv_wr_set_sge_list_no_op(struct ibv_qp_ex *qp,
104 size_t num_sge,
105 const struct ibv_sge *sge_list)
106 {
107 }
108
109 void efa_mock_ibv_wr_set_ud_addr_no_op(struct ibv_qp_ex *qp, struct ibv_ah *ah,
110 uint32_t remote_qpn, uint32_t remote_qkey)
111 {
112 }
113
114 int efa_mock_ibv_wr_complete_no_op(struct ibv_qp_ex *qp)
115 {
116 return 0;
117 }
118
119 void efa_mock_ibv_wr_rdma_write_save_wr(struct ibv_qp_ex *qp, uint32_t rkey,
120 uint64_t remote_addr)
121 {
122 g_ibv_submitted_wr_id_vec[g_ibv_submitted_wr_id_cnt] = (void *)qp->wr_id;
123 g_ibv_submitted_wr_id_cnt++;
124 }
125
126 int efa_mock_ibv_start_poll_return_mock(struct ibv_cq_ex *ibvcqx,
127 struct ibv_poll_cq_attr *attr)
128 {
129 return mock();
130 }
131
132 static inline
133 int efa_mock_use_saved_send_wr(struct ibv_cq_ex *ibv_cqx, int status)
134 {
135 int i;
136
137 if (g_ibv_submitted_wr_id_cnt == 0)
138 return ENOENT;
139
140 ibv_cqx->wr_id = (uintptr_t)g_ibv_submitted_wr_id_vec[0];
141 ibv_cqx->status = status;
142
143 for (i = 1; i < g_ibv_submitted_wr_id_cnt; ++i)
144 g_ibv_submitted_wr_id_vec[i-1] = g_ibv_submitted_wr_id_vec[i];
145
146 g_ibv_submitted_wr_id_cnt--;
147 return 0;
148 }
149
150 int efa_mock_ibv_start_poll_use_saved_send_wr_with_mock_status(struct ibv_cq_ex *ibv_cqx,
151 struct ibv_poll_cq_attr *attr)
152 {
153 return efa_mock_use_saved_send_wr(ibv_cqx, mock());
154 }
155
156 int efa_mock_ibv_next_poll_return_mock(struct ibv_cq_ex *ibvcqx)
157 {
158 return mock();
159 }
160
161 int efa_mock_ibv_next_poll_use_saved_send_wr_with_mock_status(struct ibv_cq_ex *ibv_cqx)
162 {
163 return efa_mock_use_saved_send_wr(ibv_cqx, mock());
164 }
165
166 void efa_mock_ibv_end_poll_check_mock(struct ibv_cq_ex *ibvcqx)
167 {
168 mock();
169 }
170
171 uint32_t efa_mock_ibv_read_opcode_return_mock(struct ibv_cq_ex *current)
172 {
173 return mock();
174 }
175
176 uint32_t efa_mock_ibv_read_vendor_err_return_mock(struct ibv_cq_ex *current)
177 {
178 return mock();
179 }
180
181 uint32_t efa_mock_ibv_read_qp_num_return_mock(struct ibv_cq_ex *current)
182 {
183 return mock();
184 }
185
186 uint32_t efa_mock_ibv_read_wc_flags_return_mock(struct ibv_cq_ex *current)
187 {
188 return mock();
189 }
190
191 uint32_t efa_mock_ibv_wc_read_imm_data_return_mock(struct ibv_cq_ex *current)
192 {
193 return mock();
194 }
195
196 bool efa_mock_efadv_wc_is_unsolicited(struct efadv_cq *efadv_cq)
197 {
198 return mock();
199 }
200
201 int g_ofi_copy_from_hmem_iov_call_counter;
202 ssize_t efa_mock_ofi_copy_from_hmem_iov_inc_counter(void *dest, size_t size,
203 enum fi_hmem_iface hmem_iface, uint64_t device,
204 const struct iovec *hmem_iov,
205 size_t hmem_iov_count, uint64_t hmem_iov_offset)
206 {
207 g_ofi_copy_from_hmem_iov_call_counter += 1;
208 return __real_ofi_copy_from_hmem_iov(dest, size, hmem_iface, device, hmem_iov, hmem_iov_count, hmem_iov_offset);
209 }
210
211 int efa_mock_efa_rdm_pke_read_return_mock(struct efa_rdm_ope *ope)
212 {
213 return mock();
214 }
215
216 ssize_t efa_mock_efa_rdm_ope_post_send_return_mock(struct efa_rdm_ope *ope, int pkt_type)
217 {
218 return mock();
219 }
220
221 ssize_t efa_mock_efa_rdm_pke_proc_matched_rtm_no_op(struct efa_rdm_pke *pkt_entry)
222 {
223 return FI_SUCCESS;
224 }
225
226 bool efa_mock_efa_device_support_unsolicited_write_recv()
227 {
228 return mock();
229 }
230
231 int efa_mock_ibv_post_recv(struct ibv_qp *qp, struct ibv_recv_wr *wr,
232 struct ibv_recv_wr **bad_wr)
233 {
234 return mock();
235 }
236
237 void efa_mock_ibv_wr_rdma_read_save_wr(struct ibv_qp_ex *qp, uint32_t rkey,
238 uint64_t remote_addr)
239 {
240 g_ibv_submitted_wr_id_vec[g_ibv_submitted_wr_id_cnt] = (void *)qp->wr_id;
241 g_ibv_submitted_wr_id_cnt++;
242 }
243
244 void efa_mock_ibv_wr_rdma_write_imm_save_wr(struct ibv_qp_ex *qp, uint32_t rkey,
245 uint64_t remote_addr,
246 __be32 imm_data)
247 {
248 g_ibv_submitted_wr_id_vec[g_ibv_submitted_wr_id_cnt] = (void *) qp->wr_id;
249 g_ibv_submitted_wr_id_cnt++;
250 }
251
252 struct efa_unit_test_mocks g_efa_unit_test_mocks = {
253 .local_host_id = 0,
254 .peer_host_id = 0,
255 .ibv_create_ah = __real_ibv_create_ah,
256 .efadv_query_device = __real_efadv_query_device,
257 #if HAVE_EFADV_CQ_EX
258 .efadv_create_cq = __real_efadv_create_cq,
259 #endif
260 #if HAVE_NEURON
261 .neuron_alloc = __real_neuron_alloc,
262 #endif
263 #if HAVE_CUDA
264 .ofi_cudaMalloc = __real_ofi_cudaMalloc,
265 #endif
266 .ofi_copy_from_hmem_iov = __real_ofi_copy_from_hmem_iov,
267 .efa_rdm_pke_read = __real_efa_rdm_pke_read,
268 .efa_rdm_pke_proc_matched_rtm = __real_efa_rdm_pke_proc_matched_rtm,
269 .efa_rdm_ope_post_send = __real_efa_rdm_ope_post_send,
270 .efa_device_support_unsolicited_write_recv = __real_efa_device_support_unsolicited_write_recv,
271 .ibv_is_fork_initialized = __real_ibv_is_fork_initialized,
272 #if HAVE_EFADV_QUERY_MR
273 .efadv_query_mr = __real_efadv_query_mr,
274 #endif
275 #if HAVE_EFA_DATA_IN_ORDER_ALIGNED_128_BYTES
276 .ibv_query_qp_data_in_order = __real_ibv_query_qp_data_in_order,
277 #endif
278 };
279
280 struct ibv_ah *__wrap_ibv_create_ah(struct ibv_pd *pd, struct ibv_ah_attr *attr)
281 {
282 return g_efa_unit_test_mocks.ibv_create_ah(pd, attr);
283 }
284
285 int __wrap_efadv_query_device(struct ibv_context *ibv_ctx, struct efadv_device_attr *attr,
286 uint32_t inlen)
287 {
288 return g_efa_unit_test_mocks.efadv_query_device(ibv_ctx, attr, inlen);
289 }
290
291 struct ibv_cq_ex *efa_mock_create_cq_ex_return_null(struct ibv_context *context, struct ibv_cq_init_attr_ex *init_attr)
292 {
293 function_called();
294 return NULL;
295 };
296
297 #if HAVE_EFADV_CQ_EX
298 struct ibv_cq_ex *__wrap_efadv_create_cq(struct ibv_context *ibvctx,
299 struct ibv_cq_init_attr_ex *attr_ex,
300 struct efadv_cq_init_attr *efa_attr,
301 uint32_t inlen)
302 {
303 return g_efa_unit_test_mocks.efadv_create_cq(ibvctx, attr_ex, efa_attr, inlen);
304 }
305
306 uint32_t efa_mock_ibv_read_src_qp_return_mock(struct ibv_cq_ex *current)
307 {
308 return mock();
309 }
310
311 uint32_t efa_mock_ibv_read_byte_len_return_mock(struct ibv_cq_ex *current)
312 {
313 return mock();
314 };
315
316 uint32_t efa_mock_ibv_read_slid_return_mock(struct ibv_cq_ex *current)
317 {
318 return mock();
319 }
320
321 int efa_mock_efadv_wc_read_sgid_return_mock(struct efadv_cq *efadv_cq, union ibv_gid *sgid)
322 {
323 return mock();
324 }
325
326 int efa_mock_efadv_wc_read_sgid_return_zero_code_and_expect_next_poll_and_set_gid(struct efadv_cq *efadv_cq, union ibv_gid *sgid)
327 {
328 /* Make sure this mock is always called before ibv_next_poll */
329 expect_function_call(efa_mock_ibv_next_poll_check_function_called_and_return_mock);
330 memcpy(sgid->raw, (uint8_t *)mock(), sizeof(sgid->raw));
331 /* Must return 0 for unknown AH */
332 return 0;
333 };
334
335 int efa_mock_ibv_next_poll_check_function_called_and_return_mock(struct ibv_cq_ex *ibvcqx)
336 {
337 function_called();
338 return mock();
339 };
340
341 struct ibv_cq_ex *efa_mock_efadv_create_cq_with_ibv_create_cq_ex(struct ibv_context *ibvctx,
342 struct ibv_cq_init_attr_ex *attr_ex,
343 struct efadv_cq_init_attr *efa_attr,
344 uint32_t inlen)
345 {
346 function_called();
347 return ibv_create_cq_ex(ibvctx, attr_ex);
348 }
349
350 struct ibv_cq_ex *efa_mock_efadv_create_cq_set_eopnotsupp_and_return_null(struct ibv_context *ibvctx,
351 struct ibv_cq_init_attr_ex *attr_ex,
352 struct efadv_cq_init_attr *efa_attr,
353 uint32_t inlen)
354 {
355 function_called();
356 errno = EOPNOTSUPP;
357 return NULL;
358 }
359 #endif
360
361 #if HAVE_NEURON
362 void *__wrap_neuron_alloc(void **handle, size_t size)
363 {
364 return g_efa_unit_test_mocks.neuron_alloc(handle, size);
365 }
366
367 void *efa_mock_neuron_alloc_return_null(void **handle, size_t size)
368 {
369 return NULL;
370 }
371
372 void *efa_mock_neuron_alloc_return_mock(void **handle, size_t size)
373 {
374 /* Not mocking return value so this function will fail when it is called */
375 return (void *) mock();
376 }
377 #endif
378
379 #if HAVE_CUDA
380 cudaError_t __wrap_ofi_cudaMalloc(void **ptr, size_t size)
381 {
382 return g_efa_unit_test_mocks.ofi_cudaMalloc(ptr, size);
383 }
384
385 cudaError_t efa_mock_ofi_cudaMalloc_return_mock(void **ptr, size_t size)
386 {
387 /* Not mocking return value so this function will fail when it is called */
388 return (cudaError_t) mock();
389 }
390 #endif
391
392 ssize_t __wrap_ofi_copy_from_hmem_iov(void *dest, size_t size,
393 enum fi_hmem_iface hmem_iface, uint64_t device,
394 const struct iovec *hmem_iov,
395 size_t hmem_iov_count, uint64_t hmem_iov_offset)
396 {
397 return g_efa_unit_test_mocks.ofi_copy_from_hmem_iov(dest, size, hmem_iface, device, hmem_iov, hmem_iov_count, hmem_iov_offset);
398 }
399
400 int __wrap_efa_rdm_pke_read(struct efa_rdm_ope *ope)
401 {
402 return g_efa_unit_test_mocks.efa_rdm_pke_read(ope);
403 }
404
405 int __wrap_efa_rdm_pke_proc_matched_rtm(struct efa_rdm_pke *pkt_entry)
406 {
407 return g_efa_unit_test_mocks.efa_rdm_pke_proc_matched_rtm(pkt_entry);
408 }
409
410 int __wrap_efa_rdm_ope_post_send(struct efa_rdm_ope *ope, int pkt_type)
411 {
412 return g_efa_unit_test_mocks.efa_rdm_ope_post_send(ope, pkt_type);
413 }
414
415 bool __wrap_efa_device_support_unsolicited_write_recv(void)
416 {
417 return g_efa_unit_test_mocks.efa_device_support_unsolicited_write_recv();
418 }
419
420 enum ibv_fork_status __wrap_ibv_is_fork_initialized(void)
421 {
422 return g_efa_unit_test_mocks.ibv_is_fork_initialized();
423 }
424
425 enum ibv_fork_status efa_mock_ibv_is_fork_initialized_return_mock(void)
426 {
427 return mock();
428 }
429
430 #if HAVE_EFADV_QUERY_MR
431 int __wrap_efadv_query_mr(struct ibv_mr *ibv_mr, struct efadv_mr_attr *attr, uint32_t inlen)
432 {
433 return g_efa_unit_test_mocks.efadv_query_mr(ibv_mr, attr, inlen);
434 }
435
436 /* set recv_ic_id as 0 */
437 int efa_mock_efadv_query_mr_recv_ic_id_0(struct ibv_mr *ibv_mr, struct efadv_mr_attr *attr, uint32_t inlen)
438 {
439 attr->ic_id_validity = EFADV_MR_ATTR_VALIDITY_RECV_IC_ID;
440 attr->recv_ic_id = 0;
441 return 0;
442 }
443
444 /* set rdma_read_ic_id id as 1 */
445 int efa_mock_efadv_query_mr_rdma_read_ic_id_1(struct ibv_mr *ibv_mr, struct efadv_mr_attr *attr, uint32_t inlen)
446 {
447 attr->ic_id_validity = EFADV_MR_ATTR_VALIDITY_RDMA_READ_IC_ID;
448 attr->rdma_read_ic_id = 1;
449 return 0;
450 }
451
452 /* set rdma_recv_ic_id id as 2 */
453 int efa_mock_efadv_query_mr_rdma_recv_ic_id_2(struct ibv_mr *ibv_mr, struct efadv_mr_attr *attr, uint32_t inlen)
454 {
455 attr->ic_id_validity = EFADV_MR_ATTR_VALIDITY_RDMA_RECV_IC_ID;
456 attr->rdma_recv_ic_id = 2;
457 return 0;
458 }
459
460 /* set recv_ic_id id as 0, rdma_read_ic_id as 1 */
461 int efa_mock_efadv_query_mr_recv_and_rdma_read_ic_id_0_1(struct ibv_mr *ibv_mr, struct efadv_mr_attr *attr, uint32_t inlen)
462 {
463 attr->ic_id_validity = EFADV_MR_ATTR_VALIDITY_RECV_IC_ID;
464 attr->recv_ic_id = 0;
465 attr->ic_id_validity |= EFADV_MR_ATTR_VALIDITY_RDMA_READ_IC_ID;
466 attr->rdma_read_ic_id = 1;
467 return 0;
468 }
469
470 #endif /* HAVE_EFADV_QUERY_MR */
471
472 #if HAVE_EFA_DATA_IN_ORDER_ALIGNED_128_BYTES
473 int __wrap_ibv_query_qp_data_in_order(struct ibv_qp *qp, enum ibv_wr_opcode op, uint32_t flags)
474 {
475 return g_efa_unit_test_mocks.ibv_query_qp_data_in_order(qp, op, flags);
476 }
477
478 int efa_mock_ibv_query_qp_data_in_order_return_0(struct ibv_qp *qp, enum ibv_wr_opcode op, uint32_t flags)
479 {
480 return 0;
481 }
482
483 int efa_mock_ibv_query_qp_data_in_order_return_in_order_aligned_128_bytes(struct ibv_qp *qp, enum ibv_wr_opcode op, uint32_t flags)
484 {
485 return IBV_QUERY_QP_DATA_IN_ORDER_ALIGNED_128_BYTES;
486 }
487 #endif