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