| 1 | /* librist. SPDX-License-Identifier: BSD-2-Clause |
| 2 | * |
| 3 | * Whether a packet may serve as the new baseline for a flow whose buffer has |
| 4 | * just been emptied. Kept in a small standalone header so the decision can be |
| 5 | * unit-tested without a flow, a peer or a receiver thread. |
| 6 | */ |
| 7 | |
| 8 | #ifndef RIST_REANCHOR_H |
| 9 | #define RIST_REANCHOR_H |
| 10 | |
| 11 | #include <stdint.h> |
| 12 | |
| 13 | enum rist_reanchor_action |
| 14 | { |
| 15 | RIST_REANCHOR_OK = 0, /* current enough; anchor the flow on it */ |
| 16 | RIST_REANCHOR_WAIT = 1, /* too far behind; drop it and wait */ |
| 17 | RIST_REANCHOR_FORCED = 2, /* too far behind, but nothing better is coming */ |
| 18 | }; |
| 19 | |
| 20 | /* Anchoring adopts a packet's sequence number and clock as the flow baseline, |
| 21 | * so it has to come from a leg that is current. A bonded leg queued seconds |
| 22 | * deep keeps delivering packets that old, and anchoring on one rewinds the |
| 23 | * baseline by its lag: the next healthy packet then looks like a multi-second |
| 24 | * jump, which empties the buffer again and can repeat until the stream dies. |
| 25 | * |
| 26 | * A packet more than buffer behind the highest source time the flow has seen |
| 27 | * cannot be output anyway, so it is never a good baseline. Waiting for a better |
| 28 | * one is bounded at one buffer window, after which there is no current leg left |
| 29 | * to wait for -- or the source itself restarted on a lower clock -- and the |
| 30 | * packet in hand is taken (FORCED). |
| 31 | * |
| 32 | * max_source_time or buffer of 0 means there is no baseline to compare against |
| 33 | * yet (first packet of a flow, or just after a clock wrap): anchor freely. |
| 34 | * waiting_since is when the flow first refused a candidate, 0 if it has not. |
| 35 | * All times share one unit (RIST_CLOCK ticks). */ |
| 36 | static inline enum rist_reanchor_action |
| 37 | rist_flow_reanchor_check(uint64_t source_time, uint64_t max_source_time, |
| 38 | uint64_t buffer, uint64_t waiting_since, uint64_t now) |
| 39 | { |
| 40 | if (!max_source_time || !buffer) |
| 41 | return RIST_REANCHOR_OK; |
| 42 | if ((source_time + buffer) >= max_source_time) |
| 43 | return RIST_REANCHOR_OK; |
| 44 | if (waiting_since && (now - waiting_since) >= buffer) |
| 45 | return RIST_REANCHOR_FORCED; |
| 46 | return RIST_REANCHOR_WAIT; |
| 47 | } |
| 48 | |
| 49 | #endif /* RIST_REANCHOR_H */ |