MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
FabricInfo.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Contributors to the Media eXchange Layer project.
2//
3// SPDX-License-Identifier: Apache-2.0
4
5#include "FabricInfo.hpp"
6#include <rdma/fabric.h>
7#include <rdma/fi_errno.h>
8#include "Exception.hpp"
9#include "FabricAddress.hpp"
10#include "FabricVersion.hpp"
11#include "Format.hpp" // IWYU pragma: keep; Includes template specializations of fmt::formatter for our types
12#include "Provider.hpp"
13#include "ProviderConfig.hpp"
14
16{
17 // Main constructor, takes ownership of the provided fi_info
18 FabricInfo::FabricInfo(::fi_info* raw) noexcept
19 : _raw(raw)
20 {}
21
22 // Construct from a non-owning view of a fi_info
23 FabricInfo::FabricInfo(FabricInfoView view) noexcept
24 : _raw(::fi_dupinfo(view.raw()))
25 {}
26
27 // Clone a raw fi_info and take ownership
28 FabricInfo FabricInfo::clone(::fi_info const* info) noexcept
29 {
30 return FabricInfo{::fi_dupinfo(info)};
31 }
32
33 // Own a raw fi_info
34 FabricInfo FabricInfo::own(::fi_info* info) noexcept
35 {
36 return FabricInfo{info};
37 }
38
39 // Allocate an empty fi_info
40 FabricInfo FabricInfo::empty() noexcept
41 {
42 return FabricInfo{::fi_allocinfo()};
43 }
44
45 FabricInfo::~FabricInfo() noexcept
46 {
47 free();
48 }
49
50 FabricInfo::FabricInfo(FabricInfo const& other) noexcept
51 : _raw(::fi_dupinfo(other._raw))
52 {}
53
54 void FabricInfo::operator=(FabricInfo const& other) noexcept
55 {
56 free();
57
58 _raw = ::fi_dupinfo(other._raw);
59 }
60
61 FabricInfo::FabricInfo(FabricInfo&& other) noexcept
62 : _raw(other._raw)
63 {
64 other._raw = nullptr;
65 }
66
67 FabricInfo& FabricInfo::operator=(FabricInfo&& other) noexcept
68 {
69 _raw = other._raw;
70 other._raw = nullptr;
71
72 return *this;
73 }
74
75 ::fi_info& FabricInfo::operator*() noexcept
76 {
77 return *_raw;
78 }
79
80 ::fi_info const& FabricInfo::operator*() const noexcept
81 {
82 return *_raw;
83 }
84
85 ::fi_info* FabricInfo::operator->() noexcept
86 {
87 return _raw;
88 }
89
90 ::fi_info const* FabricInfo::operator->() const noexcept
91 {
92 return _raw;
93 }
94
95 ::fi_info* FabricInfo::raw() noexcept
96 {
97 return _raw;
98 }
99
100 ::fi_info const* FabricInfo::raw() const noexcept
101 {
102 return _raw;
103 }
104
105 FabricInfoView FabricInfo::view() const noexcept
106 {
107 return FabricInfoView{_raw};
108 }
109
110 void FabricInfo::free() noexcept
111 {
112 if (_raw != nullptr)
113 {
114 ::fi_freeinfo(_raw);
115 _raw = nullptr;
116 }
117 }
118
120 : _raw(const_cast<::fi_info*>(raw))
121 {}
122
123 ::fi_info& FabricInfoView::operator*() noexcept
124 {
125 return *_raw;
126 }
127
128 ::fi_info const& FabricInfoView::operator*() const noexcept
129 {
130 return *_raw;
131 }
132
133 ::fi_info* FabricInfoView::operator->() noexcept
134 {
135 return _raw;
136 }
137
138 ::fi_info const* FabricInfoView::operator->() const noexcept
139 {
140 return _raw;
141 }
142
143 ::fi_info* FabricInfoView::raw() noexcept
144 {
145 return _raw;
146 }
147
148 ::fi_info const* FabricInfoView::raw() const noexcept
149 {
150 return _raw;
151 }
152
154 {
155 return FabricInfo::clone(_raw);
156 }
157
158 std::size_t FabricInfoView::txIovLimit() const noexcept
159 {
160 return _raw->tx_attr->iov_limit;
161 }
162
163 std::size_t FabricInfoView::txSize() const noexcept
164 {
165 return _raw->tx_attr->size;
166 }
167
168 ::fi_ep_type FabricInfoView::endpointType() const noexcept
169 {
170 return _raw->ep_attr->type;
171 }
172
174 {
175 auto info = std::add_pointer_t<::fi_info>{nullptr};
176 fiCall(::fi_getinfo, "Failed to get provider information", fiVersion(), nullptr, nullptr, 0, nullptr, &info);
177 return FabricInfoList{info};
178 }
179
180 FabricInfoList FabricInfoList::getSourceInterfaces(ProviderConfig const& providerConfig, std::optional<FabricAddress> const& sourceAddress)
181 {
182 auto info = std::add_pointer_t<::fi_info>{nullptr};
183 auto hints = FabricInfo::empty();
184
185 auto node = std::string{};
186 auto service = std::string{};
187 auto nodeStringBuffer = std::add_pointer_t<char const>{nullptr};
188 auto serviceStringBuffer = std::add_pointer_t<char const>{nullptr};
189
190 // Provider name is owned by the fi_info object and must be ::strdupe'd here.
191 hints->fabric_attr->prov_name = ::strdup(providerConfig.getProviderName().c_str());
192
193 // Set hints defined in the provider config
194 hints->domain_attr->mr_mode = providerConfig.getSupportedMemoryRegistrationModes();
195 hints->ep_attr->type = providerConfig.getEndpointType();
196 hints->caps = providerConfig.getCaps();
197
198 if (sourceAddress)
199 {
200 // If a service name/port exists, create a std::string, and get the c_str()
201 // the std::strings need to live through the call to fi_getinfo, so they are assigned to lvalues
202 // in the parent scope too.
203 if (auto nodeValue = sourceAddress->node(); nodeValue)
204 {
205 node = *nodeValue;
206 nodeStringBuffer = node.c_str();
207 }
208
209 if (auto serviceValue = sourceAddress->service(); serviceValue)
210 {
211 service = *serviceValue;
212 serviceStringBuffer = service.c_str();
213 }
214
215 // The verbs provider need the addr_format hint set for ipv6 to work correctly
216 hints->addr_format = static_cast<std::uint32_t>(sourceAddress->addressFormat());
217 }
218
219 fiCall(::fi_getinfo,
220 "Failed to get source interface information",
221 fiVersion(),
222 nodeStringBuffer,
223 serviceStringBuffer,
224 FI_SOURCE,
225 hints.raw(),
226 &info);
227 return FabricInfoList{info};
228 }
229
230 FabricInfoList FabricInfoList::own(::fi_info* info) noexcept
231 {
232 return FabricInfoList{info};
233 }
234
236 : _begin(begin)
237 {}
238
243
245 : _begin(other._begin)
246 {
247 other._begin = nullptr;
248 }
249
251 {
252 free();
253
254 _begin = other._begin;
255 other._begin = nullptr;
256 return *this;
257 }
258
263
265 {
266 return iterator{nullptr};
267 }
268
273
275 {
276 return const_iterator{nullptr};
277 }
278
283
285 {
286 return const_iterator{nullptr};
287 }
288
290 {
291 if (_begin != nullptr)
292 {
293 ::fi_freeinfo(_begin);
294 _begin = nullptr;
295 }
296 }
297}
~FabricInfoList()
calls fi_freeinfo to deallocate the underlying linked-list
FabricInfoIterator< false > iterator
FabricInfoList(FabricInfoList const &)=delete
static FabricInfoList get()
Calls fi_getinfo without any hints and returns the full list of fabric information objects.
const_iterator cbegin() const noexcept
static FabricInfoList getSourceInterfaces(ProviderConfig const &providerConfig, std::optional< FabricAddress > const &sourceAddress)
Calls fi_getinfo with FI_SOURCE to get source interface information. Uses hints derived from the prov...
FabricInfoIterator< true > const_iterator
const_iterator cend() const noexcept
FabricInfoList & operator=(FabricInfoList const &)=delete
static FabricInfoList own(::fi_info *info) noexcept
Take ownership over a fi_info raw pointer.
Non-owning view of a FabricInfo. Returned when iterating over a FabricInfoList.
FabricInfo owned() noexcept
Return an owned version that can be moved, copied and dereferenced safely even if the original Fabric...
::fi_ep_type endpointType() const noexcept
FabricInfoView(::fi_info const *)
Private constructor.
std::size_t txSize() const noexcept
Return the transmit queue size of the endpoint.
::fi_info * operator->() noexcept
Dereferencing returns the raw fi_info object associated with this view.
::fi_info * raw() noexcept
Returns the raw fi_info object associated with this view.
::fi_info & operator*() noexcept
Dereferencing returns the raw fi_info object associated with this view.
std::size_t txIovLimit() const noexcept
Return the IOV limit for this fabric info, which is the maximum number of iovecs that can be posted i...
::fi_ep_type getEndpointType() const noexcept
Return the endpoint type for this provider (FI_EP_MSG, FI_EP_RDM, etc.).
std::uint64_t getCaps() const noexcept
Return the libfabric capability flags for this provider configuration.
std::string getProviderName() const
Get the provider name (as expected by libfabric in prov_name) as a string.
int getSupportedMemoryRegistrationModes() const noexcept
Return a value that represents all supported memory registration modes (mr_mode).
FabricInfo(FabricInfoView) noexcept
Create an owning FabricInfo object from a non-owning view.
FabricInfoView view() const noexcept
Return a non-owning view of the FabricInfo.
std::uint32_t fiVersion() noexcept
Returns the libfabric version this library was compiled with.
::fi_info * raw() noexcept
Returns the raw fi_info object managed by this FabricInfo instance.
int fiCall(F &&fun, std::string_view msg, T &&... args)
Call a libfabric function and check its return code.
void free() noexcept
Called internally to release the fi_info object.