Skip to main content

srt_runtime/
presets.rs

1//! The `broadcast_core::Profile` presets of this crate's bounds. Every number is
2//! a documented constant used only by a `for_profile` body; the caller replaces
3//! any field through its typed `with_*` setter.
4
5use core::time::Duration;
6
7use crate::arq::{ArqBounds, ReceiverBounds, SenderBounds};
8use crate::handshake_sm::HandshakeConfig;
9#[cfg(feature = "tokio")]
10use crate::io::{ChannelCapacities, CloseTimings, IoConfig, ListenerLimits};
11#[cfg(feature = "link")]
12use crate::link::LinkCadence;
13use broadcast_core::Profile;
14#[cfg(feature = "tokio")]
15use core::num::NonZeroUsize;
16
17/// Embedded: 8192 packets, over 5 s of a 15 Mbit/s stream in the loss window. Measured (#4834): exercised on a real fixture by the Embedded harness.
18const RECEIVER_MAX_TRACKED_WINDOW_EMBEDDED: u32 = 1 << 13;
19/// Device: 32 768 packets, over 5 s of a 60 Mbit/s 4K stream.
20const RECEIVER_MAX_TRACKED_WINDOW_DEVICE: u32 = 1 << 15;
21/// Server: 262 144 packets, the window the origin always tracked.
22const RECEIVER_MAX_TRACKED_WINDOW_SERVER: u32 = 1 << 18;
23/// Headend: 262 144 packets, a contribution link's high-latency window.
24const RECEIVER_MAX_TRACKED_WINDOW_HEADEND: u32 = 1 << 18;
25
26/// Embedded: 64 queued too-late skips between drains. Measured (#4834): exercised on a real fixture by the Embedded harness.
27const RECEIVER_MAX_PENDING_SKIPS_EMBEDDED: usize = 64;
28/// Device: 256 queued too-late skips between drains.
29const RECEIVER_MAX_PENDING_SKIPS_DEVICE: usize = 256;
30/// Server: 1024 queued too-late skips, the value the origin always used.
31const RECEIVER_MAX_PENDING_SKIPS_SERVER: usize = 1024;
32/// Headend: 4096 queued too-late skips on a lossy contribution link.
33const RECEIVER_MAX_PENDING_SKIPS_HEADEND: usize = 4096;
34
35/// Embedded: 64 unanswered full ACKs, many RTTs of 10 ms ACKs. No run-time cost: a timing or retry count.
36const RECEIVER_MAX_OUTSTANDING_ACKS_EMBEDDED: usize = 64;
37/// Device: 256 unanswered full ACKs.
38const RECEIVER_MAX_OUTSTANDING_ACKS_DEVICE: usize = 256;
39/// Server: 1024 unanswered full ACKs, the value the origin always used.
40const RECEIVER_MAX_OUTSTANDING_ACKS_SERVER: usize = 1024;
41/// Headend: 4096 unanswered full ACKs on a long-haul link.
42const RECEIVER_MAX_OUTSTANDING_ACKS_HEADEND: usize = 4096;
43
44/// Embedded: 4096 sequence numbers expanded from one NAK. Measured (#4834): exercised on a real fixture by the Embedded harness.
45const SENDER_MAX_NAK_SEQS_PER_PACKET_EMBEDDED: usize = 1 << 12;
46/// Device: 16 384 sequence numbers expanded from one NAK.
47const SENDER_MAX_NAK_SEQS_PER_PACKET_DEVICE: usize = 1 << 14;
48/// Server: 65 536 sequence numbers expanded from one NAK, the value the origin always used.
49const SENDER_MAX_NAK_SEQS_PER_PACKET_SERVER: usize = 1 << 16;
50/// Headend: 65 536 sequence numbers expanded from one NAK.
51const SENDER_MAX_NAK_SEQS_PER_PACKET_HEADEND: usize = 1 << 16;
52
53/// Embedded: 40 ms between resends of one packet spares a slow uplink. No run-time cost: a timing or retry count.
54const SENDER_MIN_RETRANSMIT_INTERVAL_EMBEDDED: Duration = Duration::from_millis(40);
55/// Device: 30 ms between resends of one packet.
56const SENDER_MIN_RETRANSMIT_INTERVAL_DEVICE: Duration = Duration::from_millis(30);
57/// Server: 20 ms between resends of one packet, the value the origin always used.
58const SENDER_MIN_RETRANSMIT_INTERVAL_SERVER: Duration = Duration::from_millis(20);
59/// Headend: 20 ms between resends of one packet.
60const SENDER_MIN_RETRANSMIT_INTERVAL_HEADEND: Duration = Duration::from_millis(20);
61
62/// Embedded: 4 packets per pacing burst keep a small device's socket buffer short. Measured (#4834): exercised on a real fixture by the Embedded harness.
63const SENDER_PACING_BURST_PACKETS_EMBEDDED: usize = 4;
64/// Device: 8 packets per pacing burst.
65const SENDER_PACING_BURST_PACKETS_DEVICE: usize = 8;
66/// Server: 16 packets per pacing burst, the value the origin always used.
67const SENDER_PACING_BURST_PACKETS_SERVER: usize = 16;
68/// Headend: 32 packets per pacing burst for multi-hundred-megabit feeds.
69const SENDER_PACING_BURST_PACKETS_HEADEND: usize = 32;
70
71#[cfg(feature = "tokio")]
72mod listener_values {
73    use super::*;
74    /// Embedded: 16 handshakes in progress, a few callers at once. No run-time cost: a timing or retry count.
75    pub(super) const LISTENER_MAX_PENDING_EMBEDDED: usize = 16;
76    /// Device: 64 handshakes in progress.
77    pub(super) const LISTENER_MAX_PENDING_DEVICE: usize = 64;
78    /// Server: 1024 handshakes in progress, bounding a spoofed INDUCTION flood.
79    pub(super) const LISTENER_MAX_PENDING_SERVER: usize = 1024;
80    /// Headend: 4096 handshakes in progress across many contribution feeds.
81    pub(super) const LISTENER_MAX_PENDING_HEADEND: usize = 4096;
82
83    /// Embedded: expire and resend pending handshakes every 250 ms. No run-time cost: a timing or retry count.
84    pub(super) const LISTENER_PENDING_TICK_EMBEDDED: Duration = Duration::from_millis(250);
85    /// Device: expire and resend pending handshakes every 200 ms.
86    pub(super) const LISTENER_PENDING_TICK_DEVICE: Duration = Duration::from_millis(200);
87    /// Server: expire and resend pending handshakes every 100 ms.
88    pub(super) const LISTENER_PENDING_TICK_SERVER: Duration = Duration::from_millis(100);
89    /// Headend: expire and resend pending handshakes every 100 ms.
90    pub(super) const LISTENER_PENDING_TICK_HEADEND: Duration = Duration::from_millis(100);
91
92    /// Embedded: replace the SYN-cookie key every 120 s. No run-time cost: a timing or retry count.
93    pub(super) const LISTENER_COOKIE_KEY_ROTATION_EMBEDDED: Duration = Duration::from_secs(120);
94    /// Device: replace the SYN-cookie key every 90 s.
95    pub(super) const LISTENER_COOKIE_KEY_ROTATION_DEVICE: Duration = Duration::from_secs(90);
96    /// Server: replace the SYN-cookie key every 60 s, the cookie's one-minute time bucket.
97    pub(super) const LISTENER_COOKIE_KEY_ROTATION_SERVER: Duration = Duration::from_secs(60);
98    /// Headend: replace the SYN-cookie key every 60 s, the cookie's one-minute time bucket.
99    pub(super) const LISTENER_COOKIE_KEY_ROTATION_HEADEND: Duration = Duration::from_secs(60);
100
101    /// Embedded: answer a repeated CONCLUSION for 20 s. No run-time cost: a timing or retry count.
102    pub(super) const LISTENER_RECENT_ACCEPT_TTL_EMBEDDED: Duration = Duration::from_secs(20);
103    /// Device: answer a repeated CONCLUSION for 15 s.
104    pub(super) const LISTENER_RECENT_ACCEPT_TTL_DEVICE: Duration = Duration::from_secs(15);
105    /// Server: answer a repeated CONCLUSION for 10 s, longer than any caller's retry series.
106    pub(super) const LISTENER_RECENT_ACCEPT_TTL_SERVER: Duration = Duration::from_secs(10);
107    /// Headend: answer a repeated CONCLUSION for 10 s, longer than any caller's retry series.
108    pub(super) const LISTENER_RECENT_ACCEPT_TTL_HEADEND: Duration = Duration::from_secs(10);
109}
110#[cfg(feature = "tokio")]
111use listener_values::*;
112
113/// Embedded: 80 ms latency on a nearby LAN sender; the peer's larger value wins. No run-time cost: a timing or retry count.
114const HANDSHAKE_LATENCY_EMBEDDED: Duration = Duration::from_millis(80);
115/// Device: 120 ms latency, libsrt's default; the peer's larger value wins.
116const HANDSHAKE_LATENCY_DEVICE: Duration = Duration::from_millis(120);
117/// Server: 120 ms latency, libsrt's default; the peer's larger value wins.
118const HANDSHAKE_LATENCY_SERVER: Duration = Duration::from_millis(120);
119/// Headend: 120 ms latency, libsrt's default; the peer's larger value wins.
120const HANDSHAKE_LATENCY_HEADEND: Duration = Duration::from_millis(120);
121
122/// Embedded: a 1500-byte MTU carries the usual 1316-byte live payload. No run-time cost: a timing or retry count.
123const HANDSHAKE_MTU_EMBEDDED: u32 = 1500;
124/// Device: a 1500-byte Ethernet MTU, libsrt's default.
125const HANDSHAKE_MTU_DEVICE: u32 = 1500;
126/// Server: a 1500-byte Ethernet MTU, libsrt's default.
127const HANDSHAKE_MTU_SERVER: u32 = 1500;
128/// Headend: a 1500-byte Ethernet MTU, libsrt's default.
129const HANDSHAKE_MTU_HEADEND: u32 = 1500;
130
131/// Embedded: 1024 packets in flight, a LAN round trip at tens of megabits. Measured (#4834): exercised on a real fixture by the Embedded harness.
132const HANDSHAKE_FLOW_WINDOW_EMBEDDED: u32 = 1024;
133/// Device: 4096 packets in flight.
134const HANDSHAKE_FLOW_WINDOW_DEVICE: u32 = 4096;
135/// Server: 8192 packets in flight, libsrt's default flow window.
136const HANDSHAKE_FLOW_WINDOW_SERVER: u32 = 8192;
137/// Headend: 8192 packets in flight, libsrt's default flow window.
138const HANDSHAKE_FLOW_WINDOW_HEADEND: u32 = 8192;
139
140/// Embedded: resend an unanswered handshake packet every 250 ms. No run-time cost: a timing or retry count.
141const HANDSHAKE_RETRANSMIT_AFTER_EMBEDDED: Duration = Duration::from_micros(250_000);
142/// Device: resend an unanswered handshake packet every 125 ms.
143const HANDSHAKE_RETRANSMIT_AFTER_DEVICE: Duration = Duration::from_micros(125_000);
144/// Server: resend an unanswered handshake packet every 83.333 ms, 12 in about 1 s.
145const HANDSHAKE_RETRANSMIT_AFTER_SERVER: Duration = Duration::from_micros(83_333);
146/// Headend: resend an unanswered handshake packet every 83.333 ms, 12 in about 1 s.
147const HANDSHAKE_RETRANSMIT_AFTER_HEADEND: Duration = Duration::from_micros(83_333);
148
149/// Embedded: 6 retransmissions, about a 1.5 s connect budget. No run-time cost: a timing or retry count.
150const HANDSHAKE_MAX_RETRIES_EMBEDDED: u32 = 6;
151/// Device: 8 retransmissions, about a 1 s connect budget.
152const HANDSHAKE_MAX_RETRIES_DEVICE: u32 = 8;
153/// Server: 12 retransmissions, about a 1 s connect budget.
154const HANDSHAKE_MAX_RETRIES_SERVER: u32 = 12;
155/// Headend: 12 retransmissions, about a 1 s connect budget.
156const HANDSHAKE_MAX_RETRIES_HEADEND: u32 = 12;
157
158#[cfg(feature = "link")]
159/// Every profile: resend an unanswered handshake request every 250 ms, libsrt's period.
160const CADENCE_HANDSHAKE_RETRANSMIT: Duration = Duration::from_millis(250);
161#[cfg(feature = "link")]
162/// Every profile: a Keep-Alive after 1 s without a packet, libsrt's period.
163const CADENCE_KEEPALIVE: Duration = Duration::from_secs(1);
164#[cfg(feature = "link")]
165/// Every profile: resend an unconfirmed refresh KMREQ every 500 ms.
166const CADENCE_KM_RESEND: Duration = Duration::from_millis(500);
167#[cfg(feature = "link")]
168/// Every profile: process a peer's Key Material at most every 100 ms, bounding PBKDF2 work.
169const CADENCE_KMREQ_MIN_INTERVAL: Duration = Duration::from_millis(100);
170
171#[cfg(feature = "tokio")]
172mod io_values {
173    use super::*;
174    /// Every profile: a 1500-byte datagram, the largest a 1500-byte MTU link carries.
175    pub(super) const IO_MAX_DATAGRAM: usize = 1500;
176    /// Embedded: 5 s to resolve and bind. No run-time cost: a timing or retry count.
177    pub(super) const IO_CONNECT_TIMEOUT_EMBEDDED: Duration = Duration::from_secs(5);
178    /// Device: 8 s to resolve and bind on a mobile network.
179    pub(super) const IO_CONNECT_TIMEOUT_DEVICE: Duration = Duration::from_secs(8);
180    /// Server: 10 s to resolve and bind, the value the origin always used.
181    pub(super) const IO_CONNECT_TIMEOUT_SERVER: Duration = Duration::from_secs(10);
182    /// Headend: 10 s to resolve and bind.
183    pub(super) const IO_CONNECT_TIMEOUT_HEADEND: Duration = Duration::from_secs(10);
184    /// Embedded: 3 s for the caller handshake, libsrt's connect timeout. No run-time cost: a timing or retry count.
185    pub(super) const IO_HANDSHAKE_TIMEOUT_EMBEDDED: Duration = Duration::from_secs(3);
186    /// Device: 3 s for the caller handshake, libsrt's connect timeout.
187    pub(super) const IO_HANDSHAKE_TIMEOUT_DEVICE: Duration = Duration::from_secs(3);
188    /// Server: 5 s for the caller handshake, the value the origin always used.
189    pub(super) const IO_HANDSHAKE_TIMEOUT_SERVER: Duration = Duration::from_secs(5);
190    /// Headend: 5 s for a long-haul handshake.
191    pub(super) const IO_HANDSHAKE_TIMEOUT_HEADEND: Duration = Duration::from_secs(5);
192    /// Embedded: 5 s of peer silence, libsrt's peer idle timeout. No run-time cost: a timing or retry count.
193    pub(super) const IO_READ_IDLE_TIMEOUT_EMBEDDED: Duration = Duration::from_secs(5);
194    /// Device: 5 s of peer silence, libsrt's peer idle timeout.
195    pub(super) const IO_READ_IDLE_TIMEOUT_DEVICE: Duration = Duration::from_secs(5);
196    /// Server: 5 s of peer silence, the value the origin always used.
197    pub(super) const IO_READ_IDLE_TIMEOUT_SERVER: Duration = Duration::from_secs(5);
198    /// Headend: 10 s of peer silence on a sparse contribution link.
199    pub(super) const IO_READ_IDLE_TIMEOUT_HEADEND: Duration = Duration::from_secs(10);
200    /// Embedded: 5 s for one send. No run-time cost: a timing or retry count.
201    pub(super) const IO_WRITE_TIMEOUT_EMBEDDED: Duration = Duration::from_secs(5);
202    /// Device: 5 s for one send.
203    pub(super) const IO_WRITE_TIMEOUT_DEVICE: Duration = Duration::from_secs(5);
204    /// Server: 5 s for one send, the value the origin always used.
205    pub(super) const IO_WRITE_TIMEOUT_SERVER: Duration = Duration::from_secs(5);
206    /// Headend: 10 s for one send on a congested egress.
207    pub(super) const IO_WRITE_TIMEOUT_HEADEND: Duration = Duration::from_secs(10);
208    /// Embedded: 256 packets per drift-sampling window. Measured (#4834): exercised on a real fixture by the Embedded harness.
209    pub(super) const IO_DRIFT_SAMPLE_COUNT_EMBEDDED: u32 = 256;
210    /// Device: 512 packets per drift-sampling window.
211    pub(super) const IO_DRIFT_SAMPLE_COUNT_DEVICE: u32 = 512;
212    /// Server: 1000 packets per drift-sampling window, the value the origin always used.
213    pub(super) const IO_DRIFT_SAMPLE_COUNT_SERVER: u32 = 1000;
214    /// Headend: 2000 packets per drift-sampling window.
215    pub(super) const IO_DRIFT_SAMPLE_COUNT_HEADEND: u32 = 2000;
216    /// Every profile: pace DATA to 1 Gbit/s, libsrt's default `MAXBW`.
217    pub(super) const IO_MAX_BW: crate::livecc::MaxBwConfig =
218        crate::livecc::MaxBwConfig::Set(125_000_000);
219    /// Embedded: 512 inbound datagrams queued per connection. Measured (#4834): exercised on a real fixture by the Embedded harness.
220    pub(super) const CHANNEL_RX_EMBEDDED: NonZeroUsize = non_zero(512);
221    /// Device: 1024 inbound datagrams queued per connection.
222    pub(super) const CHANNEL_RX_DEVICE: NonZeroUsize = non_zero(1024);
223    /// Server: 4096 inbound datagrams, the value the origin always used.
224    pub(super) const CHANNEL_RX_SERVER: NonZeroUsize = non_zero(4096);
225    /// Headend: 16 384 inbound datagrams queued per connection.
226    pub(super) const CHANNEL_RX_HEADEND: NonZeroUsize = non_zero(16_384);
227    /// Embedded: 128 unrouted datagrams queued; provisional until measured (#4834): only `SrtListener::bind` reads it; the Embedded harness drives a rendezvous, not a listener.
228    pub(super) const CHANNEL_UNROUTED_EMBEDDED: NonZeroUsize = non_zero(128);
229    /// Device: 256 unrouted datagrams queued.
230    pub(super) const CHANNEL_UNROUTED_DEVICE: NonZeroUsize = non_zero(256);
231    /// Server: 1024 unrouted datagrams, the value the origin always used.
232    pub(super) const CHANNEL_UNROUTED_SERVER: NonZeroUsize = non_zero(1024);
233    /// Headend: 4096 unrouted datagrams queued.
234    pub(super) const CHANNEL_UNROUTED_HEADEND: NonZeroUsize = non_zero(4096);
235    /// Embedded: 128 payloads queued to send. Measured (#4834): exercised on a real fixture by the Embedded harness.
236    pub(super) const CHANNEL_SEND_EMBEDDED: NonZeroUsize = non_zero(128);
237    /// Device: 256 payloads queued to send.
238    pub(super) const CHANNEL_SEND_DEVICE: NonZeroUsize = non_zero(256);
239    /// Server: 1024 payloads queued to send, the value the origin always used.
240    pub(super) const CHANNEL_SEND_SERVER: NonZeroUsize = non_zero(1024);
241    /// Headend: 4096 payloads queued to send.
242    pub(super) const CHANNEL_SEND_HEADEND: NonZeroUsize = non_zero(4096);
243    /// Embedded: 512 delivered items awaiting the application. Measured (#4834): exercised on a real fixture by the Embedded harness.
244    pub(super) const CHANNEL_DELIVER_EMBEDDED: NonZeroUsize = non_zero(512);
245    /// Device: 1024 delivered items awaiting the application.
246    pub(super) const CHANNEL_DELIVER_DEVICE: NonZeroUsize = non_zero(1024);
247    /// Server: 4096 delivered items, the value the origin always used.
248    pub(super) const CHANNEL_DELIVER_SERVER: NonZeroUsize = non_zero(4096);
249    /// Headend: 16 384 delivered items awaiting the application.
250    pub(super) const CHANNEL_DELIVER_HEADEND: NonZeroUsize = non_zero(16_384);
251    /// Embedded: 8 finished handshakes awaiting `accept`; provisional until measured (#4834): only `SrtListener::bind` reads it; the Embedded harness drives a rendezvous, not a listener.
252    pub(super) const CHANNEL_ACCEPT_EMBEDDED: NonZeroUsize = non_zero(8);
253    /// Device: 16 finished handshakes awaiting `accept`.
254    pub(super) const CHANNEL_ACCEPT_DEVICE: NonZeroUsize = non_zero(16);
255    /// Server: 64 finished handshakes, the value the origin always used.
256    pub(super) const CHANNEL_ACCEPT_SERVER: NonZeroUsize = non_zero(64);
257    /// Headend: 256 finished handshakes awaiting `accept`.
258    pub(super) const CHANNEL_ACCEPT_HEADEND: NonZeroUsize = non_zero(256);
259
260    /// `n` as a [`NonZeroUsize`], or the smallest one for `0`, so a preset
261    /// constant is written as a plain count.
262    const fn non_zero(n: usize) -> NonZeroUsize {
263        match NonZeroUsize::new(n) {
264            Some(n) => n,
265            None => NonZeroUsize::MIN,
266        }
267    }
268}
269#[cfg(feature = "tokio")]
270use io_values::*;
271
272impl ReceiverBounds {
273    /// The [`Profile`] preset, replaceable through its `with_*` setters.
274    #[must_use]
275    pub fn for_profile(profile: Profile) -> Self {
276        Self {
277            max_tracked_window: profile.select(
278                RECEIVER_MAX_TRACKED_WINDOW_EMBEDDED,
279                RECEIVER_MAX_TRACKED_WINDOW_DEVICE,
280                RECEIVER_MAX_TRACKED_WINDOW_SERVER,
281                RECEIVER_MAX_TRACKED_WINDOW_HEADEND,
282            ),
283            max_pending_skips: profile.select(
284                RECEIVER_MAX_PENDING_SKIPS_EMBEDDED,
285                RECEIVER_MAX_PENDING_SKIPS_DEVICE,
286                RECEIVER_MAX_PENDING_SKIPS_SERVER,
287                RECEIVER_MAX_PENDING_SKIPS_HEADEND,
288            ),
289            max_outstanding_acks: profile.select(
290                RECEIVER_MAX_OUTSTANDING_ACKS_EMBEDDED,
291                RECEIVER_MAX_OUTSTANDING_ACKS_DEVICE,
292                RECEIVER_MAX_OUTSTANDING_ACKS_SERVER,
293                RECEIVER_MAX_OUTSTANDING_ACKS_HEADEND,
294            ),
295        }
296    }
297
298    /// Replace [`Self::max_tracked_window`].
299    #[must_use]
300    pub fn with_max_tracked_window(mut self, value: u32) -> Self {
301        self.max_tracked_window = value;
302        self
303    }
304
305    /// Replace [`Self::max_pending_skips`].
306    #[must_use]
307    pub fn with_max_pending_skips(mut self, value: usize) -> Self {
308        self.max_pending_skips = value;
309        self
310    }
311
312    /// Replace [`Self::max_outstanding_acks`].
313    #[must_use]
314    pub fn with_max_outstanding_acks(mut self, value: usize) -> Self {
315        self.max_outstanding_acks = value;
316        self
317    }
318}
319
320impl SenderBounds {
321    /// The [`Profile`] preset, replaceable through its `with_*` setters.
322    #[must_use]
323    pub fn for_profile(profile: Profile) -> Self {
324        Self {
325            max_nak_seqs_per_packet: profile.select(
326                SENDER_MAX_NAK_SEQS_PER_PACKET_EMBEDDED,
327                SENDER_MAX_NAK_SEQS_PER_PACKET_DEVICE,
328                SENDER_MAX_NAK_SEQS_PER_PACKET_SERVER,
329                SENDER_MAX_NAK_SEQS_PER_PACKET_HEADEND,
330            ),
331            min_retransmit_interval: profile.select(
332                SENDER_MIN_RETRANSMIT_INTERVAL_EMBEDDED,
333                SENDER_MIN_RETRANSMIT_INTERVAL_DEVICE,
334                SENDER_MIN_RETRANSMIT_INTERVAL_SERVER,
335                SENDER_MIN_RETRANSMIT_INTERVAL_HEADEND,
336            ),
337            pacing_burst_packets: profile.select(
338                SENDER_PACING_BURST_PACKETS_EMBEDDED,
339                SENDER_PACING_BURST_PACKETS_DEVICE,
340                SENDER_PACING_BURST_PACKETS_SERVER,
341                SENDER_PACING_BURST_PACKETS_HEADEND,
342            ),
343        }
344    }
345
346    /// Replace [`Self::max_nak_seqs_per_packet`].
347    #[must_use]
348    pub fn with_max_nak_seqs_per_packet(mut self, value: usize) -> Self {
349        self.max_nak_seqs_per_packet = value;
350        self
351    }
352
353    /// Replace [`Self::min_retransmit_interval`].
354    #[must_use]
355    pub fn with_min_retransmit_interval(mut self, value: Duration) -> Self {
356        self.min_retransmit_interval = value;
357        self
358    }
359
360    /// Replace [`Self::pacing_burst_packets`].
361    #[must_use]
362    pub fn with_pacing_burst_packets(mut self, value: usize) -> Self {
363        self.pacing_burst_packets = value;
364        self
365    }
366}
367
368impl ArqBounds {
369    /// The [`Profile`] preset, replaceable through its `with_*` setters.
370    #[must_use]
371    pub fn for_profile(profile: Profile) -> Self {
372        Self {
373            receiver: ReceiverBounds::for_profile(profile),
374            sender: SenderBounds::for_profile(profile),
375        }
376    }
377
378    /// Replace [`Self::receiver`].
379    #[must_use]
380    pub fn with_receiver(mut self, value: crate::arq::ReceiverBounds) -> Self {
381        self.receiver = value;
382        self
383    }
384
385    /// Replace [`Self::sender`].
386    #[must_use]
387    pub fn with_sender(mut self, value: crate::arq::SenderBounds) -> Self {
388        self.sender = value;
389        self
390    }
391}
392
393#[cfg(feature = "tokio")]
394impl ListenerLimits {
395    /// The [`Profile`] preset, replaceable through its `with_*` setters.
396    #[must_use]
397    pub fn for_profile(profile: Profile) -> Self {
398        Self {
399            max_pending: profile.select(
400                LISTENER_MAX_PENDING_EMBEDDED,
401                LISTENER_MAX_PENDING_DEVICE,
402                LISTENER_MAX_PENDING_SERVER,
403                LISTENER_MAX_PENDING_HEADEND,
404            ),
405            pending_tick: profile.select(
406                LISTENER_PENDING_TICK_EMBEDDED,
407                LISTENER_PENDING_TICK_DEVICE,
408                LISTENER_PENDING_TICK_SERVER,
409                LISTENER_PENDING_TICK_HEADEND,
410            ),
411            cookie_key_rotation: profile.select(
412                LISTENER_COOKIE_KEY_ROTATION_EMBEDDED,
413                LISTENER_COOKIE_KEY_ROTATION_DEVICE,
414                LISTENER_COOKIE_KEY_ROTATION_SERVER,
415                LISTENER_COOKIE_KEY_ROTATION_HEADEND,
416            ),
417            recent_accept_ttl: profile.select(
418                LISTENER_RECENT_ACCEPT_TTL_EMBEDDED,
419                LISTENER_RECENT_ACCEPT_TTL_DEVICE,
420                LISTENER_RECENT_ACCEPT_TTL_SERVER,
421                LISTENER_RECENT_ACCEPT_TTL_HEADEND,
422            ),
423        }
424    }
425
426    /// Replace [`Self::max_pending`].
427    #[must_use]
428    pub fn with_max_pending(mut self, value: usize) -> Self {
429        self.max_pending = value;
430        self
431    }
432
433    /// Replace [`Self::pending_tick`].
434    #[must_use]
435    pub fn with_pending_tick(mut self, value: Duration) -> Self {
436        self.pending_tick = value;
437        self
438    }
439
440    /// Replace [`Self::cookie_key_rotation`].
441    #[must_use]
442    pub fn with_cookie_key_rotation(mut self, value: Duration) -> Self {
443        self.cookie_key_rotation = value;
444        self
445    }
446
447    /// Replace [`Self::recent_accept_ttl`].
448    #[must_use]
449    pub fn with_recent_accept_ttl(mut self, value: Duration) -> Self {
450        self.recent_accept_ttl = value;
451        self
452    }
453}
454
455impl HandshakeConfig {
456    /// The [`Profile`] preset, built from this crate's feature set with no
457    /// stream id, group or encryption, replaceable through its `with_*` setters.
458    #[must_use]
459    pub fn for_profile(profile: Profile) -> Self {
460        Self::new(
461            profile.select(
462                HANDSHAKE_LATENCY_EMBEDDED,
463                HANDSHAKE_LATENCY_DEVICE,
464                HANDSHAKE_LATENCY_SERVER,
465                HANDSHAKE_LATENCY_HEADEND,
466            ),
467            profile.select(
468                HANDSHAKE_MTU_EMBEDDED,
469                HANDSHAKE_MTU_DEVICE,
470                HANDSHAKE_MTU_SERVER,
471                HANDSHAKE_MTU_HEADEND,
472            ),
473            profile.select(
474                HANDSHAKE_FLOW_WINDOW_EMBEDDED,
475                HANDSHAKE_FLOW_WINDOW_DEVICE,
476                HANDSHAKE_FLOW_WINDOW_SERVER,
477                HANDSHAKE_FLOW_WINDOW_HEADEND,
478            ),
479            profile.select(
480                HANDSHAKE_RETRANSMIT_AFTER_EMBEDDED,
481                HANDSHAKE_RETRANSMIT_AFTER_DEVICE,
482                HANDSHAKE_RETRANSMIT_AFTER_SERVER,
483                HANDSHAKE_RETRANSMIT_AFTER_HEADEND,
484            ),
485            profile.select(
486                HANDSHAKE_MAX_RETRIES_EMBEDDED,
487                HANDSHAKE_MAX_RETRIES_DEVICE,
488                HANDSHAKE_MAX_RETRIES_SERVER,
489                HANDSHAKE_MAX_RETRIES_HEADEND,
490            ),
491        )
492    }
493}
494
495#[cfg(feature = "link")]
496impl LinkCadence {
497    /// The [`Profile`] preset, replaceable through its `with_*` setters. The
498    /// cadence is a protocol rhythm, the same on every profile.
499    #[must_use]
500    pub fn for_profile(_profile: Profile) -> Self {
501        Self {
502            handshake_retransmit: CADENCE_HANDSHAKE_RETRANSMIT,
503            keepalive: CADENCE_KEEPALIVE,
504            km_resend: CADENCE_KM_RESEND,
505            kmreq_min_interval: CADENCE_KMREQ_MIN_INTERVAL,
506        }
507    }
508
509    /// Replace [`Self::handshake_retransmit`].
510    #[must_use]
511    pub fn with_handshake_retransmit(mut self, handshake_retransmit: Duration) -> Self {
512        self.handshake_retransmit = handshake_retransmit;
513        self
514    }
515
516    /// Replace [`Self::keepalive`].
517    #[must_use]
518    pub fn with_keepalive(mut self, keepalive: Duration) -> Self {
519        self.keepalive = keepalive;
520        self
521    }
522
523    /// Replace [`Self::km_resend`].
524    #[must_use]
525    pub fn with_km_resend(mut self, km_resend: Duration) -> Self {
526        self.km_resend = km_resend;
527        self
528    }
529
530    /// Replace [`Self::kmreq_min_interval`].
531    #[must_use]
532    pub fn with_kmreq_min_interval(mut self, kmreq_min_interval: Duration) -> Self {
533        self.kmreq_min_interval = kmreq_min_interval;
534        self
535    }
536}
537
538#[cfg(feature = "tokio")]
539impl ChannelCapacities {
540    /// The [`Profile`] preset, replaceable through its `with_*` setters.
541    #[must_use]
542    pub fn for_profile(profile: Profile) -> Self {
543        Self {
544            rx: profile.select(
545                CHANNEL_RX_EMBEDDED,
546                CHANNEL_RX_DEVICE,
547                CHANNEL_RX_SERVER,
548                CHANNEL_RX_HEADEND,
549            ),
550            unrouted: profile.select(
551                CHANNEL_UNROUTED_EMBEDDED,
552                CHANNEL_UNROUTED_DEVICE,
553                CHANNEL_UNROUTED_SERVER,
554                CHANNEL_UNROUTED_HEADEND,
555            ),
556            send: profile.select(
557                CHANNEL_SEND_EMBEDDED,
558                CHANNEL_SEND_DEVICE,
559                CHANNEL_SEND_SERVER,
560                CHANNEL_SEND_HEADEND,
561            ),
562            deliver: profile.select(
563                CHANNEL_DELIVER_EMBEDDED,
564                CHANNEL_DELIVER_DEVICE,
565                CHANNEL_DELIVER_SERVER,
566                CHANNEL_DELIVER_HEADEND,
567            ),
568            accept: profile.select(
569                CHANNEL_ACCEPT_EMBEDDED,
570                CHANNEL_ACCEPT_DEVICE,
571                CHANNEL_ACCEPT_SERVER,
572                CHANNEL_ACCEPT_HEADEND,
573            ),
574        }
575    }
576
577    /// Replace [`Self::rx`].
578    #[must_use]
579    pub fn with_rx(mut self, rx: NonZeroUsize) -> Self {
580        self.rx = rx;
581        self
582    }
583
584    /// Replace [`Self::unrouted`].
585    #[must_use]
586    pub fn with_unrouted(mut self, unrouted: NonZeroUsize) -> Self {
587        self.unrouted = unrouted;
588        self
589    }
590
591    /// Replace [`Self::send`].
592    #[must_use]
593    pub fn with_send(mut self, send: NonZeroUsize) -> Self {
594        self.send = send;
595        self
596    }
597
598    /// Replace [`Self::deliver`].
599    #[must_use]
600    pub fn with_deliver(mut self, deliver: NonZeroUsize) -> Self {
601        self.deliver = deliver;
602        self
603    }
604
605    /// Replace [`Self::accept`].
606    #[must_use]
607    pub fn with_accept(mut self, accept: NonZeroUsize) -> Self {
608        self.accept = accept;
609        self
610    }
611}
612
613#[cfg(feature = "tokio")]
614impl IoConfig {
615    /// The [`Profile`] preset: every field stated, the listener, ARQ, cadence
616    /// and channel bounds from their own presets. Every preset passes
617    /// [`Self::validate`] (pinned by a test); the existing `with_*` setters and
618    /// the public fields replace any part.
619    #[must_use]
620    pub fn for_profile(profile: Profile) -> Self {
621        Self {
622            max_datagram: IO_MAX_DATAGRAM,
623            timeouts: broadcast_net::timeouts::Timeouts::new(
624                profile.select(
625                    IO_CONNECT_TIMEOUT_EMBEDDED,
626                    IO_CONNECT_TIMEOUT_DEVICE,
627                    IO_CONNECT_TIMEOUT_SERVER,
628                    IO_CONNECT_TIMEOUT_HEADEND,
629                ),
630                profile.select(
631                    IO_HANDSHAKE_TIMEOUT_EMBEDDED,
632                    IO_HANDSHAKE_TIMEOUT_DEVICE,
633                    IO_HANDSHAKE_TIMEOUT_SERVER,
634                    IO_HANDSHAKE_TIMEOUT_HEADEND,
635                ),
636                profile.select(
637                    IO_READ_IDLE_TIMEOUT_EMBEDDED,
638                    IO_READ_IDLE_TIMEOUT_DEVICE,
639                    IO_READ_IDLE_TIMEOUT_SERVER,
640                    IO_READ_IDLE_TIMEOUT_HEADEND,
641                ),
642                profile.select(
643                    IO_WRITE_TIMEOUT_EMBEDDED,
644                    IO_WRITE_TIMEOUT_DEVICE,
645                    IO_WRITE_TIMEOUT_SERVER,
646                    IO_WRITE_TIMEOUT_HEADEND,
647                ),
648            ),
649            km_refresh_thresholds: crate::km_refresh::KmRefreshThresholds::RECOMMENDED,
650            cadence: LinkCadence::for_profile(profile),
651            listener: ListenerLimits::for_profile(profile),
652            arq: ArqBounds::for_profile(profile),
653            drift_sample_count: profile.select(
654                IO_DRIFT_SAMPLE_COUNT_EMBEDDED,
655                IO_DRIFT_SAMPLE_COUNT_DEVICE,
656                IO_DRIFT_SAMPLE_COUNT_SERVER,
657                IO_DRIFT_SAMPLE_COUNT_HEADEND,
658            ),
659            channels: ChannelCapacities::for_profile(profile),
660            max_bw: IO_MAX_BW,
661            close: CloseTimings::for_profile(profile),
662        }
663    }
664}
665
666#[cfg(test)]
667mod tests {
668    use super::*;
669
670    #[test]
671    fn server_and_headend_literals_are_pinned() {
672        let arq = crate::arq::ArqBounds::for_profile(Profile::Server);
673        assert_eq!(arq.receiver.max_tracked_window, 1 << 18);
674        assert_eq!(arq.receiver.max_pending_skips, 1024);
675        assert_eq!(arq.receiver.max_outstanding_acks, 1024);
676        assert_eq!(arq.sender.max_nak_seqs_per_packet, 1 << 16);
677        assert_eq!(
678            arq.sender.min_retransmit_interval,
679            Duration::from_millis(20)
680        );
681        assert_eq!(arq.sender.pacing_burst_packets, 16);
682        let headend = crate::arq::ArqBounds::for_profile(Profile::Headend);
683        assert_eq!(headend.receiver.max_pending_skips, 4096);
684        assert_eq!(headend.sender.pacing_burst_packets, 32);
685        #[cfg(feature = "tokio")]
686        let listener = crate::io::ListenerLimits::for_profile(Profile::Server);
687        #[cfg(feature = "tokio")]
688        {
689            assert_eq!(listener.max_pending, 1024);
690            assert_eq!(listener.pending_tick, Duration::from_millis(100));
691            assert_eq!(listener.cookie_key_rotation, Duration::from_secs(60));
692            assert_eq!(listener.recent_accept_ttl, Duration::from_secs(10));
693            assert_eq!(
694                crate::io::ListenerLimits::for_profile(Profile::Headend).max_pending,
695                4096
696            );
697        }
698        let handshake = crate::handshake_sm::HandshakeConfig::for_profile(Profile::Server);
699        assert_eq!(handshake.latency, Duration::from_millis(120));
700        assert_eq!(handshake.mtu, 1500);
701        assert_eq!(handshake.max_flow_window_size, 8192);
702        assert_eq!(handshake.retransmit_after, Duration::from_micros(83_333));
703        assert_eq!(handshake.max_retries, 12);
704        assert_eq!(
705            crate::handshake_sm::HandshakeConfig::for_profile(Profile::Headend).mtu,
706            1500
707        );
708    }
709
710    #[cfg(feature = "tokio")]
711    #[test]
712    fn every_io_preset_validates_and_server_is_the_origin_value() {
713        for profile in Profile::ALL {
714            IoConfig::for_profile(profile)
715                .validate()
716                .unwrap_or_else(|e| panic!("{profile:?}: {e}"));
717        }
718        let server = IoConfig::for_profile(Profile::Server);
719        assert_eq!(server.max_datagram, 1500);
720        assert_eq!(server.drift_sample_count, 1000);
721        assert_eq!(server.timeouts.connect, Duration::from_secs(10));
722        assert_eq!(server.timeouts.write, Duration::from_secs(5));
723        assert_eq!(server.channels.rx.get(), 4096);
724        assert_eq!(server.channels.accept.get(), 64);
725        assert_eq!(server.max_bw, crate::livecc::MaxBwConfig::Set(125_000_000));
726        assert_eq!(server.cadence.keepalive, Duration::from_secs(1));
727        let rx = Profile::ALL.map(|p| ChannelCapacities::for_profile(p).rx);
728        assert!(rx.windows(2).all(|w| w[0] <= w[1]));
729    }
730
731    #[test]
732    fn capacities_grow_with_the_profile() {
733        let windows = Profile::ALL.map(|p| ReceiverBounds::for_profile(p).max_tracked_window);
734        assert!(windows.windows(2).all(|w| w[0] <= w[1]));
735        let skips = Profile::ALL.map(|p| ReceiverBounds::for_profile(p).max_pending_skips);
736        assert!(skips.windows(2).all(|w| w[0] <= w[1]));
737        #[cfg(feature = "tokio")]
738        {
739            let pending =
740                Profile::ALL.map(|p| crate::io::ListenerLimits::for_profile(p).max_pending);
741            assert!(pending.windows(2).all(|w| w[0] <= w[1]));
742        }
743    }
744}