MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
Target.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 "Target.hpp"
6#include <exception>
7#include <memory>
8#include <utility>
9#include <fmt/format.h>
11#include <rdma/fabric.h>
12#include <rdma/fi_errno.h>
13#include "mxl/fabrics.h"
14#include "Exception.hpp"
16#include "LocalRegion.hpp"
17#include "RCTarget.hpp"
18#include "RDMTarget.hpp"
19
21{
23 {
24 return LocalRegion{
25 .addr = std::bit_cast<std::uint64_t>(&data),
26 .len = sizeof(std::uint64_t),
27 .desc = nullptr,
28 };
29 }
30
35
37 {
38 if (!_inner)
39 {
40 return;
41 }
42
43 try
44 {
45 _inner->shutdown();
46 }
47 catch (std::exception const& e)
48 {
49 MXL_ERROR("Failed to shut down the target: {}", e.what());
50 }
51
52 _inner.reset();
53 }
54
56 {
57 return reinterpret_cast<TargetWrapper*>(api);
58 }
59
61 {
62 return reinterpret_cast<mxlFabricsTarget>(this);
63 }
64
65 std::optional<Target::ReadResult> TargetWrapper::read()
66 {
67 if (!_inner)
68 {
69 throw Exception::invalidState("Target is not set up.");
70 }
71
72 return _inner->read();
73 }
74
75 std::optional<Target::ReadResult> TargetWrapper::readBlocking(std::chrono::steady_clock::duration timeout)
76 {
77 if (!_inner)
78 {
79 throw Exception::invalidState("Target is not set up.");
80 }
81
82 return _inner->readBlocking(timeout);
83 }
84
85 template<typename TargetT>
86 std::unique_ptr<TargetInfo> TargetWrapper::setup(mxlFabricsTargetConfig const& config, FabricInfoView info, TargetSetupOptions const& options)
87 {
88 auto [inner, targetInfo] = TargetT::setup(config, info, options);
89 _inner = std::move(inner);
90 return std::move(targetInfo);
91 }
92
93 std::unique_ptr<TargetInfo> TargetWrapper::setup(mxlFabricsTargetConfig const& config, TargetSetupOptions const& options)
94 {
96
97 auto [info, providerConfig] = selectSourceInterface(config.interface, /* target */ true);
98 switch (info->ep_attr->type)
99 {
100 case FI_EP_MSG: return setup<RCTarget>(config, info.view(), options);
101 case FI_EP_RDM: return setup<RDMTarget>(config, info.view(), options);
102 default: throw Exception::invalidState("unsupported endpoint type");
103 }
104 }
105}
#define MXL_ERROR(...)
Definition Logging.hpp:67
static Exception invalidState(fmt::format_string< T... > fmt, T &&... args)
Make an MXL_ERR_INVALID_STATE exception.
Definition Exception.hpp:59
Non-owning view of a FabricInfo. Returned when iterating over a FabricInfoList.
A wrapper around Target implementations.
Definition Target.hpp:99
std::optional< Target::ReadResult > read()
Definition Target.cpp:65
~TargetWrapper()
Shut down the underlying target before it is destroyed.
Definition Target.cpp:31
std::unique_ptr< Target > _inner
Definition Target.hpp:152
std::optional< Target::ReadResult > readBlocking(std::chrono::steady_clock::duration timeout)
Definition Target.cpp:75
void destroyInner() noexcept
Shut down and destroy the underlying target. Errors are logged, because this runs in the destructor.
Definition Target.cpp:36
mxlFabricsTarget toAPI() noexcept
Convert this TargetWrapper to its API representation.
Definition Target.cpp:60
static TargetWrapper * fromAPI(mxlFabricsTarget api) noexcept
Convert an mxlFabricsTarget API object to its underlying TargetWrapper.
Definition Target.cpp:55
std::unique_ptr< TargetInfo > setup(mxlFabricsTargetConfig const &config, TargetSetupOptions const &options={})
Set up the target with the specified configuration.
Definition Target.cpp:93
struct mxlFabricsTargetConfig_t mxlFabricsTargetConfig
struct mxlFabricsTarget_t * mxlFabricsTarget
Definition fabrics.h:34
std::pair< FabricInfo, ProviderConfig > selectSourceInterface(::mxlFabricsInterfaceConfig const &interfaceConfig, bool isTarget)
mxlFabricsInterfaceConfig interface
Definition fabrics.h:130
Represent a source memory region used for for data transfer.
Provider-independent tuning options for target setup.
Definition Target.hpp:23
LocalRegion toLocalRegion() const noexcept
Get the underlying local region of the immediate data.
Definition Target.cpp:22