MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
test_Address.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 <cstdint>
6#include <cstring>
7#include <array>
8#include <variant>
9#include <vector>
10#include <endian.h>
11#include <sys/socket.h>
12#include <arpa/inet.h>
13#include <catch2/catch_test_macros.hpp>
14#include <netinet/in.h>
15#include "Address.hpp"
16#include "Exception.hpp"
17#include "FabricAddress.hpp"
18#include "Provider.hpp"
19
20using namespace mxl::lib::fabrics::ofi;
21
22namespace
23{
25 [[nodiscard]]
26 ::sockaddr_in makeSockaddrIn(char const* ip, std::uint16_t port)
27 {
28 auto sin = ::sockaddr_in{};
29 sin.sin_family = AF_INET;
30 sin.sin_port = htons(port);
31 REQUIRE(::inet_pton(AF_INET, ip, &sin.sin_addr) == 1);
32 return sin;
33 }
34
35 [[nodiscard]]
36 ::sockaddr_in6 makeSockaddrIn6(char const* ip, std::uint16_t port)
37 {
38 auto sin6 = ::sockaddr_in6{};
39 sin6.sin6_family = AF_INET6;
40 sin6.sin6_port = htons(port);
41 REQUIRE(::inet_pton(AF_INET6, ip, &sin6.sin6_addr) == 1);
42 return sin6;
43 }
44
47 [[nodiscard]]
48 OfiSockaddrIb makeSockaddrIb(char const* gid, std::uint16_t pkey, std::uint16_t portSpace, std::uint64_t scopeId, std::uint16_t port)
49 {
50 auto sib = OfiSockaddrIb{};
51 sib.sib_family = AF_IB;
52 sib.sib_pkey = htons(pkey);
53 REQUIRE(::inet_pton(AF_INET6, gid, sib.sib_addr) == 1);
54 sib.sib_sid = htobe64((static_cast<std::uint64_t>(portSpace) << 16) | port);
55 sib.sib_scope_id = htobe64(scopeId);
56 return sib;
57 }
58
61 [[nodiscard]]
62 std::array<std::uint8_t, 24> makeEfa(char const* gid, std::uint16_t qpn, std::uint32_t qkey)
63 {
64 auto buf = std::array<std::uint8_t, 24>{};
65 REQUIRE(::inet_pton(AF_INET6, gid, buf.data()) == 1);
66 std::memcpy(buf.data() + 16, &qpn, sizeof(qpn));
67 std::memcpy(buf.data() + 20, &qkey, sizeof(qkey));
68 return buf;
69 }
70}
71
72TEST_CASE("ofi: FabricAddress default construction", "[ofi][FabricAddress]")
73{
74 auto const empty = RawFabricAddress{};
75 REQUIRE(empty.size() == 0);
76 REQUIRE(empty.raw() == nullptr);
77 REQUIRE(empty.raw() == static_cast<void*>(nullptr));
78 REQUIRE(empty.toBase64().empty());
79}
80
81TEST_CASE("ofi: FabricAddress base64 decode", "[ofi][FabricAddress]")
82{
83 auto const addr = RawFabricAddress::fromBase64("AQIDBAU=", FabricAddressFormat::Unspec); // base64 for {1,2,3,4,5}
84 auto* addrInner = static_cast<uint8_t const*>(addr.raw());
85
86 REQUIRE(addr.size() == 5);
87
88 // Simulate a FabricAddress with some data
89 std::vector<uint8_t> expected = {1, 2, 3, 4, 5};
90 for (size_t i = 0; i < addr.size(); i++)
91 {
92 REQUIRE(addrInner[i] == expected[i]);
93 }
94
95 // Encode again and validate we get the same string
96 std::string b64 = addr.toBase64();
97 REQUIRE(b64 == "AQIDBAU=");
98}
99
100TEST_CASE("ofi: FabricAddress::decode FI_SOCKADDR_IN", "[ofi][FabricAddress]")
101{
102 auto const native = makeSockaddrIn("10.130.40.13", 9000);
103 auto const addr = FabricAddress::decode(FabricAddressFormat::SockaddrIn, &native, sizeof(native));
104
105 REQUIRE_FALSE(addr.empty());
106 REQUIRE(std::holds_alternative<::sockaddr_in>(addr.native()));
107
108 auto const& sin = std::get<::sockaddr_in>(addr.native());
109 REQUIRE(sin.sin_family == AF_INET);
110 REQUIRE(ntohs(sin.sin_port) == 9000);
111
112 char ip[INET_ADDRSTRLEN] = {};
113 REQUIRE(::inet_ntop(AF_INET, &sin.sin_addr, ip, sizeof(ip)) != nullptr);
114 REQUIRE(std::string{ip} == "10.130.40.13");
115
116 // Renders to the canonical ofi_straddr string.
117 REQUIRE(addr.toString() == "fi_sockaddr_in://10.130.40.13:9000");
118}
119
120TEST_CASE("ofi: FabricAddress::decode FI_SOCKADDR_IN without a port", "[ofi][FabricAddress]")
121{
122 auto const native = makeSockaddrIn("10.130.40.13", 0);
123 auto const addr = FabricAddress::decode(FabricAddressFormat::SockaddrIn, &native, sizeof(native));
124
125 REQUIRE(std::holds_alternative<::sockaddr_in>(addr.native()));
126 auto const& sin = std::get<::sockaddr_in>(addr.native());
127 REQUIRE(sin.sin_family == AF_INET);
128 REQUIRE(ntohs(sin.sin_port) == 0);
129
130 // An absent port is rendered as :0 by ofi_straddr.
131 REQUIRE(addr.toString() == "fi_sockaddr_in://10.130.40.13:0");
132}
133
134TEST_CASE("ofi: FabricAddress::decode FI_SOCKADDR_IN6", "[ofi][FabricAddress]")
135{
136 auto const native = makeSockaddrIn6("2001:db8::1", 443);
137 auto const addr = FabricAddress::decode(FabricAddressFormat::SockaddrIn6, &native, sizeof(native));
138
139 REQUIRE(std::holds_alternative<::sockaddr_in6>(addr.native()));
140 auto const& sin6 = std::get<::sockaddr_in6>(addr.native());
141 REQUIRE(sin6.sin6_family == AF_INET6);
142 REQUIRE(ntohs(sin6.sin6_port) == 443);
143
144 REQUIRE(addr.toString() == "fi_sockaddr_in6://[2001:db8::1]:443");
145}
146
147TEST_CASE("ofi: FabricAddress::decode FI_ADDR_EFA", "[ofi][FabricAddress]")
148{
149 auto const native = makeEfa("fe80::1", 12, 34);
150 auto const addr = FabricAddress::decode(FabricAddressFormat::Efa, native.data(), native.size());
151
152 REQUIRE(std::holds_alternative<EfaAddress>(addr.native()));
153 auto const& efa = std::get<EfaAddress>(addr.native());
154 REQUIRE(efa.qpn == 12);
155 REQUIRE(efa.qkey == 34);
156
157 REQUIRE(addr.toString() == "fi_addr_efa://[fe80::1]:12:34");
158}
159
160TEST_CASE("ofi: FabricAddress::decode FI_SOCKADDR_IB", "[ofi][FabricAddress]")
161{
162 // P_Key, port space and scope id are hexadecimal; the lower 16 bits of the service id carry
163 // the port.
164 auto const native = makeSockaddrIb("fe80::1", /*pkey=*/0x1234, /*portSpace=*/0x10b, /*scopeId=*/0xff, /*port=*/0x1f90);
165 auto const addr = FabricAddress::decode(FabricAddressFormat::SockaddrIb, &native, sizeof(native));
166
167 REQUIRE(std::holds_alternative<OfiSockaddrIb>(addr.native()));
168 auto const& sib = std::get<OfiSockaddrIb>(addr.native());
169 REQUIRE(sib.sib_family == AF_IB);
170 REQUIRE(ntohs(sib.sib_pkey) == 0x1234);
171 REQUIRE(be64toh(sib.sib_scope_id) == 0xff);
172
173 // The service id packs the port space in bits 16..31 and the port in bits 0..15.
174 REQUIRE(((be64toh(sib.sib_sid) >> 16) & 0xfff) == 0x10b);
175 REQUIRE((be64toh(sib.sib_sid) & 0xffff) == 0x1f90);
176
177 // ofi_straddr does not emit the port, so the rendering drops it (but keeps the port space).
178 REQUIRE(addr.toString() == "fi_sockaddr_ib://[fe80::1]:0x1234:0x10b:0xff");
179}
180
181TEST_CASE("ofi: FabricAddress::host returns the address portion of each type", "[ofi][FabricAddress]")
182{
183 auto const sin = makeSockaddrIn("10.130.40.13", 9000);
184 REQUIRE(FabricAddress::decode(FabricAddressFormat::SockaddrIn, &sin, sizeof(sin)).node() == "10.130.40.13");
185
186 auto const sin6 = makeSockaddrIn6("2001:db8::1", 443);
187 REQUIRE(FabricAddress::decode(FabricAddressFormat::SockaddrIn6, &sin6, sizeof(sin6)).node() == "2001:db8::1");
188
189 auto const efa = makeEfa("fe80::1", 12, 34);
190 REQUIRE(FabricAddress::decode(FabricAddressFormat::Efa, efa.data(), efa.size()).node() == "fe80::1");
191
192 auto const sib = makeSockaddrIb("fe80::1", 0x1234, 0x10b, 0xff, 0x1f90);
193 REQUIRE(FabricAddress::decode(FabricAddressFormat::SockaddrIb, &sib, sizeof(sib)).node() == "fe80::1");
194
195 // An empty / undecoded address has no host.
196 REQUIRE_FALSE(FabricAddress{}.node().has_value());
197}
198
199TEST_CASE("ofi: FabricAddress::decode returns an empty address for undecodable input", "[ofi][FabricAddress]")
200{
201 auto const sin = makeSockaddrIn("10.0.0.1", 9000);
202
203 // A null address buffer.
205
206 // A buffer that is too small for the declared format.
208
209 auto const efa = makeEfa("fe80::1", 12, 34);
210 REQUIRE(FabricAddress::decode(FabricAddressFormat::SockaddrIn6, efa.data(), efa.size() - 1).empty());
211
212 // An unsupported / unrecognised address format.
214
215 // FI_SOCKADDR dispatches on the embedded family; an unknown family does not decode.
216 auto unknown = ::sockaddr{};
217 unknown.sa_family = AF_UNSPEC;
219}
220
221TEST_CASE("ofi: FabricAddress::parse checks the TCP port", "[ofi][FabricAddress]")
222{
223 auto const addr = FabricAddress::parse(Provider::TCP, "10.130.40.13", "9000");
224 REQUIRE(std::holds_alternative<::sockaddr_in>(addr.native()));
225 REQUIRE(std::holds_alternative<::sockaddr_in>(FabricAddress::parse(Provider::TCP, "10.130.40.13", "65535").native()));
226
227 // std::stoi accepted these, and the int was then narrowed to a 16-bit port.
228 for (auto const* service : {"65536", "-1", "9000x", " 9000", "+9000"})
229 {
230 CAPTURE(service);
231 try
232 {
233 (void)FabricAddress::parse(Provider::TCP, "10.130.40.13", service);
234 FAIL("parse accepted an invalid port");
235 }
236 catch (Exception const& e)
237 {
239 }
240 }
241}
Custom exception type used in the fabrics library.
Definition Exception.hpp:28
mxlStatus status() const noexcept
Return the mxlStatus status code that describes the condition that led to the exception being thrown.
Definition Exception.cpp:16
Owning, type-safe representation of a libfabric address.
static FabricAddress parse(Provider provider, std::optional< std::string > node, std::optional< std::string > service)
Parse a node/service pair into a typed address for the given provider.
std::optional< std::string > node() const
Return the host portion of the address as a string.
static FabricAddress decode(FabricAddressFormat addrFormat, void const *addr, std::size_t len)
Decode a raw libfabric address buffer of the given addr_format.
Wrapper around a libfabric endpoint address.
Definition Address.hpp:20
static RawFabricAddress fromBase64(std::string_view data, FabricAddressFormat addressFormat)
Parse a fabric address from a base64 encoded string.
Definition Address.cpp:67
@ MXL_ERR_INVALID_ARG
Definition mxl.h:35
static FabricInfo empty() noexcept
Create and empty FabricInfo.
Local declaration of libfabric's internal struct ofi_sockaddr_ib.
TEST_CASE("ofi: FabricAddress default construction", "[ofi][FabricAddress]")
REQUIRE(timeDelta< 33 '366 '667LL)