A datagram already has boundaries (transport plane, udp)¶
Every stream kind in the tree — tcp, ws, quic — has to invent message boundaries,
because a stream has none. UDP already has them, so udp_transport_t adds nothing: a send is
one sendmsg, an inbound datagram is one frame, and there is no reassembler, no length prefix
and no partial-frame state anywhere in the kind.
That single difference is what the whole page is about, and it has two consequences worth seeing before wiring a datagram link.
What to notice¶
One datagram is one frame, with zero bytes of framing. What the sender handed to
sendis what the receiver’s callback sees, and its length came from the datagram rather than from anything in the bytes. Two sends are two frames — never one coalesced read the receiver has to split apart, which is exactly the work the TCP page shows.The bound is hard past one datagram. Unset, the cap is
kDefaultMaxFrame, one Ethernet MTU (1,472 B), and it sizes the receive segment.max_framemay raise it up tokMaxDatagram, what a datagram can physically be; a configured cap above that is inert rather than loosening. A frame larger than a datagram is not a UDP frame; that is what a streaming kind is for.The listener has no peer until one talks to it. Constructed with no peer address, the transport LEARNS its peer from the source address of the first inbound datagram. That is what lets a config-created
role=listenerreply to a dialer whose ephemeral source port could not have been known in advance.A send before the peer is known is a no-op — not an error, and not a queued frame. There is no address to send to and UDP has nowhere to hold it. The example asserts it rather than leaving it to be discovered.
The two counters mean different things.
dropped_rxis this node’s resources (RX-backend exhaustion — backpressure, never an OOM);malformed_rxis the peer’s fault (a datagram over the cap). UDP is connectionless, so neither tears anything down: the next datagram is served normally.Both sockets bind port 0. Real loopback sockets on kernel-chosen ports, the way
udp_testdoes, so nothing here can collide with what else is running.This target needs the UDP transport. It is built only when
LIBTRACER_TRANSPORT_UDPis 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 — on a DATAGRAM kind the frame boundary is the wire's own, so `udp`
9 * carries no framing layer at all: one datagram in, one whole frame out, and the
10 * MTU is therefore the frame size limit rather than a tuning knob.
11 *
12 * Every stream kind in the tree (`tcp`, `ws`, `quic`) has to invent message boundaries,
13 * because a stream has none. UDP already has them, so `udp_transport_t` adds nothing: a
14 * `send` is one `sendto`, an inbound datagram is one frame, and there is no reassembler,
15 * no length prefix and no partial-frame state anywhere in the kind. That is the whole
16 * difference between the two families, and it has two consequences worth seeing:
17 *
18 * - **The bound is hard past one datagram.** The unset cap is `kDefaultMaxFrame`, one
19 * Ethernet MTU; `:settings max_frame` may raise it up to `kMaxDatagram`, what a datagram can
20 * physically be, and no further. A frame larger than a datagram is not a UDP frame; that
21 * is what streaming kinds are for.
22 * - **The listener has no peer until one talks to it.** UDP is connectionless, so a
23 * listener-mode transport — constructed with no peer address — LEARNS its peer from the
24 * source address of the first inbound datagram. Until then `send` is a no-op, which is
25 * exactly what lets a config-created listener reply to a dialer whose ephemeral source
26 * port could not have been known in advance.
27 *
28 * The example runs two real UDP sockets on the loopback interface, the way `udp_test` does:
29 * both bind port 0, so the kernel picks the ports and nothing here can collide with
30 * whatever else is running on the machine.
31 *
32 * Needs the UDP transport (`LIBTRACER_TRANSPORT_UDP`, on by default). Runs under ctest as
33 * `example_net_udp_datagram`; returns non-zero on any failed check.
34 */
35
36#include <chrono>
37#include <condition_variable>
38#include <cstddef>
39#include <cstdint>
40#include <cstdio>
41#include <mutex>
42#include <span>
43#include <vector>
44
45#include "libtracer/transport_udp.hpp"
46
47namespace {
48
49using namespace std::chrono_literals;
50using tr::net::udp_transport_t;
51
52/** @brief Report expectation @p what and record a failure on @p ok. */
53void check(bool& ok, bool cond, const char* what) {
54 std::printf(" [%s] %s\n", cond ? "ok" : "FAIL", what);
55 ok = ok && cond;
56}
57
58/** @brief A thread-safe borrowed-span sink: the recv thread pushes, `main` waits. */
59class sink_t {
60 public:
61 /** @brief The receiver callback — copies the span, which dies when it returns. */
62 void operator()(std::span<const std::byte> frame) {
63 {
64 const std::lock_guard lock(m_);
65 frames_.emplace_back(frame.begin(), frame.end());
66 }
67 cv_.notify_all();
68 }
69
70 /** @brief Wait until at least @p n frames have landed, or @p budget expires. */
71 [[nodiscard]] bool wait_for(std::size_t n, std::chrono::milliseconds budget) {
72 std::unique_lock lock(m_);
73 return cv_.wait_for(lock, budget, [&] { return frames_.size() >= n; });
74 }
75
76 /** @brief How many frames have landed so far. */
77 [[nodiscard]] std::size_t count() const {
78 const std::lock_guard lock(m_);
79 return frames_.size();
80 }
81
82 /** @brief Frame @p i, by value. */
83 [[nodiscard]] std::vector<std::byte> at(std::size_t i) const {
84 const std::lock_guard lock(m_);
85 return frames_.at(i);
86 }
87
88 private:
89 mutable std::mutex m_;
90 std::condition_variable cv_;
91 std::vector<std::vector<std::byte>> frames_;
92};
93
94/** @brief @p n bytes counting up from @p seed — a stand-in for an encoded frame. */
95std::vector<std::byte> frame_of(std::size_t n, unsigned seed) {
96 std::vector<std::byte> f(n);
97 for (std::size_t i = 0; i < n; ++i) f[i] = static_cast<std::byte>(seed + i);
98 return f;
99}
100
101} // namespace
102
103int main() {
104 bool ok = true;
105
106 // The listener half: bind an ephemeral port, name NO peer. This is the shape a
107 // `role=listener` config creates, and the reason `send` has to tolerate having nobody to
108 // send to yet.
109 sink_t at_listener;
110 udp_transport_t listener(/*bind_port=*/0, /*peer_host=*/"", /*peer_port=*/0);
111 listener.set_receiver(at_listener);
112 check(ok, listener.ok(), "the listener socket bound");
113 const std::uint16_t port = listener.local_port();
114 check(ok, port != 0, "local_port() resolved the ephemeral 0");
115
116 // A send before any peer is known is a NO-OP, not an error and not a queued frame: there
117 // is no address to send to, and UDP has nowhere to hold it.
118 listener.send(frame_of(4, 0x01));
119 check(ok, at_listener.count() == 0, "a send with no peer learned yet went nowhere");
120
121 // The dialer half: also an ephemeral local port, but with the peer named up front.
122 sink_t at_dialer;
123 udp_transport_t dialer(/*bind_port=*/0, "127.0.0.1", port);
124 dialer.set_receiver(at_dialer);
125 check(ok, dialer.ok(), "the dialer socket bound");
126
127 // One datagram, one frame. No prefix is written and none is stripped — what the sender
128 // handed to `send` is what the receiver's callback sees, and its LENGTH came from the
129 // datagram rather than from anything in the bytes.
130 std::printf("one datagram is one frame:\n");
131 const auto f1 = frame_of(9, 0x10);
132 dialer.send(f1);
133 check(ok, at_listener.wait_for(1, 2s), "the datagram arrived as one whole frame");
134 check(ok, at_listener.at(0) == f1, "byte-identical, with no framing bytes added");
135
136 // Two sends are two datagrams and therefore two frames — never one coalesced read the
137 // receiver has to split. This is the property a stream kind has to reconstruct.
138 const auto f2 = frame_of(3, 0x20);
139 const auto f3 = frame_of(11, 0x30);
140 dialer.send(f2);
141 dialer.send(f3);
142 check(ok, at_listener.wait_for(3, 2s), "two more sends arrived as exactly two more frames");
143 check(ok, at_listener.at(1) == f2 && at_listener.at(2) == f3,
144 "…each with its own boundary, in order");
145
146 // The listener has now heard from the dialer, so it knows where to reply — learned from
147 // the datagram's source address, with nothing stored per request.
148 std::printf("the peer is learned from ingress:\n");
149 const auto reply = frame_of(7, 0x40);
150 listener.send(reply);
151 check(ok, at_dialer.wait_for(1, 2s), "the listener could reply once it had heard a peer");
152 check(ok, at_dialer.at(0) == reply, "…and the reply is byte-identical too");
153
154 // The bound is the datagram's: `max_frame` never raises it past kMaxDatagram.
155 check(ok, listener.effective_max_frame() <= udp_transport_t::kMaxDatagram,
156 "the receive cap never exceeds what a datagram can carry");
157 check(ok, listener.drop_stats().malformed_rx == 0 && listener.drop_stats().dropped_rx == 0,
158 "nothing was refused and nothing was shed on this run");
159
160 std::printf("udp: %zu frames at the listener, %zu at the dialer, 0 bytes of framing\n",
161 at_listener.count(), at_dialer.count());
162 return ok ? 0 : 1;
163}
See also: transport module · connection config · module catalog reference · the stream family’s answer.