Debian Codegraph @

prov/efa/test/efa_unit_test_info.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_prov_info.h"
6
7 /**
8 * @brief test that when a wrong fi_info was used to open resource, the error is handled
9 * gracefully
10 */
11 void test_info_open_ep_with_wrong_info()
12 {
13 struct fi_info *hints, *info;
14 struct fid_fabric *fabric = NULL;
15 struct fid_domain *domain = NULL;
16 struct fid_ep *ep = NULL;
17 int err;
18
19 hints = efa_unit_test_alloc_hints(FI_EP_DGRAM, EFA_FABRIC_NAME);
20
21 err = fi_getinfo(FI_VERSION(1, 14), NULL, NULL, 0ULL, hints, &info);
22 assert_int_equal(err, 0);
23
24 /* dgram endpoint require FI_MSG_PREFIX */
25 assert_int_equal(info->mode, FI_MSG_PREFIX | FI_CONTEXT2);
26
27 /* make the info wrong by setting the mode to 0 */
28 info->mode = 0;
29
30 err = fi_fabric(info->fabric_attr, &fabric, NULL);
31 assert_int_equal(err, 0);
32
33 err = fi_domain(fabric, info, &domain, NULL);
34 assert_int_equal(err, 0);
35
36 /* because of the error in the info object, fi_endpoint() should fail with -FI_ENODATA */
37 err = fi_endpoint(domain, info, &ep, NULL);
38 assert_int_equal(err, -FI_ENODATA);
39 assert_null(ep);
40
41 err = fi_close(&domain->fid);
42 assert_int_equal(err, 0);
43
44 err = fi_close(&fabric->fid);
45 assert_int_equal(err, 0);
46 }
47
48 /**
49 * @brief Verify that efa rdm path fi_info objects have some expected values
50 */
51 void test_info_rdm_attributes()
52 {
53 struct fi_info *hints, *info = NULL, *info_head = NULL;
54 int err;
55
56 hints = efa_unit_test_alloc_hints(FI_EP_RDM, EFA_FABRIC_NAME);
57 assert_non_null(hints);
58
59 err = fi_getinfo(FI_VERSION(1,6), NULL, NULL, 0, hints, &info_head);
60 assert_int_equal(err, 0);
61 assert_non_null(info_head);
62
63 for (info = info_head; info; info = info->next) {
64 assert_true(!strcmp(info->fabric_attr->name, EFA_FABRIC_NAME));
65 assert_true(strstr(info->domain_attr->name, "rdm"));
66 assert_int_equal(info->ep_attr->max_msg_size, UINT64_MAX);
67 assert_int_equal(info->domain_attr->progress, FI_PROGRESS_MANUAL);
68 assert_int_equal(info->domain_attr->control_progress, FI_PROGRESS_MANUAL);
69 #if HAVE_CUDA || HAVE_NEURON || HAVE_SYNAPSEAI
70 assert_true(info->caps | FI_HMEM);
71 #endif
72 }
73 }
74
75 /**
76 * @brief Verify that efa dgram path fi_info objects have some expected values
77 */
78 void test_info_dgram_attributes()
79 {
80 struct fi_info *hints, *info = NULL, *info_head = NULL;
81 int err;
82
83 hints = efa_unit_test_alloc_hints(FI_EP_DGRAM, EFA_FABRIC_NAME);
84 assert_non_null(hints);
85
86 err = fi_getinfo(FI_VERSION(1,6), NULL, NULL, 0, hints, &info_head);
87 assert_int_equal(err, 0);
88 assert_non_null(info_head);
89
90 for (info = info_head; info; info = info->next) {
91 assert_true(!strcmp(info->fabric_attr->name, EFA_FABRIC_NAME));
92 assert_true(strstr(info->domain_attr->name, "dgrm"));
93 }
94 }
95
96 /**
97 * @brief Verify that efa direct path fi_info objects have some expected values
98 */
99 static void test_info_direct_attributes_impl(struct fi_info *hints,
100 int expected_return)
101 {
102 struct fi_info *info = NULL, *info_head = NULL;
103 int err;
104
105 err = fi_getinfo(FI_VERSION(1, 6), NULL, NULL, 0, hints, &info_head);
106 assert_int_equal(err, expected_return);
107 if (expected_return)
108 return;
109
110 assert_non_null(info_head);
111 for (info = info_head; info; info = info->next) {
112 assert_true(!strcmp(info->fabric_attr->name,
113 EFA_DIRECT_FABRIC_NAME));
114 assert_true(strstr(info->domain_attr->name, "rdm"));
115 assert_false(info->caps & (FI_ATOMIC | FI_TAGGED));
116 assert_false(info->tx_attr->msg_order & FI_ORDER_SAS);
117 assert_int_equal(info->domain_attr->progress, FI_PROGRESS_AUTO);
118 assert_int_equal(info->domain_attr->control_progress, FI_PROGRESS_AUTO);
119 assert_int_equal(
120 g_device_list[0].rdm_info->ep_attr->max_msg_size,
121 (info->caps & FI_RMA) ?
122 g_device_list[0].max_rdma_size :
123 g_device_list[0].ibv_port_attr.max_msg_sz);
124 assert_int_equal(
125 info->ep_attr->max_msg_size,
126 (hints->caps & FI_RMA) ?
127 g_device_list[0].max_rdma_size :
128 g_device_list[0].ibv_port_attr.max_msg_sz);
129 }
130
131 fi_freeinfo(info_head);
132 }
133
134 void test_info_direct_attributes_no_rma()
135 {
136 struct fi_info *hints;
137
138 hints = efa_unit_test_alloc_hints(FI_EP_RDM, EFA_DIRECT_FABRIC_NAME);
139 assert_non_null(hints);
140
141 /* The default hints for efa-direct only has FI_MSG cap, so it should
142 * succeed anyway */
143 test_info_direct_attributes_impl(hints, FI_SUCCESS);
144 fi_freeinfo(hints);
145 }
146
147 void test_info_direct_attributes_rma()
148 {
149 struct fi_info *hints;
150 bool support_fi_rma = (efa_device_support_rdma_read() &&
151 efa_device_support_rdma_write() &&
152 efa_device_support_unsolicited_write_recv());
153
154 int expected_return = support_fi_rma ? FI_SUCCESS : -FI_ENODATA;
155
156 hints = efa_unit_test_alloc_hints(FI_EP_RDM, EFA_DIRECT_FABRIC_NAME);
157 assert_non_null(hints);
158
159 hints->caps |= FI_RMA;
160 test_info_direct_attributes_impl(hints, expected_return);
161 fi_freeinfo(hints);
162 }
163
164 /**
165 * @brief Verify that efa direct only supports HMEM with p2p
166 */
167 #if HAVE_CUDA || HAVE_NEURON || HAVE_SYNAPSEAI
168 void test_info_direct_hmem_support_p2p()
169 {
170 struct fi_info *info;
171 bool hmem_ops_cuda_init;
172
173 info = fi_allocinfo();
174
175 memset(g_efa_hmem_info, 0, OFI_HMEM_MAX * sizeof(struct efa_hmem_info));
176
177 /* Save current value of hmem_ops[FI_HMEM_CUDA].initialized to reset later
178 * hmem_ops is populated in ofi_hmem_init and only runs once
179 *
180 * CUDA iface will be initialized on Nvidia GPU platforms but not on others
181 * Force setting hmem_ops[FI_HMEM_CUDA].initialized allows this test to
182 * run on all instance types
183 */
184 hmem_ops_cuda_init = hmem_ops[FI_HMEM_CUDA].initialized;
185 hmem_ops[FI_HMEM_CUDA].initialized = true;
186
187 /* g_efa_hmem_info is backed up and reset after every test in
188 * efa_unit_test_mocks_teardown. So no need to save and reset these fields
189 */
190 g_efa_hmem_info[FI_HMEM_CUDA].initialized = true;
191 g_efa_hmem_info[FI_HMEM_CUDA].p2p_supported_by_device = true;
192
193 efa_prov_info_set_hmem_flags(info, FI_EP_RDM);
194 assert_true(info->caps & FI_HMEM);
195 assert_true(info->tx_attr->caps & FI_HMEM);
196 assert_true(info->rx_attr->caps & FI_HMEM);
197 fi_freeinfo(info);
198
199 info = fi_allocinfo();
200 g_efa_hmem_info[FI_HMEM_CUDA].initialized = true;
201 g_efa_hmem_info[FI_HMEM_CUDA].p2p_supported_by_device = false;
202
203 efa_prov_info_set_hmem_flags(info, FI_EP_RDM);
204 assert_false(info->caps & FI_HMEM);
205 assert_false(info->tx_attr->caps & FI_HMEM);
206 assert_false(info->rx_attr->caps & FI_HMEM);
207 fi_freeinfo(info);
208
209 /* Reset hmem_ops[FI_HMEM_CUDA].initialized */
210 hmem_ops[FI_HMEM_CUDA].initialized = hmem_ops_cuda_init;
211 }
212 #else
213 void test_info_direct_hmem_support_p2p()
214 {
215 }
216 #endif
217
218 /**
219 * @brief Verify info->tx/rx_attr->msg_order is set according to hints.
220 *
221 */
222 static void
223 test_info_tx_rx_msg_order_from_hints(struct fi_info *hints, int expected_ret)
224 {
225 struct fi_info *info;
226 int err;
227
228 err = fi_getinfo(FI_VERSION(FI_MAJOR_VERSION, FI_MINOR_VERSION), NULL, NULL, 0ULL, hints, &info);
229
230 assert_int_equal(err, expected_ret);
231
232 if (expected_ret == FI_SUCCESS) {
233 assert_true(hints->tx_attr->msg_order == info->tx_attr->msg_order);
234 assert_true(hints->rx_attr->msg_order == info->rx_attr->msg_order);
235 }
236
237 fi_freeinfo(info);
238 }
239
240 /**
241 * @brief Verify info->tx/rx_attr->op_flags is set according to hints.
242 *
243 */
244 static void
245 test_info_tx_rx_op_flags_from_hints(struct fi_info *hints, int expected_ret)
246 {
247 struct fi_info *info;
248 int err;
249
250 err = fi_getinfo(FI_VERSION(FI_MAJOR_VERSION, FI_MINOR_VERSION), NULL, NULL, 0ULL, hints, &info);
251
252 assert_int_equal(err, expected_ret);
253
254 if (expected_ret == FI_SUCCESS) {
255 assert_true(hints->tx_attr->op_flags == info->tx_attr->op_flags);
256 assert_true(hints->rx_attr->op_flags == info->rx_attr->op_flags);
257 }
258
259 fi_freeinfo(info);
260 }
261
262 /**
263 * @brief Verify info->tx/rx_attr->size is set according to hints.
264 *
265 */
266 static void test_info_tx_rx_size_from_hints(struct fi_info *hints, int expected_ret)
267 {
268 struct fi_info *info;
269 int err;
270
271 err = fi_getinfo(FI_VERSION(FI_MAJOR_VERSION, FI_MINOR_VERSION), NULL, NULL, 0ULL, hints, &info);
272
273 assert_int_equal(err, expected_ret);
274
275 if (expected_ret == FI_SUCCESS) {
276 assert_true(hints->tx_attr->size == info->tx_attr->size);
277 assert_true(hints->rx_attr->size == info->rx_attr->size);
278 }
279
280 fi_freeinfo(info);
281 }
282
283 void test_info_tx_rx_msg_order_rdm_order_none(struct efa_resource **state)
284 {
285 struct efa_resource *resource = *state;
286
287 resource->hints = efa_unit_test_alloc_hints(FI_EP_RDM, EFA_FABRIC_NAME);
288 assert_non_null(resource->hints);
289
290 test_info_tx_rx_msg_order_from_hints(resource->hints, 0);
291 }
292
293 void test_info_tx_rx_msg_order_rdm_order_sas(struct efa_resource **state)
294 {
295 struct efa_resource *resource = *state;
296
297 resource->hints = efa_unit_test_alloc_hints(FI_EP_RDM, EFA_FABRIC_NAME);
298 assert_non_null(resource->hints);
299
300 resource->hints->tx_attr->msg_order = FI_ORDER_SAS;
301 resource->hints->rx_attr->msg_order = FI_ORDER_SAS;
302 test_info_tx_rx_msg_order_from_hints(resource->hints, 0);
303 }
304
305 void test_info_tx_rx_msg_order_dgram_order_none(struct efa_resource **state)
306 {
307 struct efa_resource *resource = *state;
308
309 resource->hints = efa_unit_test_alloc_hints(FI_EP_DGRAM, EFA_FABRIC_NAME);
310 assert_non_null(resource->hints);
311
312 test_info_tx_rx_msg_order_from_hints(resource->hints, 0);
313 }
314
315 /**
316 * @brief dgram endpoint doesn't support any ordering, so fi_getinfo
317 * should return -FI_ENODATA if hints requests sas
318 */
319 void test_info_tx_rx_msg_order_dgram_order_sas(struct efa_resource **state)
320 {
321 struct efa_resource *resource = *state;
322
323 resource->hints = efa_unit_test_alloc_hints(FI_EP_DGRAM, EFA_FABRIC_NAME);
324 assert_non_null(resource->hints);
325
326 resource->hints->tx_attr->msg_order = FI_ORDER_SAS;
327 resource->hints->rx_attr->msg_order = FI_ORDER_SAS;
328 test_info_tx_rx_msg_order_from_hints(resource->hints, -FI_ENODATA);
329 }
330
331 /**
332 * @brief Verify max order size is set correctly according to hints
333 *
334 * @param hints hints
335 * @param expected_ret expected rc of fi_getinfo
336 * @param expected_size expected value of max_order_*_size. Ignored when expected_ret is non-zero.
337 */
338 static void
339 test_info_max_order_size_from_hints(struct fi_info *hints, int expected_ret, size_t expected_size)
340 {
341 struct fi_info *info;
342 int err;
343
344 err = fi_getinfo(FI_VERSION(FI_MAJOR_VERSION, FI_MINOR_VERSION), NULL, NULL, 0ULL, hints, &info);
345
346 assert_int_equal(err, expected_ret);
347
348 if (expected_ret == FI_SUCCESS) {
349 assert_true(info->ep_attr->max_order_raw_size == expected_size);
350 assert_true(info->ep_attr->max_order_war_size == expected_size);
351 assert_true(info->ep_attr->max_order_waw_size == expected_size);
352 }
353
354 fi_freeinfo(info);
355 }
356
357 /**
358 * DGRAM ep type doesn't support FI_ATOMIC, fi_getinfo should return
359 * ENODATA when FI_ATOMIC is requested in hints.
360 */
361 void test_info_max_order_size_dgram_with_atomic(struct efa_resource **state)
362 {
363 struct efa_resource *resource = *state;
364
365 resource->hints = efa_unit_test_alloc_hints(FI_EP_DGRAM, EFA_FABRIC_NAME);
366 assert_non_null(resource->hints);
367
368 resource->hints->caps = FI_ATOMIC;
369
370 test_info_max_order_size_from_hints(resource->hints, -FI_ENODATA, 0);
371 }
372
373 /**
374 * RDM ep type supports FI_ATOMIC. When FI_ORDER_ATOMIC_* is NOT requested,
375 * max_order_*_size should be 0
376 */
377 void test_info_max_order_size_rdm_with_atomic_no_order(struct efa_resource **state)
378 {
379 struct efa_resource *resource = *state;
380
381 resource->hints = efa_unit_test_alloc_hints(FI_EP_RDM, EFA_FABRIC_NAME);
382 assert_non_null(resource->hints);
383
384
385 resource->hints->caps = FI_ATOMIC;
386 resource->hints->domain_attr->mr_mode |= FI_MR_VIRT_ADDR | FI_MR_PROV_KEY;
387
388 test_info_max_order_size_from_hints(resource->hints, FI_SUCCESS, 0);
389 }
390
391 /**
392 * RDM ep type supports FI_ATOMIC. When FI_ORDER_ATOMIC_* is requested,
393 * max_order_*_size should be the max atomic size derived from mtu and headers
394 */
395 void test_info_max_order_size_rdm_with_atomic_order(struct efa_resource **state)
396 {
397 struct efa_resource *resource = *state;
398 size_t max_atomic_size = g_device_list[0].ibv_port_attr.max_msg_sz
399 - sizeof(struct efa_rdm_rta_hdr)
400 - g_device_list[0].rdm_info->src_addrlen
401 - EFA_RDM_IOV_LIMIT * sizeof(struct fi_rma_iov);
402
403 resource->hints = efa_unit_test_alloc_hints(FI_EP_RDM, EFA_FABRIC_NAME);
404 assert_non_null(resource->hints);
405
406 resource->hints->caps = FI_ATOMIC;
407 resource->hints->domain_attr->mr_mode |= FI_MR_VIRT_ADDR | FI_MR_PROV_KEY;
408 resource->hints->tx_attr->msg_order |= FI_ORDER_ATOMIC_RAR | FI_ORDER_ATOMIC_RAW | FI_ORDER_ATOMIC_WAR | FI_ORDER_ATOMIC_WAW;
409 resource->hints->rx_attr->msg_order = resource->hints->tx_attr->msg_order;
410
411 test_info_max_order_size_from_hints(resource->hints, FI_SUCCESS, max_atomic_size);
412 }
413
414 void test_info_tx_rx_op_flags_rdm(struct efa_resource **state)
415 {
416 struct efa_resource *resource = *state;
417
418 resource->hints = efa_unit_test_alloc_hints(FI_EP_RDM, EFA_FABRIC_NAME);
419 assert_non_null(resource->hints);
420
421 resource->hints->tx_attr->op_flags = FI_DELIVERY_COMPLETE;
422 resource->hints->rx_attr->op_flags = FI_COMPLETION;
423 test_info_tx_rx_op_flags_from_hints(resource->hints, 0);
424 }
425
426 void test_info_tx_rx_size_rdm(struct efa_resource **state)
427 {
428 struct efa_resource *resource = *state;
429
430 resource->hints = efa_unit_test_alloc_hints(FI_EP_RDM, EFA_FABRIC_NAME);
431 assert_non_null(resource->hints);
432
433 resource->hints->tx_attr->size = 16;
434 resource->hints->rx_attr->size = 16;
435 test_info_tx_rx_size_from_hints(resource->hints, 0);
436 }
437
438 static void test_info_check_shm_info_from_hints(struct fi_info *hints)
439 {
440 struct fi_info *info;
441 struct fid_fabric *fabric = NULL;
442 struct fid_domain *domain = NULL;
443 int err;
444 struct efa_domain *efa_domain;
445
446 err = fi_getinfo(FI_VERSION(1, 14), NULL, NULL, 0ULL, hints, &info);
447 /* Do nothing if the current setup does not support FI_HMEM */
448 if (err && hints->caps & FI_HMEM) {
449 return;
450 }
451 assert_int_equal(err, 0);
452
453 err = fi_fabric(info->fabric_attr, &fabric, NULL);
454 assert_int_equal(err, 0);
455
456 err = fi_domain(fabric, info, &domain, NULL);
457 assert_int_equal(err, 0);
458
459 efa_domain = container_of(domain, struct efa_domain, util_domain.domain_fid);
460 if (efa_domain->shm_info) {
461 if (hints->caps & FI_HMEM)
462 assert_true(efa_domain->shm_info->caps & FI_HMEM);
463 else
464 assert_false(efa_domain->shm_info->caps & FI_HMEM);
465
466 assert_true(efa_domain->shm_info->tx_attr->op_flags == info->tx_attr->op_flags);
467
468 assert_true(efa_domain->shm_info->rx_attr->op_flags == info->rx_attr->op_flags);
469
470 if (hints->domain_attr->threading) {
471 assert_true(hints->domain_attr->threading == info->domain_attr->threading);
472 assert_true(hints->domain_attr->threading == efa_domain->shm_info->domain_attr->threading);
473 }
474 }
475
476 fi_close(&domain->fid);
477
478 fi_close(&fabric->fid);
479
480 fi_freeinfo(info);
481 }
482
483 /**
484 * @brief Check shm info created by efa_domain() has correct caps.
485 *
486 */
487 void test_info_check_shm_info_hmem()
488 {
489 struct fi_info *hints;
490
491 hints = efa_unit_test_alloc_hints(FI_EP_RDM, EFA_FABRIC_NAME);
492
493 hints->caps |= FI_HMEM;
494 test_info_check_shm_info_from_hints(hints);
495
496 hints->caps &= ~FI_HMEM;
497 test_info_check_shm_info_from_hints(hints);
498 }
499
500 void test_info_check_shm_info_op_flags()
501 {
502 struct fi_info *hints;
503
504 hints = efa_unit_test_alloc_hints(FI_EP_RDM, EFA_FABRIC_NAME);
505
506 hints->tx_attr->op_flags |= FI_COMPLETION;
507 hints->rx_attr->op_flags |= FI_COMPLETION;
508 test_info_check_shm_info_from_hints(hints);
509
510 hints->tx_attr->op_flags |= FI_DELIVERY_COMPLETE;
511 hints->rx_attr->op_flags |= FI_MULTI_RECV;
512 test_info_check_shm_info_from_hints(hints);
513 }
514
515 void test_info_check_shm_info_threading()
516 {
517 struct fi_info *hints;
518
519 hints = efa_unit_test_alloc_hints(FI_EP_RDM, EFA_FABRIC_NAME);
520
521 hints->domain_attr->threading = FI_THREAD_DOMAIN;
522 test_info_check_shm_info_from_hints(hints);
523 }
524
525 /**
526 * @brief Check that case when a user requested FI_HMEM support
527 * using libfabric API < 1.18,
528 */
529 void test_info_check_hmem_cuda_support_on_api_lt_1_18()
530 {
531 struct fi_info *hints, *info = NULL;
532 int err;
533
534 if (!hmem_ops[FI_HMEM_CUDA].initialized)
535 skip();
536
537 hints = efa_unit_test_alloc_hints(FI_EP_RDM, EFA_FABRIC_NAME);
538
539 hints->caps |= FI_HMEM;
540 hints->domain_attr->mr_mode |= FI_MR_HMEM;
541
542 /* For libfabric API < 1.18,
543 * on a system that support GPUDirect RDMA read,
544 * HMEM cuda is available when GPUDirect RDMA is available,
545 * and environment variable FI_EFA_USE_DEVICE_RDMA set to 1/on/true
546 * otherwise it is not available.
547 */
548 setenv("FI_EFA_USE_DEVICE_RDMA", "1", true /* overwrite */);
549 err = fi_getinfo(FI_VERSION(1, 6), NULL, NULL, 0, hints, &info);
550 if (efa_device_support_rdma_read()) {
551 assert_int_equal(err, 0);
552 fi_freeinfo(info);
553 } else {
554 assert_int_equal(err, -FI_ENODATA);
555 }
556
557 setenv("FI_EFA_USE_DEVICE_RDMA", "0", true /* overwrite */);
558 err = fi_getinfo(FI_VERSION(1, 14), NULL, NULL, 0, hints, &info);
559 assert_int_equal(err, -FI_ENODATA);
560
561 unsetenv("FI_EFA_USE_DEVICE_RDMA");
562 }
563
564 /**
565 * @brief Check that case when a user requested FI_HMEM support
566 * using libfabric API >= 1.18,
567 */
568 void test_info_check_hmem_cuda_support_on_api_ge_1_18()
569 {
570 struct fi_info *hints, *info = NULL;
571 int err;
572
573 if (!hmem_ops[FI_HMEM_CUDA].initialized)
574 skip();
575
576 hints = efa_unit_test_alloc_hints(FI_EP_RDM, EFA_FABRIC_NAME);
577
578 hints->caps |= FI_HMEM;
579 hints->domain_attr->mr_mode |= FI_MR_HMEM;
580
581 /* Piror to version 1.18, libfabric EFA provider support CUDA
582 * memory only when GPUDirect RDMA is available.
583 * In version 1.18, libfabric EFA provider implemented
584 * universal CUDA support through CUDA library.
585 * However, this feature (universal CUDA support) can cause some
586 * middleware to deadlock, thus it is only available
587 * when a user is using 1.18 API.
588 */
589 err = fi_getinfo(FI_VERSION(1, 18), NULL, NULL, 0, hints, &info);
590 assert_int_equal(err, 0);
591 fi_freeinfo(info);
592 }
593
594 void check_no_hmem_support_when_not_requested(char *fabric_name)
595 {
596 struct fi_info *hints, *info = NULL;
597 int err;
598
599 hints = efa_unit_test_alloc_hints(FI_EP_RDM, fabric_name);
600
601 err = fi_getinfo(FI_VERSION(1,6), NULL, NULL, 0, hints, &info);
602 assert_int_equal(err, 0);
603 assert_non_null(info);
604 assert_false(info->caps & FI_HMEM);
605 fi_freeinfo(info);
606 }
607
608 /**
609 * @brief Check that EFA does not claim support of FI_HMEM when
610 * it is not requested
611 */
612 void test_info_check_no_hmem_support_when_not_requested() {
613 check_no_hmem_support_when_not_requested(EFA_FABRIC_NAME);
614 check_no_hmem_support_when_not_requested(EFA_DIRECT_FABRIC_NAME);
615 }
616
617 /**
618 * @brief Check that EFA direct info object is not returned when atomic
619 * or ordering capabilities are requested
620 */
621 void test_info_direct_unsupported()
622 {
623 struct fi_info *hints, *info = NULL;
624 int err;
625
626 hints = efa_unit_test_alloc_hints(FI_EP_RDM, EFA_DIRECT_FABRIC_NAME);
627 assert_non_null(hints);
628
629 err = fi_getinfo(FI_VERSION(1,6), NULL, NULL, 0, hints, &info);
630 assert_int_equal(err, 0);
631 assert_non_null(info);
632
633 hints->caps |= FI_ATOMIC;
634 err = fi_getinfo(FI_VERSION(1,6), NULL, NULL, 0, hints, &info);
635 assert_int_equal(err, -FI_ENODATA);
636 assert_null(info);
637
638 hints->caps &= ~FI_ATOMIC;
639 hints->tx_attr->msg_order = FI_ORDER_SAS;
640 hints->rx_attr->msg_order = FI_ORDER_SAS;
641 err = fi_getinfo(FI_VERSION(1,6), NULL, NULL, 0, hints, &info);
642 assert_int_equal(err, -FI_ENODATA);
643 assert_null(info);
644 }
645
646 /**
647 * @brief Verify that efa-direct fi_info objects are returned before efa info objects
648 */
649 void test_info_direct_ordering()
650 {
651 struct fi_info *hints, *info = NULL, *info_head = NULL;
652 bool efa_direct_returned = false, efa_returned = false;
653 bool efa_direct_returned_after_efa = false, efa_returned_after_efa_direct = false;
654 int err;
655
656 hints = efa_unit_test_alloc_hints(FI_EP_RDM, NULL);
657 assert_non_null(hints);
658
659 err = fi_getinfo(FI_VERSION(1,6), NULL, NULL, 0, hints, &info_head);
660 assert_int_equal(err, 0);
661 assert_non_null(info_head);
662
663 for (info = info_head; info; info = info->next) {
664 if (!strcmp(info->fabric_attr->name, EFA_DIRECT_FABRIC_NAME)) {
665 efa_direct_returned = true;
666 if (efa_returned)
667 efa_direct_returned_after_efa = true;
668 }
669 if (!strcmp(info->fabric_attr->name, EFA_FABRIC_NAME)) {
670 efa_returned = true;
671 if (efa_direct_returned)
672 efa_returned_after_efa_direct = true;
673 }
674 }
675
676 assert_true(efa_direct_returned);
677 assert_true(efa_returned);
678 assert_true(efa_returned_after_efa_direct);
679 assert_false(efa_direct_returned_after_efa);
680 }
681
682 /**
683 * @brief core test function for use_device_rdma
684 *
685 * @param env_val 0/1/-1: set FI_EFA_USE_DEVICE_RDMA to 0 or 1, or leave it unset (-1)
686 * @param setopt_val 0/1/-1: set use_device_rdma using fi_setopt to 0 or 1, or leave it unset (-1)
687 * @param expected_val expected result of ep->use_device_rdma
688 * @param api_version API version to use.
689 */
690 void test_use_device_rdma( const int env_val,
691 const int setopt_val,
692 const int expected_val,
693 const uint32_t api_version )
694 {
695 int ret = 0;
696 struct fi_info *hints, *info;
697 struct fid_fabric *fabric = NULL;
698 struct fid_domain *domain = NULL;
699 struct fid_ep *ep = NULL;
700 struct efa_rdm_ep *efa_rdm_ep;
701 bool rdma_capable_hw;
702 char env_str[16];
703
704 if (env_val >= 0) {
705 snprintf(env_str, 15, "%d", env_val);
706 setenv("FI_EFA_USE_DEVICE_RDMA", env_str, 1);
707 } else {
708 unsetenv("FI_EFA_USE_DEVICE_RDMA");
709 }
710
711 hints = efa_unit_test_alloc_hints(FI_EP_RDM, EFA_FABRIC_NAME);
712
713 ret = fi_getinfo(api_version, NULL, NULL, 0ULL, hints, &info);
714 assert_int_equal(ret, 0);
715
716 rdma_capable_hw = efa_device_support_rdma_read();
717
718 if (expected_val && !rdma_capable_hw) {
719 /* cannot test USE_DEVICE_RDMA=1, hardware
720 doesn't support it, and will abort() */
721 fi_freeinfo(info);
722 skip();
723 return;
724 }
725
726 ret = fi_fabric(info->fabric_attr, &fabric, NULL);
727 assert_int_equal(ret, 0);
728
729 ret = fi_domain(fabric, info, &domain, NULL);
730 assert_int_equal(ret, 0);
731
732 fi_endpoint(domain, info, &ep, NULL);
733 assert_int_equal(ret, 0);
734
735 if (setopt_val >= 0) {
736 bool b_val = (bool) setopt_val;
737 int ret_setopt;
738 ret_setopt = fi_setopt(&ep->fid, FI_OPT_ENDPOINT,
739 FI_OPT_EFA_USE_DEVICE_RDMA, &b_val, sizeof(bool));
740 if (FI_VERSION_LT(api_version, FI_VERSION(1,18))) {
741 assert_int_not_equal(ret_setopt, 0);
742 }
743 else if (expected_val != setopt_val) {
744 assert_int_not_equal(ret_setopt, 0);
745 }
746 else {
747 assert_int_equal(ret_setopt, 0);
748 }
749 }
750
751 efa_rdm_ep = container_of(ep, struct efa_rdm_ep,
752 base_ep.util_ep.ep_fid.fid);
753 assert_int_equal( efa_rdm_ep->use_device_rdma, expected_val );
754
755 assert_int_equal(fi_close(&ep->fid), 0);
756 assert_int_equal(fi_close(&domain->fid), 0);
757 assert_int_equal(fi_close(&fabric->fid), 0);
758 fi_freeinfo(info);
759
760 return;
761 }
762
763 /**
764 * Get the name of the "first"(random order) NIC
765 *
766 * @param[out] name The returned name string.
767 * It should be free'd after use.
768 * @returns FI_SUCCESS on success or a non-zero error code
769 */
770 static int get_first_nic_name(char **name) {
771 int ret;
772 char *nic_name = NULL;
773 struct fi_info *hints, *info;
774
775 hints = efa_unit_test_alloc_hints(FI_EP_RDM, EFA_FABRIC_NAME);
776 ret = fi_getinfo(FI_VERSION(1, 14), NULL, NULL, 0ULL, hints, &info);
777 fi_freeinfo(hints);
778 if (ret)
779 return ret;
780
781 nic_name = info->nic->device_attr->name;
782 assert_non_null(nic_name);
783 assert_int_not_equal(strlen(nic_name), 0);
784
785 *name = malloc(strlen(nic_name) + 1);
786 if (!name)
787 return FI_ENOMEM;
788
789 strcpy(*name, nic_name);
790
791 fi_freeinfo(info);
792
793 return FI_SUCCESS;
794 }
795
796 /**
797 * Verify the returned NIC from fi_getinfo.
798 * Ideally we want to test multi-NIC selection logic, but this test is most likely
799 * run on single-NIC platforms. Therefore we make a compromise and only verify the
800 * "first" NIC.
801 *
802 * @param[in] filter The value that would be set for FI_EFA_IFACE
803 * @param[in] expect_first_name The expected name of the "first" NIC
804 */
805 static void test_efa_nic_selection(const char *filter, const char *expect_first_name, char *fabric_name) {
806 int ret;
807 struct fi_info *hints, *info;
808
809 efa_env.iface = (char *) filter;
810 hints = efa_unit_test_alloc_hints(FI_EP_RDM, fabric_name);
811 ret = fi_getinfo(FI_VERSION(1, 14), NULL, NULL, 0ULL, hints, &info);
812 fi_freeinfo(hints);
813 if (expect_first_name) {
814 assert_int_equal(FI_SUCCESS, ret);
815 assert_string_equal(expect_first_name, info->nic->device_attr->name);
816 fi_freeinfo(info);
817 } else {
818 assert_int_not_equal(FI_SUCCESS, ret);
819 }
820 }
821
822 /**
823 * Verify NICs are returned if FI_EFA_IFACE=all
824 */
825 void test_efa_nic_select_all_devices_matches() {
826 int ret;
827 char *nic_name;
828
829 ret = get_first_nic_name(&nic_name);
830 assert_int_equal(ret, FI_SUCCESS);
831
832 test_efa_nic_selection("all", nic_name, EFA_FABRIC_NAME);
833 test_efa_nic_selection("all", nic_name, EFA_DIRECT_FABRIC_NAME);
834
835 free(nic_name);
836 }
837
838 /**
839 * Verify the "first" NIC can be selected by name
840 */
841 void test_efa_nic_select_first_device_matches() {
842 int ret;
843 char *nic_name;
844
845 ret = get_first_nic_name(&nic_name);
846 assert_int_equal(ret, FI_SUCCESS);
847
848 test_efa_nic_selection(nic_name, nic_name, EFA_FABRIC_NAME);
849 test_efa_nic_selection(nic_name, nic_name, EFA_DIRECT_FABRIC_NAME);
850
851 free(nic_name);
852 }
853
854 /**
855 * Verify that surrounding commas are handled correctly,
856 * i.e. ignored, to match the NIC name.
857 */
858 void test_efa_nic_select_first_device_with_surrounding_comma_matches() {
859 int ret;
860 char *nic_name, *filter;
861
862 ret = get_first_nic_name(&nic_name);
863 assert_int_equal(ret, FI_SUCCESS);
864
865 filter = malloc(strlen(nic_name) + 3);
866 assert_non_null(filter);
867
868 strcpy(filter, ",");
869 strcat(filter, nic_name);
870 strcat(filter, ",");
871
872 test_efa_nic_selection(filter, nic_name, EFA_FABRIC_NAME);
873 test_efa_nic_selection(filter, nic_name, EFA_DIRECT_FABRIC_NAME);
874
875 free(filter);
876 free(nic_name);
877 }
878
879 /**
880 * Verify that only full NIC names are matched, and prefixes,
881 * e.g. the first letter, will not accidentally select the wrong NIC.
882 */
883 void test_efa_nic_select_first_device_first_letter_no_match() {
884 int ret;
885 char *nic_name, filter[2];
886
887 ret = get_first_nic_name(&nic_name);
888 assert_int_equal(ret, FI_SUCCESS);
889
890 filter[0] = nic_name[0];
891 filter[1] = '\0';
892
893 test_efa_nic_selection(filter, NULL, EFA_FABRIC_NAME);
894 test_efa_nic_selection(filter, NULL, EFA_DIRECT_FABRIC_NAME);
895
896 free(nic_name);
897 }
898
899 /**
900 * Verify that empty NIC names will not select any NIC
901 */
902 void test_efa_nic_select_empty_device_no_match() {
903 test_efa_nic_selection(",", NULL, EFA_FABRIC_NAME);
904 test_efa_nic_selection(",", NULL, EFA_DIRECT_FABRIC_NAME);
905 }
906
907 /* indicates the test shouldn't set the setopt or environment
908 variable during setup. */
909 const int VALUE_NOT_SET = -1;
910
911 /* settings agree: on */
912 void test_efa_use_device_rdma_env1_opt1() {
913 test_use_device_rdma(1, 1, 1, FI_VERSION(1,18));
914 }
915 /* settings agree: off */
916 void test_efa_use_device_rdma_env0_opt0() {
917 test_use_device_rdma(0, 0, 0, FI_VERSION(1,18));
918 }
919 /* settings conflict, env on */
920 void test_efa_use_device_rdma_env1_opt0() {
921 test_use_device_rdma(1, 0, 1, FI_VERSION(1,18));
922 }
923 /* settings conflict, env off */
924 void test_efa_use_device_rdma_env0_opt1() {
925 test_use_device_rdma(0, 1, 0, FI_VERSION(1,18));
926 }
927 /* setopt only on */
928 void test_efa_use_device_rdma_opt1() {
929 test_use_device_rdma(VALUE_NOT_SET, 1, 1, FI_VERSION(1,18));
930 }
931 /* setopt only off */
932 void test_efa_use_device_rdma_opt0() {
933 test_use_device_rdma(VALUE_NOT_SET, 0, 0, FI_VERSION(1,18));
934 }
935 /* environment only on */
936 void test_efa_use_device_rdma_env1() {
937 test_use_device_rdma(1, VALUE_NOT_SET, 1, FI_VERSION(1,18));
938 }
939 /* environment only off */
940 void test_efa_use_device_rdma_env0() {
941 test_use_device_rdma(0, VALUE_NOT_SET, 0, FI_VERSION(1,18));
942 }
943 /* setopt rejected in 1,17 */
944 void test_efa_use_device_rdma_opt_old() {
945 test_use_device_rdma(1, 1, 1, FI_VERSION(1,17));
946 test_use_device_rdma(0, 0, 0, FI_VERSION(1,17));
947 }