Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 : #include "endpoint.h"
11 : #include <functional>
12 : #include "aicpu_ts_roce_endpoint.h"
13 : #include "cpu_roce_endpoint.h"
14 : #include "urma_endpoint.h"
15 : #include "ub_mem_endpoint.h"
16 : #include "uboe_endpoint.h"
17 : #include "ubg_endpoint.h"
18 : #include "cpu_urma_endpoint.h"
19 : #include "aicputs_hccs_endpoint.h"
20 : #include "hccp_nda.h"
21 : #include "adapter_rts_common.h"
22 :
23 : namespace hcomm{
24 68 : static bool IsSupported(const EndpointDesc &endpointDesc)
25 : {
26 68 : bool protocolSupported = false;
27 68 : bool locTypeSupported = false;
28 68 : switch (endpointDesc.protocol) {
29 68 : case COMM_PROTOCOL_ROCE:
30 : case COMM_PROTOCOL_UBC_TP:
31 : case COMM_PROTOCOL_UBC_CTP:
32 : case COMM_PROTOCOL_UB_MEM:
33 : case COMM_PROTOCOL_PCIE:
34 : case COMM_PROTOCOL_UBOE:
35 : case COMM_PROTOCOL_UBG:
36 : case COMM_PROTOCOL_HCCS:
37 68 : protocolSupported = true;
38 68 : break;
39 0 : default:
40 0 : return false;
41 : }
42 68 : switch (endpointDesc.loc.locType) {
43 68 : case ENDPOINT_LOC_TYPE_DEVICE:
44 : case ENDPOINT_LOC_TYPE_HOST:
45 68 : locTypeSupported = true;
46 68 : break;
47 0 : default:
48 0 : return false;
49 : }
50 :
51 68 : return protocolSupported && locTypeSupported;
52 : }
53 :
54 283 : Endpoint::Endpoint(const EndpointDesc &endpointDesc)
55 : {
56 283 : endpointDesc_ = endpointDesc;
57 283 : }
58 :
59 68 : HcclResult Endpoint::CreateEndpoint(const EndpointDesc &endpointDesc, std::unique_ptr<Endpoint> &endpointPtr)
60 : {
61 68 : if (!IsSupported(endpointDesc)) {
62 0 : HCCL_ERROR("[%s]endpointDesc is not supported. endpointDesc.protocol [%d] endpointDesc.loc.locType [%d].", __func__, endpointDesc.protocol, endpointDesc.loc.locType);
63 0 : return HCCL_E_PARA;
64 : }
65 :
66 68 : HCCL_INFO("[%s]endpointDesc.protocol [%d] endpointDesc.loc.locType [%d].", __func__, endpointDesc.protocol, endpointDesc.loc.locType);
67 :
68 68 : return CreateEndpointBase(endpointDesc, endpointPtr);
69 : }
70 :
71 68 : HcclResult Endpoint::CreateEndpointBase(const EndpointDesc &endpointDesc, std::unique_ptr<Endpoint> &endpointPtr)
72 : {
73 : using EndpointCreator = std::function<std::unique_ptr<Endpoint>(const EndpointDesc &)>;
74 : struct Entry {
75 : CommProtocol protocol;
76 : EndpointLocType locType;
77 : EndpointCreator creator;
78 : };
79 : static const Entry table[] = {
80 7 : {COMM_PROTOCOL_ROCE, ENDPOINT_LOC_TYPE_HOST, [](const EndpointDesc &d) { return std::make_unique<CpuRoceEndpoint>(d); }},
81 0 : {COMM_PROTOCOL_UBC_TP, ENDPOINT_LOC_TYPE_HOST, [](const EndpointDesc &d) { return std::make_unique<CpuUrmaEndpoint>(d); }},
82 0 : {COMM_PROTOCOL_UBC_CTP, ENDPOINT_LOC_TYPE_HOST, [](const EndpointDesc &d) { return std::make_unique<CpuUrmaEndpoint>(d); }},
83 0 : {COMM_PROTOCOL_UBC_TP, ENDPOINT_LOC_TYPE_DEVICE, [](const EndpointDesc &d) { return std::make_unique<UrmaEndpoint>(d); }},
84 18 : {COMM_PROTOCOL_UBC_CTP, ENDPOINT_LOC_TYPE_DEVICE, [](const EndpointDesc &d) { return std::make_unique<UrmaEndpoint>(d); }},
85 11 : {COMM_PROTOCOL_UB_MEM, ENDPOINT_LOC_TYPE_DEVICE, [](const EndpointDesc &d) { return std::make_unique<UbMemEndpoint>(d); }},
86 0 : {COMM_PROTOCOL_PCIE, ENDPOINT_LOC_TYPE_DEVICE, [](const EndpointDesc &d) { return std::make_unique<UbMemEndpoint>(d); }},
87 13 : {COMM_PROTOCOL_UBOE, ENDPOINT_LOC_TYPE_DEVICE, [](const EndpointDesc &d) { return std::make_unique<UboeEndpoint>(d); }},
88 2 : {COMM_PROTOCOL_UBG, ENDPOINT_LOC_TYPE_DEVICE, [](const EndpointDesc &d) { return std::make_unique<UbgEndpoint>(d); }},
89 1 : {COMM_PROTOCOL_ROCE, ENDPOINT_LOC_TYPE_DEVICE, [](const EndpointDesc &d) { return std::make_unique<AicpuTsRoceEndpoint>(d); }},
90 17 : {COMM_PROTOCOL_HCCS, ENDPOINT_LOC_TYPE_DEVICE, [](const EndpointDesc &d) { return std::make_unique<AicpuTsHccsEndpoint>(d); }},
91 68 : };
92 :
93 473 : for (const auto &entry : table) {
94 471 : if (entry.protocol == endpointDesc.protocol && entry.locType == endpointDesc.loc.locType) {
95 66 : EXCEPTION_CATCH(endpointPtr = entry.creator(endpointDesc), return HCCL_E_PTR);
96 66 : return HCCL_SUCCESS;
97 : }
98 : }
99 :
100 2 : HCCL_ERROR("[%s] failed, endpointDesc.protocol [%d] and endpointDesc.loc.locType [%d] do not match.",
101 : __func__, endpointDesc.protocol, endpointDesc.loc.locType);
102 2 : return HCCL_E_PARA;
103 : }
104 :
105 1 : HcclResult Endpoint::CheckFeature(const EndpointDesc &endpointDesc, HcommEndpointFeatureType featureType, bool &value)
106 : {
107 1 : if (featureType == HCOMM_ENDPOINT_FEATURE_NDA) {
108 1 : if (endpointDesc.protocol != COMM_PROTOCOL_ROCE || endpointDesc.loc.locType != ENDPOINT_LOC_TYPE_HOST) {
109 0 : HCCL_WARNING("[%s] not support NDA, protocol[%d], locType[%d]",
110 : __func__, endpointDesc.protocol, endpointDesc.loc.locType);
111 0 : value = false;
112 0 : return HCCL_SUCCESS;
113 : }
114 :
115 1 : Hccl::IpAddress ipAddr{};
116 1 : CHK_RET(CommAddrToIpAddress(endpointDesc.commAddr, ipAddr));
117 1 : s32 devId = 0;
118 1 : CHK_RET(hrtGetDevice(&devId));
119 1 : u32 devPhyId = 0;
120 1 : CHK_RET(hrtGetDevicePhyIdByIndex(devId, devPhyId));
121 :
122 1 : auto &rdmaHandleMgr = Hccl::RdmaHandleManager::GetInstance();
123 : void *rdmaHandle = static_cast<void *>(
124 1 : rdmaHandleMgr.GetByAddr(devPhyId, Hccl::LinkProtoType::RDMA, ipAddr, Hccl::PortDeploymentType::HOST_NET));
125 1 : CHK_PTR_NULL(rdmaHandle);
126 :
127 1 : s32 directFlag = 0;
128 1 : s32 ret = RaNdaGetDirectFlag(rdmaHandle, &directFlag);
129 1 : CHK_PRT_RET(ret != HCCL_SUCCESS,
130 : HCCL_ERROR("[%s] failed to get directFlag, ret[%d]", __func__, ret), HCCL_E_INTERNAL);
131 1 : value = (directFlag != DIRECT_FLAG_NOTSUPP);
132 1 : HCCL_INFO("[%s] %s NDA, rdmaHandle[%p], directFlag[%d]",
133 : __func__, value ? "support" : "not support", rdmaHandle, directFlag);
134 : } else {
135 0 : HCCL_WARNING("[%s] unsupported featureType[%d]", __func__, featureType);
136 0 : value = false;
137 : }
138 :
139 1 : return HCCL_SUCCESS;
140 : }
141 : }
|