MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
FabricInterfaceDescription.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 <cstring> // IWYU pragma: keep; // ::strdup
5#include <optional>
6#include <string>
7#include <type_traits>
8#include <utility>
9#include <unistd.h>
10#include <rdma/fabric.h>
11#include "CompletionQueue.hpp"
12#include "FabricAddress.hpp"
13#include "Format.hpp"
14#include "Provider.hpp"
15
17{
18 namespace
19 {
20 [[nodiscard]]
21 std::string getHostname()
22 {
23 char buf[256] = {};
24 ::gethostname(buf, sizeof(buf));
25 return buf;
26 }
27
28 void copyStringProperty(picojson::object& destination, char const* value, std::string const& name)
29 {
30 if ((value != nullptr) && (value[0] != '\0'))
31 {
32 destination[name] = picojson::value{std::string{value}};
33 }
34 }
35
36 void copyStringProperty(picojson::object& destination, std::string const& value, std::string const& name)
37 {
38 if (!value.empty())
39 {
40 destination[name] = picojson::value{value};
41 }
42 }
43
44 template<typename T>
45 void copyNumberProperty(picojson::object& destination, T value, std::string const& name)
46 {
47 destination[name] = picojson::value{static_cast<double>(value)};
48 }
49
50 void copyNicProperties(picojson::object& destination, ::fid_nic* nic)
51 {
52 if (nic->device_attr)
53 {
54 auto const device = nic->device_attr;
55 copyStringProperty(destination, device->name, "device_name");
56 copyStringProperty(destination, device->device_id, "device_id");
57 copyStringProperty(destination, device->vendor_id, "device_vendor_id");
58 copyStringProperty(destination, device->device_version, "device_version");
59 copyStringProperty(destination, device->driver, "device_driver");
60 copyStringProperty(destination, device->firmware, "device_firmware");
61 }
62 if (nic->link_attr != nullptr)
63 {
64 auto const link = nic->link_attr;
65 auto linkState = std::string{};
66 switch (link->state)
67 {
68 case FI_LINK_UP: linkState = "up"; break;
69 case FI_LINK_DOWN: linkState = "down"; break;
70 default: linkState = "unknown";
71 }
72 destination["link_state"] = picojson::value{linkState};
73 if (link->speed != 0)
74 {
75 destination["link_speed"] = picojson::value{static_cast<double>(link->speed)};
76 }
77 copyStringProperty(destination, link->address, "link_address");
78 copyStringProperty(destination, link->network_type, "link_type");
79 }
80 if ((nic->bus_attr) && (nic->bus_attr->bus_type == FI_BUS_PCI))
81 {
82 auto const pci = nic->bus_attr->attr.pci;
83 copyNumberProperty(destination, pci.domain_id, "pci_domain_id");
84 copyNumberProperty(destination, pci.bus_id, "pci_bus_id");
85 copyNumberProperty(destination, pci.device_id, "pci_device_id");
86 copyNumberProperty(destination, pci.function_id, "pci_function_id");
87 }
88 }
89
90 void copyDomainProperties(picojson::object& destination, ::fi_domain_attr* domain)
91 {
92 copyStringProperty(destination, domain->name, "fi_domain_name");
93 copyNumberProperty(destination, domain->cq_cnt, "fi_domain_cq_cnt");
94 copyNumberProperty(destination, domain->ep_cnt, "fi_domain_ep_cnt");
95 copyNumberProperty(destination, domain->mr_cnt, "fi_domain_mr_cnt");
96 }
97
98 }
99
100 FabricInterfaceDescription::FabricInterfaceDescription(Provider provider, std::string node, std::string service, std::uint64_t rawCaps,
101 std::uint64_t maxMessageSize, picojson::object extraInfo)
102 : _provider{provider}
103 , _node{std::move(node)}
104 , _service{std::move(service)}
105 , _maxMessageSize{maxMessageSize}
106 , _rawCaps{rawCaps}
107 , _attr{std::move(extraInfo)}
108 {}
109
110 std::optional<FabricInterfaceDescription> FabricInterfaceDescription::create(FabricInfoView info)
111 {
112 auto caps = std::uint64_t{};
113 auto attr = picojson::object{};
114 auto const provider = providerFromString(info->fabric_attr->prov_name);
115 if (!provider)
116 {
117 return std::nullopt;
118 }
119
120 auto faddr = FabricAddress::fromSource(info);
121 auto node = std::string{};
122 auto service = std::string{};
123 if (*provider == Provider::SHM)
124 {
125 node = getHostname();
126 service = faddr.service().value_or("");
127 }
128 else
129 {
130 node = faddr.node().value_or("");
131 }
132
133 if ((info->caps & (FI_SEND | FI_RECV)) != 0)
134 {
136 }
137 if ((info->caps & (FI_REMOTE_WRITE | FI_WRITE | FI_RMA)) != 0)
138 {
140 }
142 {
144 }
145
146 copyStringProperty(attr, fiToString(&info->addr_format, FI_TYPE_ADDR_FORMAT), "ep_addr_format");
147 copyStringProperty(attr, fiToString(&info->ep_attr->protocol, FI_TYPE_PROTOCOL), "ep_protocol");
148 copyStringProperty(attr, fiToString(&info->ep_attr->type, FI_TYPE_EP_TYPE), "ep_type");
149 copyDomainProperties(attr, info->domain_attr);
150
151 if (info->nic != nullptr)
152 {
153 copyNicProperties(attr, info->nic);
154 }
155
156 if (provider == Provider::TCP)
157 {
158 // For the TCP provider, the device_name is always the domain_attr->name.
159 copyStringProperty(attr, info->domain_attr->name, "device_name");
160 }
161
162 return FabricInterfaceDescription{*provider, std::move(node), std::move(service), caps, info->ep_attr->max_msg_size, std::move(attr)};
163 }
164
166 {
167 auto attr = std::add_pointer_t<char const>{nullptr};
168 if (!_attr.empty())
169 {
170 attr = ::strdup(picojson::value{_attr}.serialize(false).c_str());
171 }
172
173 // clang-format off
174 return new ::mxlFabricsInterfaceList{
175 .next = next,
176 .interface = {
177 .version = MXL_FABRICS_API_VERSION,
178 .provider = providerToAPI(_provider),
179 .caps = {
180 .version = MXL_FABRICS_API_VERSION,
181 .flags = _rawCaps,
182 .maxMessageSize = _maxMessageSize,
183 },
184 .address = {
185 .node = ::strdup(_node.c_str()),
186 .service = ::strdup(_service.c_str()),
187 },
188 .attr = attr,
189 },
190 };
191 // clang-format on
192 }
193
195 {
196 auto interface = list->interface;
197 auto next = list->next;
198 ::free(const_cast<char*>(interface.address.node));
199 ::free(const_cast<char*>(interface.address.service));
200 ::free(const_cast<char*>(interface.attr));
201 delete list;
202 return next;
203 }
204}
static bool isWaitObjectSupportedForEFA() noexcept
Check if wait any wait object type supported by the EFA provider for this libfabric version.
static FabricAddress fromSource(FabricInfoView info) noexcept
Build a FabricAddress from the source address of info.
Non-owning view of a FabricInfo. Returned when iterating over a FabricInfoList.
static std::optional< FabricInterfaceDescription > create(FabricInfoView info)
::mxlFabricsInterfaceList * freeRawLinkedListNode(::mxlFabricsInterfaceList *)
::mxlFabricsInterfaceList * toRawLinkedListNode(::mxlFabricsInterfaceList *next=nullptr)
FabricInterfaceDescription(Provider, std::string, std::string, std::uint64_t, std::uint64_t, picojson::object)
std::uint64_t caps() const noexcept
Return the raw MXL_FABRICS_IFACE_CAP_* flags for this interface.
@ MXL_FABRICS_IFACE_CAP_SEND_RECEIVE
Definition fabrics.h:80
@ MXL_FABRICS_IFACE_CAP_BLOCKING_OPERATIONS
Definition fabrics.h:78
@ MXL_FABRICS_IFACE_CAP_REMOTE_WRITE
Definition fabrics.h:79
#define MXL_FABRICS_API_VERSION
Definition fabrics.h:19
struct mxlFabricsInterfaceList_t mxlFabricsInterfaceList
Definition fabrics.h:116
std::string fiToString(void *any, ::fi_type type)
Definition Format.cpp:45
std::optional< Provider > providerFromString(std::string const &s) noexcept
Parse a provider name string, and return the enum value.
Definition Provider.cpp:49
void free() noexcept
Called internally to release the fi_info object.
Provider
Internal representation of supported libfabric providers.
Definition Provider.hpp:17
mxlFabricsProvider providerToAPI(Provider provider) noexcept
Convert between external and internal versions of this type.
Definition Provider.cpp:21
mxlFabricsInterfaceConfig interface
Definition fabrics.h:122
mxlFabricsInterfaceList * next
Definition fabrics.h:121