MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
mxl::lib::fabrics::ofi::FabricAddress Class Reference

Owning, type-safe representation of a libfabric address. More...

#include <FabricAddress.hpp>

Public Types

using Native

Public Member Functions

FabricAddressFormat addressFormat () const noexcept
 Return the libfabric address format constant (FI_SOCKADDR_IN, etc.).
bool empty () const noexcept
 True if the address could not be decoded (holds std::monostate).
 FabricAddress () noexcept
Native const & native () const noexcept
 Access the underlying variant.
std::optional< std::string > node () const
 Return the host portion of the address as a string.
std::optional< std::string > service () const
 Return the service/port portion of the address as a string.
std::string toString () const
 Format the address exactly as libfabric's internal ofi_straddr.

Static Public Member Functions

static FabricAddress decode (FabricAddressFormat addrFormat, void const *addr, std::size_t len)
 Decode a raw libfabric address buffer of the given addr_format.
static FabricAddress fromDestination (FabricInfoView info) noexcept
 Build a FabricAddress from the destination address of info.
static FabricAddress fromSource (FabricInfoView info) noexcept
 Build a FabricAddress from the source address of info.
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.

Private Member Functions

 FabricAddress (Native native) noexcept

Private Attributes

Native _native

Detailed Description

Owning, type-safe representation of a libfabric address.

Wraps the native address types that the various addr_format values map to. The address bytes are copied out of the source fi_info on construction, so a FabricAddress remains valid independently of the FabricInfoView it was built from.

An empty / unsupported / unrecognised address is represented by std::monostate.

Definition at line 76 of file FabricAddress.hpp.

Member Typedef Documentation

◆ Native

Initial value:
std::variant<std::monostate,
::sockaddr_in,
::sockaddr_in6,
std::string>
Decoded representation of an FI_ADDR_EFA address.
Local declaration of libfabric's internal struct ofi_sockaddr_ib.

Definition at line 79 of file FabricAddress.hpp.

Constructor & Destructor Documentation

◆ FabricAddress() [1/2]

mxl::lib::fabrics::ofi::FabricAddress::FabricAddress ( )
noexcept

Definition at line 225 of file FabricAddress.cpp.

226 : _native{std::monostate{}}
227 {}

◆ FabricAddress() [2/2]

mxl::lib::fabrics::ofi::FabricAddress::FabricAddress ( FabricAddress::Native native)
explicitprivatenoexcept

Definition at line 229 of file FabricAddress.cpp.

230 : _native{std::move(native)}
231 {}
Native const & native() const noexcept
Access the underlying variant.

Member Function Documentation

◆ addressFormat()

FabricAddressFormat mxl::lib::fabrics::ofi::FabricAddress::addressFormat ( ) const
nodiscardnoexcept

Return the libfabric address format constant (FI_SOCKADDR_IN, etc.).

Definition at line 379 of file FabricAddress.cpp.

380 {
381 return std::visit(
382 overloaded{
383 [](std::monostate const&) { return FabricAddressFormat::Unspec; },
384 [](::sockaddr_in const&) { return FabricAddressFormat::SockaddrIn; },
385 [](::sockaddr_in6 const&) { return FabricAddressFormat::SockaddrIn6; },
386 [](OfiSockaddrIb const&) { return FabricAddressFormat::SockaddrIb; },
387 [](EfaAddress const&) { return FabricAddressFormat::Efa; },
388 [](std::string const&) { return FabricAddressFormat::String; },
389 },
390 _native);
391 }

◆ decode()

FabricAddress mxl::lib::fabrics::ofi::FabricAddress::decode ( FabricAddressFormat addrFormat,
void const * addr,
std::size_t len )
staticnodiscard

Decode a raw libfabric address buffer of the given addr_format.

Parameters
addrFormatOne of the libfabric FI_ADDR_* / FI_SOCKADDR* constants.
addrPointer to the address bytes, or nullptr.
lenLength of the address buffer in bytes.

Definition at line 243 of file FabricAddress.cpp.

