MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
Address.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2025 Contributors to the Media eXchange Layer project.
2//
3// SPDX-License-Identifier: Apache-2.0
4
5#include "Address.hpp"
6#include <cstdint>
7#include <rdma/fabric.h>
8#include <rdma/fi_cm.h>
9#include <rdma/fi_errno.h>
10#include "mxl/mxl.h"
11#include "Base64.hpp"
12#include "Exception.hpp"
13#include "FabricAddress.hpp"
14
16{
21
22 RawFabricAddress::RawFabricAddress(std::vector<std::uint8_t> addr, FabricAddressFormat addressFormat)
23 : _inner{std::move(addr)}
24 , _addressFormat{addressFormat}
25 {}
26
28 {
29 return retrieveFabricAddress(fid, info);
30 }
31
32 std::string RawFabricAddress::toBase64() const
33 {
34 return base64::encode_into<std::string>(_inner.cbegin(), _inner.cend());
35 }
36
37 void* RawFabricAddress::raw() noexcept
38 {
39 return _inner.data();
40 }
41
42 void const* RawFabricAddress::raw() const noexcept
43 {
44 return _inner.data();
45 }
46
47 std::size_t RawFabricAddress::size() const noexcept
48 {
49 return _inner.size();
50 }
51
53 {
54 return _addressFormat;
55 }
56
57 bool RawFabricAddress::operator==(RawFabricAddress const& other) const noexcept
58 {
59 return (_inner == other._inner) && (_addressFormat == other._addressFormat);
60 }
61
66
68 {
70 if (decoded.empty())
71 {
72 throw std::runtime_error("Failed to decode base64 data into FabricAddress.");
73 }
74 return RawFabricAddress{std::move(decoded), addressFormat};
75 }
76
78 {
79 auto const format = mustConvertAddressFormat(info->addr_format);
80
81 // First obtain the address length
82 std::size_t addrlen = 0;
83 auto ret = fi_getname(fid, nullptr, &addrlen);
84 if (ret != -FI_ETOOSMALL)
85 {
86 throw FabricException("Failed to get address length from endpoint.", MXL_ERR_UNKNOWN, ret);
87 }
88
89 // Now that we have the address length, allocate a receiving buffer and call fi_getname again to retrieve the actual address
90 auto addr = std::vector<std::uint8_t>(addrlen);
91 fiCall(fi_getname, "Failed to retrieve endpoint's local address.", fid, addr.data(), &addrlen);
92
93 return RawFabricAddress{std::move(addr), format};
94 }
95
96}
Owning, type-safe representation of a libfabric address.
static FabricAddress decode(FabricAddressFormat addrFormat, void const *addr, std::size_t len)
Decode a raw libfabric address buffer of the given addr_format.
An internal exception type that extends.
Non-owning view of a FabricInfo. Returned when iterating over a FabricInfoList.
bool operator==(RawFabricAddress const &other) const noexcept
Definition Address.cpp:57
void * raw() noexcept
Pointer to the raw address data.
Definition Address.cpp:37
RawFabricAddress()
Default is an empty fabric address.
Definition Address.cpp:17
static RawFabricAddress retrieveFabricAddress(::fid_t fid, FabricInfoView info)
Retrieve the fabric address of an endpoint by using its fid.
Definition Address.cpp:77
static RawFabricAddress fromBase64(std::string_view data, FabricAddressFormat addressFormat)
Parse a fabric address from a base64 encoded string.
Definition Address.cpp:67
static RawFabricAddress fromFid(::fid_t fid, FabricInfoView info)
Retrieve the fabric address of an endpoint by passing its fid.
Definition Address.cpp:27
std::vector< std::uint8_t > _inner
Definition Address.hpp:90
std::string toBase64() const
Convert the raw fabric address into a base64 encoded string.
Definition Address.cpp:32
FabricAddress decode() const
Decode into a strongly typed FabricAddress.
Definition Address.cpp:62
std::size_t size() const noexcept
Byte-length of the raw address data.
Definition Address.cpp:47
FabricAddressFormat format() const noexcept
Return the fabric specific libfaric address format, or FI_FORMAT_UNSPEC if empty.
Definition Address.cpp:52
@ MXL_ERR_UNKNOWN
Definition mxl.h:27
OutputBuffer encode_into(InputIterator begin, InputIterator end)
Definition Base64.hpp:2676
OutputBuffer decode_into(std::string_view base64Text)
Definition Base64.hpp:2748
int fiCall(F &&fun, std::string_view msg, T &&... args)
Call a libfabric function and check its return code.
FabricAddressFormat mustConvertAddressFormat(std::uint32_t fiAddrFormat)
Convert from libfabric address format value to FabricAddressFormat Never returns FabricAddressFormat:...