MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
FabricInterfaceProbe.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Contributors to the Media eXchange Layer project.
2// SPDX-License-Identifier: Apache-2.0
4#include <functional>
5#include <optional>
6#include <unistd.h>
7#include <rdma/fabric.h>
9#include "Exception.hpp"
10#include "FabricAddress.hpp"
11#include "FabricInfo.hpp"
13#include "Format.hpp" // IWYU pragma: keep; used in fmt::to_string()
14#include "Provider.hpp"
15#include "ProviderConfig.hpp"
16
18{
19 namespace
20 {
21 [[nodiscard]]
22 std::optional<std::string> optStringFromCStr(char const* source)
23 {
24 return source == nullptr ? std::nullopt : std::make_optional<std::string>(source);
25 }
26
27 [[nodiscard]]
28 std::string getHostname()
29 {
30 char buf[256] = {};
31 ::gethostname(buf, sizeof(buf));
32 return buf;
33 }
34
35 [[nodiscard]]
36 bool matchesRequestedAddress(FabricAddress const& requested, FabricInfoView info)
37 {
38 auto provider = providerFromString(info->fabric_attr->prov_name);
39 if (!provider)
40 {
41 return false;
42 }
43
44 if (requested.empty())
45 {
46 return true;
47 }
48
49 if (*provider == Provider::SHM)
50 {
51 return requested.node() == getHostname();
52 }
53
54 try
55 {
56 auto const addressFormat = mustConvertAddressFormat(info->addr_format);
57 auto addr = FabricAddress::decode(addressFormat, info->src_addr, info->src_addrlen);
58 return addr.node() == requested.node();
59 }
60 catch (Exception const& ex)
61 {
62 if (ex.status() != MXL_ERR_INVALID_ARG)
63 {
64 throw;
65 }
66
67 return false;
68 }
69 }
70 }
71
72 FabricInterfaceList probeInterfaces(std::optional<std::reference_wrapper<::mxlFabricsInterfaceConfig const>> query)
73 {
74 // Maps of provider specific configurations and addresses. They are lazily created when they appear on the fabric list.
75 auto providerConfigs = std::map<Provider, ProviderConfig>{};
76 auto fabricAddresses = std::map<Provider, std::optional<FabricAddress>>{};
77 auto getProviderConfig = [&](Provider provider) -> ProviderConfig const&
78 {
79 auto it = providerConfigs.lower_bound(provider);
80 if ((it == providerConfigs.end()) || (it->first != provider))
81 {
82 it = providerConfigs.emplace_hint(it, provider, ProviderConfig::create(provider, false, std::nullopt));
83 }
84
85 return it->second;
86 };
87
88 auto getFabricAddress = [&](Provider provider) -> std::optional<FabricAddress> const&
89 {
90 auto it = fabricAddresses.lower_bound(provider);
91 if ((it == fabricAddresses.end()) || (it->first != provider))
92 {
93 try
94 {
95 it = fabricAddresses.emplace_hint(it,
96 provider,
97 FabricAddress::parse(provider,
98 query ? optStringFromCStr(query.value().get().address.node) : std::nullopt,
99 query ? optStringFromCStr(query.value().get().address.service) : std::nullopt));
100 }
101 catch (Exception const&)
102 {
103 it = fabricAddresses.emplace_hint(it, provider, std::nullopt);
104 }
105 }
106
107 return it->second;
108 };
109
110 // clang-format off
111 auto const requestedProvider = query
112 ? providerFromAPI(query.value().get().provider)
114 // clang-format on
115 if (!requestedProvider)
116 {
117 throw Exception::invalidArgument("Invalid provider");
118 }
119
120 auto list = FabricInterfaceList{};
121 for (auto&& info : FabricInfoList::get())
122 {
123 // Ignore if the provider of this fabric info is not known.
124 auto provider = providerFromString(info->fabric_attr->prov_name);
125 if (!provider)
126 {
127 continue;
128 }
129
130 // Ignore if a specific provider is requested, and this one does not match.
131 if ((requestedProvider != Provider::ANY) && (provider != requestedProvider))
132 {
133 continue;
134 }
135
136 // Ignore if the query address could not be parsed for this provider
137 auto const& address = getFabricAddress(*provider);
138 if (!address)
139 {
140 continue;
141 }
142
143 // Ignore if the info does not match the requested address
144 if (!matchesRequestedAddress(*address, info))
145 {
146 continue;
147 }
148
149 // Ignore if no supported.
150 auto const& config = getProviderConfig(*provider);
151 if (config.isSupportedFabricInfo(info))
152 {
153 auto description = FabricInterfaceDescription::create(info);
154 if (description)
155 {
156 list.push(*description);
157 }
158 }
159 }
160
161 return list;
162 }
163
164 std::pair<FabricInfo, ProviderConfig> selectSourceInterface(::mxlFabricsInterfaceConfig const& interfaceConfig, bool isTarget)
165 {
166 auto provider = providerFromAPI(interfaceConfig.provider);
167 if (!provider)
168 {
169 throw Exception::invalidArgument("invalid provider");
170 }
171
172 constexpr auto transferCapsMask = std::uint64_t{MXL_FABRICS_IFACE_CAP_REMOTE_WRITE | MXL_FABRICS_IFACE_CAP_SEND_RECEIVE};
173 auto caps = ProviderCapabilities::fromAPI(interfaceConfig.caps);
174 if ((caps.interfaceCaps & transferCapsMask) == 0)
175 {
177 "Unsupported provider constraints: Missing transfer capability. Need either SEND_RECEIVE or REMOTE_WRITE");
178 }
179 else if ((caps.interfaceCaps & MXL_FABRICS_IFACE_CAP_REMOTE_WRITE) == 0)
180 {
181 throw Exception::noFabric("Unsupported provider constraints: Only REMOTE_WRITE supported at this time.");
182 }
183 if (caps.maxMessageSize == 0)
184 {
185 MXL_WARN("maxMessageSize is not set. This field will be required in a future version.");
186 }
187 auto providerConfig = ProviderConfig::create(*provider, isTarget, caps);
188 auto fabricAddress = FabricAddress::parse(
189 *provider, optStringFromCStr(interfaceConfig.address.node), optStringFromCStr(interfaceConfig.address.service));
190
191 auto sourceInterfaces = FabricInfoList::getSourceInterfaces(providerConfig, fabricAddress);
192 if (std::ranges::distance(sourceInterfaces) == 0)
193 {
194 throw Exception::noFabric("no supported interfaces found");
195 }
196
197 return {*sourceInterfaces.begin(), std::move(providerConfig)};
198 }
199}
#define MXL_WARN(...)
Definition Logging.hpp:56
Custom exception type used in the fabrics library.
Definition Exception.hpp:28
static Exception invalidArgument(fmt::format_string< T... > fmt, T &&... args)
Make an MXL_ERR_INVALID_ARG exception.
Definition Exception.hpp:43
static Exception noFabric(fmt::format_string< T... > fmt, T &&... args)
Definition Exception.hpp:95
Owning, type-safe representation of a libfabric address.
static FabricAddress parse(Provider provider, std::optional< std::string > node, std::optional< std::string > service)
Parse a node/service pair into a typed address for the given provider.
static FabricAddress decode(FabricAddressFormat addrFormat, void const *addr, std::size_t len)
Decode a raw libfabric address buffer of the given addr_format.
static FabricInfoList get()
Calls fi_getinfo without any hints and returns the full list of fabric information objects.
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...
Non-owning view of a FabricInfo. Returned when iterating over a FabricInfoList.
static std::optional< FabricInterfaceDescription > create(FabricInfoView info)
static ProviderCapabilities fromAPI(::mxlFabricsInterfaceCaps)
static ProviderConfig create(Provider provider, bool isTarget, std::optional< ProviderCapabilities > capabilities)
@ MXL_FABRICS_IFACE_CAP_SEND_RECEIVE
Definition fabrics.h:80
@ MXL_FABRICS_IFACE_CAP_REMOTE_WRITE
Definition fabrics.h:79
struct mxlFabricsInterfaceConfig_t mxlFabricsInterfaceConfig
@ MXL_ERR_INVALID_ARG
Definition mxl.h:35
std::pair< FabricInfo, ProviderConfig > selectSourceInterface(::mxlFabricsInterfaceConfig const &interfaceConfig, bool isTarget)
FabricInterfaceList probeInterfaces(std::optional< std::reference_wrapper<::mxlFabricsInterfaceConfig const > > query)
std::optional< Provider > providerFromString(std::string const &s) noexcept
Parse a provider name string, and return the enum value.
Definition Provider.cpp:49
std::optional< Provider > providerFromAPI(mxlFabricsProvider api) noexcept
Convert between external and internal versions of this type.
Definition Provider.cpp:35
Provider
Internal representation of supported libfabric providers.
Definition Provider.hpp:17
FabricAddressFormat mustConvertAddressFormat(std::uint32_t fiAddrFormat)
Convert from libfabric address format value to FabricAddressFormat Never returns FabricAddressFormat:...
mxlFabricsProvider provider
Definition fabrics.h:100
mxlFabricsInterfaceCaps caps
Definition fabrics.h:101
mxlFabricsEndpointAddress address
Definition fabrics.h:103