-
Notifications
You must be signed in to change notification settings - Fork 60
/
na_ofi.c
3586 lines (3116 loc) · 119 KB
/
na_ofi.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
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2013-2017 Argonne National Laboratory, Department of Energy,
* UChicago Argonne, LLC and The HDF Group.
* All rights reserved.
*
* The full copyright notice, including terms governing use, modification,
* and redistribution, is contained in the COPYING file that can be
* found at the root of the source code distribution tree.
*/
/* Copyright (C) 2017 Intel Corporation
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted for any purpose (including commercial purposes)
* provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions, and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions, and the following disclaimer in the
* documentation and/or materials provided with the distribution.
*
* 3. In addition, redistributions of modified forms of the source or binary
* code must carry prominent notices stating that the original code was
* changed and the date of the change.
*
* 4. All publications or advertising materials mentioning features or use of
* this software are asked, but not required, to acknowledge that it was
* developed by Intel Corporation and credit the contributors.
*
* 5. Neither the name of Intel Corporation, nor the name of any Contributor
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "na_private.h"
#include "na_error.h"
#include "mercury_list.h"
#include "mercury_thread_mutex.h"
#include "mercury_thread_spin.h"
#include "mercury_thread_rwlock.h"
#include "mercury_hash_table.h"
#include "mercury_time.h"
#include "mercury_atomic.h"
#include "mercury_mem.h"
#include <rdma/fabric.h>
#include <rdma/fi_domain.h>
#include <rdma/fi_endpoint.h>
#include <rdma/fi_rma.h>
#include <rdma/fi_tagged.h>
#include <rdma/fi_cm.h>
#include <rdma/fi_errno.h>
#ifdef NA_OFI_HAS_EXT_GNI_H
#include <rdma/fi_ext_gni.h>
#endif
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ifaddrs.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#undef NDEBUG /* for assert */
#include <assert.h>
#include <unistd.h>
#include <inttypes.h>
#include <sys/uio.h> /* for struct iovec */
/****************/
/* Local Macros */
/****************/
/**
* FI VERSION provides binary backward and forward compatibility support.
* Specify the version of OFI is coded to, the provider will select struct
* layouts that are compatible with this version.
*/
#define NA_OFI_VERSION FI_VERSION(1, 5)
/* Name of providers */
#define NA_OFI_PROV_SOCKETS_NAME "sockets"
#define NA_OFI_PROV_PSM2_NAME "psm2"
#define NA_OFI_PROV_GNI_NAME "gni"
#define NA_OFI_PROV_VERBS_NAME "verbs"
#define NA_OFI_MAX_URI_LEN (128)
#define NA_OFI_MAX_NODE_LEN (64)
#define NA_OFI_MAX_PORT_LEN (16)
#define NA_OFI_HDR_MAGIC (0x0f106688)
/* Max tag */
#define NA_OFI_MAX_TAG ((1 << 30) -1)
#define NA_OFI_UNEXPECTED_SIZE 4096
#define NA_OFI_EXPECTED_TAG_FLAG (0x100000000ULL)
#define NA_OFI_UNEXPECTED_TAG_IGNORE (0xFFFFFFFFULL)
/* number of CQ event provided for fi_cq_read() */
#define NA_OFI_CQ_EVENT_NUM (16)
/* CQ depth (the socket provider's default value is 256 */
#define NA_OFI_CQ_DEPTH (8192)
/* The magic number for na_ofi_op_id verification */
#define NA_OFI_OP_ID_MAGIC_1 (0x1928374655627384ULL)
#define NA_OFI_OP_ID_MAGIC_2 (0x8171615141312111ULL)
/* Default basic bits */
#define NA_OFI_MR_BASIC_REQ \
(FI_MR_VIRT_ADDR | FI_MR_ALLOCATED | FI_MR_PROV_KEY)
/* the predefined RMA KEY for MR_SCALABLE */
#define NA_OFI_RMA_KEY (0x0F1B0F1BULL)
#if !defined(container_of)
/* given a pointer @ptr to the field @member embedded into type (usually
* * struct) @type, return pointer to the embedding instance of @type. */
# define container_of(ptr, type, member) \
((type *)((char *)(ptr)-(char *)(&((type *)0)->member)))
#endif
/************************************/
/* Local Type and Struct Definition */
/************************************/
enum na_ofi_prov_type {
NA_OFI_PROV_SOCKETS,
NA_OFI_PROV_PSM2,
NA_OFI_PROV_VERBS,
NA_OFI_PROV_GNI
};
enum na_ofi_mr_mode {
NA_OFI_MR_SCALABLE,
NA_OFI_MR_BASIC,
};
struct na_ofi_domain {
enum na_ofi_prov_type nod_prov_type; /* OFI provider type */
enum na_ofi_mr_mode nod_mr_mode; /* OFI memory region mode */
char *nod_prov_name; /* OFI provider name */
struct fi_info *nod_prov; /* OFI provider info */
struct fid_fabric *nod_fabric; /* Fabric domain handle */
struct fid_domain *nod_domain; /* Access domain handle */
/* Memory region handle, only valid for MR_SCALABLE */
struct fid_mr *nod_mr;
struct fid_av *nod_av; /* Address vector handle */
/*
* Address hash-table, to map the source-side address to fi_addr_t.
* The key is 64bits value serialized from source-side IP+Port (see
* na_ofi_reqhdr_2_key), the value is fi_addr_t.
*/
hg_hash_table_t *nod_addr_ht;
hg_thread_rwlock_t nod_rwlock; /* RW lock to protect nod_addr_ht */
hg_atomic_int32_t nod_refcount; /* Refcount of this domain */
HG_LIST_ENTRY(na_ofi_domain) nod_entry; /* Entry in nog_domain_list */
};
struct na_ofi_endpoint {
char *noe_node; /* Fabric address */
char *noe_service; /* Service name */
void *noe_auth_key; /* Auth key */
na_size_t noe_auth_key_size;/* Auth key size */
struct fi_info *noe_prov; /* OFI provider info */
struct fid_ep *noe_ep; /* Endpoint to communicate on */
struct fid_cq *noe_cq; /* Completion queue handle */
struct fid_wait *noe_wait; /* Wait set handle */
};
/**
* Inline header for NA_OFI (16 bytes).
*
* It is mainly to piggyback the source-side IP/port address for the unexpected
* message. For those providers that does not support FI_SOURCE/FI_SOURCE_ERR.
*/
struct na_ofi_reqhdr {
na_uint32_t fih_feats; /* feature bits */
na_uint32_t fih_magic; /* magic number for byte-order checking */
na_uint32_t fih_ip; /* IP addr in integer */
na_uint32_t fih_port; /* Port number */
};
struct na_ofi_private_data {
struct na_ofi_domain *nop_domain; /* Point back to access domain */
struct na_ofi_endpoint *nop_endpoint;
/* Unexpected op queue */
HG_QUEUE_HEAD(na_ofi_op_id) nop_unexpected_op_queue;
hg_thread_spin_t nop_unexpected_op_lock;
char *nop_uri; /* URI address string */
struct na_ofi_reqhdr nop_req_hdr; /* request header */
/* nop_mutex only used for verbs provider as it is not thread safe now */
hg_thread_mutex_t nop_mutex;
};
#define NA_OFI_PRIVATE_DATA(na_class) \
((struct na_ofi_private_data *)(na_class->private_data))
struct na_ofi_addr {
fi_addr_t noa_addr; /* FI fabric address */
char *noa_uri; /* Peer's URI */
hg_atomic_int32_t noa_refcount; /* Reference counter (dup/free) */
na_bool_t noa_unexpected; /* Address generated from unexpected recv */
na_bool_t noa_self; /* Boolean for self */
};
struct na_ofi_mem_handle {
struct fid_mr *nom_mr_hdl; /* FI MR handle */
na_uint64_t nom_mr_key; /* FI MR key */
na_ptr_t nom_base; /* Initial address of memory */
na_size_t nom_size; /* Size of memory */
na_uint8_t nom_attr; /* Flag of operation access */
na_uint8_t nom_remote; /* Flag of remote handle */
};
struct na_ofi_info_lookup {
na_addr_t noi_addr;
};
struct na_ofi_info_recv_unexpected {
void *noi_buf;
na_size_t noi_buf_size;
na_size_t noi_msg_size;
na_tag_t noi_tag;
};
struct na_ofi_info_recv_expected {
void *noi_buf;
na_size_t noi_buf_size;
na_size_t noi_msg_size;
na_tag_t noi_tag;
};
struct na_ofi_op_id {
/* noo_magic_1 and noo_magic_2 are for data verification */
na_uint64_t noo_magic_1;
HG_QUEUE_ENTRY(na_ofi_op_id) noo_entry;
na_context_t *noo_context;
struct fi_context noo_fi_ctx;
na_cb_type_t noo_type;
na_cb_t noo_callback;
void *noo_arg;
na_addr_t noo_addr;
hg_atomic_int32_t noo_refcount;
hg_atomic_int32_t noo_completed; /* Operation completed */
hg_atomic_int32_t noo_canceled; /* Operation canceled */
union {
struct na_ofi_info_lookup noo_lookup;
struct na_ofi_info_recv_unexpected noo_recv_unexpected;
struct na_ofi_info_recv_expected noo_recv_expected;
} noo_info;
struct na_cb_completion_data noo_completion_data;
na_uint64_t noo_magic_2;
};
/*****************/
/* Local Helpers */
/*****************/
#define na_ofi_bswap16(x) ((x) >> 8 | ((x) & 0xFFU) << 8)
#define na_ofi_bswap32(x) ((na_ofi_bswap16((x) >> 16) & 0xFFFFU) |\
(na_ofi_bswap16((x) & 0xFFFFU) << 16))
#define na_ofi_bswap32s(x) do { *(x) = na_ofi_bswap32(*(x)); } while (0)
static NA_INLINE void
na_ofi_class_lock(na_class_t *na_class)
{
struct na_ofi_private_data *priv = NA_OFI_PRIVATE_DATA(na_class);
struct na_ofi_domain *domain = priv->nop_domain;
if (domain->nod_prov_type == NA_OFI_PROV_VERBS)
hg_thread_mutex_lock(&priv->nop_mutex);
}
static NA_INLINE void
na_ofi_class_unlock(na_class_t *na_class)
{
struct na_ofi_private_data *priv = NA_OFI_PRIVATE_DATA(na_class);
struct na_ofi_domain *domain = priv->nop_domain;
if (domain->nod_prov_type == NA_OFI_PROV_VERBS)
hg_thread_mutex_unlock(&priv->nop_mutex);
}
static NA_INLINE na_bool_t
na_ofi_with_reqhdr(const na_class_t *na_class)
{
struct na_ofi_domain *domain = NA_OFI_PRIVATE_DATA(na_class)->nop_domain;
return domain->nod_prov_type != NA_OFI_PROV_PSM2;
}
/**
* Converts the inline header to a 64 bits key to search corresponding FI addr.
*/
static NA_INLINE na_uint64_t
na_ofi_reqhdr_2_key(struct na_ofi_reqhdr *hdr)
{
return (((na_uint64_t)hdr->fih_ip) << 32 | hdr->fih_port);
}
static int
av_addr_ht_key_equal(hg_hash_table_key_t vlocation1,
hg_hash_table_key_t vlocation2)
{
return *((na_uint64_t *) vlocation1) == *((na_uint64_t *) vlocation2);
}
static unsigned int
av_addr_ht_key_hash(hg_hash_table_key_t vlocation)
{
na_uint64_t key = *((na_uint64_t *) vlocation);
na_uint32_t hi, lo;
hi = (na_uint32_t) (key >> 32);
lo = (key & 0xFFFFFFFFU);
return ((hi & 0xFFFF0000U) | (lo & 0xFFFFU));
}
static void
av_addr_ht_key_free(hg_hash_table_key_t key)
{
free((na_uint64_t *) key);
}
static void
av_addr_ht_value_free(hg_hash_table_value_t value)
{
free((fi_addr_t *) value);
}
static na_return_t
na_ofi_av_insert(na_class_t *na_class, char *node_str, char *service_str,
fi_addr_t *fi_addr)
{
struct na_ofi_domain *domain = NA_OFI_PRIVATE_DATA(na_class)->nop_domain;
na_return_t ret = NA_SUCCESS;
int rc;
/* address resolution by fi AV */
assert(domain != NULL);
/* Use fi_av_insertsvc if possible */
na_ofi_class_lock(na_class);
rc = fi_av_insertsvc(domain->nod_av, node_str, service_str,
fi_addr, 0 /* flags */, NULL /* context */);
na_ofi_class_unlock(na_class);
if (rc == -FI_ENOSYS) { /* Not supported by PSM2/GNI providers */
struct fi_info *tmp_info = NULL;
/* Resolve node / service (always pass a numeric host) */
rc = fi_getinfo(NA_OFI_VERSION, node_str, service_str, 0,
NULL /* hints */, &tmp_info);
if (rc != 0) {
NA_LOG_ERROR("fi_getinfo (%s:%s) failed, rc: %d(%s).",
node_str, service_str, rc, fi_strerror(-rc));
ret = NA_PROTOCOL_ERROR;
goto out;
}
na_ofi_class_lock(na_class);
rc = fi_av_insert(domain->nod_av, tmp_info->dest_addr, 1, fi_addr,
0 /* flags */, NULL /* context */);
na_ofi_class_unlock(na_class);
fi_freeinfo(tmp_info);
}
if (rc < 0) {
NA_LOG_ERROR("fi_av_insert/svc failed(node %s, service %s), rc: %d(%s).",
node_str, service_str, rc, fi_strerror(-rc));
ret = NA_PROTOCOL_ERROR;
goto out;
}
if (rc != 1) {
NA_LOG_ERROR("fi_av_insert/svc failed(node %s, service %s), rc: %d.",
node_str, service_str, rc);
ret = NA_PROTOCOL_ERROR;
goto out;
}
/* The below just to verify the AV address resolution */
/*
void *peer_addr;
size_t addrlen;
char peer_addr_str[NA_OFI_MAX_URI_LEN] = {'\0'};
addrlen = domain->nod_src_addrlen;
peer_addr = malloc(addrlen);
if (peer_addr == NULL) {
NA_LOG_ERROR("Could not allocate peer_addr.");
ret = NA_NOMEM_ERROR;
goto out;
}
rc = fi_av_lookup(domain->nod_av, *fi_addr, peer_addr, &addrlen);
if (rc != 0) {
NA_LOG_ERROR("fi_av_lookup failed, rc: %d(%s).", rc, fi_strerror(-rc));
ret = NA_PROTOCOL_ERROR;
goto out;
}
addrlen = NA_OFI_MAX_URI_LEN;
fi_av_straddr(domain->nod_av, peer_addr, peer_addr_str, &addrlen);
NA_LOG_DEBUG("node %s, service %s, peer address %s.",
node_str, service_str, peer_addr_str);
free(peer_addr);
*/
out:
return ret;
}
/* lookup the address hash-table */
static na_return_t
na_ofi_addr_ht_lookup(na_class_t *na_class, struct na_ofi_reqhdr *reqhdr,
fi_addr_t *src_addr)
{
struct na_ofi_domain *domain = NA_OFI_PRIVATE_DATA(na_class)->nop_domain;
na_uint64_t addr_key, *new_key = NULL;
fi_addr_t *fi_addr, tmp_addr, *new_value = NULL;
char *node, service[16];
struct in_addr in;
na_return_t ret = NA_SUCCESS;
addr_key = na_ofi_reqhdr_2_key(reqhdr);
hg_thread_rwlock_rdlock(&domain->nod_rwlock);
fi_addr = hg_hash_table_lookup(domain->nod_addr_ht, &addr_key);
if (fi_addr != HG_HASH_TABLE_NULL) {
/*
in.s_addr = reqhdr->fih_ip;
node = inet_ntoa(in);
NA_LOG_DEBUG("hg_hash_table_lookup(%s:%d) succeed, fi_addr: %d.\n",
node, reqhdr->fih_port, *fi_addr);
*/
*src_addr = *fi_addr;
hg_thread_rwlock_release_rdlock(&domain->nod_rwlock);
return ret;
}
hg_thread_rwlock_release_rdlock(&domain->nod_rwlock);
hg_thread_rwlock_wrlock(&domain->nod_rwlock);
fi_addr = hg_hash_table_lookup(domain->nod_addr_ht, &addr_key);
if (fi_addr != HG_HASH_TABLE_NULL) {
*src_addr = *fi_addr;
hg_thread_rwlock_release_wrlock(&domain->nod_rwlock);
return ret;
}
in.s_addr = reqhdr->fih_ip;
node = inet_ntoa(in);
memset(service, 0, 16);
sprintf(service, "%d", reqhdr->fih_port);
ret = na_ofi_av_insert(na_class, node, service, &tmp_addr);
if (ret != NA_SUCCESS) {
NA_LOG_ERROR("na_ofi_av_insert(%s:%s) failed, ret: %d.",
node, service, ret);
goto unlock;
}
*src_addr = tmp_addr;
fi_addr = hg_hash_table_lookup(domain->nod_addr_ht, &addr_key);
if (fi_addr != HG_HASH_TABLE_NULL) {
/* in race condition, use addr in HT and remove the new addr from AV */
*src_addr = *fi_addr;
hg_thread_rwlock_release_wrlock(&domain->nod_rwlock);
fi_av_remove(domain->nod_av, &tmp_addr, 1 /* count */, 0 /* flag */);
return ret;
}
new_key = (na_uint64_t *)malloc(sizeof(*new_key));
new_value = (fi_addr_t *)malloc(sizeof(*new_value));
if (new_key == NULL || new_value == NULL) {
NA_LOG_ERROR("cannot allocate memory for new_key/new_value.");
free(new_key);
free(new_value);
ret = NA_NOMEM_ERROR;
goto unlock;
}
*new_key = addr_key;
*new_value = tmp_addr;
if (hg_hash_table_insert(domain->nod_addr_ht, new_key, new_value) == 0) {
NA_LOG_ERROR("hg_hash_table_insert(%s:%s) failed.", node, service);
ret = NA_NOMEM_ERROR;
} else {
/*
NA_LOG_DEBUG("hg_hash_table_insert(%s:%s) succeed, fi_addr: %d.",
node, service, tmp_addr);
*/
}
unlock:
hg_thread_rwlock_release_wrlock(&domain->nod_rwlock);
return ret;
}
/********************/
/* Local Prototypes */
/********************/
static int
na_ofi_getinfo(const char *prov_name, struct fi_info **providers);
static na_return_t
na_ofi_check_interface(const char *hostname, char *node, size_t node_len,
char *domain, size_t domain_len);
static NA_INLINE na_bool_t
na_ofi_verify_provider(const char *prov_name, const char *domain_name,
const struct fi_info *fi_info);
static na_return_t
na_ofi_domain_open(const char *prov_name, const char *domain_name,
struct na_ofi_domain **na_ofi_domain_p);
static na_return_t
na_ofi_domain_close(struct na_ofi_domain *na_ofi_domain);
static na_return_t
na_ofi_endpoint_open(const struct na_ofi_domain *na_ofi_domain,
const char *node, const char *service, const char *auth_key,
struct na_ofi_endpoint **na_ofi_endpoint_p);
static na_return_t
na_ofi_endpoint_close(struct na_ofi_endpoint *na_ofi_endpoint);
static na_return_t
na_ofi_get_ep_addr(const struct na_ofi_domain *na_ofi_domain,
const struct na_ofi_endpoint *na_ofi_endpoint, char **uri_p);
static na_return_t
na_ofi_gen_req_hdr(const char *uri, struct na_ofi_reqhdr *na_ofi_reqhdr);
/* check_protocol */
static na_bool_t
na_ofi_check_protocol(const char *protocol_name);
/* initialize */
static na_return_t
na_ofi_initialize(na_class_t *na_class, const struct na_info *na_info,
na_bool_t listen);
/* finalize */
static na_return_t
na_ofi_finalize(na_class_t *na_class);
/* op_create */
static na_op_id_t
na_ofi_op_create(na_class_t *na_class);
/* op_destroy */
static na_return_t
na_ofi_op_destroy(na_class_t *na_class, na_op_id_t op_id);
/* addr_lookup */
static na_return_t
na_ofi_addr_lookup(na_class_t *na_class, na_context_t *context,
na_cb_t callback, void *arg, const char *name, na_op_id_t *op_id);
/* addr_self */
static na_return_t
na_ofi_addr_self(na_class_t *na_class, na_addr_t *addr);
/* addr_dup */
static na_return_t
na_ofi_addr_dup(na_class_t *na_class, na_addr_t addr, na_addr_t *new_addr);
/* addr_free */
static na_return_t
na_ofi_addr_free(na_class_t *na_class, na_addr_t addr);
/* addr_is_self */
static na_bool_t
na_ofi_addr_is_self(na_class_t *na_class, na_addr_t addr);
/* addr_to_string */
static na_return_t
na_ofi_addr_to_string(na_class_t *na_class, char *buf, na_size_t *buf_size,
na_addr_t addr);
/* msg_get_max_unexpected_size */
static na_size_t
na_ofi_msg_get_max_unexpected_size(const na_class_t *na_class);
/* msg_get_max_expected_size */
static na_size_t
na_ofi_msg_get_max_expected_size(const na_class_t *na_class);
/* msg_get_unexpected_header_size */
static na_size_t
na_ofi_msg_get_unexpected_header_size(const na_class_t *na_class);
/* msg_get_max_tag */
static na_tag_t
na_ofi_msg_get_max_tag(const na_class_t *na_class);
/* msg_buf_alloc */
static void *
na_ofi_msg_buf_alloc(na_class_t *na_class, na_size_t size, void **plugin_data);
/* msg_buf_free */
static na_return_t
na_ofi_msg_buf_free(na_class_t *na_class, void *buf, void *plugin_data);
/* msg_init_unexpected */
static na_return_t
na_ofi_msg_init_unexpected(na_class_t *na_class, void *buf, na_size_t buf_size);
/* msg_send_unexpected */
static na_return_t
na_ofi_msg_send_unexpected(na_class_t *na_class, na_context_t *context,
na_cb_t callback, void *arg, const void *buf, na_size_t buf_size,
void *plugin_data, na_addr_t dest, na_tag_t tag, na_op_id_t *op_id);
/* msg_recv_unexpected */
static na_return_t
na_ofi_msg_recv_unexpected(na_class_t *na_class, na_context_t *context,
na_cb_t callback, void *arg, void *buf, na_size_t buf_size,
void *plugin_data, na_tag_t mask, na_op_id_t *op_id);
/* msg_send_expected */
static na_return_t
na_ofi_msg_send_expected(na_class_t *na_class, na_context_t *context,
na_cb_t callback, void *arg, const void *buf, na_size_t buf_size,
void *plugin_data, na_addr_t dest, na_tag_t tag, na_op_id_t *op_id);
/* msg_recv_expected */
static na_return_t
na_ofi_msg_recv_expected(na_class_t *na_class, na_context_t *context,
na_cb_t callback, void *arg, void *buf, na_size_t buf_size,
void *plugin_data, na_addr_t source, na_tag_t tag, na_op_id_t *op_id);
/* mem_handle */
static na_return_t
na_ofi_mem_handle_create(na_class_t *na_class, void *buf, na_size_t buf_size,
unsigned long flags, na_mem_handle_t *mem_handle);
static na_return_t
na_ofi_mem_handle_free(na_class_t *na_class, na_mem_handle_t mem_handle);
static na_return_t
na_ofi_mem_register(na_class_t *na_class, na_mem_handle_t mem_handle);
static na_return_t
na_ofi_mem_deregister(na_class_t *na_class, na_mem_handle_t mem_handle);
/* mem_handle serialization */
static na_size_t
na_ofi_mem_handle_get_serialize_size(na_class_t *na_class,
na_mem_handle_t mem_handle);
static na_return_t
na_ofi_mem_handle_serialize(na_class_t *na_class, void *buf,
na_size_t buf_size, na_mem_handle_t mem_handle);
static na_return_t
na_ofi_mem_handle_deserialize(na_class_t *na_class,
na_mem_handle_t *mem_handle, const void *buf, na_size_t buf_size);
/* put */
static na_return_t
na_ofi_put(na_class_t *na_class, na_context_t *context, na_cb_t callback,
void *arg, na_mem_handle_t local_mem_handle, na_offset_t local_offset,
na_mem_handle_t remote_mem_handle, na_offset_t remote_offset,
na_size_t length, na_addr_t remote_addr, na_op_id_t *op_id);
/* get */
static na_return_t
na_ofi_get(na_class_t *na_class, na_context_t *context, na_cb_t callback,
void *arg, na_mem_handle_t local_mem_handle, na_offset_t local_offset,
na_mem_handle_t remote_mem_handle, na_offset_t remote_offset,
na_size_t length, na_addr_t remote_addr, na_op_id_t *op_id);
/* poll_get_fd */
static int
na_ofi_poll_get_fd(na_class_t *na_class, na_context_t *context);
/* poll_try_wait */
static na_bool_t
na_ofi_poll_try_wait(na_class_t *na_class, na_context_t *context);
/* progress */
static na_return_t
na_ofi_progress(na_class_t *na_class, na_context_t *context,
unsigned int timeout);
static na_return_t
na_ofi_complete(struct na_ofi_addr *na_ofi_addr, struct na_ofi_op_id *na_ofi_op_id,
na_return_t ret);
static void
na_ofi_release(void *arg);
/* cancel */
static na_return_t
na_ofi_cancel(na_class_t *na_class, na_context_t *context, na_op_id_t op_id);
/*******************/
/* Local Variables */
/*******************/
const na_class_t na_ofi_class_g = {
NULL, /* private_data */
"ofi", /* name */
na_ofi_check_protocol, /* check_protocol */
na_ofi_initialize, /* initialize */
na_ofi_finalize, /* finalize */
NULL, /* cleanup */
NULL, /* check_feature */
NULL, /* context_create */
NULL, /* context_destroy */
na_ofi_op_create, /* op_create */
na_ofi_op_destroy, /* op_destroy */
na_ofi_addr_lookup, /* addr_lookup */
na_ofi_addr_free, /* addr_free */
na_ofi_addr_self, /* addr_self */
na_ofi_addr_dup, /* addr_dup */
na_ofi_addr_is_self, /* addr_is_self */
na_ofi_addr_to_string, /* addr_to_string */
na_ofi_msg_get_max_unexpected_size, /* msg_get_max_unexpected_size */
na_ofi_msg_get_max_expected_size, /* msg_get_max_expected_size */
na_ofi_msg_get_unexpected_header_size, /* msg_get_unexpected_header_size */
NULL, /* msg_get_expected_header_size */
na_ofi_msg_get_max_tag, /* msg_get_max_tag */
na_ofi_msg_buf_alloc, /* msg_buf_alloc */
na_ofi_msg_buf_free, /* msg_buf_free */
na_ofi_msg_init_unexpected, /* msg_init_unexpected */
na_ofi_msg_send_unexpected, /* msg_send_unexpected */
na_ofi_msg_recv_unexpected, /* msg_recv_unexpected */
NULL, /* msg_init_expected */
na_ofi_msg_send_expected, /* msg_send_expected */
na_ofi_msg_recv_expected, /* msg_recv_expected */
na_ofi_mem_handle_create, /* mem_handle_create */
NULL, /* mem_handle_create_segment */
na_ofi_mem_handle_free, /* mem_handle_free */
na_ofi_mem_register, /* mem_register */
na_ofi_mem_deregister, /* mem_deregister */
NULL, /* mem_publish */
NULL, /* mem_unpublish */
na_ofi_mem_handle_get_serialize_size, /* mem_handle_get_serialize_size */
na_ofi_mem_handle_serialize, /* mem_handle_serialize */
na_ofi_mem_handle_deserialize, /* mem_handle_deserialize */
na_ofi_put, /* put */
na_ofi_get, /* get */
na_ofi_poll_get_fd, /* poll_get_fd */
na_ofi_poll_try_wait, /* poll_try_wait */
na_ofi_progress, /* progress */
na_ofi_cancel /* cancel */
};
/* OFI access domain list */
static HG_LIST_HEAD(na_ofi_domain)
na_ofi_domain_list_g = HG_LIST_HEAD_INITIALIZER(na_ofi_domain);
/* Protects domain list */
static hg_thread_mutex_t na_ofi_domain_list_mutex_g =
HG_THREAD_MUTEX_INITIALIZER;
/*****************/
/* Local Helpers */
/*****************/
static int
na_ofi_getinfo(const char *prov_name, struct fi_info **providers)
{
struct fi_info *hints = NULL;
na_return_t ret = NA_SUCCESS;
int rc;
/**
* Hints to query && filter providers.
*/
hints = fi_allocinfo();
if (!hints) {
NA_LOG_ERROR("fi_allocinfo failed.\n");
ret = NA_NOMEM_ERROR;
goto out;
}
/* Protocol name is provider name, filter out providers within libfabric */
hints->fabric_attr->prov_name = strdup(prov_name);
if (!hints->fabric_attr->prov_name) {
NA_LOG_ERROR("Could not duplicate name");
ret = NA_NOMEM_ERROR;
goto out;
}
/* mode: operational mode, NA_OFI passes in context for communication calls. */
hints->mode = FI_CONTEXT;
/* ep_type: reliable datagram (connection-less). */
hints->ep_attr->type = FI_EP_RDM;
/* caps: capabilities required. */
hints->caps = FI_TAGGED | FI_RMA;
/**
* msg_order: guarantee that messages with same tag are ordered.
* (FI_ORDER_SAS - Send after send. If set, message send operations,
* including tagged sends, are transmitted in the order submitted relative
* to other message send. If not set, message sends may be transmitted out
* of order from their submission).
*/
//hints->tx_attr->msg_order = FI_ORDER_SAS;
//hints->rx_attr->msg_order = FI_ORDER_SAS;
hints->domain_attr->threading = FI_THREAD_UNSPEC;
hints->domain_attr->av_type = FI_AV_MAP;
hints->domain_attr->resource_mgmt = FI_RM_ENABLED;
/* Provider specific configuration (MR mode / caps) */
if (!strcmp(prov_name, NA_OFI_PROV_SOCKETS_NAME)) {
/* Limit ourselves to TCP for now */
hints->ep_attr->protocol = FI_PROTO_SOCK_TCP;
/* For versions 1.5 and later, scalable is implied by the lack of any
* mr_mode bits being set. */
hints->domain_attr->mr_mode = FI_MR_UNSPEC;
/* As "sockets" provider does not support manual progress and wait
* objects, set progress to auto for now. Note that the provider
* effectively creates a thread for internal progress in that case.
*/
hints->domain_attr->control_progress = FI_PROGRESS_AUTO;
hints->domain_attr->data_progress = FI_PROGRESS_AUTO;
} else {
/* FI_MR_BASIC */
hints->domain_attr->mr_mode = NA_OFI_MR_BASIC_REQ | FI_MR_LOCAL;
/* Manual progress (no internal progress thread) */
hints->domain_attr->control_progress = FI_PROGRESS_MANUAL;
hints->domain_attr->data_progress = FI_PROGRESS_MANUAL;
if (!strcmp(prov_name, NA_OFI_PROV_PSM2_NAME)) {
/* Can retrieve source address from processes not inserted in AV */
hints->caps |= (FI_SOURCE | FI_SOURCE_ERR);
/* PSM2 provider requires FI_MR_BASIC bit to be set for now */
hints->domain_attr->mr_mode |= FI_MR_BASIC;
}
else if (!strcmp(prov_name, NA_OFI_PROV_VERBS_NAME)) {
hints->rx_attr->mode |= FI_CONTEXT;
}
}
/**
* fi_getinfo: returns information about fabric services.
* Pass NULL for name/service to list all providers supported with above
* requirement hints.
*/
rc = fi_getinfo(NA_OFI_VERSION, /* OFI version requested */
NULL, /* Optional name or fabric to resolve */
NULL, /* Optional service name or port to request */
0ULL, /* Optional flag */
hints, /* In: Hints to filter providers */
providers); /* Out: List of matching providers */
if (rc != 0) {
NA_LOG_ERROR("fi_getinfo failed, rc: %d(%s).", rc, fi_strerror(-rc));
ret = NA_PROTOCOL_ERROR;
goto out;
}
out:
if (hints) {
free(hints->fabric_attr->prov_name);
hints->fabric_attr->prov_name = NULL;
fi_freeinfo(hints);
}
return ret;
}
/*---------------------------------------------------------------------------*/
static na_return_t
na_ofi_check_interface(const char *hostname, char *node, size_t node_len,
char *domain, size_t domain_len)
{
struct ifaddrs *ifaddrs = NULL, *ifaddr;
struct addrinfo hints, *hostname_res = NULL;
char ip_res[INET_ADDRSTRLEN] = {'\0'}; /* This restricts to ipv4 addresses */
na_return_t ret = NA_SUCCESS;
na_bool_t found = NA_FALSE;
int s;
/* Try to resolve hostname first so that we can later compare the IP */
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = 0;
hints.ai_protocol = 0;
s = getaddrinfo(hostname, NULL, &hints, &hostname_res);
if (s == 0) {
struct addrinfo *rp;
for (rp = hostname_res; rp != NULL; rp = rp->ai_next) {
/* Get IP */
if (!inet_ntop(rp->ai_addr->sa_family,
&((struct sockaddr_in *) rp->ai_addr)->sin_addr, ip_res,
INET_ADDRSTRLEN)) {
NA_LOG_ERROR("IP could not be resolved");
ret = NA_PROTOCOL_ERROR;
goto out;
}
break;
}
}
/* Check and compare interfaces */
if (getifaddrs(&ifaddrs) == -1) {
NA_LOG_ERROR("getifaddrs() failed");
ret = NA_PROTOCOL_ERROR;
goto out;
}
for (ifaddr = ifaddrs; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
char ip[INET_ADDRSTRLEN] = {'\0'}; /* This restricts to ipv4 addresses */
if (ifaddr->ifa_addr == NULL)
continue;
if (ifaddr->ifa_addr->sa_family != AF_INET)
continue;
/* Get IP */
if (!inet_ntop(ifaddr->ifa_addr->sa_family,
&((struct sockaddr_in *) ifaddr->ifa_addr)->sin_addr, ip,
INET_ADDRSTRLEN)) {
NA_LOG_ERROR("IP could not be resolved for: %s", ifaddr->ifa_name);
ret = NA_PROTOCOL_ERROR;
goto out;
}
/* Compare hostnames / device names */
if (!strcmp(ip, ip_res) || !strcmp(ifaddr->ifa_name, hostname)) {
if (node_len)
strncpy(node, ip, node_len);
if (domain_len)
strncpy(domain, ifaddr->ifa_name, domain_len);
found = NA_TRUE;
break;
}
}
/* Allow for passing hostname/device name directly if no match */
if (!found) {
strncpy(node, hostname, node_len);
strncpy(domain, hostname, domain_len);
}
out:
freeifaddrs(ifaddrs);
if (hostname_res)
freeaddrinfo(hostname_res);
return ret;
}
/*---------------------------------------------------------------------------*/
static NA_INLINE na_bool_t
na_ofi_verify_provider(const char *prov_name, const char *domain_name,
const struct fi_info *fi_info)
{
na_bool_t ret = NA_FALSE;
/* Does not match provider name */
if (strcmp(prov_name, fi_info->fabric_attr->prov_name))
goto out;
/* Only for sockets providers is the provider name ambiguous and requires
* checking the domain name as well */
if (!strcmp(prov_name, NA_OFI_PROV_SOCKETS_NAME)) {
/* Does not match domain name */
if (domain_name && strcmp(domain_name, fi_info->domain_attr->name))
goto out;
}
ret = NA_TRUE;
out:
return ret;
}
/*---------------------------------------------------------------------------*/
static na_return_t
na_ofi_domain_open(const char *prov_name, const char *domain_name,
struct na_ofi_domain **na_ofi_domain_p)
{
struct na_ofi_domain *na_ofi_domain;
struct fi_av_attr av_attr = {0};