244 {
245 // Mirror ofi_straddr: nothing to decode without an address.
246 if (!addr)
247 {
248 return FabricAddress{};
249 }
250
251 auto const sockAddrFamily = [&]
252 {
253 return static_cast<::sockaddr const*>(addr)->sa_family;
254 };
255
256 switch (format)
257 {
259 // Dispatch on the embedded address family (IPv4, IPv6 or IB).
260 switch (sockAddrFamily())
261 {
262 case AF_INET: return decode(FabricAddressFormat::SockaddrIn, addr, len);
263 case AF_INET6: return decode(FabricAddressFormat::SockaddrIn6, addr, len);
264 case AF_IB: return decode(FabricAddressFormat::SockaddrIb, addr, len);
265 default: return FabricAddress{};
266 }
268 // Like FI_SOCKADDR, but IB is not a valid family here.
269 switch (sockAddrFamily())
270 {
271 case AF_INET: return decode(FabricAddressFormat::SockaddrIn, addr, len);
272 case AF_INET6: return decode(FabricAddressFormat::SockaddrIn6, addr, len);
273 default: return FabricAddress{};
274 }
276 {
277 if (len < sizeof(::sockaddr_in))
278 {
279 return FabricAddress{};
280 }
281 auto sin = ::sockaddr_in{};
282 std::memcpy(&sin, addr, sizeof(sin));
283 return FabricAddress{sin};
284 }
286 {
287 if (len < sizeof(::sockaddr_in6))
288 {
289 return FabricAddress{};
290 }
291 auto sin6 = ::sockaddr_in6{};
292 std::memcpy(&sin6, addr, sizeof(sin6));
293 return FabricAddress{sin6};
294 }
296 {
297 if (len < sizeof(OfiSockaddrIb))
298 {
299 return FabricAddress{};
300 }
301 auto sib = OfiSockaddrIb{};
302 std::memcpy(&sib, addr, sizeof(sib));
303 return FabricAddress{sib};
304 }
306 {
307 // 16-byte IPv6 GID, 16-bit QPN at offset 16, 32-bit QKey at offset 20.
308 constexpr auto efaAddrLen = std::size_t{24};
309 if (len < efaAddrLen)
310 {
311 return FabricAddress{};
312 }
313 auto const* bytes = static_cast<std::uint8_t const*>(addr);
314 auto efa = EfaAddress{};
315 std::memcpy(efa.gid, bytes, sizeof(efa.gid));
316 std::memcpy(&efa.qpn, bytes + 16, sizeof(efa.qpn));
317 std::memcpy(&efa.qkey, bytes + 20, sizeof(efa.qkey));
318 return FabricAddress{efa};
319 }
320 case FabricAddressFormat::String: return FabricAddress{std::string{static_cast<char const*>(addr)}};
321 default: return FabricAddress{};
322 }
323 }
static FabricAddress decode(FabricAddressFormat addrFormat, void const *addr, std::size_t len)
Decode a raw libfabric address buffer of the given addr_format.

◆ empty()

bool mxl::lib::fabrics::ofi::FabricAddress::empty ( ) const
inlinenodiscardnoexcept

True if the address could not be decoded (holds std::monostate).

Definition at line 114 of file FabricAddress.hpp.

115 {
116 return std::holds_alternative<std::monostate>(_native);
117 }

◆ fromDestination()

FabricAddress mxl::lib::fabrics::ofi::FabricAddress::fromDestination ( FabricInfoView info)
staticnodiscardnoexcept

Build a FabricAddress from the destination address of info.

Definition at line 238 of file FabricAddress.cpp.

239 {
240 return decode(mustConvertAddressFormat(info->addr_format), info->dest_addr, info->dest_addrlen);
241 }
FabricAddressFormat mustConvertAddressFormat(std::uint32_t fiAddrFormat)
Convert from libfabric address format value to FabricAddressFormat Never returns FabricAddressFormat:...

◆ fromSource()

FabricAddress mxl::lib::fabrics::ofi::FabricAddress::fromSource ( FabricInfoView info)
staticnodiscardnoexcept

Build a FabricAddress from the source address of info.

Definition at line 233 of file FabricAddress.cpp.

234 {
235 return decode(mustConvertAddressFormat(info->addr_format), info->src_addr, info->src_addrlen);
236 }

◆ native()

Native const & mxl::lib::fabrics::ofi::FabricAddress::native ( ) const
inlinenodiscardnoexcept

Access the underlying variant.

Definition at line 107 of file FabricAddress.hpp.

108 {
109 return _native;
110 }

◆ node()

std::optional< std::string > mxl::lib::fabrics::ofi::FabricAddress::node ( ) const
nodiscard

Return the host portion of the address as a string.

For the socket address types this is the textual IP address; for the IB and EFA types it is the GID. For an FI_ADDR_STR address it is the address string itself, and for an empty address it is the empty string.

Definition at line 343 of file FabricAddress.cpp.

