forked from openucx/ucx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwireup.c
More file actions
2510 lines (2150 loc) · 91.9 KB
/
Copy pathwireup.c
File metadata and controls
2510 lines (2150 loc) · 91.9 KB
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) NVIDIA CORPORATION & AFFILIATES, 2001-2026. ALL RIGHTS RESERVED.
*
* See file LICENSE for terms.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "wireup.h"
#include "address.h"
#include "wireup_cm.h"
#include "wireup_ep.h"
#include <ucs/async/async.h>
#include <ucs/datastruct/queue.h>
#include <ucp/core/ucp_ep.h>
#include <ucp/core/ucp_listener.h>
#include <ucp/core/ucp_proxy_ep.h>
#include <ucp/core/ucp_worker.h>
#include <ucp/proto/proto_common.h>
#include <ucs/sys/iovec.h>
#include <ucp/tag/eager.h>
#include <ucp/core/ucp_request.inl>
#include <ucp/proto/proto_am.inl>
/*
* Description of the protocol in UCX wiki:
* https://github.com/openucx/ucx/wiki/Connection-establishment
*/
/* Validate wireup message, implemented as a macro to prevent static checker
* warnings */
#define UCP_WIREUP_MSG_CHECK(_msg, _ep, _msg_type) \
do { \
ucs_assert((_msg)->type == (_msg_type)); \
if ((_msg_type) == UCP_WIREUP_MSG_REQUEST) { \
ucs_assert(((_msg)->dst_ep_id == UCS_PTR_MAP_KEY_INVALID) != \
((_ep) != NULL)); \
} else { \
ucs_assert((_msg)->dst_ep_id != UCS_PTR_MAP_KEY_INVALID); \
ucs_assert((_ep) != NULL); \
} \
} while (0)
size_t ucp_wireup_msg_pack(void *dest, void *arg)
{
struct iovec *wireup_msg_iov = (struct iovec*)arg;
return ucs_iov_copy(wireup_msg_iov, 2, 0, dest,
wireup_msg_iov[0].iov_len + wireup_msg_iov[1].iov_len,
UCS_IOV_COPY_TO_BUF);
}
const char* ucp_wireup_msg_str(uint8_t msg_type)
{
switch (msg_type) {
case UCP_WIREUP_MSG_PRE_REQUEST:
return "PRE_REQ";
case UCP_WIREUP_MSG_REQUEST:
return "REQ";
case UCP_WIREUP_MSG_REPLY:
return "REP";
case UCP_WIREUP_MSG_ACK:
return "ACK";
case UCP_WIREUP_MSG_EP_CHECK:
return "EP_CHECK";
case UCP_WIREUP_MSG_EP_REMOVED:
return "EP_REMOVED";
case UCP_WIREUP_MSG_LANES_ADDR_REQUEST:
return "LANES_ADDR_REQ";
case UCP_WIREUP_MSG_LANES_ADDR_REPLY:
return "LANES_ADDR_REP";
default:
return "<unknown>";
}
}
static ucp_lane_index_t ucp_wireup_get_msg_lane(ucp_ep_h ep, uint8_t msg_type)
{
ucp_context_h context = ep->worker->context;
ucp_ep_config_t *ep_config = ucp_ep_config(ep);
ucp_lane_index_t lane, fallback_lane;
if ((msg_type == UCP_WIREUP_MSG_ACK) ||
(msg_type == UCP_WIREUP_MSG_LANES_ADDR_REQUEST) ||
(msg_type == UCP_WIREUP_MSG_LANES_ADDR_REPLY)) {
/* Post-failover, wireup_msg_lane may itself be failed - prefer the
* re-selected operable AM lane. */
lane = ep_config->key.am_lane;
fallback_lane = ep_config->key.wireup_msg_lane;
} else {
/* for request/response, try wireup_msg_lane first */
lane = ep_config->key.wireup_msg_lane;
fallback_lane = ep_config->key.am_lane;
}
if (lane == UCP_NULL_LANE) {
lane = fallback_lane;
}
if (lane == UCP_NULL_LANE) {
ucs_fatal("ep %p to %s: could not find a lane to send CONN_%s%s",
ep, ucp_ep_peer_name(ep), ucp_wireup_msg_str(msg_type),
context->config.ext.unified_mode ?
". try to set UCX_UNIFIED_MODE=n." : "");
}
return lane;
}
static inline ucp_rsc_index_t
ucp_wireup_ep_get_rsc_index(ucp_ep_h ep, ucp_lane_index_t lane)
{
uct_ep_h uct_ep = ucp_ep_get_lane(ep, lane);
return ucp_wireup_ep_test(uct_ep)?
ucp_wireup_ep_get_msg_rsc_index(ucp_wireup_ep(uct_ep)) :
ucp_ep_get_rsc_index(ep, lane);
}
ucs_status_t ucp_wireup_msg_progress(uct_pending_req_t *self)
{
ucp_request_t *req = ucs_container_of(self, ucp_request_t, send.uct);
ucp_ep_h ep = req->send.ep;
ucp_context_h context = ep->worker->context;
ucs_status_t status;
ssize_t packed_len;
unsigned am_flags;
struct iovec wireup_msg_iov[2];
ucp_rsc_index_t rsc_index;
ucp_worker_iface_t *wiface;
UCS_ASYNC_BLOCK(&ep->worker->async);
if (req->send.wireup.msg_hdr.type == UCP_WIREUP_MSG_REQUEST) {
if (ep->flags & UCP_EP_FLAG_REMOTE_CONNECTED) {
ucs_trace("ep %p: not sending wireup message - remote already connected",
ep);
status = UCS_OK;
goto out_free_req;
}
} else if (req->send.wireup.msg_hdr.type == UCP_WIREUP_MSG_PRE_REQUEST) {
ucs_assert (!(ep->flags & UCP_EP_FLAG_REMOTE_CONNECTED));
}
/* send the active message */
req->send.lane = ucp_wireup_get_msg_lane(ep, req->send.wireup.msg_hdr.type);
am_flags = 0;
if ((req->send.wireup.msg_hdr.type == UCP_WIREUP_MSG_REQUEST) ||
(req->send.wireup.msg_hdr.type == UCP_WIREUP_MSG_PRE_REQUEST)) {
am_flags |= UCT_SEND_FLAG_SIGNALED;
}
VALGRIND_CHECK_MEM_IS_DEFINED(&req->send.wireup.msg_hdr,
sizeof(req->send.wireup.msg_hdr));
VALGRIND_CHECK_MEM_IS_DEFINED(req->send.buffer, req->send.length);
wireup_msg_iov[0].iov_base = &req->send.wireup.msg_hdr;
wireup_msg_iov[0].iov_len = sizeof(req->send.wireup.msg_hdr);
if ((req->send.wireup.msg_hdr.type == UCP_WIREUP_MSG_LANES_ADDR_REQUEST) ||
(req->send.wireup.msg_hdr.type == UCP_WIREUP_MSG_LANES_ADDR_REPLY)) {
VALGRIND_CHECK_MEM_IS_DEFINED(&req->send.wireup.lanes_info,
sizeof(req->send.wireup.lanes_info));
wireup_msg_iov[0].iov_len += sizeof(req->send.wireup.lanes_info);
}
wireup_msg_iov[1].iov_base = req->send.buffer;
wireup_msg_iov[1].iov_len = req->send.length;
/* bcopy size may be limited by max_bcopy capability, make sure we have
* enough space.
* TODO: find reliable way of exchange wireup message without this limit */
packed_len = wireup_msg_iov[0].iov_len + wireup_msg_iov[1].iov_len;
rsc_index = ucp_wireup_ep_get_rsc_index(ep, req->send.lane);
wiface = ucp_worker_iface(ep->worker, rsc_index);
if (packed_len > wiface->attr.cap.am.max_bcopy) {
ucs_error("ep %p: wireup message size %zu exceeds max bcopy size %zu on "
UCT_TL_RESOURCE_DESC_FMT, ep, packed_len,
wiface->attr.cap.am.max_bcopy,
UCT_TL_RESOURCE_DESC_ARG(&context->tl_rscs[rsc_index].tl_rsc));
status = UCS_OK;
goto out_free_req;
}
packed_len = uct_ep_am_bcopy(ucp_ep_get_lane(ep, req->send.lane),
UCP_AM_ID_WIREUP, ucp_wireup_msg_pack,
wireup_msg_iov, am_flags);
if (ucs_unlikely(packed_len < 0)) {
status = (ucs_status_t)packed_len;
if (ucs_likely(status == UCS_ERR_NO_RESOURCE)) {
goto out;
}
ucs_diag("failed to send wireup: %s", ucs_status_string(status));
ucp_ep_set_lanes_failed_schedule(ep, UCS_BIT(req->send.lane), status);
status = UCS_OK;
goto out_free_req;
} else {
status = UCS_OK;
}
switch (req->send.wireup.msg_hdr.type) {
case UCP_WIREUP_MSG_PRE_REQUEST:
ucp_ep_update_flags(ep, UCP_EP_FLAG_CONNECT_PRE_REQ_SENT, 0);
break;
case UCP_WIREUP_MSG_REQUEST:
ucp_ep_update_flags(ep, UCP_EP_FLAG_CONNECT_REQ_SENT, 0);
break;
case UCP_WIREUP_MSG_REPLY:
case UCP_WIREUP_MSG_REPLY_RECONFIG:
ucp_ep_update_flags(ep, UCP_EP_FLAG_CONNECT_REP_SENT, 0);
break;
case UCP_WIREUP_MSG_ACK:
ucp_ep_update_flags(ep, UCP_EP_FLAG_CONNECT_ACK_SENT, 0);
break;
}
out_free_req:
ucs_free(req->send.buffer);
ucp_request_mem_free(req);
out:
UCS_ASYNC_UNBLOCK(&ep->worker->async);
return status;
}
ucs_status_t
ucp_wireup_msg_prepare(ucp_ep_h ep, uint8_t type,
const ucp_tl_bitmap_t *tl_bitmap,
const ucp_lane_index_t *lanes2remote,
ucp_lane_map_t requested_lane_map,
ucp_lane_map_t provided_lane_map,
ucp_wireup_msg_t *msg_hdr, void **address_p,
size_t *address_length_p)
{
ucp_context_h context = ep->worker->context;
unsigned pack_flags = ucp_worker_default_address_pack_flags(ep->worker) |
UCP_ADDRESS_PACK_FLAG_TL_RSC_IDX;
ucp_wireup_msg_lanes_info_t *lanes_info;
ucs_status_t status;
msg_hdr->type = type;
msg_hdr->err_mode = ucp_ep_config(ep)->key.err_mode;
msg_hdr->conn_sn = ep->conn_sn;
msg_hdr->src_ep_id = ucp_ep_local_id(ep);
if (ep->flags & UCP_EP_FLAG_REMOTE_ID) {
msg_hdr->dst_ep_id = ucp_ep_remote_id(ep);
} else {
/* Destination UCP endpoint ID must be packed in case of CM */
ucs_assert(!ucp_ep_has_cm_lane(ep));
msg_hdr->dst_ep_id = UCS_PTR_MAP_KEY_INVALID;
}
if ((type == UCP_WIREUP_MSG_LANES_ADDR_REQUEST) ||
(type == UCP_WIREUP_MSG_LANES_ADDR_REPLY)) {
lanes_info = (ucp_wireup_msg_lanes_info_t *)(msg_hdr + 1);
lanes_info->requested_lane_map = requested_lane_map;
lanes_info->provided_lane_map = provided_lane_map;
} else {
ucs_assert((requested_lane_map == 0) && (provided_lane_map == 0));
}
/* pack all addresses */
status = ucp_address_pack(ep->worker, ep, tl_bitmap, pack_flags,
context->config.ext.worker_addr_version,
lanes2remote, UINT_MAX, address_length_p,
address_p);
if (status != UCS_OK) {
ucs_error("failed to pack address: %s", ucs_status_string(status));
}
return status;
}
static ucs_status_t
ucp_wireup_msg_send_full(ucp_ep_h ep, uint8_t type,
const ucp_tl_bitmap_t *tl_bitmap,
const ucp_lane_index_t *lanes2remote,
ucp_lane_map_t requested_lane_map,
ucp_lane_map_t provided_lane_map)
{
ucp_request_t *req;
ucs_status_t status;
ucs_assert(ep->cfg_index != UCP_WORKER_CFG_INDEX_NULL);
if (ep->flags & UCP_EP_FLAG_FAILED) {
ucs_debug("ep %p: not sending WIREUP message (%u), because ep failed",
ep, type);
return UCS_ERR_CONNECTION_RESET;
}
/* We cannot allocate from memory pool because it's not thread safe
* and this function may be called from any thread
*/
req = ucp_request_mem_alloc("wireup_msg_req");
if (req == NULL) {
ucs_error("failed to allocate request for sending WIREUP message");
status = UCS_ERR_NO_MEMORY;
goto err;
}
req->flags = 0;
req->send.ep = ep;
req->send.uct.func = ucp_wireup_msg_progress;
req->send.datatype = ucp_dt_make_contig(1);
ucp_request_send_state_init(req, ucp_dt_make_contig(1), 0);
status = ucp_wireup_msg_prepare(ep, type, tl_bitmap, lanes2remote,
requested_lane_map, provided_lane_map,
&req->send.wireup.msg_hdr,
&req->send.buffer, &req->send.length);
if (status != UCS_OK) {
ucp_request_mem_free(req);
goto err;
}
ucp_request_send(req);
/* coverity[leaked_storage] */
return UCS_OK;
err:
ucp_ep_set_lanes_failed_schedule(ep, 0, status);
return status;
}
static ucs_status_t
ucp_wireup_msg_send(ucp_ep_h ep, uint8_t type, const ucp_tl_bitmap_t *tl_bitmap,
const ucp_lane_index_t *lanes2remote)
{
return ucp_wireup_msg_send_full(ep, type, tl_bitmap, lanes2remote, 0, 0);
}
static ucp_tl_bitmap_t
ucp_wireup_get_ep_tl_bitmap(ucp_ep_h ep, ucp_lane_map_t lane_map)
{
ucp_tl_bitmap_t tl_bitmap = UCS_STATIC_BITMAP_ZERO_INITIALIZER;
ucp_lane_index_t lane;
ucs_for_each_bit(lane, lane_map) {
ucs_assert(lane < UCP_MAX_LANES);
if (ucp_ep_get_rsc_index(ep, lane) == UCP_NULL_RESOURCE) {
continue;
}
UCS_STATIC_BITMAP_SET(&tl_bitmap, ucp_ep_get_rsc_index(ep, lane));
}
return tl_bitmap;
}
int ucp_wireup_connect_p2p(ucp_worker_h worker, ucp_rsc_index_t rsc_index,
int has_cm_lane)
{
/* The EP with CM lane has to be connected to remote EP, so prefer native
* UCT p2p capability. */
return has_cm_lane ?
ucp_worker_is_tl_p2p(worker, rsc_index) :
!ucp_worker_is_tl_2iface(worker, rsc_index);
}
/*
* Select remote ep address for every remote address entry (because there
* could be multiple ep addresses per entry). This selection is used to create
* 'lanes2remote' mapping with the remote lane index for each local lane.
*/
static void
ucp_wireup_match_p2p_lanes(ucp_ep_h ep,
const ucp_unpacked_address_t *remote_address,
const unsigned *addr_indices,
ucp_lane_index_t *lanes2remote)
{
const ucp_address_entry_t *address;
unsigned address_index;
ucp_lane_index_t lane, remote_lane, num_lanes;
unsigned *ep_addr_indexes;
unsigned ep_addr_index;
uint64_t used_remote_lanes;
/* Initialize the counters of ep address index for each address entry */
ep_addr_indexes = ucs_alloca(sizeof(ep_addr_index) *
remote_address->address_count);
for (address_index = 0; address_index < remote_address->address_count;
++address_index) {
ep_addr_indexes[address_index] = 0;
}
/* Initialize lanes2remote array */
for (lane = 0; lane < UCP_MAX_LANES; ++lane) {
lanes2remote[lane] = UCP_NULL_LANE;
}
used_remote_lanes = 0;
num_lanes = ucp_ep_num_lanes(ep);
for (lane = 0; lane < num_lanes; ++lane) {
if (!ucp_ep_is_lane_p2p(ep, lane)) {
continue;
}
/* Select next remote ep address within the address_index as specified
* by addr_indices argument
*/
address_index = addr_indices[lane];
address = &remote_address->address_list[address_index];
ep_addr_index = ep_addr_indexes[address_index]++;
ucs_assertv(ep_addr_index < address->num_ep_addrs,
"lane=%d/%d tl_name_csum=0x%02x address_index=%u "
"ep_addr_index=%u num_ep_addrs=%u",
lane, num_lanes, address->tl_name_csum, address_index,
ep_addr_index, address->num_ep_addrs);
remote_lane = address->ep_addrs[ep_addr_index].lane;
lanes2remote[lane] = remote_lane;
if (used_remote_lanes & UCS_BIT(remote_lane)) {
ucs_fatal("ep %p: remote lane %d is used more than once", ep,
remote_lane);
}
used_remote_lanes |= UCS_BIT(remote_lane);
ucs_trace("ep %p: lane[%d]->remote_lane[%d] (address[%d].ep_address[%d])",
ep, lane, remote_lane, address_index, ep_addr_index);
}
}
ucs_status_t
ucp_wireup_find_remote_p2p_addr(ucp_ep_h ep, ucp_lane_index_t remote_lane,
const ucp_unpacked_address_t *remote_address,
const ucp_address_entry_t **address_entry_p,
const ucp_address_entry_ep_addr_t **ep_entry_p)
{
const ucp_address_entry_t *address;
unsigned ep_addr_index;
ucp_unpacked_address_for_each(address, remote_address) {
for (ep_addr_index = 0; ep_addr_index < address->num_ep_addrs;
++ep_addr_index) {
if (remote_lane == address->ep_addrs[ep_addr_index].lane) {
*address_entry_p = address;
*ep_entry_p = &address->ep_addrs[ep_addr_index];
return UCS_OK;
}
}
}
return UCS_ERR_UNREACHABLE;
}
ucs_status_t
ucp_wireup_connect_local(ucp_ep_h ep,
const ucp_unpacked_address_t *remote_address,
const ucp_lane_index_t *lanes2remote)
{
ucp_lane_index_t lane, remote_lane;
const ucp_address_entry_t *address_entry;
const ucp_address_entry_ep_addr_t *ep_entry;
ucs_status_t status;
ucs_trace("ep %p: connect local transports", ep);
ucs_log_indent(1);
for (lane = 0; lane < ucp_ep_num_lanes(ep); ++lane) {
if (!ucp_ep_is_lane_p2p(ep, lane)) {
continue;
}
remote_lane = (lanes2remote == NULL) ? lane : lanes2remote[lane];
status = ucp_wireup_find_remote_p2p_addr(ep, remote_lane, remote_address,
&address_entry, &ep_entry);
if (status != UCS_OK) {
ucs_error("ep %p: no remote ep address for lane[%d]->remote_lane[%d]",
ep, lane, remote_lane);
goto out;
}
status = ucp_wireup_ep_connect_to_ep_v2(ucp_ep_get_lane(ep, lane),
address_entry, ep_entry);
if (status != UCS_OK) {
goto out;
}
}
status = UCS_OK;
out:
ucs_log_indent(-1);
return status;
}
static int ucp_ep_has_wireup_msg_pending(ucp_ep_h ucp_ep)
{
ucp_wireup_ep_t *wireup_ep;
ucp_lane_index_t lane;
for (lane = 0; lane < ucp_ep_num_lanes(ucp_ep); ++lane) {
wireup_ep = ucp_wireup_ep(ucp_ep_get_lane(ucp_ep, lane));
if ((wireup_ep != NULL) && (wireup_ep->pending_count != 0)) {
return 1;
}
}
return 0;
}
static void ucp_wireup_eps_progress_sched(ucp_ep_h ucp_ep)
{
/* Use one-shot mode to avoid the need to save prog_id */
ucs_callbackq_add_oneshot(&ucp_ep->worker->uct->progress_q, ucp_ep,
ucp_wireup_eps_progress, ucp_ep);
ucp_worker_signal_internal(ucp_ep->worker);
}
/*
* We switch the endpoints in this function (instead in wireup code) since
* this is guaranteed to run from the main thread.
*/
unsigned ucp_wireup_eps_progress(void *arg)
{
ucp_ep_h ucp_ep = arg;
ucp_wireup_ep_t *wireup_ep;
ucs_queue_head_t tmp_pending_queue;
ucp_lane_index_t lane;
UCS_ASYNC_BLOCK(&ucp_ep->worker->async);
/* If we still have pending wireup messages, send them out first */
if (ucp_ep_has_wireup_msg_pending(ucp_ep)) {
/* Reschedule the progress since it's one-shot */
ucp_wireup_eps_progress_sched(ucp_ep);
goto out_unblock;
}
/* If an error happened on the endpoint (but perhaps the deferred error handler,
* ucp_worker_iface_err_handle_progress(), was not called yet, avoid changing
* ep state, and let the error handler take care of cleanup.
*/
if (ucp_ep->flags & UCP_EP_FLAG_FAILED) {
ucs_trace("ep %p: not switching wireup eps to ready state because of "
"error", ucp_ep);
goto out_unblock;
}
/* Move wireup pending queue to temporary queue and remove references to
* the wireup progress function
*/
ucp_wireup_eps_pending_extract(ucp_ep, &tmp_pending_queue);
for (lane = 0; lane < ucp_ep_num_lanes(ucp_ep); ++lane) {
wireup_ep = ucp_wireup_ep(ucp_ep_get_lane(ucp_ep, lane));
if ((wireup_ep == NULL) ||
(!(wireup_ep->flags & UCP_WIREUP_EP_FLAG_READY))) {
continue;
}
ucs_assert(wireup_ep->super.uct_ep != NULL);
ucs_trace("ep %p: switching wireup_ep %p to ready state", ucp_ep,
wireup_ep);
/* Switch to real transport and destroy proxy endpoint (aux_ep as well) */
ucp_proxy_ep_replace(&wireup_ep->super);
}
/* Replay pending requests */
ucp_wireup_replay_pending_requests(ucp_ep, &tmp_pending_queue);
UCS_ASYNC_UNBLOCK(&ucp_ep->worker->async);
return 1;
out_unblock:
UCS_ASYNC_UNBLOCK(&ucp_ep->worker->async);
return 0;
}
void ucp_wireup_update_flags(ucp_ep_h ucp_ep, ucp_lane_map_t lanes,
uint32_t new_flags)
{
ucp_lane_index_t lane;
ucp_wireup_ep_t *wireup_ep;
ucs_for_each_bit(lane, lanes) {
wireup_ep = ucp_wireup_ep(ucp_ep_get_lane(ucp_ep, lane));
if (wireup_ep == NULL) {
continue;
}
ucs_trace("ep %p: wireup_ep=%p flags=0x%x new_flags=0x%x", ucp_ep,
wireup_ep, wireup_ep->flags, new_flags);
wireup_ep->flags |= new_flags;
}
}
void ucp_wireup_remote_connected(ucp_ep_h ep)
{
if (ep->flags & UCP_EP_FLAG_REMOTE_CONNECTED) {
return;
}
ucs_trace("ep %p: remote connected, ep_cfg[%u]", ep, ep->cfg_index);
if (!(ep->flags & UCP_EP_FLAG_CLOSED)) {
/* set REMOTE_CONNECTED flag if an EP is not closed, otherwise -
* just make UCT EPs remote connected to remove WIREUP_EP for them
* and complete flush(LOCAL) operation in UCP EP close procedure
* (don't set REMOTE_CONNECTED flag to avoid possible wrong behavior
* in ucp_ep_close_flushed_callback() when a peer was already
* disconnected, but we set REMOTE_CONNECTED flag again) */
ucp_ep_update_flags(ep, UCP_EP_FLAG_REMOTE_CONNECTED, 0);
}
ucp_wireup_update_flags(ep, UCS_MASK(ucp_ep_num_lanes(ep)),
UCP_WIREUP_EP_FLAG_REMOTE_CONNECTED |
UCP_WIREUP_EP_FLAG_READY);
ucp_wireup_eps_progress_sched(ep);
ucs_assert(ep->flags & UCP_EP_FLAG_REMOTE_ID);
}
static UCS_F_NOINLINE void
ucp_wireup_process_pre_request(ucp_worker_h worker, ucp_ep_h ep,
const ucp_wireup_msg_t *msg,
const ucp_unpacked_address_t *remote_address)
{
unsigned ep_init_flags = UCP_EP_INIT_CREATE_AM_LANE |
UCP_EP_INIT_CM_WIREUP_CLIENT |
ucp_ep_err_mode_init_flags(msg->err_mode);
unsigned addr_indices[UCP_MAX_LANES];
int am_need_flush;
ucs_status_t status;
UCP_WIREUP_MSG_CHECK(msg, ep, UCP_WIREUP_MSG_PRE_REQUEST);
ucs_trace("got wireup pre_request from 0x%"PRIx64" src_ep_id 0x%"PRIx64
" dst_ep_id 0x%"PRIx64" conn_sn %u address version %u/%u",
remote_address->uuid, msg->src_ep_id, msg->dst_ep_id,
msg->conn_sn, remote_address->addr_version,
remote_address->dst_version);
ucs_assert(ucp_ep_get_cm_wireup_ep(ep) != NULL);
ucs_assert(ep->flags & UCP_EP_FLAG_CONNECT_WAIT_PRE_REQ);
status = ucp_ep_config_err_mode_check_mismatch(ep, msg->err_mode);
if (status != UCS_OK) {
goto err_ep_set_failed;
}
/* restore the EP here to avoid access to incomplete configuration before
this point */
ucp_ep_update_remote_id(ep, msg->src_ep_id);
/* initialize transport endpoints */
status = ucp_wireup_init_lanes(ep, ep_init_flags, &ucp_tl_bitmap_max,
remote_address, addr_indices,
&am_need_flush);
if (status != UCS_OK) {
goto err_ep_set_failed;
}
ucp_wireup_send_request(ep);
return;
err_ep_set_failed:
ucp_ep_set_lanes_failed_schedule(ep, 0, status);
}
static UCS_F_NOINLINE void
ucp_wireup_process_request(ucp_worker_h worker, ucp_ep_h ep,
const ucp_wireup_msg_t *msg,
const ucp_unpacked_address_t *remote_address)
{
uint64_t remote_uuid = remote_address->uuid;
int send_reply = 0;
unsigned ep_init_flags = ucp_ep_err_mode_init_flags(msg->err_mode);
ucp_tl_bitmap_t tl_bitmap = UCS_STATIC_BITMAP_ZERO_INITIALIZER;
ucp_lane_index_t lanes2remote[UCP_MAX_LANES];
unsigned addr_indices[UCP_MAX_LANES];
ucs_status_t status;
int has_cm_lane, am_need_flush, full_handshake_required;
UCP_WIREUP_MSG_CHECK(msg, ep, UCP_WIREUP_MSG_REQUEST);
ucs_trace("got wireup request from 0x%"PRIx64" src_ep_id 0x%"PRIx64
" dst_ep_id 0x%"PRIx64" conn_sn %d address version %u/%u",
remote_address->uuid, msg->src_ep_id, msg->dst_ep_id,
msg->conn_sn, remote_address->addr_version,
remote_address->dst_version);
if (ep != NULL) {
ucs_assert(msg->dst_ep_id != UCS_PTR_MAP_KEY_INVALID);
ucp_ep_update_remote_id(ep, msg->src_ep_id);
ep_init_flags |= UCP_EP_INIT_CREATE_AM_LANE;
} else {
ucs_assert(msg->dst_ep_id == UCS_PTR_MAP_KEY_INVALID);
ep = ucp_ep_match_retrieve(worker, remote_uuid,
msg->conn_sn ^
(remote_uuid == worker->uuid),
UCS_CONN_MATCH_QUEUE_EXP);
if (ep == NULL) {
/* Create a new endpoint if does not exist */
status = ucp_ep_create_base(worker, ep_init_flags,
remote_address->name,
"remote-request", &ep);
if (status != UCS_OK) {
return;
}
/* add internal endpoint to hash */
ep->conn_sn = msg->conn_sn;
if (!ucp_ep_match_insert(worker, ep, remote_uuid, ep->conn_sn,
UCS_CONN_MATCH_QUEUE_UNEXP)) {
if (worker->context->config.features & UCP_FEATURE_STREAM) {
ucs_diag("worker %p: created the endpoint %p without"
" connection matching, but Stream API support was"
" requested on the context %p",
worker, ep, worker->context);
}
}
} else {
status = ucp_ep_config_err_mode_check_mismatch(ep, msg->err_mode);
if (status != UCS_OK) {
goto err_set_ep_failed;
}
}
ucp_ep_update_remote_id(ep, msg->src_ep_id);
/*
* If the current endpoint already sent a connection request, we have a
* "simultaneous connect" situation. In this case, only one of the endpoints
* (instead of both) should respect the connect request, otherwise they
* will end up being connected to "internal" endpoints on the remote side
* instead of each other. We use the uniqueness of worker uuid to decide
* which connect request should be ignored.
*/
if ((ep->flags & UCP_EP_FLAG_CONNECT_REQ_QUEUED) &&
(remote_uuid > worker->uuid)) {
ucs_trace("ep %p: ignoring simultaneous connect request", ep);
ucp_ep_update_flags(ep, UCP_EP_FLAG_CONNECT_REQ_IGNORED, 0);
return;
}
}
has_cm_lane = ucp_ep_has_cm_lane(ep);
if (has_cm_lane) {
ep_init_flags |= UCP_EP_INIT_CM_WIREUP_SERVER;
}
/* Initialize lanes (possible destroy existing lanes) */
status = ucp_wireup_init_lanes(ep, ep_init_flags, &ucp_tl_bitmap_max,
remote_address, addr_indices,
&am_need_flush);
if (status != UCS_OK) {
goto err_set_ep_failed;
}
ucp_wireup_match_p2p_lanes(ep, remote_address, addr_indices, lanes2remote);
/* Full handshake is required in the following cases:
* 1) CM flow (the client's EP has to be marked as REMOTE_CONNECTED)
* 2) P2P lanes exist in EP configuration (client-server flow)
* 3) Old AM lane was replaced (ensures message order) */
full_handshake_required = has_cm_lane || ucp_ep_config(ep)->p2p_lanes ||
am_need_flush;
/* Send a reply if remote side does not have ep_ptr (active-active flow) or
* full handshake is required
*/
send_reply = (msg->dst_ep_id == UCS_PTR_MAP_KEY_INVALID) ||
full_handshake_required;
/* Connect p2p addresses to remote endpoint, if at least one is true: */
if (/* - EP has not been connected locally yet */
!(ep->flags & UCP_EP_FLAG_LOCAL_CONNECTED) ||
/* - EP has CM lane (it is locally connected, since CM lanes are
* connected) */
has_cm_lane) {
status = ucp_wireup_connect_local(ep, remote_address, lanes2remote);
if (status != UCS_OK) {
goto err_set_ep_failed;
}
tl_bitmap = ucp_wireup_get_ep_tl_bitmap(ep,
ucp_ep_config(ep)->p2p_lanes);
ucp_ep_update_flags(ep, UCP_EP_FLAG_LOCAL_CONNECTED, 0);
ucs_assert(send_reply);
}
if (!full_handshake_required) {
/* mark the endpoint as connected to remote */
ucp_wireup_remote_connected(ep);
}
if (send_reply) {
ucs_trace("ep %p: sending wireup reply", ep);
ucp_wireup_msg_send(ep,
am_need_flush ? UCP_WIREUP_MSG_REPLY_RECONFIG :
UCP_WIREUP_MSG_REPLY,
&tl_bitmap, lanes2remote);
}
return;
err_set_ep_failed:
ucp_ep_set_lanes_failed_schedule(ep, 0, status);
}
static unsigned ucp_wireup_send_msg_ack(void *arg)
{
ucp_ep_h ep = (ucp_ep_h)arg;
ucs_status_t status;
/* Send ACK without any address, we've already sent it as part of the request */
ucs_trace("ep %p: sending wireup ack", ep);
status = ucp_wireup_msg_send(ep, UCP_WIREUP_MSG_ACK, &ucp_tl_bitmap_min,
NULL);
return (status == UCS_OK);
}
int ucp_wireup_msg_ack_cb_pred(const ucs_callbackq_elem_t *elem, void *arg)
{
return ((elem->arg == arg) && (elem->cb == ucp_wireup_send_msg_ack));
}
static void
ucp_wireup_process_reply_common(ucp_worker_h worker, ucp_ep_h ep,
const ucp_wireup_msg_t *msg,
const ucp_unpacked_address_t *remote_address)
{
ucs_status_t status;
int ack;
ucs_trace("ep %p: got wireup reply src_ep_id 0x%"PRIx64
" dst_ep_id 0x%"PRIx64" sn %d", ep, msg->src_ep_id,
msg->dst_ep_id, msg->conn_sn);
ucp_ep_match_remove_ep(worker, ep);
ucp_ep_update_remote_id(ep, msg->src_ep_id);
/* Connect p2p addresses to remote endpoint */
if (!(ep->flags & UCP_EP_FLAG_LOCAL_CONNECTED) ||
ucp_ep_has_cm_lane(ep)) {
/*
* In the wireup reply message, the lane indexes specify which
* **receiver** ep lane should be connected to a given ep address. So we
* don't pass 'lanes2remote' mapping, and use local lanes directly.
*/
status = ucp_wireup_connect_local(ep, remote_address, NULL);
if (status != UCS_OK) {
ucp_ep_set_lanes_failed_schedule(ep, 0, status);
return;
}
ucp_ep_update_flags(ep, UCP_EP_FLAG_LOCAL_CONNECTED, 0);
ack = 1;
} else {
ack = 0;
}
ucp_wireup_remote_connected(ep);
if (ack || (msg->type == UCP_WIREUP_MSG_REPLY_RECONFIG)) {
/* Send `UCP_WIREUP_MSG_ACK` from progress function
* to avoid calling UCT routines from an async thread */
ucs_callbackq_add_oneshot(&worker->uct->progress_q, ep,
ucp_wireup_send_msg_ack, ep);
}
}
static UCS_F_NOINLINE void
ucp_wireup_process_reply(ucp_worker_h worker, ucp_ep_h ep,
const ucp_wireup_msg_t *msg,
const ucp_unpacked_address_t *remote_address)
{
UCP_WIREUP_MSG_CHECK(msg, ep, UCP_WIREUP_MSG_REPLY);
ucp_wireup_process_reply_common(worker, ep, msg, remote_address);
}
static UCS_F_NOINLINE void
ucp_wireup_process_reply_reconfig(ucp_worker_h worker, ucp_ep_h ep,
const ucp_wireup_msg_t *msg,
const ucp_unpacked_address_t *remote_address)
{
UCP_WIREUP_MSG_CHECK(msg, ep, UCP_WIREUP_MSG_REPLY_RECONFIG);
ucp_wireup_process_reply_common(worker, ep, msg, remote_address);
}
static void ucp_ep_removed_flush_completion(ucp_request_t *req)
{
ucs_log_level_t level = UCS_STATUS_IS_ERR(req->status) ?
UCS_LOG_LEVEL_DIAG :
UCS_LOG_LEVEL_DEBUG;
ucs_log(level, "flushing ep_removed (req %p) completed with status %s", req,
ucs_status_string(req->status));
ucp_ep_register_disconnect_progress(req);
}
static UCS_F_NOINLINE void
ucp_wireup_send_ep_removed(ucp_worker_h worker, const ucp_wireup_msg_t *msg,
const ucp_unpacked_address_t *remote_address)
{
/* 1. Request a peer failure detection support from a reply EP to be able
* to do discarding of lanes when destroying all UCP EPs in UCP worker
* destroy.
* 2. Create UCP EP with CONNECT_TO_IFACE connection mode to not do
* WIREUP_MSG phase between peers which require a direct EP ID.
* 3. Create UCP EP with AM lane only, because WIREUP_MSGs are sent using
* AM lane.
* 4. Allow selecting auxiliary transports for AM lane.
*/
unsigned ep_init_flags = UCP_EP_INIT_ERR_MODE_PEER_FAILURE |
UCP_EP_INIT_FLAG_INTERNAL |
UCP_EP_INIT_CONNECT_TO_IFACE_ONLY |
UCP_EP_INIT_CREATE_AM_LANE |
UCP_EP_INIT_CREATE_AM_LANE_ONLY |
UCP_EP_INIT_ALLOW_AM_AUX_TL;
ucs_status_t status;
ucp_ep_h reply_ep;
unsigned addr_indices[UCP_MAX_LANES];
ucs_status_ptr_t req;
int am_need_flush;
/* If endpoint does not exist - create a temporary endpoint to send a
* UCP_WIREUP_MSG_EP_REMOVED reply */
status = ucp_ep_create_base(worker, ep_init_flags, remote_address->name,
"wireup ep_check reply", &reply_ep);
if (status != UCS_OK) {
ucs_error("failed to create EP: %s", ucs_status_string(status));
return;
}
/* Initialize lanes of the reply EP */
status = ucp_wireup_init_lanes(reply_ep, ep_init_flags, &ucp_tl_bitmap_max,
remote_address, addr_indices,
&am_need_flush);
if (status != UCS_OK) {
goto out_delete_ep;
}
ucp_ep_update_remote_id(reply_ep, msg->src_ep_id);
status = ucp_wireup_msg_send(reply_ep, UCP_WIREUP_MSG_EP_REMOVED,
&ucp_tl_bitmap_min, NULL);
if (status != UCS_OK) {
goto out_cleanup_lanes;
}
req = ucp_ep_flush_internal(reply_ep, UCP_REQUEST_FLAG_RELEASED,
&ucp_request_null_param, NULL,
ucp_ep_removed_flush_completion, "close",
UCT_FLUSH_FLAG_LOCAL);
if (UCS_PTR_IS_PTR(req)) {
return;
}
out_cleanup_lanes:
ucp_ep_cleanup_lanes(reply_ep);
out_delete_ep:
ucp_ep_delete(reply_ep);
}
static UCS_F_NOINLINE
void ucp_wireup_process_ack(ucp_worker_h worker, ucp_ep_h ep,
const ucp_wireup_msg_t *msg)
{
UCP_WIREUP_MSG_CHECK(msg, ep, UCP_WIREUP_MSG_ACK);
ucs_trace("ep %p: got wireup ack", ep);
ucs_assert(ep->flags & UCP_EP_FLAG_REMOTE_ID);
ucs_assert(ep->flags & UCP_EP_FLAG_CONNECT_REP_SENT);
if (!(ep->flags & UCP_EP_FLAG_LOCAL_CONNECTED)) {
/* drop the processing of ACK since close protocol or error
* handling is started */
ucs_assert(ucp_ep_has_cm_lane(ep) &&
(ep->flags & UCP_EP_FLAG_DISCONNECTED_CM_LANE));
return;
}
ucp_wireup_remote_connected(ep);
}
static void
ucp_wireup_augment_aux_tls(ucp_ep_h ep, ucp_lane_map_t lane_map,
ucp_tl_bitmap_t *tl_bitmap)
{
ucp_context_h context = ep->worker->context;
ucp_rsc_index_t lane_rsc, aux_rsc;
ucp_lane_index_t lane;
uint64_t iface_flags;
ucs_for_each_bit(lane, lane_map) {
lane_rsc = ucp_ep_get_rsc_index(ep, lane);
if (lane_rsc == UCP_NULL_RESOURCE) {
continue;
}
for (aux_rsc = 0; aux_rsc < context->num_tls; ++aux_rsc) {
if (UCS_STATIC_BITMAP_GET(*tl_bitmap, aux_rsc)) {