MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
Event.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 "Event.hpp"
6#include <fmt/format.h>
7#include <rdma/fabric.h>
8#include "EventQueue.hpp"
9#include "Exception.hpp"
10#include "FabricInfo.hpp"
11#include "VariantUtils.hpp"
12
14{
19
20 ::fid_t Event::ConnectionRequested::fid() const noexcept
21 {
22 return _fid;
23 }
24
26 {
27 return _info.view();
28 }
29
33
34 ::fid_t Event::Connected::fid() const noexcept
35 {
36 return _fid;
37 }
38
40 : _fid(fid)
41 {}
42
43 ::fid_t Event::Shutdown::fid() const noexcept
44 {
45 return _fid;
46 }
47
48 Event::Error::Error(std::shared_ptr<EventQueue> eq, ::fid_t fid, int err, int providerErr, std::vector<std::uint8_t> errData)
49 : _eq(std::move(eq))
50 , _fid(fid)
51 , _err(err)
52 , _providerErr(providerErr)
53 , _errData(std::move(errData))
54 {}
55
56 int Event::Error::code() const noexcept
57 {
58 return _err;
59 }
60
61 int Event::Error::providerCode() const noexcept
62 {
63 return _providerErr;
64 }
65
66 ::fid_t Event::Error::fid() const noexcept
67 {
68 return _fid;
69 }
70
71 std::string Event::Error::toString() const
72 {
73 // _err is the libfabric-level error (FI_E*); _providerErr is set by the
74 // underlying provider (e.g. the tcp provider's errno). Surface both so
75 // callers see the real reason even when the provider has nothing extra
76 // to say (in which case fi_eq_strerror would otherwise return "Success").
77 auto const* fiStr = ::fi_strerror(_err);
78 auto const* provStr = ::fi_eq_strerror(_eq->raw(), _providerErr, _errData.data(), nullptr, 0);
79 return fmt::format("fi_err={} ({}) prov_err={} ({})", _err, fiStr ? fiStr : "", _providerErr, provStr ? provStr : "");
80 }
81
83 : _event(std::move(ev))
84 {}
85
86 Event Event::fromRawEntry(::fi_eq_entry const&, std::uint32_t)
87 {
88 throw Exception::internal("unimplemented");
89 }
90
91 Event Event::fromRawCMEntry(::fi_eq_cm_entry const& entry, std::uint32_t eventType)
92 {
93 // clang-format off
94 switch (eventType)
95 {
96 case FI_CONNREQ: return {ConnectionRequested{entry.fid, FabricInfo::own(entry.info)}};
97 case FI_CONNECTED: return {Connected{entry.fid}};
98 case FI_SHUTDOWN: return {Shutdown{entry.fid}};
99 default: throw Exception::internal("Unsupported event type returned from queue");
100 }
101 // clang-format on
102 }
103
104 Event Event::fromError(std::shared_ptr<EventQueue> queue, ::fi_eq_err_entry const* raw)
105 {
106 auto errDataBuffer = reinterpret_cast<std::uint8_t*>(raw->err_data);
107
108 return {
109 Error{
110 std::move(queue),
111 raw->fid,
112 raw->err,
113 raw->prov_errno,
114 std::vector<uint8_t>(errDataBuffer, errDataBuffer + raw->err_data_size),
115 },
116 };
117 }
118
119 bool Event::isConnReq() const noexcept
120 {
121 return std::holds_alternative<ConnectionRequested>(_event);
122 }
123
125 {
126 return std::get<Event::ConnectionRequested>(_event);
127 }
128
129 bool Event::isConnected() const noexcept
130 {
131 return std::holds_alternative<Connected>(_event);
132 }
133
135 {
136 return std::get<Event::Connected>(_event);
137 }
138
139 bool Event::isShutdown() const noexcept
140 {
141 return std::holds_alternative<Shutdown>(_event);
142 }
143
145 {
146 return std::get<Event::Shutdown>(_event);
147 }
148
149 bool Event::isError() const noexcept
150 {
151 return std::holds_alternative<Error>(_event);
152 }
153
155 {
156 return std::get<Event::Error>(_event);
157 }
158
159 ::fid_t Event::fid() noexcept
160 {
161 return std::visit(
163 [](ConnectionRequested& ev) { return ev.fid(); },
164 [](Connected& ev) { return ev.fid(); },
165 [](Shutdown& ev) { return ev.fid(); },
166 [](Error& err) { return err.fid(); },
167 },
168 _event);
169 }
170}
A type that wraps a "FI_CONNECTED" event type.
Definition Event.hpp:48
::fid_t fid() const noexcept
fabric descriptor associated with the event
Definition Event.cpp:34
A type that wraps a "FI_CONNREQ" event type.
Definition Event.hpp:26
FabricInfoView info() const noexcept
libfabric info struct of the endpoint that requested connection
Definition Event.cpp:25
::fid_t fid() const noexcept
fabric descriptor associated with the event
Definition Event.cpp:20
ConnectionRequested(::fid_t fid, FabricInfo info)
Definition Event.cpp:15
A type that wraps an error received from the event queue.
Definition Event.hpp:80
int providerCode() const noexcept
return the provider-specific error code.
Definition Event.cpp:61
Error(std::shared_ptr< EventQueue > eq, ::fid_t fid, int err, int providerErr, std::vector< std::uint8_t > errData)
Definition Event.cpp:48
::fid_t fid() const noexcept
fabric descriptor associated with the event
Definition Event.cpp:66
int code() const noexcept
return the libfabric error code.
Definition Event.cpp:56
std::shared_ptr< EventQueue > _eq
Definition Event.hpp:105
std::string toString() const
Convert the error to a human-readable string.
Definition Event.cpp:71
std::vector< std::uint8_t > _errData
Definition Event.hpp:109
A type that wraps a "FI_SHUTDOWN" event type.
Definition Event.hpp:64
::fid_t fid() const noexcept
fabric descriptor associated with the event
Definition Event.cpp:43
fid_t fid() noexcept
fabric descriptor associated with the event
Definition Event.cpp:159
bool isConnected() const noexcept
Returns true if this event is a FI_CONNECTED.
Definition Event.cpp:129
std::variant< ConnectionRequested, Connected, Shutdown, Error > Inner
Definition Event.hpp:112
Connected const & connected() const
Get a connected representation of the event. Throws if the event is not a connected event....
Definition Event.cpp:134
bool isError() const noexcept
Returns true if this event is an error entry.
Definition Event.cpp:149
static Event fromRawCMEntry(::fi_eq_cm_entry const &raw, std::uint32_t eventType)
create an Event from a libfabric Connection Management event
Definition Event.cpp:91
static Event fromRawEntry(::fi_eq_entry const &raw, std::uint32_t eventType)
Definition Event.cpp:86
bool isShutdown() const noexcept
returns true if this event is a FI_SHUTDOWN
Definition Event.cpp:139
Error const & error() const
Get an error representation of the event. Throws if the event is an error. Users should previously ch...
Definition Event.cpp:154
static Event fromError(std::shared_ptr< EventQueue > queue, ::fi_eq_err_entry const *raw)
create an Event from a libfabric event error entry
Definition Event.cpp:104
ConnectionRequested const & connReq() const
Get a connection request representation of the event. Throws if the event is not a connection request...
Definition Event.cpp:124
Shutdown const & shutdown() const
Get a shutdown representation of the event. Throws if the event is not a shutdown event....
Definition Event.cpp:144
bool isConnReq() const noexcept
returns true if this event is a FI_CONNREQ
Definition Event.cpp:119
static Exception internal(fmt::format_string< T... > fmt, T &&... args)
Make an MXL_ERR_INTERNAL exception.
Definition Exception.hpp:51
Non-owning view of a FabricInfo. Returned when iterating over a FabricInfoList.
FabricInfo(FabricInfoView) noexcept
Create an owning FabricInfo object from a non-owning view.
::fi_info * raw() noexcept
Returns the raw fi_info object managed by this FabricInfo instance.