Debian Codegraph @

src/rist-rtt-mute.h

raw symbols SCIP
1 /* librist. SPDX-License-Identifier: BSD-2-Clause
2 *
3 * RTT-based bonded-leg muting hysteresis state machine. Kept in a small
4 * standalone header so the transition logic can be unit-tested in isolation,
5 * without constructing full peer objects or starting sender threads.
6 */
7
8 #ifndef RIST_RTT_MUTE_H
9 #define RIST_RTT_MUTE_H
10
11 #include <stdbool.h>
12 #include <stdint.h>
13
14 enum rist_rtt_mute_action
15 {
16 RIST_RTT_MUTE_NONE = 0, /* no state change this step */
17 RIST_RTT_MUTE_DROP = 1, /* transition active -> muted */
18 RIST_RTT_MUTE_RESTORE = 2, /* transition muted -> active */
19 };
20
21 struct rist_rtt_mute_state
22 {
23 bool muted; /* leg currently out of the unique-payload rotation */
24 bool pending; /* a transition condition is currently held */
25 uint64_t pending_since; /* tick the pending condition first held */
26 };
27
28 /* Advance the hysteresis machine for one leg by one sample.
29 *
30 * All RTT/time arguments share one unit (RIST_CLOCK ticks):
31 * smoothed_rtt : the leg's smoothed (EWMA) round-trip time
32 * drop : ceiling; staying above it for drop_settle mutes the leg
33 * restore : low-water; staying below it for restore_settle restores it
34 * drop_settle : dwell required before muting (active -> muted)
35 * restore_settle: dwell required before rejoining (muted -> active)
36 * now : monotonic tick
37 *
38 * The dwell is deliberately asymmetric (drop_settle short, restore_settle
39 * longer) so a bad leg is pulled quickly but a still-marginal one does not flap
40 * back in; the threshold split (restore < drop) is the spatial half of the
41 * hysteresis. A drop of 0 disables muting. Returns the transition taken this
42 * step (NONE if unchanged) and updates *st in place. */
43 static inline enum rist_rtt_mute_action
44 rist_rtt_mute_step(struct rist_rtt_mute_state *st, uint64_t smoothed_rtt,
45 uint64_t drop, uint64_t restore,
46 uint64_t drop_settle, uint64_t restore_settle, uint64_t now)
47 {
48 if (drop == 0) {
49 st->pending = false;
50 if (st->muted) {
51 st->muted = false;
52 return RIST_RTT_MUTE_RESTORE;
53 }
54 return RIST_RTT_MUTE_NONE;
55 }
56
57 bool condition = st->muted ? (smoothed_rtt < restore)
58 : (smoothed_rtt > drop);
59 if (!condition) {
60 st->pending = false;
61 return RIST_RTT_MUTE_NONE;
62 }
63 if (!st->pending) {
64 st->pending = true;
65 st->pending_since = now;
66 return RIST_RTT_MUTE_NONE;
67 }
68 uint64_t settle = st->muted ? restore_settle : drop_settle;
69 if (now - st->pending_since < settle)
70 return RIST_RTT_MUTE_NONE;
71
72 st->pending = false;
73 if (st->muted) {
74 st->muted = false;
75 return RIST_RTT_MUTE_RESTORE;
76 }
77 st->muted = true;
78 return RIST_RTT_MUTE_DROP;
79 }
80
81 /* Should the sole-carrier role move from the incumbent to a challenger?
82 *
83 * When every leg wants muting, one has to keep carrying. Choosing the leg with
84 * the lowest RTT each tick sounds right but ping-pongs the payload between two
85 * equally bad legs, which is worse for the stream than committing to either, so
86 * hand over only when the challenger measures margin times better and the
87 * incumbent has served at least held_min. */
88 static inline bool
89 rist_rtt_sole_carrier_handover(uint64_t incumbent_rtt, uint64_t challenger_rtt,
90 uint64_t held_for, uint64_t held_min, unsigned margin)
91 {
92 if (held_for < held_min)
93 return false;
94 return challenger_rtt * margin <= incumbent_rtt;
95 }
96
97 /* Is a trickle on a muted leg still worth sending?
98 *
99 * The trickle keeps a muted leg carrying a little payload so a restore starts
100 * warm. Once the leg is queued deeper than the receiver's buffer, nothing sent
101 * on it can arrive in time to be output, and the arrivals do harm: they are old
102 * enough to be the only candidate the receiver has to rebuild its baseline on
103 * after a reset, which then rewinds it. Judge by the one-way delay, half the
104 * round trip. A buffer of 0 means unknown, so keep trickling. */
105 static inline bool
106 rist_rtt_trickle_useful(uint64_t smoothed_rtt, uint64_t buffer)
107 {
108 if (!buffer)
109 return true;
110 return (smoothed_rtt / 2) <= buffer;
111 }
112
113 /* Share of the configured weight a rejoining leg should carry, ramped linearly
114 * to full over ramp ticks since it was restored.
115 *
116 * A muted leg drains, so it measures well right up until it carries traffic
117 * again; handing back the full share at once refloods the queue and mutes it
118 * moments later. Ramping lets the leg reveal congestion under a rising load.
119 * ramp_start of 0 (never muted) or an elapsed ramp both yield full weight, and
120 * a ramping leg always keeps at least 1 so it never falls out entirely. */
121 static inline uint32_t
122 rist_rtt_ramped_weight(uint32_t weight, uint64_t ramp_start, uint64_t ramp, uint64_t now)
123 {
124 if (!ramp_start || !ramp || weight <= 1)
125 return weight;
126 if (now <= ramp_start || (now - ramp_start) >= ramp)
127 return weight;
128 uint32_t scaled = (uint32_t)((uint64_t)weight * (now - ramp_start) / ramp);
129 return scaled ? scaled : 1;
130 }
131
132 #endif /* RIST_RTT_MUTE_H */