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 "inner_net_dev.h"
11 : #include "null_ptr_exception.h"
12 : #include "exception_util.h"
13 : #include "network_api_exception.h"
14 :
15 : namespace Hccl {
16 17 : InnerNetDev::InnerNetDev(const NetDevInfo &info)
17 : {
18 17 : IpAddress localIp = info.addr;
19 :
20 17 : RaInterface intf{};
21 17 : intf.address = localIp;
22 17 : intf.phyId = info.devId;
23 :
24 51 : HCCL_DEBUG("InnerNetDev::Init, devPhyId[%u]", info.devId);
25 :
26 17 : localProto_ = info.protoType;
27 17 : if (info.type == PortDeploymentType::HOST_NET) {
28 2 : netMode_ = HrtNetworkMode::PEER;
29 : }
30 :
31 : try {
32 17 : if (localProto_ == LinkProtoType::RDMA) {
33 12 : rdmaHandle_ = HrtRaRdmaInit(netMode_, intf);
34 12 : auto dieAndFuncId = HraGetDieAndFuncId(rdmaHandle_);
35 12 : dieId_ = dieAndFuncId.first;
36 12 : funcId_ = dieAndFuncId.second;
37 :
38 12 : auto tokenIdHandlePair = RaUbAllocTokenIdHandle(rdmaHandle_);
39 12 : tokenHandle_ = tokenIdHandlePair.first;
40 12 : tokenId_ = tokenIdHandlePair.second;
41 5 : } else if (localProto_ == LinkProtoType::UB) {
42 4 : HrtRaUbCtxInitParam in(HrtNetworkMode::HDC, info.devId, localIp);
43 4 : rdmaHandle_ = HrtRaUbCtxInit(in);
44 4 : tokenInfoManager_ = make_unique<TokenInfoManager>(info.devId, rdmaHandle_);
45 : }
46 17 : isValid_ = true;
47 0 : } catch (const NetworkApiException &e) {
48 0 : HCCL_ERROR(e.what());
49 0 : isValid_ = false;
50 0 : }
51 17 : }
52 :
53 2 : JfcHandle InnerNetDev::getUbJfcHandle(HrtUbJfcMode jfcMode)
54 : {
55 2 : if (rdmaHandle_ == nullptr) {
56 1 : THROW<NullPtrException>("[InnerNetDev::%s] rdmaHandle_ is nullptr", __func__);
57 : }
58 : CqCreateInfo cqInfo;
59 1 : ubJfcHandle_ = HrtRaUbCreateJfc(rdmaHandle_, cqInfo, jfcMode);
60 1 : return ubJfcHandle_;
61 : }
62 :
63 2 : std::pair<TokenIdHandle, uint32_t> InnerNetDev::getTokenIdInfo(const BufferKey<uintptr_t, u64> &bufKey)
64 : {
65 2 : if (tokenInfoManager_ == nullptr) {
66 0 : THROW<NullPtrException>("[InnerNetDev::%s] tokenInfoManager_ is nullptr", __func__);
67 : }
68 :
69 2 : return tokenInfoManager_->GetTokenInfo(bufKey);
70 : }
71 :
72 17 : InnerNetDev::~InnerNetDev()
73 : {
74 17 : if (ubJfcHandle_ != 0) {
75 4 : DECTOR_TRY_CATCH("jfc handle destroy", HrtRaUbDestroyJfc(rdmaHandle_, ubJfcHandle_));
76 : }
77 17 : if (localProto_ == LinkProtoType::RDMA) {
78 12 : if (tokenHandle_ != 0) {
79 1 : RaUbFreeTokenIdHandle(rdmaHandle_, tokenId_);
80 : }
81 12 : if (rdmaHandle_ != nullptr) {
82 11 : HrtRaRdmaDeInit(rdmaHandle_, netMode_);
83 : }
84 5 : } else if (localProto_ == LinkProtoType::UB) {
85 4 : if (tokenInfoManager_ != nullptr) {
86 4 : tokenInfoManager_->Destroy();
87 : }
88 4 : if (rdmaHandle_ != nullptr) {
89 4 : HrtRaUbCtxDestroy(rdmaHandle_);
90 : }
91 : }
92 17 : }
93 :
94 : } // namespace Hccl
|