A repeating flow buys its route back (L4 routing)¶
“A delivery IS a FWD WRITE” is the right model and the wrong bill. Taken literally it makes
every streamed sample re-carry its full return route — roughly 16× overhead on a small,
high-rate sample
(RFC-0004
§D, §E.1). The route-handle is header elision generalized: the producer advertises the route
once, the consumer records label → binding, and the steady state becomes
COMPACT{ label, payload }
(ADR-0035,
tr::net::route_handle_t).
The bare word label always means this — RFC-0004 §E.1’s per-link u16. RFC-0027’s
per-element alias is always spelled “path label”, and it is a different mechanism
(CONTEXT.md §Path label).
What to notice¶
The label is per LINK, not global. It is minted by
advertiseagainst one child name and means nothing anywhere else; a forwarding hop swaps it, MPLS-style, exactly as a CAN ID is re-resolved against each bus (ADR-0030).Only flagged flows pay. A binding exists because someone advertised. A cold, one-shot or non-compact flow allocates no entry at all — so the stateless-forwarder property of multi-hop survives alongside this one, rather than being traded away.
Minted once per
(link, route), then reused (#913). Advertising the same route again re-sends the frame — which is the reconnect self-heal — but grows no table. The example checks both halves: same label back, table still at one.The delivery is still a write. The consumer’s binding here is a terminus binding: the advertised route names a vertex on the consumer, so a COMPACT is written straight to it and reported through
on_compact_delivery. Compaction changes the bytes, never the semantics.The byte delta is measured, not asserted at a threshold. The example checks only
compact < fulland prints the numbers, because the delta depends on how long the route is — a hard-coded figure would be a CI flake waiting for a longer path.A
0fromadvertiseis a refusal, not an error. No such link, or that link’s label space is exhausted / its egress table is full under an injected bound — in which case the flow simply keeps using the full-route form. The example checks for it rather than assuming success.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 — a repeating flow buys its route back: an ADVERTISE binds `label ↔ route`
9 * on ONE link, and every later delivery carries the label instead of the route.
10 *
11 * "A delivery IS a FWD WRITE" (RFC-0004 §D) is the right model and the wrong bill. Taken
12 * literally it makes every streamed sample re-carry its full return route — roughly 16× overhead
13 * on a small, high-rate sample (RFC-0004 §E.1). The route-handle is header elision generalized:
14 * the producer advertises the route once, the consumer records `label → binding`, and the
15 * steady state becomes `COMPACT{ label, payload }` (ADR-0035, `route_handle_t`).
16 *
17 * Three properties the example makes visible:
18 *
19 * - **The label is per LINK, not global.** It is minted by `advertise` against one child name
20 * and means nothing anywhere else; a forwarding hop SWAPS it, MPLS-style, exactly as a CAN
21 * ID is re-resolved against each bus.
22 * - **Only flagged flows pay.** A binding exists because someone advertised. A cold, one-shot
23 * or non-compact flow allocates no entry at all, so the stateless-forwarder property of
24 * route_multi_hop survives alongside this one.
25 * - **The label is minted once per `(link, route)` and then REUSED** (#913): advertising the
26 * same route again re-sends the frame — which is the reconnect self-heal — but grows no
27 * table.
28 *
29 * The consumer's binding here is a TERMINUS binding: the advertised route names a vertex on the
30 * consumer itself, so a COMPACT is written straight to that vertex and reported through
31 * `on_compact_delivery`. The delivery is a write, label or no label — the compaction changes
32 * the bytes, never the semantics.
33 *
34 * Runs under ctest as `example_route_label_compact`; returns non-zero on any failed check.
35 */
36
37#include <cstddef>
38#include <cstdint>
39#include <cstdio>
40#include <initializer_list>
41#include <span>
42#include <string_view>
43#include <vector>
44
45#include "libtracer/fwd_router.hpp"
46#include "libtracer/route_handle.hpp"
47#include "libtracer/tlv_emit.hpp"
48#include "libtracer/tracer.hpp"
49
50namespace {
51
52using tr::graph::fwd_op_t;
53using tr::graph::graph_t;
54using tr::graph::path_t;
55using tr::graph::role_t;
56using tr::wire::opt_t;
57using tr::wire::type_t;
58
59/** @brief Report expectation @p what and record a failure on @p ok. */
60void check(bool& ok, bool cond, const char* what) {
61 std::printf(" [%s] %s\n", cond ? "ok" : "FAIL", what);
62 ok = ok && cond;
63}
64
65/** @brief A `transport_t` that keeps every frame handed to it — the "wire", made inspectable. */
66struct recording_link_t : tr::net::transport_t {
67 std::vector<std::vector<std::byte>> sent; /**< @brief Frames emitted on this link, in order. */
68 void send(std::span<const std::byte> frame) override {
69 sent.emplace_back(frame.begin(), frame.end());
70 }
71 void send(std::span<const std::span<const std::byte>> iov) override {
72 std::vector<std::byte> flat;
73 for (const auto part : iov) flat.insert(flat.end(), part.begin(), part.end());
74 sent.push_back(std::move(flat));
75 }
76};
77
78/** @brief What the consumer's compact-delivery sink saw. */
79struct compact_sink_t {
80 std::size_t deliveries = 0; /**< @brief COMPACTs that resolved to a local terminus. */
81 std::size_t payload_bytes = 0; /**< @brief The last delivery's payload TLV size. */
82};
83
84/** @brief A `PATH` TLV over @p segs — RFC-0018 packed segment records, `opt.PL` clear. */
85std::vector<std::byte> path_tlv(std::initializer_list<std::string_view> segs) {
86 std::vector<std::byte> body;
87 for (std::string_view s : segs) (void)tr::wire::emit_path_segment(body, s);
88 std::vector<std::byte> out;
89 tr::wire::emit_tlv(out, type_t::PATH, opt_t{}, body);
90 return out;
91}
92
93/** @brief A one-byte `VALUE` TLV carrying @p v. */
94std::vector<std::byte> value_tlv(std::uint8_t v) {
95 const std::byte b{v};
96 std::vector<std::byte> out;
97 tr::wire::emit_tlv(out, type_t::VALUE, opt_t{}, std::span<const std::byte>(&b, 1));
98 return out;
99}
100
101/** @brief The full-route form of the same delivery — what a COMPACT is measured against. */
102std::vector<std::byte> fwd_write(std::initializer_list<std::string_view> dst) {
103 std::vector<std::byte> body = value_tlv(static_cast<std::uint8_t>(fwd_op_t::WRITE));
104 for (const auto& part : {path_tlv(dst), path_tlv({}), value_tlv(0x2A)}) {
105 body.insert(body.end(), part.begin(), part.end());
106 }
107 std::vector<std::byte> out;
108 tr::wire::emit_tlv(out, type_t::FWD, opt_t{.pl = true}, body);
109 return out;
110}
111
112} // namespace
113
114int main() {
115 bool ok = true;
116
117 // Producer P and consumer C, one link between them. Each knows the other by its OWN name.
118 graph_t graph_p, graph_c;
119 tr::net::fwd_router_t producer(graph_p), consumer(graph_c);
120 recording_link_t p_to_c, c_to_p;
121 if (!producer.add_child("c", p_to_c) || !consumer.add_child("p", c_to_p)) {
122 std::fprintf(stderr, "route_label_compact: add_child failed — nothing registered\n");
123 return 1;
124 }
125 (void)graph_c.register_vertex(path_t("/mirror"), role_t::STORED_VALUE);
126
127 compact_sink_t sink;
128 consumer.on_compact_delivery(
129 [](void* ctx, std::span<const std::byte> /*route*/, std::span<const std::byte> payload) {
130 auto* s = static_cast<compact_sink_t*>(ctx);
131 ++s->deliveries;
132 s->payload_bytes = payload.size();
133 },
134 &sink);
135
136 // 1. The producer advertises the delivery route ONCE, over the link it calls "c".
137 const std::vector<std::byte> route = path_tlv({"mirror"});
138 const std::uint16_t label = producer.advertise("c", route);
139 check(ok, label != 0, "advertise minted a label (0 would mean: no such link, or table full)");
140 check(ok, p_to_c.sent.size() == 1, "and put exactly one ADVERTISE frame on that link");
141 if (label == 0 || p_to_c.sent.size() != 1) return 1;
142
143 check(ok, consumer.handles().ingress_count() == 0,
144 "the consumer holds nothing before it arrives");
145 consumer.on_frame("p", p_to_c.sent[0]);
146 check(ok, consumer.handles().ingress_count() == 1,
147 "the consumer learned exactly one ingress binding for this (link, label)");
148
149 // 2. The steady state: the route does not ride any more.
150 p_to_c.sent.clear();
151 const std::vector<std::byte> payload = value_tlv(0x2A);
152 producer.send_compact("c", label, payload);
153 check(ok, p_to_c.sent.size() == 1, "a COMPACT went out");
154 if (p_to_c.sent.size() != 1) return 1;
155 const std::size_t compact_bytes = p_to_c.sent[0].size();
156 const std::size_t full_bytes = fwd_write({"mirror"}).size();
157 check(ok, compact_bytes < full_bytes,
158 "and it is smaller than the equivalent full-route FWD{WRITE} — the whole point");
159
160 consumer.on_frame("p", p_to_c.sent[0]);
161 check(ok, sink.deliveries == 1, "the label resolved to a LOCAL terminus binding");
162 check(ok, graph_c.read(path_t("/mirror")).has_value(),
163 "and the delivery was a write, exactly as the full-route form would have been");
164
165 // 3. Re-advertising the same route reuses the label and binds nothing new. That is what
166 // makes the reconnect self-heal (route_label_stale) cheap enough to run unconditionally.
167 p_to_c.sent.clear();
168 const std::uint16_t again = producer.advertise("c", route);
169 check(ok, again == label, "the same (link, route) mints the SAME label — minted once, reused");
170 check(ok, p_to_c.sent.size() == 1, "the ADVERTISE frame is re-sent...");
171 consumer.on_frame("p", p_to_c.sent[0]);
172 check(ok, consumer.handles().ingress_count() == 1, "...and the consumer's table did not grow");
173
174 // 4. The label is scoped to its link. Nothing is bound on any other name.
175 check(ok, consumer.handles().link_count() == 1,
176 "one link shell: a label means nothing on a link it was not bound for");
177
178 std::printf(
179 "COMPACT %zu B vs full-route FWD{WRITE} %zu B (%.0f%% of the frame elided)\n",
180 compact_bytes, full_bytes,
181 100.0 * static_cast<double>(full_bytes - compact_bytes) / static_cast<double>(full_bytes));
182 return ok ? 0 : 1;
183}
See also: fwd-router module · CAN transport · network formation · multi-hop · a stale label.