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 "dev_rdma_connection.h"
11 : #include "not_support_exception.h"
12 : #include "orion_adapter_rts.h"
13 : namespace Hccl {
14 :
15 10 : DevRdmaConnection::DevRdmaConnection(Socket* socket, RdmaHandle rdmaHandle, OpMode opMode)
16 10 : : RmaConnection(socket, RmaConnType::RDMA)
17 : {
18 10 : int qpMode = 0;
19 10 : DevType devType = HrtGetDeviceType();
20 10 : if (devType == DevType::DEV_TYPE_910A2 || devType == DevType::DEV_TYPE_910A3) {
21 9 : qpMode = (opMode == OpMode::OPBASE) ? OPBASE_QP_MODE_EXT : OFFLINE_QP_MODE_EXT;
22 1 : } else if (devType == DevType::DEV_TYPE_910A) {
23 0 : qpMode = (opMode == OpMode::OPBASE) ? OPBASE_QP_MODE : OFFLINE_QP_MODE;
24 : } else {
25 3 : HCCL_ERROR(
26 : "Cannot support this device type!"
27 : "errNo[0x%016llx], device type[%s]",
28 : HCCL_ERROR_CODE(HcclResult::HCCL_E_NOT_SUPPORT), DevTypeToString(devType).c_str());
29 1 : throw NotSupportException(DevTypeToString(devType));
30 : }
31 9 : qpHandle = HrtRaQpCreate(rdmaHandle, QP_FLAG_RC, qpMode);
32 10 : }
33 :
34 0 : void DevRdmaConnection::Connect() { GetStatus(); }
35 :
36 4 : RmaConnStatus DevRdmaConnection::GetStatus()
37 : {
38 4 : if (status == RmaConnStatus::INIT) {
39 4 : if (rdmaConnStatus == RdmaConnStatus::INIT) {
40 3 : if (socket->GetStatus() == SocketStatus::OK) {
41 2 : HrtRaQpConnectAsync(qpHandle, socket->GetFdHandle());
42 2 : rdmaConnStatus = RdmaConnStatus::CONNECTING;
43 2 : CheckQpStatus(qpHandle);
44 1 : } else if (socket->GetStatus() == SocketStatus::TIMEOUT) {
45 1 : rdmaConnStatus = RdmaConnStatus::SOCKET_TIMEOUT;
46 1 : status = RmaConnStatus::CONN_INVALID;
47 : }
48 1 : } else if (rdmaConnStatus == RdmaConnStatus::CONNECTING) {
49 1 : CheckQpStatus(qpHandle);
50 : }
51 : }
52 4 : return status;
53 : }
54 :
55 3 : void DevRdmaConnection::CheckQpStatus(QpHandle handle)
56 : {
57 3 : if (HrtGetRaQpStatus(handle) == 1) { // 为1时,qp 建链成功
58 2 : status = RmaConnStatus::READY;
59 : }
60 3 : }
61 :
62 1 : QpHandle DevRdmaConnection::GetHandle() { return qpHandle; }
63 :
64 9 : DevRdmaConnection::~DevRdmaConnection() { HrtRaQpDestroy(qpHandle); }
65 :
66 6 : unique_ptr<BaseTask> DevRdmaConnection::PrepareWrite(
67 : const MemoryBuffer& remoteMemBuf, const MemoryBuffer& localMemBuf, const SqeConfig& config)
68 : {
69 8 : VerifySizeIsEqual(remoteMemBuf, localMemBuf, "DevRdmaConnection::PrepareWrite");
70 :
71 4 : if (localMemBuf.size == 0) {
72 1 : return nullptr;
73 : }
74 : // RDMA_WRITE: op = 0
75 3 : HRaSendWr wr(localMemBuf.addr, localMemBuf.size, remoteMemBuf.addr, 0, RA_SEND_SIGNALED);
76 3 : RaSendWrResp resp = HrtRaSendOneWr(qpHandle, wr);
77 :
78 3 : return make_unique<TaskRdmaSend>(resp.dbIndex, static_cast<u64>(resp.dbInfo));
79 : }
80 :
81 0 : string DevRdmaConnection::Describe() const
82 : {
83 0 : return StringFormat("DevRdmaConnection[status=%s]", status.Describe().c_str());
84 : }
85 :
86 : } // namespace Hccl
|