The src you accumulated is the way home (L4 routing)¶
Every hop that stripped a dst mount run prepended its own name for the inbound link to src.
By the time the request reaches the terminus, src spells the whole way back — in the frame, in
the hands of the node that has to answer. So the terminus builds FWD{REPLY} with
dst = the request's src and sends it back over the bidirectional link the request arrived on;
each hop retraces its own link the same way, consuming one mount run of that dst as it goes.
No reply address and no correlation id are needed anywhere
(RFC-0004
§D, CONTEXT.md §Path-as-route).
That is the other half of the statelessness on the multi-hop page. A forwarder with a request table would need an entry per in-flight request, a timeout to reap it, and a policy for what happens when it overflows.
What to notice¶
The
src→dsthandoff is asserted directly. The example re-encodes thePATHchild of each frame and compares: the request’ssrcis/app, and the reply’sdstis/app.The reply’s own
srcnames the responder./sensor/temp, not the terminus node — which is how an answer carries its provenance without a separate field.The reply is handed over rope-native.
on_replyreceives arope_t; the router performs no decode and no flatten (ADR-0055). A sink that wants contiguous bytes callsmaterialize()once and keeps the view alive while it reads — a single-link reply, the common case, costs no copy at all.The route terminates by running out. At the origin,
appnames no child, so the reply is terminal and the sink fires. Nothing marks a frame as “the last hop”; the address does.Neither node stored anything. Both routers report zero bindings after the round trip.
This target needs the FWD net plane. It is built only when
LIBTRACER_NET_PLANEis on (the default). Nothing in it is conditional at run time.
Source¶
1/*
2 * SPDX-License-Identifier: Apache-2.0
3 * SPDX-FileCopyrightText: Copyright 2026 avatarsd LLC
4 */
5
6/**
7 * @file
8 * @brief ONE CONCEPT — nothing remembers the request: the `src` a frame accumulated on the way
9 * out IS the route its `FWD{REPLY}` comes home along.
10 *
11 * Every hop that stripped a `dst` mount run prepended its own name for the inbound link to `src`
12 * (route_dst_is_source_route). By the time the request reaches the terminus, `src` spells the
13 * whole way back — in the frame, in the hands of the node that has to answer. So the terminus
14 * builds `FWD{REPLY}` with `dst = the request's src` and sends it back over the bidirectional
15 * link the request arrived on; each hop retraces its own link the same way, consuming one mount
16 * run of that `dst` as it goes. No reply address and no correlation id are needed anywhere
17 * (RFC-0004 §D, `CONTEXT.md` §Path-as-route).
18 *
19 * That is the other half of the statelessness in route_multi_hop. A forwarder with a request
20 * table would need an entry per in-flight request, a timeout to reap it, and a policy for what
21 * happens when it overflows. Carrying the return route instead costs bytes on the wire and
22 * nothing on the hop.
23 *
24 * The reply terminates where `dst` runs out of route segments that name a child: at the origin,
25 * whose `on_reply` sink fires (ADR-0055 — rope-native, no decode and no flatten in the router).
26 *
27 * Runs under ctest as `example_route_reply_home`; returns non-zero on any failed check.
28 */
29
30#include <array>
31#include <cstddef>
32#include <cstdint>
33#include <cstdio>
34#include <initializer_list>
35#include <iterator>
36#include <span>
37#include <string_view>
38#include <vector>
39
40#include "libtracer/fwd_router.hpp"
41#include "libtracer/path_pair.hpp"
42#include "libtracer/tlv_emit.hpp"
43#include "libtracer/tracer.hpp"
44
45namespace {
46
47using tr::graph::fwd_op_t;
48using tr::graph::graph_t;
49using tr::graph::path_t;
50using tr::graph::reply_kind_t;
51using tr::graph::role_t;
52using tr::wire::opt_t;
53using tr::wire::type_t;
54
55/** @brief Report expectation @p what and record a failure on @p ok. */
56void check(bool& ok, bool cond, const char* what) {
57 std::printf(" [%s] %s\n", cond ? "ok" : "FAIL", what);
58 ok = ok && cond;
59}
60
61/** @brief A `transport_t` that keeps every frame handed to it — the "wire", made inspectable. */
62struct recording_link_t : tr::net::transport_t {
63 std::vector<std::vector<std::byte>> sent; /**< @brief Frames emitted on this link, in order. */
64 void send(std::span<const std::byte> frame) override {
65 sent.emplace_back(frame.begin(), frame.end());
66 }
67 void send(std::span<const std::span<const std::byte>> iov) override {
68 std::vector<std::byte> flat;
69 for (const auto part : iov) flat.insert(flat.end(), part.begin(), part.end());
70 sent.push_back(std::move(flat));
71 }
72};
73
74/** @brief What the origin's reply sink saw — the terminal `FWD{REPLY}`, flattened once. */
75struct reply_sink_t {
76 std::size_t count = 0; /**< @brief Replies that terminated here. */
77 std::vector<std::byte> last_frame; /**< @brief The last one's complete bytes. */
78};
79
80/** @brief A `PATH` TLV over @p segs — RFC-0018 packed segment records, `opt.PL` clear. */
81std::vector<std::byte> path_tlv(std::initializer_list<std::string_view> segs) {
82 std::vector<std::byte> body;
83 for (std::string_view s : segs) (void)tr::wire::emit_path_segment(body, s);
84 std::vector<std::byte> out;
85 tr::wire::emit_tlv(out, type_t::PATH, opt_t{}, body);
86 return out;
87}
88
89/** @brief A one-byte `VALUE` TLV carrying @p v. */
90std::vector<std::byte> value_tlv(std::uint8_t v) {
91 const std::byte b{v};
92 std::vector<std::byte> out;
93 tr::wire::emit_tlv(out, type_t::VALUE, opt_t{}, std::span<const std::byte>(&b, 1));
94 return out;
95}
96
97/** @brief `FWD[.pl]{ VALUE op, PATH dst, PATH src }` — a READ carries no payload. */
98std::vector<std::byte> fwd_read(std::initializer_list<std::string_view> dst,
99 std::initializer_list<std::string_view> src) {
100 std::vector<std::byte> body = value_tlv(static_cast<std::uint8_t>(fwd_op_t::READ));
101 for (const auto& part : {path_tlv(dst), path_tlv(src)}) {
102 body.insert(body.end(), part.begin(), part.end());
103 }
104 std::vector<std::byte> out;
105 tr::wire::emit_tlv(out, type_t::FWD, opt_t{.pl = true}, body);
106 return out;
107}
108
109/** @brief The `PATH` child at index @p i of @p frame, re-encoded — or empty if absent. */
110std::vector<std::byte> path_child(std::span<const std::byte> frame, std::size_t i) {
111 const auto tlv = tr::wire::tlv_node_t::over(frame);
112 if (!tlv) return {};
113 const tr::wire::tlv_children_t kids = tlv->children();
114 const auto at = std::ranges::next(kids.begin(), static_cast<std::ptrdiff_t>(i), kids.end());
115 if (at == kids.end() || (*at).type() != type_t::PATH) return {};
116 const std::span<const std::byte> whole = (*at).bytes(); // the child's own encoding
117 return {whole.begin(), whole.end()};
118}
119
120} // namespace
121
122int main() {
123 bool ok = true;
124
125 // The origin: it holds the link to A and a sink for replies that get all the way back.
126 graph_t graph_o;
127 tr::net::fwd_router_t router_o(graph_o);
128 recording_link_t o_to_a;
129
130 // The terminus: it holds the vertex and its link back toward the origin.
131 graph_t graph_b;
132 tr::net::fwd_router_t router_b(graph_b);
133 recording_link_t b_to_o;
134
135 if (!router_o.attach_link("a", o_to_a) || !router_b.attach_link("o", b_to_o)) {
136 std::fprintf(stderr, "route_reply_home: add_child failed — nothing registered\n");
137 return 1;
138 }
139 const auto v = graph_b.register_vertex(path_t("/sensor/temp"), role_t::STORED_VALUE);
140 (void)graph_b.write(v, *tr::view::over_bytes(value_tlv(0x2A)));
141
142 reply_sink_t sink;
143 router_o.on_reply(
144 [](void* ctx, const tr::view::rope_t& reply) {
145 auto* s = static_cast<reply_sink_t*>(ctx);
146 ++s->count;
147 // The router hands the reply over rope-native; a sink that wants contiguous bytes
148 // materializes once and keeps the view alive while it reads (ADR-0052/ADR-0055).
149 const tr::view::view_t flat = reply.materialize();
150 const auto b = flat.bytes();
151 s->last_frame.assign(b.begin(), b.end());
152 },
153 &sink);
154
155 // The app hands its READ to its own router over a link the router calls "app". The frame
156 // carries an EMPTY src: the origin has nothing to prepend yet.
157 router_o.on_frame("app", fwd_read({"a", "sensor", "temp"}, {}));
158 check(ok, o_to_a.sent.size() == 1, "the origin forwarded the READ toward A");
159 if (o_to_a.sent.size() != 1) return 1;
160 check(ok, path_child(o_to_a.sent[0], 2) == path_tlv({"app"}),
161 "and grew src to /app — the one hop the request has taken so far");
162
163 // The terminus resolves /sensor/temp locally and answers. It sends the reply on the link
164 // the request ARRIVED on, addressed to the src it was handed.
165 router_b.on_frame("o", o_to_a.sent[0]);
166 check(ok, b_to_o.sent.size() == 1, "the terminus answered on the link the request came in on");
167 if (b_to_o.sent.size() != 1) return 1;
168 check(ok, path_child(b_to_o.sent[0], 1) == path_tlv({"app"}),
169 "the REPLY's dst IS the request's accumulated src — no correlation table was consulted");
170 // Its src is the terminus's PAIR for /sensor/temp (RFC-0029 §6.2): the responder's address
171 // from the origin's vantage, which the origin can adopt and spell as its next `dst`.
172 std::vector<std::byte> want_src;
173 if (const auto slot = graph_b.vertex_slot(v)) {
174 std::array<std::byte, tr::wire::kPathPairRecordBytes> rec{};
175 tr::wire::path_pair_store(rec, {.index = slot->index, .generation = slot->generation});
176 tr::wire::emit_tlv(want_src, tr::wire::type_t::PATH, tr::wire::opt_t{}, rec);
177 }
178 check(ok, path_child(b_to_o.sent[0], 2) == want_src,
179 "and its src is the responder's PAIR, which is how the answer carries its provenance");
180
181 // Back at the origin, "app" names no child, so the route is spent and the reply terminates.
182 router_o.on_frame("a", b_to_o.sent[0]);
183 check(ok, sink.count == 1, "the reply reached the origin's on_reply sink");
184 check(ok, !sink.last_frame.empty() && sink.last_frame == b_to_o.sent[0],
185 "byte-for-byte the frame the terminus emitted — the origin was one hop away");
186
187 const auto reply = tr::wire::tlv_node_t::over(sink.last_frame);
188 bool is_result = false;
189 if (reply) {
190 const tr::wire::tlv_children_t kids = reply->children();
191 const auto kind = std::ranges::next(kids.begin(), 3, kids.end()); // VALUE kind
192 is_result = kind != kids.end() && (*kind).payload().size() == 1 &&
193 static_cast<reply_kind_t>((*kind).payload()[0]) == reply_kind_t::RESULT;
194 }
195 check(ok, is_result, "and it is kind=RESULT, carrying the value the terminus read");
196
197 std::printf("request src /app -> reply dst /app; no correlation entry anywhere\n");
198 return ok ? 0 : 1;
199}
See also: fwd-router module · communication flows · views and ownership · the source route · terminus or forward.