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 :
11 : #ifndef RMA_BUFFER_H
12 : #define RMA_BUFFER_H
13 :
14 : #include <memory>
15 :
16 : #include "hccl_common.h"
17 : #include "hccl_inner_common.h"
18 : #include "transport_mem.h"
19 :
20 : namespace hccl {
21 : class RmaBuffer {
22 : public:
23 52 : RmaBuffer(const HcclNetDevCtx netDevCtx, void* addr, u64 size, const RmaMemType memType, const RmaType rmaType)
24 52 : : netDevCtx(netDevCtx),
25 52 : addr(addr),
26 52 : size(size),
27 52 : memType(memType),
28 52 : rmaType(rmaType)
29 52 : {}
30 :
31 22 : RmaBuffer(
32 : const HcclNetDevCtx netDevCtx, void* addr, u64 size, const RmaMemType memType, const RmaType rmaType,
33 : bool isAlias)
34 22 : : netDevCtx(netDevCtx),
35 22 : addr(addr),
36 22 : size(size),
37 22 : memType(memType),
38 22 : rmaType(rmaType),
39 22 : isAlias_(isAlias)
40 22 : {}
41 :
42 74 : virtual ~RmaBuffer() = default;
43 :
44 : RmaBuffer(const RmaBuffer& that) = delete;
45 :
46 : RmaBuffer& operator=(const RmaBuffer& that) = delete;
47 :
48 37 : inline bool IsAlias() const { return isAlias_; }
49 :
50 130 : inline void* GetAddr() const { return addr; }
51 :
52 121 : inline u64 GetSize() const { return size; }
53 :
54 0 : inline RmaType GetRmaType() const // used for grant check
55 : {
56 0 : return rmaType;
57 : }
58 :
59 2 : inline RmaMemType GetMemType() const { return memType; }
60 :
61 28 : inline void* GetDevAddr() const { return devAddr; }
62 :
63 0 : inline HcclNetDevCtx GetNetDevCtx() const { return netDevCtx; }
64 :
65 : protected:
66 : const HcclNetDevCtx netDevCtx{nullptr};
67 : void* addr{nullptr};
68 : u64 size{0};
69 : void* devAddr{nullptr};
70 : RmaMemType memType{RmaMemType::TYPE_NUM};
71 : RmaType rmaType{RmaType::RMA_TYPE_RESERVED};
72 : bool isAlias_{false};
73 : };
74 :
75 : struct RmaBufferSlice {
76 : std::shared_ptr<RmaBuffer> rmaBuffer{nullptr};
77 : void* addr{nullptr};
78 : u64 len{0};
79 : RmaMemType memType{RmaMemType::DEVICE};
80 : };
81 :
82 0 : inline HcclResult CheckHcclBuffer(const void* addr, const RmaBuffer* rmaBuffer)
83 : {
84 0 : CHK_PTR_NULL(addr);
85 0 : CHK_PTR_NULL(rmaBuffer);
86 0 : if (UNLIKELY(
87 : reinterpret_cast<u64>(addr) < reinterpret_cast<u64>(rmaBuffer->GetAddr())
88 : || reinterpret_cast<u64>(addr) > (reinterpret_cast<u64>(rmaBuffer->GetAddr()) + rmaBuffer->GetSize()))) {
89 0 : HCCL_ERROR(
90 : "[CheckHcclBuffer]check buffer failed, hccl buffer addr[%p], "
91 : "ramBuffer addr[%p], rmaBuffer size[%u]",
92 : addr, rmaBuffer->GetAddr(), rmaBuffer->GetSize());
93 0 : return HCCL_E_PARA;
94 : }
95 0 : return HCCL_SUCCESS;
96 : }
97 : } // namespace hccl
98 : #endif // RDMA_RMA_BUFFER_H
|