Debian Codegraph @

prov/efa/test/efa_unit_test_common.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 #include "efa_unit_tests.h"
5 #include "efa_cq.h"
6 #include "efa_rdm_pke_utils.h"
7 #include "efa_rdm_pke_nonreq.h"
8 #include "efa_rdm_pke_req.h"
9
10 void efa_unit_test_construct_msg(struct fi_msg *msg, struct iovec *iov,
11 size_t iov_count, fi_addr_t addr,
12 void *context, uint64_t data,
13 void **desc)
14 {
15 msg->msg_iov = iov;
16 msg->iov_count = iov_count;
17 msg->addr = addr;
18 msg->context = context;
19 msg->data = data;
20 msg->desc = desc;
21 }
22
23 void efa_unit_test_construct_tmsg(struct fi_msg_tagged *tmsg, struct iovec *iov,
24 size_t iov_count, fi_addr_t addr,
25 void *context, uint64_t data,
26 void **desc, uint64_t tag,
27 uint64_t ignore)
28 {
29 tmsg->msg_iov = iov;
30 tmsg->iov_count = iov_count;
31 tmsg->addr = addr;
32 tmsg->context = context;
33 tmsg->data = data;
34 tmsg->desc = desc;
35 tmsg->tag = tag;
36 tmsg->ignore = ignore;
37 }
38
39 void efa_unit_test_construct_msg_rma(struct fi_msg_rma *msg, struct iovec *iov,
40 void **desc, size_t iov_count,
41 fi_addr_t addr, struct fi_rma_iov *rma_iov,
42 size_t rma_iov_count, void *context,
43 uint64_t data)
44 {
45 msg->msg_iov = iov;
46 msg->desc = desc;
47 msg->iov_count = iov_count;
48 msg->addr = addr;
49 msg->rma_iov = rma_iov;
50 msg->rma_iov_count = rma_iov_count;
51 msg->context = context;
52 msg->data = data;
53 }
54
55 struct fi_info *efa_unit_test_alloc_hints(enum fi_ep_type ep_type, char *fabric_name)
56 {
57 struct fi_info *hints;
58
59 hints = fi_allocinfo();
60 if (!hints)
61 return NULL;
62
63 if (fabric_name)
64 hints->fabric_attr->name = strdup(fabric_name);
65 hints->ep_attr->type = ep_type;
66
67 /* Use a minimal caps that efa / efa-direct should always support */
68 hints->domain_attr->mr_mode = MR_MODE_BITS;
69
70 /* EFA direct and dgram paths require FI_CONTEXT2 */
71 if (!fabric_name || !strcasecmp(fabric_name, EFA_DIRECT_FABRIC_NAME))
72 hints->mode |= FI_CONTEXT2;
73
74 if (ep_type == FI_EP_DGRAM) {
75 hints->mode |= FI_MSG_PREFIX | FI_CONTEXT2;
76 }
77
78 return hints;
79 }
80
81 struct fi_info *efa_unit_test_alloc_hints_hmem(enum fi_ep_type ep_type, char *fabric_name)
82 {
83 struct fi_info *hints;
84
85 hints = efa_unit_test_alloc_hints(ep_type, fabric_name);
86
87 hints->caps |= FI_HMEM;
88 hints->domain_attr->mr_mode |= FI_MR_HMEM;
89
90 return hints;
91 }
92
93 void efa_unit_test_resource_construct_with_hints(struct efa_resource *resource,
94 enum fi_ep_type ep_type,
95 uint32_t fi_version, struct fi_info *hints,
96 bool enable_ep, bool open_cq)
97 {
98 int ret = 0;
99 struct fi_av_attr av_attr = {0};
100 struct fi_cq_attr cq_attr = {
101 .format = FI_CQ_FORMAT_DATA
102 };
103 struct fi_eq_attr eq_attr = {0};
104
105 ret = fi_getinfo(fi_version, NULL, NULL, 0ULL, hints, &resource->info);
106 if (ret)
107 goto err;
108
109 ret = fi_fabric(resource->info->fabric_attr, &resource->fabric, NULL);
110 if (ret)
111 goto err;
112
113 ret = fi_domain(resource->fabric, resource->info, &resource->domain, NULL);
114 if (ret)
115 goto err;
116
117 ret = fi_endpoint(resource->domain, resource->info, &resource->ep, NULL);
118 if (ret)
119 goto err;
120
121 ret = fi_eq_open(resource->fabric, &eq_attr, &resource->eq, NULL);
122 if (ret)
123 goto err;
124
125 fi_ep_bind(resource->ep, &resource->eq->fid, 0);
126
127 ret = fi_av_open(resource->domain, &av_attr, &resource->av, NULL);
128 if (ret)
129 goto err;
130
131 fi_ep_bind(resource->ep, &resource->av->fid, 0);
132
133 if (open_cq) {
134 ret = fi_cq_open(resource->domain, &cq_attr, &resource->cq, NULL);
135 if (ret)
136 goto err;
137
138 fi_ep_bind(resource->ep, &resource->cq->fid, FI_SEND | FI_RECV);
139 }
140
141 if (enable_ep) {
142 ret = fi_enable(resource->ep);
143 if (ret)
144 goto err;
145 }
146
147 return;
148
149 err:
150 efa_unit_test_resource_destruct(resource);
151
152 /* Fail test early if the resource struct fails to initialize */
153 assert_int_equal(ret, 0);
154 }
155
156 void efa_unit_test_resource_construct(struct efa_resource *resource, enum fi_ep_type ep_type, char *fabric_name)
157 {
158
159 resource->hints = efa_unit_test_alloc_hints(ep_type, fabric_name);
160 if (!resource->hints)
161 goto err;
162 if (!strcmp(EFA_DIRECT_FABRIC_NAME, fabric_name))
163 efa_unit_test_resource_construct_with_hints(resource, ep_type, FI_VERSION(2, 0),
164 resource->hints, true, true);
165 else
166 efa_unit_test_resource_construct_with_hints(resource, ep_type, FI_VERSION(1, 14),
167 resource->hints, true, true);
168 return;
169
170 err:
171 efa_unit_test_resource_destruct(resource);
172
173 /* Fail test early if the resource struct fails to initialize */
174 assert_int_equal(1, 0);
175 }
176
177 void efa_unit_test_resource_construct_ep_not_enabled(struct efa_resource *resource,
178 enum fi_ep_type ep_type, char *fabric_name)
179 {
180 resource->hints = efa_unit_test_alloc_hints(ep_type, fabric_name);
181 if (!resource->hints)
182 goto err;
183
184 if (!strcmp(EFA_DIRECT_FABRIC_NAME, fabric_name))
185 efa_unit_test_resource_construct_with_hints(resource, ep_type, FI_VERSION(2, 0),
186 resource->hints, false, true);
187 else
188 efa_unit_test_resource_construct_with_hints(resource, ep_type, FI_VERSION(1, 14),
189 resource->hints, false, true);
190 return;
191
192 err:
193 efa_unit_test_resource_destruct(resource);
194
195 /* Fail test early if the resource struct fails to initialize */
196 fail();
197 }
198
199 void efa_unit_test_resource_construct_no_cq_and_ep_not_enabled(struct efa_resource *resource,
200 enum fi_ep_type ep_type, char *fabric_name)
201 {
202 resource->hints = efa_unit_test_alloc_hints(ep_type, fabric_name);
203 if (!resource->hints)
204 goto err;
205
206 if (!strcmp(EFA_DIRECT_FABRIC_NAME, fabric_name))
207 efa_unit_test_resource_construct_with_hints(resource, ep_type, FI_VERSION(2, 0),
208 resource->hints, false, false);
209 else
210 efa_unit_test_resource_construct_with_hints(resource, ep_type, FI_VERSION(1, 14),
211 resource->hints, false, false);
212 return;
213
214 err:
215 efa_unit_test_resource_destruct(resource);
216
217 /* Fail test early if the resource struct fails to initialize */
218 fail();
219 }
220
221 /**
222 * @brief Construct RDM ep type resources with shm disabled
223 */
224 void efa_unit_test_resource_construct_rdm_shm_disabled(struct efa_resource *resource)
225 {
226 int ret;
227 bool shm_permitted = false;
228
229 resource->hints = efa_unit_test_alloc_hints(FI_EP_RDM, EFA_FABRIC_NAME);
230 if (!resource->hints)
231 goto err;
232
233 efa_unit_test_resource_construct_with_hints(resource, FI_EP_RDM, FI_VERSION(1, 14),
234 resource->hints, false, true);
235
236 ret = fi_setopt(&resource->ep->fid, FI_OPT_ENDPOINT,
237 FI_OPT_SHARED_MEMORY_PERMITTED, &shm_permitted,
238 sizeof(shm_permitted));
239 if (ret)
240 goto err;
241
242 ret = fi_enable(resource->ep);
243 if (ret)
244 goto err;
245
246 return;
247 err:
248 efa_unit_test_resource_destruct(resource);
249
250 /* Fail test early if the resource struct fails to initialize */
251 fail();
252 }
253
254 /**
255 * @brief Clean up test resources.
256 * Note: Resources should be destroyed in order.
257 * @param[in] resource struct efa_resource to clean up.
258 */
259 void efa_unit_test_resource_destruct(struct efa_resource *resource)
260 {
261 if (resource->ep) {
262 assert_int_equal(fi_close(&resource->ep->fid), 0);
263 }
264
265 if (resource->eq) {
266 assert_int_equal(fi_close(&resource->eq->fid), 0);
267 }
268
269 if (resource->cq) {
270 assert_int_equal(fi_close(&resource->cq->fid), 0);
271 }
272
273 if (resource->av) {
274 assert_int_equal(fi_close(&resource->av->fid), 0);
275 }
276
277 if (resource->domain) {
278 assert_int_equal(fi_close(&resource->domain->fid), 0);
279 }
280
281 if (resource->fabric) {
282 assert_int_equal(fi_close(&resource->fabric->fid), 0);
283 }
284
285 if (resource->info) {
286 fi_freeinfo(resource->info);
287 }
288
289 if (resource->hints) {
290 fi_freeinfo(resource->hints);
291 }
292 }
293
294 void efa_unit_test_buff_construct(struct efa_unit_test_buff *buff, struct efa_resource *resource, size_t buff_size)
295 {
296 int err;
297
298 buff->buff = calloc(buff_size, sizeof(uint8_t));
299 assert_non_null(buff->buff);
300
301 buff->size = buff_size;
302 err = fi_mr_reg(resource->domain, buff->buff, buff_size, FI_SEND | FI_RECV,
303 0 /*offset*/, 0 /*requested_key*/, 0 /*flags*/, &buff->mr, NULL);
304 assert_int_equal(err, 0);
305 }
306
307 void efa_unit_test_buff_destruct(struct efa_unit_test_buff *buff)
308 {
309 int err;
310
311 assert_non_null(buff->mr);
312 err = fi_close(&buff->mr->fid);
313 assert_int_equal(err, 0);
314
315 free(buff->buff);
316 }
317
318 /**
319 * @brief Construct EFA_RDM_EAGER_MSGRTM_PKT
320 *
321 * @param[in] pkt_entry Packet entry. Must be non-NULL.
322 * @param[in] attr Packet attributes.
323 */
324 void efa_unit_test_eager_msgrtm_pkt_construct(struct efa_rdm_pke *pkt_entry, struct efa_unit_test_eager_rtm_pkt_attr *attr)
325 {
326 struct efa_rdm_eager_msgrtm_hdr base_hdr = {0};
327 struct efa_rdm_req_opt_connid_hdr opt_connid_hdr = {0};
328 uint32_t *connid = NULL;
329
330 base_hdr.hdr.type = EFA_RDM_EAGER_MSGRTM_PKT;
331 base_hdr.hdr.flags |= EFA_RDM_PKT_CONNID_HDR | EFA_RDM_REQ_MSG;
332 base_hdr.hdr.msg_id = attr->msg_id;
333 memcpy(pkt_entry->wiredata, &base_hdr, sizeof(struct efa_rdm_eager_msgrtm_hdr));
334 assert_int_equal(efa_rdm_pke_get_base_hdr(pkt_entry)->type, EFA_RDM_EAGER_MSGRTM_PKT);
335 assert_int_equal(efa_rdm_pke_get_req_base_hdr_size(pkt_entry), sizeof(struct efa_rdm_eager_msgrtm_hdr));
336 opt_connid_hdr.connid = attr->connid;
337 memcpy(pkt_entry->wiredata + sizeof(struct efa_rdm_eager_msgrtm_hdr), &opt_connid_hdr, sizeof(struct efa_rdm_req_opt_connid_hdr));
338 connid = efa_rdm_pke_connid_ptr(pkt_entry);
339 assert_int_equal(*connid, attr->connid);
340 pkt_entry->pkt_size = sizeof(base_hdr) + sizeof(opt_connid_hdr);
341 }
342
343 #define APPEND_OPT_HANDSHAKE_FIELD(field, opt_flag) \
344 if (attr->field) { \
345 struct efa_rdm_handshake_opt_##field##_hdr *_hdr = \
346 (struct efa_rdm_handshake_opt_##field##_hdr *) \
347 (pkt_entry->wiredata + pkt_entry->pkt_size); \
348 _hdr->field = attr->field; \
349 handshake_hdr->flags |= opt_flag; \
350 pkt_entry->pkt_size += sizeof *_hdr; \
351 }
352
353 /**
354 * @brief Construct EFA_RDM_HANDSHAKE_PKT
355 *
356 * This will append any optional handshake packet fields (see EFA RDM protocol
357 * spec) iff they are non-zero in attr
358 *
359 * @param[in,out] pkt_entry Packet entry. Must be non-NULL.
360 * @param[in] attr Packet attributes.
361 */
362 void efa_unit_test_handshake_pkt_construct(struct efa_rdm_pke *pkt_entry, struct efa_unit_test_handshake_pkt_attr *attr)
363 {
364
365 int nex = (EFA_RDM_NUM_EXTRA_FEATURE_OR_REQUEST - 1) / 64 + 1;
366 struct efa_rdm_handshake_hdr *handshake_hdr = (struct efa_rdm_handshake_hdr *)pkt_entry->wiredata;
367
368 handshake_hdr->type = EFA_RDM_HANDSHAKE_PKT;
369 handshake_hdr->version = EFA_RDM_PROTOCOL_VERSION;
370 handshake_hdr->nextra_p3 = nex + 3;
371 handshake_hdr->flags = 0;
372
373 pkt_entry->pkt_size = sizeof(struct efa_rdm_handshake_hdr) + nex * sizeof(uint64_t);
374
375 APPEND_OPT_HANDSHAKE_FIELD(connid, EFA_RDM_PKT_CONNID_HDR);
376 APPEND_OPT_HANDSHAKE_FIELD(host_id, EFA_RDM_HANDSHAKE_HOST_ID_HDR);
377 APPEND_OPT_HANDSHAKE_FIELD(device_version, EFA_RDM_HANDSHAKE_DEVICE_VERSION_HDR);
378 }
379
380
381 struct efa_rdm_ope *efa_unit_test_alloc_txe(struct efa_resource *resource, uint32_t op)
382 {
383 fi_addr_t peer_addr = 0;
384 struct efa_ep_addr raw_addr = {0};
385 size_t raw_addr_len = sizeof(raw_addr);
386 struct efa_rdm_peer *peer;
387 struct fi_msg msg = {0};
388 struct efa_rdm_ep *efa_rdm_ep;
389
390 efa_rdm_ep = container_of(resource->ep, struct efa_rdm_ep, base_ep.util_ep.ep_fid);
391
392 /* Create and register a fake peer */
393 assert_int_equal(fi_getname(&resource->ep->fid, &raw_addr, &raw_addr_len), 0);
394 raw_addr.qpn = 0;
395 raw_addr.qkey = 0x1234;
396
397 assert_int_equal(fi_av_insert(resource->av, &raw_addr, 1, &peer_addr, 0, NULL), 1);
398
399 peer = efa_rdm_ep_get_peer(efa_rdm_ep, peer_addr);
400
401 return efa_rdm_ep_alloc_txe(efa_rdm_ep, peer, &msg, op, 0, 0);
402 }
403
404 struct efa_rdm_ope *efa_unit_test_alloc_rxe(struct efa_resource *resource, uint32_t op)
405 {
406 fi_addr_t peer_addr = 0;
407 struct efa_ep_addr raw_addr = {0};
408 size_t raw_addr_len = sizeof(raw_addr);
409 struct efa_rdm_ep *efa_rdm_ep;
410
411 efa_rdm_ep = container_of(resource->ep, struct efa_rdm_ep, base_ep.util_ep.ep_fid);
412
413 /* Create and register a fake peer */
414 assert_int_equal(fi_getname(&resource->ep->fid, &raw_addr, &raw_addr_len), 0);
415 raw_addr.qpn = 0;
416 raw_addr.qkey = 0x1234;
417
418 assert_int_equal(fi_av_insert(resource->av, &raw_addr, 1, &peer_addr, 0, NULL), 1);
419
420 return efa_rdm_ep_alloc_rxe(efa_rdm_ep, peer_addr, op);
421 }