MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
RDMInitiator.hpp
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#pragma once
6
7#include <chrono>
8#include <cstdint>
9#include <map>
10#include <memory>
11#include <rdma/fabric.h>
12#include "mxl/fabrics.h"
13#include "Endpoint.hpp"
14#include "Initiator.hpp"
15#include "Protocol.hpp"
16#include "SliceRange.hpp"
17#include "TargetInfo.hpp"
18
20{
24 {
25 public:
32 RDMInitiatorTarget(std::unique_ptr<EgressProtocol> proto, TargetInfo remoteInfo);
33
36 [[nodiscard]]
37 bool isIdle() const noexcept;
38
43 void activate(Endpoint& ep);
44
51 void shutdown(Endpoint& ep);
52
58 void finishRemoval(Endpoint& ep);
59
62 [[nodiscard]]
63 bool canEvict() const noexcept;
64
67 constexpr static auto RemoveTimeout = std::chrono::seconds(2);
68
71 void transferGrain(Endpoint const& ep, std::uint64_t localIndex, std::uint64_t remoteIndex, std::uint64_t remotePayloadOffset,
72 SliceRange const& sliceRange);
73
76 void transferSamples(Endpoint const& ep, std::uint64_t headIndex, std::size_t count);
77
80 [[nodiscard]]
81 bool hasPendingWork() const noexcept;
82
85 void handleCompletion(Endpoint& ep, Completion completion);
86
87 private:
92 struct Idle
93 {};
94
99 struct Activated
100 {
101 ::fi_addr_t fiAddr;
102 };
103
109 struct Removing
110 {
111 ::fi_addr_t fiAddr;
112 std::chrono::steady_clock::time_point deadline;
113 };
114
117 struct Done
118 {};
119
123 using State = std::variant<Idle, Activated, Removing, Done>;
124
125 private:
127 std::unique_ptr<EgressProtocol> _proto;
128
130 };
131
136 class RDMInitiator /*final*/ : public Initiator
137 {
138 public:
145 [[nodiscard]]
146 static std::unique_ptr<RDMInitiator> setup(mxlFabricsInitiatorConfig const& config, FabricInfoView info,
147 InitiatorSetupOptions const& options = {});
148
151 virtual void addTarget(TargetInfo const& remoteTargetInfo) final;
152
155 virtual void removeTarget(TargetInfo const& remoteTargetInfo) final;
156
161 virtual void shutdown() final;
162
165 virtual void transferGrain(std::uint64_t grainIndex, std::uint16_t startSlice, std::uint16_t endSlice) final;
166
169 virtual void transferGrainToTarget(Endpoint::Id targetId, std::uint64_t localIndex, std::uint64_t remoteIndex, std::uint64_t payloadOffset,
170 std::uint16_t startSlice, std::uint16_t endSlice) final;
171
174 virtual void transferSamples(std::uint64_t headIndex, std::size_t count) final;
175
179
182 virtual Initiator::MakeProgressResult makeProgressBlocking(std::chrono::steady_clock::duration timeout) final;
183
184 private:
189 RDMInitiator(Endpoint ep, std::unique_ptr<EgressProtocolTemplate> proto);
190
195 [[nodiscard]]
197
202 [[nodiscard]]
204
207 [[nodiscard]]
209
212 void blockOnCQ(std::chrono::steady_clock::duration timeout);
213
216 void drainCQ();
217
224 template<typename F>
225 void forEachTarget(F&& transfer);
226
230
233 void evictRemovedTargets();
234
237 void processCompletion(Completion completion);
238
242
246
249 constexpr static auto ShutdownTimeout = RDMInitiatorTarget::RemoveTimeout + std::chrono::seconds(1);
250
253 constexpr static auto ShutdownPollInterval = std::chrono::milliseconds(1);
254
255 private:
256 std::unique_ptr<EgressProtocolTemplate> _proto;
257
259 std::map<Completion::Token, RDMInitiatorTarget> _targets{};
260 std::map<Endpoint::Id, Completion::Token> _remoteEndpoints{};
261
262 // The endpoint is declared last so that it is destroyed before the protocols. Writes can read from memory that the protocols own
263 // until the endpoint is closed.
265 };
266}
Data variant of the completion entry instance.
Error variant of the completion entry instance.
Type to represent a completion entry from a CompletionQueue.
std::uintptr_t Token
A token used to associate a completion entry with an endpoint.
RAII Wrapper around a libfabric endpoint (fi_ep).
Definition Endpoint.hpp:29
std::uintptr_t Id
An id type that can be used to reference this endpoint from completions and events.
Definition Endpoint.hpp:38
Non-owning view of a FabricInfo. Returned when iterating over a FabricInfoList.
Abstract base class for Initiator implementations.
Definition Initiator.hpp:31
std::variant< Ready, NotReady, Interrupted > MakeProgressResult
Definition Initiator.hpp:42
A single endpoint within an RDMInitiator.
void handleCompletion(Endpoint &ep, Completion completion)
Handle a completion event for this endpoint.
bool canEvict() const noexcept
Returns true if the target was shut down and can be evicted from the initiator.
void transferGrain(Endpoint const &ep, std::uint64_t localIndex, std::uint64_t remoteIndex, std::uint64_t remotePayloadOffset, SliceRange const &sliceRange)
Post a grain data transfer request to this endpoint.
bool isIdle() const noexcept
Returns true if the endpoint is idle and could be actived.
RDMInitiatorTarget(std::unique_ptr< EgressProtocol > proto, TargetInfo remoteInfo)
Construct a new RDMInitiatorEndpoint object.
bool hasPendingWork() const noexcept
Returns true if there's pending work for this target.
std::variant< Idle, Activated, Removing, Done > State
The various states that the endpoint can be in are stored inside a variant that we move from and then...
void finishRemoval(Endpoint &ep)
Complete the removal once all outstanding writes have completed or RemoveTimeout has elapsed.
void transferSamples(Endpoint const &ep, std::uint64_t headIndex, std::size_t count)
Post a sample data transfer request to this endpoint.
void shutdown(Endpoint &ep)
Stop transfers to this target.
void activate(Endpoint &ep)
Try to activate the endpoint.
std::unique_ptr< EgressProtocol > _proto
static constexpr auto RemoveTimeout
Upper bound for the removing state. A target that disappears may never complete the outstanding write...
std::map< Completion::Token, RDMInitiatorTarget > _targets
virtual void removeTarget(TargetInfo const &remoteTargetInfo) final
Remove a target from the initiator.
std::unique_ptr< EgressProtocolTemplate > _proto
std::map< Endpoint::Id, Completion::Token > _remoteEndpoints
RDMInitiator(Endpoint ep, std::unique_ptr< EgressProtocolTemplate > proto)
Construct a new RDMInitiator object.
Initiator::MakeProgressResult afterProgressResult() const noexcept
Returns true if any of the endpoints contained in this initiator have pending work.
virtual void addTarget(TargetInfo const &remoteTargetInfo) final
Add a target to the initiator.
void processCompletion(Completion completion)
Consume a completion entry.
static constexpr auto ShutdownPollInterval
Sleep time between two queue reads in shutdown().
void blockOnCQ(std::chrono::steady_clock::duration timeout)
Block on the completion queue until a completion arrives or the timeout elapses, then process all ava...
void evictRemovedTargets()
Complete the removal of targets whose outstanding writes have completed, and evict them.
void handleCompletionError(Completion::Error error)
Handle a completion error event.
virtual void transferGrainToTarget(Endpoint::Id targetId, std::uint64_t localIndex, std::uint64_t remoteIndex, std::uint64_t payloadOffset, std::uint16_t startSlice, std::uint16_t endSlice) final
Transfer a grain to a specific target.
virtual Initiator::MakeProgressResult makeProgressBlocking(std::chrono::steady_clock::duration timeout) final
Attempts to progress execution, including connection management and data operations.
static std::unique_ptr< RDMInitiator > setup(mxlFabricsInitiatorConfig const &config, FabricInfoView info, InitiatorSetupOptions const &options={})
Set up a new RDMInitiator.
virtual void transferSamples(std::uint64_t headIndex, std::size_t count) final
Transfer samples to all targets.
void handleCompletionData(Completion::Data data)
Handle a completion data event.
static constexpr auto ShutdownTimeout
Upper bound for shutdown(). It is longer than RDMInitiatorTarget::RemoveTimeout, so that each removal...
void activateIdleEndpoints()
Try to activate any idle endpoints.
RDMInitiatorTarget & findRemoteByEndpoint(Endpoint::Id id)
Find a remote target by its endpoint id.
void drainCQ()
Process completions until the completion queue is empty.
virtual void transferGrain(std::uint64_t grainIndex, std::uint16_t startSlice, std::uint16_t endSlice) final
Transfer a grain to all targets.
virtual Initiator::MakeProgressResult makeProgress() final
Attempts to progress execution, including connection management and data operations.
virtual void shutdown() final
Shut down the initiator gracefully.
void forEachTarget(F &&transfer)
Offer a transfer to every target.
RDMInitiatorTarget & findRemoteByToken(Completion::Token token)
Find a remote target by its completion token.
A SliceRange represents a range of slices within a grain.
struct mxlFabricsInitiatorConfig_t mxlFabricsInitiatorConfig
std::size_t count(uuids::uuid id) const noexcept
Returns the number of writers registered for flow id 'id'.
Provider-independent tuning options for initiator setup.
Definition Initiator.hpp:19
The endpoint is done and can be evicted from the initiator.
std::chrono::steady_clock::time_point deadline
TargetInfo contains all the information required by an initiator to operate transfers to the given ta...