344 {
345 return std::visit(
346 [](auto const& value) -> std::optional<std::string>
347 {
348 using T = std::decay_t<decltype(value)>;
349 if constexpr (std::is_same_v<T, std::monostate>)
350 {
351 return std::nullopt;
352 }
353 else
354 {
355 return ofi::hostString(value);
356 }
357 },
358 _native);
359 }

◆ parse()

FabricAddress mxl::lib::fabrics::ofi::FabricAddress::parse ( Provider provider,
std::optional< std::string > node,
std::optional< std::string > service )
staticnodiscard

Parse a node/service pair into a typed address for the given provider.

Definition at line 393 of file FabricAddress.cpp.

394 {
395 if (!node)
396 {
397 if (service)
398 {
399 throw Exception::invalidArgument("node address is required when service address is specified");
400 }
401
402 return FabricAddress{};
403 }
404
405 switch (provider)
406 {
407 // SHM provider always expected FI_ADDR_STR
408 case Provider::SHM:
409 {
410 if (service && !service->empty())
411 {
412 return FabricAddress{*node + ":" + *service};
413 }
414 else
415 {
416 return FabricAddress{*node};
417 }
418 };
419 // For TCP and Verbs we only support FI_SOCKADDR_IN and FI_SOCKADDR_IN6
420 case Provider::TCP: [[fallthrough]];
421 case Provider::VERBS:
422 {
423 if (service && !service->empty())
424 {
425 auto port = std::uint16_t{0};
426 try
427 {
428 port = stringToInteger<std::uint16_t>(*service, "service");
429 }
430 catch (std::invalid_argument const& e)
431 {
432 throw Exception::invalidArgument("{}", e.what());
433 }
434 return FabricAddress{parseInternetAddress(*node, port)};
435 }
436 return FabricAddress{parseInternetAddress(*node)};
437 }
438 // EFA only supports FI_ADDR_EFA
439 case Provider::EFA:
440 {
441 auto parsed = parseInternetAddress(*node);
442 auto sin6 = std::get_if<::sockaddr_in6>(&parsed);
443 if (!sin6)
444 {
445 throw Exception::invalidArgument("invalid address format for EFA provider");
446 }
447
448 auto addr = EfaAddress{.gid = {}, .qpn = 0, .qkey = 0};
449 std::memcpy(addr.gid, &sin6->sin6_addr, sizeof(addr.gid));
450 return FabricAddress{addr};
451 }
452 default: throw Exception::invalidState("must specify provider");
453 }
454 }
static Exception invalidArgument(fmt::format_string< T... > fmt, T &&... args)
Make an MXL_ERR_INVALID_ARG exception.
Definition Exception.hpp:43
static Exception invalidState(fmt::format_string< T... > fmt, T &&... args)
Make an MXL_ERR_INVALID_STATE exception.
Definition Exception.hpp:59
std::optional< std::string > node() const
Return the host portion of the address as a string.
std::optional< std::string > service() const
Return the service/port portion of the address as a string.
T stringToInteger(std::string_view text, std::string_view name)

◆ service()

std::optional< std::string > mxl::lib::fabrics::ofi::FabricAddress::service ( ) const
nodiscard

Return the service/port portion of the address as a string.

Definition at line 361 of file FabricAddress.cpp.

362 {
363 return std::visit(
364 [](auto const& value) -> std::optional<std::string>
365 {
366 using T = std::decay_t<decltype(value)>;
367 if constexpr (std::is_same_v<T, std::monostate>)
368 {
369 return std::nullopt;
370 }
371 else
372 {
373 return ofi::serviceString(value);
374 }
375 },
376 _native);
377 }

◆ toString()

std::string mxl::lib::fabrics::ofi::FabricAddress::toString ( ) const
nodiscard

Format the address exactly as libfabric's internal ofi_straddr.

Returns an empty string for an empty / undecodable address (mirroring ofi_straddr returning NULL).

Definition at line 325 of file FabricAddress.cpp.

326 {
327 return std::visit(
328 [](auto const& value) -> std::string
329 {
330 using T = std::decay_t<decltype(value)>;
331 if constexpr (std::is_same_v<T, std::monostate>)
332 {
333 return "";
334 }
335 else
336 {
337 return ofi::toString(value).value_or("");
338 }
339 },
340 _native);
341 }

Member Data Documentation

◆ _native

Native mxl::lib::fabrics::ofi::FabricAddress::_native
private

Definition at line 152 of file FabricAddress.hpp.


The documentation for this class was generated from the following files: