MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
TargetInfo.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 "TargetInfo.hpp"
6#include <cstdint>
7#include <optional>
8#include <stdexcept>
9#include <string>
11#include <picojson/wrapper.h>
12#include <uuid/uuid.h>
13#include "Address.hpp"
14#include "Exception.hpp"
15#include "FabricAddress.hpp"
16#include "Format.hpp" // IWYU pragma: keep; for fmt::format
17#include "Provider.hpp"
18#include "RemoteRegion.hpp"
19
21{
22
24 {
25 return reinterpret_cast<TargetInfo*>(api);
26 }
27
29 {
30 return reinterpret_cast<mxlFabricsTargetInfo>(this);
31 }
32
33 std::string TargetInfo::toJSON() const
34 {
35 auto root = picojson::object{};
36
37 root["fabricAddress"] = picojson::value(fabricAddress.toBase64());
38 root["addressFormat"] = picojson::value{static_cast<double>(fabricAddress.format())};
39
40 auto regions = picojson::array{};
41 for (auto const& remoteRegion : remoteRegions)
42 {
43 auto region = picojson::object{};
44 region["addr"] = picojson::value(std::to_string(remoteRegion.addr));
45 region["len"] = picojson::value(std::to_string(remoteRegion.len));
46 region["rkey"] = picojson::value(std::to_string(remoteRegion.rkey));
47 regions.emplace_back(region);
48 }
49 root["regions"] = picojson::value(regions);
50
52 {
53 auto bounceBufferInfoObj = picojson::object{};
54 bounceBufferInfoObj["entryCount"] = picojson::value(std::to_string(bounceBufferInfo->entryCount));
55 bounceBufferInfoObj["entrySize"] = picojson::value(std::to_string(bounceBufferInfo->entrySize));
56 root["bounceBufferInfo"] = picojson::value(bounceBufferInfoObj);
57 }
58
59 root["id"] = picojson::value(std::to_string(id));
60 root["provider"] = picojson::value{fmt::to_string(provider)};
61
62 return picojson::value(root).serialize(false);
63 }
64
65 TargetInfo TargetInfo::fromJSON(std::string const& s)
66 {
67 // The target info has one region per grain, so it can be much larger than a flow definition.
68 constexpr auto limits = JsonLimits{.maxInputSize = std::size_t{4} * 1024U * 1024U, .maxDepth = 8U, .maxContainerSize = 65536U};
69
70 auto parsed = picojson::value{};
71 auto const err = parseJson(parsed, s, limits);
72 if (!err.empty())
73 {
74 throw Exception::invalidArgument("Failure when parsing JSON: {} ", err);
75 }
76
77 if (!parsed.is<picojson::object>())
78 {
79 throw Exception::invalidArgument("Expected a JSON object");
80 }
81
82 auto root = parsed.get<picojson::object>();
83
84 // The conversion helpers throw std::invalid_argument, which the C API would report as MXL_ERR_UNKNOWN.
85 try
86 {
87 auto const addressFormat = mustConvertAddressFormat(jsonToInteger<std::uint32_t>(root.at("addressFormat"), "addressFormat"));
88 auto const fabricAddress = RawFabricAddress::fromBase64(root.at("fabricAddress").get<std::string>(), addressFormat);
89
90 auto regions = std::vector<RemoteRegion>{};
91 for (auto const& regionValue : root.at("regions").get<picojson::array>())
92 {
93 auto const regionObj = regionValue.get<picojson::object>();
94
95 // Conversions of types that represent memory addresses to strings is preferred over converting to float
96 // to make sure nobody can reduce precision when converting on the way.
97 auto const addr = jsonStringToInteger<std::uint64_t>(regionObj.at("addr"), "addr");
98 auto const len = jsonStringToInteger<std::size_t>(regionObj.at("len"), "len");
99 auto const rkey = jsonStringToInteger<std::uint64_t>(regionObj.at("rkey"), "rkey");
100 regions.emplace_back(addr, len, rkey);
101 }
102
103 auto bounceBufferInfo = std::optional<TargetInfoBounceBufferInfo>{std::nullopt};
104 if (root.contains("bounceBufferInfo"))
105 {
106 auto const bounceBufferInfoObj = root.at("bounceBufferInfo").get<picojson::object>();
107 auto const entryCount = jsonStringToInteger<std::size_t>(bounceBufferInfoObj.at("entryCount"), "entryCount");
108 auto const entrySize = jsonStringToInteger<std::size_t>(bounceBufferInfoObj.at("entrySize"), "entrySize");
109 bounceBufferInfo = std::make_optional<TargetInfoBounceBufferInfo>(entryCount, entrySize);
110 }
111
112 auto const id = jsonStringToInteger<Endpoint::Id>(root.at("id"), "id");
113 auto const provider = providerFromString(root.at("provider").get<std::string>()).value_or(Provider::ANY);
114 return {.id = id, .fabricAddress = fabricAddress, .provider = provider, .remoteRegions = regions, .bounceBufferInfo = bounceBufferInfo};
115 }
116 catch (std::invalid_argument const& e)
117 {
118 throw Exception::invalidArgument("{}", e.what());
119 }
120 }
121
122 bool TargetInfo::operator==(TargetInfo const& other) const noexcept
123 {
124 return (fabricAddress == other.fabricAddress) && (remoteRegions == other.remoteRegions) && (id == other.id);
125 }
126}
static Exception invalidArgument(fmt::format_string< T... > fmt, T &&... args)
Make an MXL_ERR_INVALID_ARG exception.
Definition Exception.hpp:43
static RawFabricAddress fromBase64(std::string_view data, FabricAddressFormat addressFormat)
Parse a fabric address from a base64 encoded string.
Definition Address.cpp:67
struct mxlFabricsTargetInfo_t * mxlFabricsTargetInfo
Definition fabrics.h:39
std::optional< Provider > providerFromString(std::string const &s) noexcept
Parse a provider name string, and return the enum value.
Definition Provider.cpp:49
FabricAddressFormat mustConvertAddressFormat(std::uint32_t fiAddrFormat)
Convert from libfabric address format value to FabricAddressFormat Never returns FabricAddressFormat:...
T jsonStringToInteger(picojson::value const &value, std::string_view name)
T jsonToInteger(picojson::value const &value, std::string_view name)
std::string parseJson(picojson::value &out, std::string_view text, JsonLimits const &limits={})
Limits applied by parseJson().
Definition JsonUtils.hpp:23
TargetInfo contains all the information required by an initiator to operate transfers to the given ta...
std::vector< RemoteRegion > remoteRegions
std::optional< TargetInfoBounceBufferInfo > bounceBufferInfo
static TargetInfo fromJSON(std::string const &s)
Construct a TargetInfo instance from a JSON representation.
std::string toJSON() const
Serialize a TargetInfo instance to a JSON representation.
::mxlFabricsTargetInfo toAPI() noexcept
Cast a pointer to a TargetInfo instance to an mxlFabricsTargetInfo opaque pointer.
static TargetInfo * fromAPI(mxlFabricsTargetInfo api) noexcept
Cast an mxlFabricsTargetInfo opaque pointer to a TargetInfo pointer.
bool operator==(TargetInfo const &other) const noexcept