Support overlapping TG prefixes with longest-prefix-match routing

isSharedTG now finds the longest matching remote prefix and verifies
no other prefix in the mesh is more specific. Each TrunkLink receives
the full set of all prefixes at startup via setAllPrefixes().
initTrunkLinks is split into two passes: collect all prefixes first,
then create links with full knowledge.
This commit is contained in:
Audric committed 2026-03-26 10:55:47 +01:00
1 parent dda6ac5531
commit 53a9a6fc75
4 files changed
+60 -6

No files matched your search

+21 -2
View File
@@ -46,7 +46,9 @@ dedicated port (default 5302, separate from the client port 5300).
## TG Ownership and Routing
A TG number belongs to the reflector whose `LOCAL_PREFIX` is a string prefix
of the TG's decimal representation:
of the TG's decimal representation. When multiple prefixes match (because one
prefix is itself a prefix of another), the **longest matching prefix wins** —
analogous to longest-prefix-match in IP routing.
| TG number | Decimal string | Owned by |
|-----------|---------------|----------|
@@ -58,7 +60,24 @@ of the TG's decimal representation:
| 25 | "25" | prefix "2" |
| 30 | "30" | prefix "3" |
Ownership check: `std::to_string(tg).substr(0, prefix.size()) == prefix`
### Overlapping Prefixes
Prefixes may overlap — one prefix can be a prefix of another. This enables
hierarchical TG numbering schemes. For example, with prefixes `"12"` and
`"120"`:
| TG number | Matches | Longest match | Owned by |
|-----------|----------------|---------------|--------------|
| 125 | "12" | "12" (len 2) | prefix "12" |
| 1205 | "12" and "120" | "120" (len 3) | prefix "120" |
| 12 | "12" | "12" (len 2) | prefix "12" |
| 1200 | "12" and "120" | "120" (len 3) | prefix "120" |
At startup, each `TrunkLink` receives the complete set of all prefixes in the
mesh (local + all remotes). `isSharedTG(tg)` finds the best matching remote
prefix for the peer, then verifies no other prefix in the mesh is a longer
match. If a longer match exists, the TG belongs to that other reflector
instead.
In a full mesh of N reflectors, each `[TRUNK_x]` section on reflector A points
at reflector B and carries only TGs whose decimal string starts with B's prefix
+8 -1
View File
@@ -1994,14 +1994,16 @@ void Reflector::initTrunkLinks(void)
}
}
// First pass: collect all remote prefixes so we can do longest-prefix-match
std::vector<std::string> trunk_sections;
for (const auto& section : m_cfg->listSections())
{
if (section.substr(0, 6) != "TRUNK_")
{
continue;
}
trunk_sections.push_back(section);
// Collect remote prefixes for validation
std::string remote_prefix_str;
m_cfg->getValue(section, "REMOTE_PREFIX", remote_prefix_str);
{
@@ -2014,10 +2016,15 @@ void Reflector::initTrunkLinks(void)
if (!token.empty()) all_prefixes.push_back(token);
}
}
}
// Second pass: create trunk links with full prefix knowledge
for (const auto& section : trunk_sections)
{
auto* link = new TrunkLink(this, *m_cfg, section);
if (link->initialize())
{
link->setAllPrefixes(all_prefixes);
m_trunk_links.push_back(link);
}
else
+26 -3
View File
@@ -199,13 +199,36 @@ bool TrunkLink::initialize(void)
bool TrunkLink::isSharedTG(uint32_t tg) const
{
const std::string s = std::to_string(tg);
// Find the best (longest) matching remote prefix for this peer
size_t best_remote_len = 0;
for (const auto& prefix : m_remote_prefix)
{
if (s.size() >= prefix.size() &&
s.compare(0, prefix.size(), prefix) == 0)
return true;
s.compare(0, prefix.size(), prefix) == 0 &&
prefix.size() > best_remote_len)
{
best_remote_len = prefix.size();
}
return false;
}
if (best_remote_len == 0)
{
return false; // no remote prefix matches at all
}
// Check if any prefix in the mesh is a longer match — if so, that other
// reflector is more specific and this TG doesn't belong to this peer.
for (const auto& prefix : m_all_prefixes)
{
if (prefix.size() > best_remote_len &&
s.size() >= prefix.size() &&
s.compare(0, prefix.size(), prefix) == 0)
{
return false; // a longer prefix claims this TG
}
}
return true;
} /* TrunkLink::isSharedTG */
+5
View File
@@ -92,6 +92,10 @@ class TrunkLink : public sigc::trackable
bool initialize(void);
bool isSharedTG(uint32_t tg) const;
void setAllPrefixes(const std::vector<std::string>& all_prefixes)
{
m_all_prefixes = all_prefixes;
}
const std::string& section(void) const { return m_section; }
Json::Value statusJson(void) const;
@@ -128,6 +132,7 @@ class TrunkLink : public sigc::trackable
Async::Timer m_heartbeat_timer;
unsigned m_hb_tx_cnt;
unsigned m_hb_rx_cnt;
std::vector<std::string> m_all_prefixes; // all prefixes in the mesh
// TGs where we suppressed our local talker to defer to the peer
std::set<uint32_t> m_yielded_tgs;
// TGs currently held by this specific trunk peer (for scoped cleanup)