MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
AddressVector.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 "AddressVector.hpp"
6#include <memory>
7#include <utility>
8#include <sys/types.h>
10#include <rdma/fabric.h>
11#include <rdma/fi_domain.h>
12#include <rdma/fi_errno.h>
13#include "Exception.hpp"
14
16{
17
19 {
20 return AddressVector::Attributes{.count = 4, .epPerNode = 0};
21 }
22
23 ::fi_av_attr AddressVector::Attributes::toRaw() const noexcept
24 {
25 ::fi_av_attr attr{};
26 attr.type = FI_AV_TABLE;
27 attr.count = count;
28 attr.ep_per_node = epPerNode;
29 attr.name = nullptr;
30 attr.map_addr = nullptr;
31 attr.flags = 0;
32 return attr;
33 }
34
35 std::shared_ptr<AddressVector> AddressVector::open(std::shared_ptr<Domain> domain, Attributes attr)
36 {
37 fid_av* raw;
38
39 auto fiAttr = attr.toRaw();
40
41 fiCall(::fi_av_open, "Failed to open address vector", domain->raw(), &fiAttr, &raw, nullptr);
42
43 // expose the private constructor to std::make_shared inside this function
44 struct MakeSharedEnabler : public AddressVector
45 {
46 MakeSharedEnabler(fid_av* raw, std::shared_ptr<Domain> domain)
47 : AddressVector(raw, std::move(domain))
48 {}
49 };
50
51 return std::make_shared<MakeSharedEnabler>(raw, domain);
52 }
53
55 {
56 ::fi_addr_t fiAddr{FI_ADDR_UNSPEC};
57
58 if (auto ret = ::fi_av_insert(_raw, addr.raw(), 1, &fiAddr, 0, nullptr); ret != 1)
59 {
60 throw Exception::internal("Failed to insert address into the address vector. {}", ::fi_strerror(ret));
61 }
62 MXL_INFO("Remote endpoint address {} was added to the address vector", addrToString(addr));
63
64 return fiAddr;
65 }
66
67 void AddressVector::remove(::fi_addr_t addr) noexcept
68 {
69 fiCall(::fi_av_remove, "Failed to remove address from address vector", _raw, &addr, 1, 0);
70 }
71
72 std::string AddressVector::addrToString(RawFabricAddress const& addr) const
73 {
74 std::string s;
75 std::size_t len = 0;
76
77 ::fi_av_straddr(_raw, addr.raw(), nullptr, &len);
78 s.resize(len);
79
80 auto ret = ::fi_av_straddr(_raw, addr.raw(), s.data(), &len);
81 if (ret == nullptr)
82 {
83 throw Exception::internal("Failed to convert address to string");
84 }
85
86 return s;
87 }
88
89 AddressVector::AddressVector(::fid_av* raw, std::shared_ptr<Domain> domain)
90 : _raw{raw}
91 , _domain{std::move(domain)}
92 {}
93
95 {
96 catchAndLogFabricError([this]() { close(); }, "Failed to close address vector");
97 }
98
100 : _raw(other._raw)
101 , _domain(std::move(other._domain))
102 {
103 other._raw = nullptr;
104 }
105
107 {
108 close();
109
110 _raw = other._raw;
111 other._raw = nullptr;
112 _domain = std::move(other._domain);
113
114 return *this;
115 }
116
118 {
119 if (_raw)
120 {
121 MXL_DEBUG("Closing address vector");
122
123 fiCall(::fi_close, "Failed to close address vector", &_raw->fid);
124 _raw = nullptr;
125 }
126 }
127
128 ::fid_av* AddressVector::raw() noexcept
129 {
130 return _raw;
131 }
132
133 ::fid_av const* AddressVector::raw() const noexcept
134 {
135 return _raw;
136 }
137}
#define MXL_DEBUG(...)
Definition Logging.hpp:34
#define MXL_INFO(...)
Definition Logging.hpp:45
::fi_addr_t insert(RawFabricAddress const &addr)
Insert an address into the address vector. User of this API are responsible for keeping track of inse...
AddressVector(AddressVector const &)=delete
static std::shared_ptr< AddressVector > open(std::shared_ptr< Domain > domain, Attributes attr=Attributes::defaults())
Create an AddressVector.
std::shared_ptr< Domain > _domain
void remove(::fi_addr_t addr) noexcept
Remove an address of the address vector.
std::string addrToString(RawFabricAddress const &addr) const
Convert a libfabric endpoint address into a human-readable string.
void close()
Close the address vector and release all resources. Called from the destructor and the move assignmen...
::fid_av * raw() noexcept
Mutable accessor of the underlying fi_av instance.
void operator=(AddressVector const &)=delete
static Exception internal(fmt::format_string< T... > fmt, T &&... args)
Make an MXL_ERR_INTERNAL exception.
Definition Exception.hpp:51
Wrapper around a libfabric endpoint address.
Definition Address.hpp:20
void * raw() noexcept
Pointer to the raw address data.
Definition Address.cpp:37
::fi_info * raw() noexcept
Returns the raw fi_info object managed by this FabricInfo instance.
void catchAndLogFabricError(F &&fun, std::string_view msg)
int fiCall(F &&fun, std::string_view msg, T &&... args)
Call a libfabric function and check its return code.
std::filesystem::path _domain
The monitored domain.
Configuration attributes of the AddressVector.
::fi_av_attr toRaw() const noexcept
Convert to libfabric fi_av_attr object.
static Attributes defaults() noexcept
Get default attributes.