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

+84
View File
@@ -0,0 +1,84 @@
#pragma once
// CUSTOM (TeTeHacko): per-origin advert window, ported from DutchMeshCore's
// AdvertLimiter (Dutch-MeshCore/MeshCore d26421b0, examples/simple_repeater/
// AdvertLimiter.h; same MIT licence as this tree). Behaviour is theirs, names
// and the eviction counter are ours.
//
// Each origin's flood advert is forwarded at most once per window, so a node
// that re-advertises every minute (CP1 on the CZ mesh, since 30. 10. 2026:
// a flood advert every 60 s, seen by 40-90 observers each) stops being
// re-flooded by this repeater while it still stays reachable through it once
// per window. FairnessLimiter's advert bucket cannot do that:
// it is keyed on 7 bits of the key, so the chatty node and ~8 innocent ones
// share a bucket, and loosening it for them lets the chatty one through.
//
// Origins are keyed on the first 4 bytes of the advert's public key (payload
// starts with it). Fixed ring of ADVERT_WINDOW_SIZE entries: once full, the
// oldest entry is overwritten -- that origin gets a free pass next time. RAM
// only, so after a reboot every origin gets one free pass. Time is millis and
// compared with unsigned subtraction, so it survives the 49-day wrap.
// Header-only and dependency-free so it runs in the native tests.
#include <stdint.h>
#include <string.h>
#ifndef ADVERT_WINDOW_SIZE
#define ADVERT_WINDOW_SIZE 256 // 8 B each => 2 KB RAM
#endif
#define ADVERT_WINDOW_KEY_LEN 4
#define ADVERT_WINDOW_MAX_HOURS 720 // 30 days; keeps hours*3600*1000 inside uint32
class AdvertWindow {
struct Entry {
uint8_t key[ADVERT_WINDOW_KEY_LEN];
uint32_t seen_ms;
};
Entry _cache[ADVERT_WINDOW_SIZE];
uint16_t _count = 0; // entries in use
uint16_t _head = 0; // next entry to overwrite once the cache is full
uint16_t _hours = 0; // 0 = off
uint32_t _evicted = 0; // origins forgotten because the ring was full
public:
AdvertWindow() { memset(_cache, 0, sizeof(_cache)); }
void setHours(uint16_t hours) { _hours = hours > ADVERT_WINDOW_MAX_HOURS ? ADVERT_WINDOW_MAX_HOURS : hours; }
uint16_t hours() const { return _hours; }
int count() const { return _count; }
int capacity() const { return ADVERT_WINDOW_SIZE; }
// Non-zero = the ring is too small for the mesh: origins fall out before
// their window ends and get a pass they should not have had.
uint32_t evicted() const { return _evicted; }
void clear() { _count = 0; _head = 0; }
// True when the advert from `key` may be forwarded now (and records it),
// false when the same origin already passed inside the current window.
bool allow(const uint8_t* key, uint32_t now_ms) {
if (_hours == 0 || key == nullptr) return true;
uint32_t window_ms = (uint32_t)_hours * 3600UL * 1000UL;
for (int i = 0; i < _count; i++) {
Entry& e = _cache[i];
if (memcmp(e.key, key, ADVERT_WINDOW_KEY_LEN) != 0) continue;
if (now_ms - e.seen_ms < window_ms) return false; // unsigned: wrap-safe
e.seen_ms = now_ms; // window elapsed: pass, and start a new window
return true;
}
Entry* e;
if (_count < ADVERT_WINDOW_SIZE) {
e = &_cache[_count++];
} else {
e = &_cache[_head]; // overwrite the oldest entry
_head = (uint16_t)((_head + 1) % ADVERT_WINDOW_SIZE);
_evicted++;
}
memcpy(e->key, key, ADVERT_WINDOW_KEY_LEN);
e->seen_ms = now_ms;
return true;
}
};
+4
View File
@@ -89,6 +89,8 @@ public:
// FairnessLimiter runtime tuning; 0 = use the firmware build default.
uint8_t fair_group_cap = 0, fair_sender_cap = 0, fair_advert_cap = 0;
uint16_t fair_group_refill_s = 0, fair_sender_refill_s = 0, fair_advert_refill_s = 0;
uint16_t fair_advert_hours = 0; // per-origin advert window, hours; 0 = off
uint8_t fair_dry_run = 0; // count FairnessLimiter verdicts, forward anyway
private:
class RadioPrefs : public CommonRadioPrefs {
@@ -216,6 +218,8 @@ private:
def("fair_grf", _parent->fair_group_refill_s);
def("fair_srf", _parent->fair_sender_refill_s);
def("fair_arf", _parent->fair_advert_refill_s);
def("fair_advh", _parent->fair_advert_hours);
def("fair_dry", _parent->fair_dry_run);
}
public:
RepeatPrefs(NodePrefs* parent) : _parent(parent) { }
+20 -4
View File
@@ -94,7 +94,8 @@ void FairnessLimiter::refillSenderLow() {
}
}
bool FairnessLimiter::allowPacket(const mesh::Packet* pkt) {
bool FairnessLimiter::allowPacket(const mesh::Packet* pkt, uint32_t now_ms) {
last_shed = FAIR_SHED_NONE;
// Don't limit versions we don't understand.
if (pkt->getPayloadVer() != PAYLOAD_VER_1) {
return true;
@@ -110,11 +111,26 @@ bool FairnessLimiter::allowPacket(const mesh::Packet* pkt) {
uint8_t hash[1] = { 0x00 };
auto type = pkt->getPayloadType();
// Per-origin advert window -- flood adverts only, ahead of the buckets. The
// payload starts with the origin's public key.
if (type == PAYLOAD_TYPE_ADVERT && pkt->isRouteFlood() && pkt->payload_len >= ADVERT_WINDOW_KEY_LEN
&& !advert_window.allow(pkt->payload, now_ms)) {
denied_window++;
MESH_DEBUG_PRINTLN("FAIR, advert window origin=%02X%02X%02X%02X dry=%u", (uint32_t)pkt->payload[0],
(uint32_t)pkt->payload[1], (uint32_t)pkt->payload[2], (uint32_t)pkt->payload[3],
(uint32_t)dry_run);
return shed(FAIR_SHED_WINDOW);
}
if (!buckets_enabled) {
return true;
}
// Packets by group channel hash
if (type == PAYLOAD_TYPE_GRP_TXT || type == PAYLOAD_TYPE_GRP_DATA) {
if (pkt->payload_len < 1) return true; // malformed, not ours to judge
hash[0] = pkt->payload[0];
return takeGroup(hash);
return takeGroup(hash) || shed(FAIR_SHED_GROUP);
}
// Packets with a plaintext 1-byte src hash at payload[1] (see Mesh.cpp,
@@ -123,13 +139,13 @@ bool FairnessLimiter::allowPacket(const mesh::Packet* pkt) {
type == PAYLOAD_TYPE_TXT_MSG || type == PAYLOAD_TYPE_ANON_REQ) {
if (pkt->payload_len < 2) return true; // malformed, not ours to judge
hash[0] = pkt->payload[1];
return takeSenderNormal(hash);
return takeSenderNormal(hash) || shed(FAIR_SHED_SENDER);
}
if (type == PAYLOAD_TYPE_ADVERT) {
if (pkt->payload_len < 1) return true; // malformed, not ours to judge
hash[0] = pkt->payload[0]; // first byte of sender pub key
return takeSenderLow(hash);
return takeSenderLow(hash) || shed(FAIR_SHED_ADVERT);
}
// Not classified (allowed through):
+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; }