MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
PassiveEndpoint.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 "PassiveEndpoint.hpp"
7#include <rdma/fi_cm.h>
8#include <rdma/fi_endpoint.h>
9#include "EventQueue.hpp"
10#include "Exception.hpp"
11#include "Fabric.hpp"
12
14{
15 PassiveEndpoint PassiveEndpoint::create(std::shared_ptr<Fabric> fabric, Endpoint::Id id)
16 {
17 ::fid_pep* pep;
18
19 fiCall(::fi_passive_ep, "Failed to create passive endpoint", fabric->raw(), fabric->info().raw(), &pep, nullptr);
20
21 return {pep, fabric, id, std::nullopt};
22 }
23
24 PassiveEndpoint PassiveEndpoint::create(std::shared_ptr<Fabric> fabric)
25 {
26 return PassiveEndpoint::create(std::move(fabric), Endpoint::randomId());
27 }
28
30 {
31 catchAndLogFabricError([this]() { close(); }, "Failed to close passive endpoint");
32 }
33
34 PassiveEndpoint::PassiveEndpoint(::fid_pep* raw, std::shared_ptr<Fabric> fabric, Endpoint::Id id, std::optional<std::shared_ptr<EventQueue>> eq)
35 : _raw(raw)
36 , _fabric(std::move(fabric))
37 , _id(id)
38 , _eq(std::move(eq))
39 {}
40
42 {
43 return _id;
44 }
45
47 {
48 if (_raw)
49 {
50 fiCall(::fi_close, "Failed to close passive endpoint", &_raw->fid);
51 _raw = nullptr;
52 }
53 }
54
56 : _raw(other._raw)
57 , _fabric(std::move(other._fabric))
58 , _id(other._id)
59 , _eq(std::move(other._eq))
60 {
61 other._raw = nullptr;
62 }
63
65 {
66 catchAndLogFabricError([this]() { close(); }, "Failed to close passive endpoint");
67
68 _raw = other._raw;
69 _id = other._id;
70 other._raw = nullptr;
71 other._id = 0;
72
73 static_assert(std::is_nothrow_move_assignable_v<decltype(other._fabric)>, "Member must be nothrow move assignable");
74 static_assert(std::is_nothrow_move_assignable_v<decltype(other._eq)>, "Member must be nothrow move assignable");
75
76 _fabric = std::move(other._fabric);
77 _eq = std::move(other._eq);
78
79 return *this;
80 }
81
82 void PassiveEndpoint::bind(std::shared_ptr<EventQueue> eq)
83 {
84 fiCall(::fi_pep_bind, "Failed to bind event queue to passive endpoint", _raw, &eq->raw()->fid, 0);
85
86 _eq = eq;
87 }
88
90 {
91 fiCall(::fi_listen, "Failed to transition the endpoint into listener mode", _raw);
92 }
93
95 {
96 auto fid = entry.fid();
97 if (!fid)
98 {
99 MXL_ERROR("Cannot reject a connection notification entry without a fid");
100 return;
101 }
102
103 fiCall(::fi_reject, "Failed to reject connection request", _raw, fid, nullptr, 0);
104 }
105
106 std::shared_ptr<EventQueue> PassiveEndpoint::eventQueue() const
107 {
108 if (!_eq)
109 {
110 throw Exception::internal("No event queue bound to this endpoint");
111 }
112
113 return *_eq;
114 }
115
116 ::fid_pep* PassiveEndpoint::raw() noexcept
117 {
118 return _raw;
119 }
120
121 ::fid_pep const* PassiveEndpoint::raw() const noexcept
122 {
123 return _raw;
124 }
125
126}
#define MXL_ERROR(...)
Definition Logging.hpp:67
static Id randomId() noexcept
Generate a new random endpoint id.
Definition Endpoint.cpp:32
std::uintptr_t Id
An id type that can be used to reference this endpoint from completions and events.
Definition Endpoint.hpp:38
Type to represent an event entry from an EventQueue.
Definition Event.hpp:21
fid_t fid() noexcept
fabric descriptor associated with the event
Definition Event.cpp:159
static Exception internal(fmt::format_string< T... > fmt, T &&... args)
Make an MXL_ERR_INTERNAL exception.
Definition Exception.hpp:51
Endpoint::Id id() const noexcept
Get the endpoints id.
std::shared_ptr< EventQueue > eventQueue() const
Accessor for the associated event queue.
std::optional< std::shared_ptr< EventQueue > > _eq
void bind(std::shared_ptr< EventQueue > eq)
Bind the endpoint to an event queue.
::fid_pep * raw() noexcept
Access the underlying raw fid_pep pointer.
void operator=(PassiveEndpoint const &)=delete
static PassiveEndpoint create(std::shared_ptr< Fabric > fabric)
Allocate a new PassiveEndpoint associated with the given Fabric.
void reject(Event &entry)
Reject a connection request event.
PassiveEndpoint(PassiveEndpoint const &)=delete
void listen()
Put the passive endpoint into listening mode.
void close()
Releases all underlying resources. This is called from the destructor and the move-assignment operato...
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.