simple_repeater: per-origin advert window + fairness dry-run (tth #2, #1)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
tthandClaude Opus 5.5 committed 2026-11-02 09:40:20 +01:00
1 parent af1714db08
commit 910bee899a
7 files changed
+351 -11

No files matched your search

+40 -1
View File
@@ -18,6 +18,7 @@
#include <Packet.h>
#include <stdint.h>
#include "AdvertWindow.h"
// Sender hashes 0x00 and 0xFF are NOT special here. Our firmware never creates
// such an identity (re-roll on create, validatePrivateKey() on import), but the
@@ -85,6 +86,10 @@
* Until a V2 protocol provides new means of limiting, the goal is a "better than
* nothing" reduction in wasteful repeats, freeing airtime for useful repeats.
*/
// Why a packet was shed (lastShed()). WINDOW = per-origin advert window.
enum FairShed : uint8_t { FAIR_SHED_NONE = 0, FAIR_SHED_GROUP, FAIR_SHED_SENDER, FAIR_SHED_ADVERT, FAIR_SHED_WINDOW,
FAIR_SHED_N };
class FairnessLimiter {
// Capacity of when a bucket is full. Runtime-settable (setGroupCap etc.) so
// limits can be loosened over RF admin without a reflash -- 0 to a setter
@@ -104,6 +109,22 @@ class FairnessLimiter {
uint16_t sender_normal_deny[SENDER_NORMAL_MAP_SIZE] = {};
uint16_t sender_low_deny[SENDER_LOW_MAP_SIZE] = {};
// Per-origin advert window (issue #2), runs ahead of the advert bucket so a
// repeat advert never takes a token a legitimate one would need.
AdvertWindow advert_window;
uint32_t denied_window = 0;
// Buckets on/off (`fairness on|off`); the window has its own switch (hours).
bool buckets_enabled = true;
// Dry-run (issue #1): every verdict is counted as if enforced, but the packet
// is forwarded anyway -- sizes a setting on a live mast before it bites.
bool dry_run = false;
FairShed last_shed = FAIR_SHED_NONE;
// Estimated airtime of what was shed (or would have been, in dry-run), ms,
// per FairShed reason. Fed by the caller, which owns the radio.
uint32_t shed_air_ms[FAIR_SHED_N] = {};
bool shed(FairShed why) { last_shed = why; return dry_run; }
public:
FairnessLimiter() {}
@@ -120,7 +141,25 @@ public:
* would otherwise be forwarded, and only once per packet.
* \returns true if the packet should be forwarded
*/
bool allowPacket(const mesh::Packet* pkt);
bool allowPacket(const mesh::Packet* pkt, uint32_t now_ms);
// Without a clock: the advert window sees every call at t=0. For tests and
// callers that never enable the window.
bool allowPacket(const mesh::Packet* pkt) { return allowPacket(pkt, 0); }
// What the LAST allowPacket() call shed (FAIR_SHED_NONE = passed on its
// merits). In dry-run it is set although allowPacket() returned true.
FairShed lastShed() const { return last_shed; }
void addShedAirtime(FairShed why, uint32_t ms) { if (why < FAIR_SHED_N) shed_air_ms[why] += ms; }
uint32_t shedAirtimeMs(FairShed why) const { return why < FAIR_SHED_N ? shed_air_ms[why] : 0; }
void setBucketsEnabled(bool on) { buckets_enabled = on; }
bool bucketsEnabled() const { return buckets_enabled; }
void setDryRun(bool on) { dry_run = on; }
bool dryRun() const { return dry_run; }
AdvertWindow& advertWindow() { return advert_window; }
const AdvertWindow& advertWindow() const { return advert_window; }
uint32_t deniedWindow() const { return denied_window; }
// Inspect stats
uint32_t deniedSenderNormal() const { return denied_sender_normal; }