feat(repeater): fairness grid viditelnost + runtime cap/refill tuning

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
tthandClaude Fable 5 committed 2026-09-05 23:39:04 +02:00
1 parent 6f90c80dcc
commit 33af8ecc05
6 files changed
+231 -27

No files matched your search

+9
View File
@@ -77,6 +77,9 @@ public:
// just leave them at defaults.
uint8_t fairness_enabled = 0; // FairnessLimiter runtime kill switch
char filter_deny[128]; // channel-filter deny list, comma-separated node names
// 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;
private:
class RadioPrefs : public CommonRadioPrefs {
@@ -193,6 +196,12 @@ private:
def("loop", _parent->loop_detect);
def("fair_en", _parent->fairness_enabled);
def("f_deny", _parent->filter_deny, sizeof(_parent->filter_deny));
def("fair_gcap", _parent->fair_group_cap);
def("fair_scap", _parent->fair_sender_cap);
def("fair_acap", _parent->fair_advert_cap);
def("fair_grf", _parent->fair_group_refill_s);
def("fair_srf", _parent->fair_sender_refill_s);
def("fair_arf", _parent->fair_advert_refill_s);
}
public:
RepeatPrefs(NodePrefs* parent) : _parent(parent) { }
+50
View File
@@ -1,6 +1,8 @@
#include "FairnessLimiter.h"
#include <MeshCore.h>
#include <cstdio>
#include <cstring>
uint8_t FairnessLimiter::peekGroup(const uint8_t* hash) {
auto i = group_idx(hash);
@@ -18,6 +20,7 @@ bool FairnessLimiter::takeGroup(const uint8_t* hash) {
allow = true;
} else {
denied_group++;
if (i < GROUP_MAP_SIZE && group_deny[i] != 0xFFFF) group_deny[i]++;
}
MESH_DEBUG_PRINTLN("FAIR, group=%02X allow=%u", (uint32_t)hash[0], (uint32_t)allow);
return allow;
@@ -57,6 +60,7 @@ bool FairnessLimiter::takeSenderNormal(const uint8_t* hash) {
allow = true;
} else {
denied_sender_normal++;
if (i < SENDER_NORMAL_MAP_SIZE && sender_normal_deny[i] != 0xFFFF) sender_normal_deny[i]++;
}
MESH_DEBUG_PRINTLN("FAIR, sender=%02X prio=normal allow=%u", (uint32_t)hash[0], (uint32_t)allow);
return allow;
@@ -96,6 +100,7 @@ bool FairnessLimiter::takeSenderLow(const uint8_t* hash) {
allow = true;
} else {
denied_sender_low++;
if (i < SENDER_LOW_MAP_SIZE && sender_low_deny[i] != 0xFFFF) sender_low_deny[i]++;
}
MESH_DEBUG_PRINTLN("FAIR, sender=%02X prio=low allow=%u", (uint32_t)hash[0], (uint32_t)allow);
return allow;
@@ -152,3 +157,48 @@ bool FairnessLimiter::allowPacket(const mesh::Packet* pkt) {
// PAYLOAD_TYPE_CONTROL, PAYLOAD_TYPE_RAW_CUSTOM
return true;
}
// Append the busiest deny buckets of one category to `out`. Repeated max-scan
// over a small array (<=128) -- picks up to `top` entries, busiest first, and
// skips a bucket once emitted by nulling a scratch copy. Returns chars written.
static int fmt_deny_cat(char* out, int max_len, const char* label,
uint16_t* deny, int size, uint8_t cap, int top) {
int w = snprintf(out, max_len, "%s(cap %u):", label, (unsigned)cap);
if (w < 0 || w >= max_len) return (w < 0) ? 0 : max_len - 1;
bool any = false;
for (int n = 0; n < top; n++) {
int best = -1;
uint16_t best_v = 0;
for (int i = 0; i < size; i++) {
if (deny[i] > best_v) { best_v = deny[i]; best = i; }
}
if (best < 0) break; // no non-zero left
int adv = snprintf(out + w, max_len - w, " %02x=%u", (unsigned)best, (unsigned)best_v);
if (adv < 0 || adv >= max_len - w) { out[w] = 0; break; } // no room, stop clean
w += adv;
any = true;
deny[best] = 0; // consumed for the next pass; `deny` is formatGrid's scratch copy
}
if (!any) {
int adv = snprintf(out + w, max_len - w, " -");
if (adv > 0 && adv < max_len - w) w += adv;
}
return w;
}
int FairnessLimiter::formatGrid(char* out, int max_len) const {
// Work on scratch copies so the busiest-first selection can null entries as
// it consumes them without disturbing the live counters.
static uint16_t g[GROUP_MAP_SIZE], sn[SENDER_NORMAL_MAP_SIZE], sl[SENDER_LOW_MAP_SIZE];
memcpy(g, group_deny, sizeof(g));
memcpy(sn, sender_normal_deny, sizeof(sn));
memcpy(sl, sender_low_deny, sizeof(sl));
int w = 0;
w += fmt_deny_cat(out + w, max_len - w, "group", g, GROUP_MAP_SIZE, group_cap, 6);
if (w < max_len - 2) { int a = snprintf(out + w, max_len - w, " | "); if (a > 0) w += a; }
w += fmt_deny_cat(out + w, max_len - w, "sender", sn, SENDER_NORMAL_MAP_SIZE, sender_normal_cap, 6);
if (w < max_len - 2) { int a = snprintf(out + w, max_len - w, " | "); if (a > 0) w += a; }
w += fmt_deny_cat(out + w, max_len - w, "advert", sl, SENDER_LOW_MAP_SIZE, sender_low_cap, 4);
return w;
}
+22 -1
View File
@@ -70,7 +70,9 @@
* nothing" reduction in wasteful repeats, freeing airtime for useful repeats.
*/
class FairnessLimiter {
// Capacity of when a bucket is full.
// 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
// resets to the build default below.
uint8_t group_cap = FAIRNESS_GROUP_CAP, sender_normal_cap = FAIRNESS_SENDER_NORMAL_CAP,
sender_low_cap = FAIRNESS_SENDER_LOW_CAP;
@@ -80,6 +82,11 @@ class FairnessLimiter {
uint8_t sender_normal_map[SENDER_NORMAL_MAP_SIZE] = {};
uint8_t sender_low_map[SENDER_LOW_MAP_SIZE] = {};
uint8_t group_map[GROUP_MAP_SIZE] = {};
// Per-bucket deny counters -- the "grid": which hash buckets we are actually
// shedding, so a shed is visible instead of a bare total. Saturating uint16.
uint16_t group_deny[GROUP_MAP_SIZE] = {};
uint16_t sender_normal_deny[SENDER_NORMAL_MAP_SIZE] = {};
uint16_t sender_low_deny[SENDER_LOW_MAP_SIZE] = {};
public:
FairnessLimiter() {}
@@ -104,6 +111,20 @@ public:
uint32_t deniedSenderLow() const { return denied_sender_low; }
uint32_t deniedGroup() const { return denied_group; }
// Runtime cap tuning (0 = restore the build default for that bucket).
void setGroupCap(uint8_t c) { group_cap = c ? c : FAIRNESS_GROUP_CAP; }
void setSenderNormalCap(uint8_t c) { sender_normal_cap = c ? c : FAIRNESS_SENDER_NORMAL_CAP; }
void setSenderLowCap(uint8_t c) { sender_low_cap = c ? c : FAIRNESS_SENDER_LOW_CAP; }
uint8_t groupCap() const { return group_cap; }
uint8_t senderNormalCap() const { return sender_normal_cap; }
uint8_t senderLowCap() const { return sender_low_cap; }
// The grid: append "<pfx>=<count>" for each bucket with denies, per category,
// busiest first, into `out` (bounded by max_len). Returns chars written.
// `pfx` is the low bits of the sender/channel hash -- cross-reference the
// observer feed / CoreScope to map a hot bucket back to an actual node.
int formatGrid(char* out, int max_len) const;
static int sender_normal_idx(const uint8_t* hash) { return hash[0] & SENDER_NORMAL_MASK; }
static int sender_low_idx(const uint8_t* hash) { return hash[0] & SENDER_LOW_MASK; }
static int group_idx(const uint8_t* hash) { return hash[0] & GROUP_MASK; }