Merge pull request #8053 from GUVWAF/assymRelay

Make sure next-hop is only set when they received us directly
This commit is contained in:
Ben Meadors 2025-09-25 13:54:22 -05:00 committed by GitHub
commit 44636cc9f5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 80 additions and 31 deletions

View File

@ -143,8 +143,7 @@ void FloodingRouter::perhapsRebroadcast(const meshtastic_MeshPacket *p)
LOG_INFO("Rebroadcast received floodmsg"); LOG_INFO("Rebroadcast received floodmsg");
// Note: we are careful to resend using the original senders node id // Note: we are careful to resend using the original senders node id
// We are careful not to call our hooked version of send() - because we don't want to check this again send(tosend);
Router::send(tosend);
} else { } else {
LOG_DEBUG("No rebroadcast: Role = CLIENT_MUTE or Rebroadcast Mode = NONE"); LOG_DEBUG("No rebroadcast: Role = CLIENT_MUTE or Rebroadcast Mode = NONE");
} }

View File

@ -110,11 +110,14 @@ void NextHopRouter::sniffReceived(const meshtastic_MeshPacket *p, const meshtast
if (origTx) { if (origTx) {
// Either relayer of ACK was also a relayer of the packet, or we were the *only* relayer and the ACK came directly // Either relayer of ACK was also a relayer of the packet, or we were the *only* relayer and the ACK came directly
// from the destination // from the destination
if (wasRelayer(p->relay_node, p->decoded.request_id, p->to) || bool wasAlreadyRelayer = wasRelayer(p->relay_node, p->decoded.request_id, p->to);
(p->hop_start != 0 && p->hop_start == p->hop_limit && bool weWereSoleRelayer = false;
wasSoleRelayer(ourRelayID, p->decoded.request_id, p->to))) { bool weWereRelayer = wasRelayer(ourRelayID, p->decoded.request_id, p->to, &weWereSoleRelayer);
if ((weWereRelayer && wasAlreadyRelayer) ||
(p->hop_start != 0 && p->hop_start == p->hop_limit && weWereSoleRelayer)) {
if (origTx->next_hop != p->relay_node) { // Not already set if (origTx->next_hop != p->relay_node) { // Not already set
LOG_INFO("Update next hop of 0x%x to 0x%x based on ACK/reply", p->from, p->relay_node); LOG_INFO("Update next hop of 0x%x to 0x%x based on ACK/reply (was relayer %d we were sole %d)", p->from,
p->relay_node, wasAlreadyRelayer, weWereSoleRelayer);
origTx->next_hop = p->relay_node; origTx->next_hop = p->relay_node;
} }
} }

View File

@ -67,8 +67,14 @@ bool PacketHistory::wasSeenRecently(const meshtastic_MeshPacket *p, bool withUpd
r.id = p->id; r.id = p->id;
r.sender = getFrom(p); // If 0 then use our ID r.sender = getFrom(p); // If 0 then use our ID
r.next_hop = p->next_hop; r.next_hop = p->next_hop;
r.hop_limit = p->hop_limit; setHighestHopLimit(r, p->hop_limit);
bool weWillRelay = false;
uint8_t ourRelayID = nodeDB->getLastByteOfNodeNum(nodeDB->getNodeNum());
if (p->relay_node == ourRelayID) { // If the relay_node is us, store it
weWillRelay = true;
setOurTxHopLimit(r, p->hop_limit);
r.relayed_by[0] = p->relay_node; r.relayed_by[0] = p->relay_node;
}
r.rxTimeMsec = millis(); // r.rxTimeMsec = millis(); //
if (r.rxTimeMsec == 0) // =0 every 49.7 days? 0 is special if (r.rxTimeMsec == 0) // =0 every 49.7 days? 0 is special
@ -94,8 +100,6 @@ bool PacketHistory::wasSeenRecently(const meshtastic_MeshPacket *p, bool withUpd
} }
if (seenRecently) { if (seenRecently) {
uint8_t ourRelayID = nodeDB->getLastByteOfNodeNum(nodeDB->getNodeNum()); // Get our relay ID from our node number
if (wasFallback) { if (wasFallback) {
// If it was seen with a next-hop not set to us and now it's NO_NEXT_HOP_PREFERENCE, and the relayer relayed already // If it was seen with a next-hop not set to us and now it's NO_NEXT_HOP_PREFERENCE, and the relayer relayed already
// before, it's a fallback to flooding. If we didn't already relay and the next-hop neither, we might need to handle // before, it's a fallback to flooding. If we didn't already relay and the next-hop neither, we might need to handle
@ -137,16 +141,40 @@ bool PacketHistory::wasSeenRecently(const meshtastic_MeshPacket *p, bool withUpd
found->sender, found->id, found->next_hop, found->relayed_by[0], found->relayed_by[1], found->relayed_by[2], found->sender, found->id, found->next_hop, found->relayed_by[0], found->relayed_by[1], found->relayed_by[2],
millis() - found->rxTimeMsec); millis() - found->rxTimeMsec);
#endif #endif
// Only update the relayer if it heard us directly (meaning hopLimit is decreased by 1)
uint8_t startIdx = weWillRelay ? 1 : 0;
if (!weWillRelay) {
bool weWereRelayer = wasRelayer(ourRelayID, *found);
// We were a relayer and the packet came in with a hop limit that is one less than when we sent it out
if (weWereRelayer && (p->hop_limit == getOurTxHopLimit(*found) || p->hop_limit == getOurTxHopLimit(*found) - 1)) {
r.relayed_by[0] = p->relay_node;
startIdx = 1; // Start copying existing relayers from index 1
}
// keep the original ourTxHopLimit
setOurTxHopLimit(r, getOurTxHopLimit(*found));
}
// Preserve the highest hop_limit we've ever seen for this packet so upgrades aren't lost when a later copy has // Preserve the highest hop_limit we've ever seen for this packet so upgrades aren't lost when a later copy has
// fewer hops remaining. // fewer hops remaining.
if (found->hop_limit > r.hop_limit) if (getHighestHopLimit(*found) > getHighestHopLimit(r))
r.hop_limit = found->hop_limit; setHighestHopLimit(r, getHighestHopLimit(*found));
// Add the existing relayed_by to the new record // Add the existing relayed_by to the new record, avoiding duplicates
for (uint8_t i = 0; i < (NUM_RELAYERS - 1); i++) { for (uint8_t i = 0; i < (NUM_RELAYERS - startIdx); i++) {
if (found->relayed_by[i] != 0) if (found->relayed_by[i] == 0)
r.relayed_by[i + 1] = found->relayed_by[i]; continue;
bool exists = false;
for (uint8_t j = 0; j < NUM_RELAYERS; j++) {
if (r.relayed_by[j] == found->relayed_by[i]) {
exists = true;
break;
}
}
if (!exists) {
r.relayed_by[i + startIdx] = found->relayed_by[i];
}
} }
r.next_hop = found->next_hop; // keep the original next_hop (such that we check whether we were originally asked) r.next_hop = found->next_hop; // keep the original next_hop (such that we check whether we were originally asked)
#if VERBOSE_PACKET_HISTORY #if VERBOSE_PACKET_HISTORY
@ -369,14 +397,6 @@ bool PacketHistory::wasRelayer(const uint8_t relayer, const PacketRecord &r, boo
return found; return found;
} }
// Check if a certain node was the *only* relayer of a packet in the history given an ID and sender
bool PacketHistory::wasSoleRelayer(const uint8_t relayer, const uint32_t id, const NodeNum sender)
{
bool wasSole = false;
wasRelayer(relayer, id, sender, &wasSole);
return wasSole;
}
// Remove a relayer from the list of relayers of a packet in the history given an ID and sender // Remove a relayer from the list of relayers of a packet in the history given an ID and sender
void PacketHistory::removeRelayer(const uint8_t relayer, const uint32_t id, const NodeNum sender) void PacketHistory::removeRelayer(const uint8_t relayer, const uint32_t id, const NodeNum sender)
{ {
@ -418,3 +438,24 @@ void PacketHistory::removeRelayer(const uint8_t relayer, const uint32_t id, cons
found->id, found->relayed_by[0], found->relayed_by[1], found->relayed_by[2], relayer, i != j); found->id, found->relayed_by[0], found->relayed_by[1], found->relayed_by[2], relayer, i != j);
#endif #endif
} }
// Getters and setters for hop limit fields packed in hop_limit
inline uint8_t PacketHistory::getHighestHopLimit(PacketRecord &r)
{
return r.hop_limit & HOP_LIMIT_HIGHEST_MASK;
}
inline void PacketHistory::setHighestHopLimit(PacketRecord &r, uint8_t hopLimit)
{
r.hop_limit = (r.hop_limit & ~HOP_LIMIT_HIGHEST_MASK) | (hopLimit & HOP_LIMIT_HIGHEST_MASK);
}
inline uint8_t PacketHistory::getOurTxHopLimit(PacketRecord &r)
{
return (r.hop_limit & HOP_LIMIT_OUR_TX_MASK) >> HOP_LIMIT_OUR_TX_SHIFT;
}
inline void PacketHistory::setOurTxHopLimit(PacketRecord &r, uint8_t hopLimit)
{
r.hop_limit = (r.hop_limit & ~HOP_LIMIT_OUR_TX_MASK) | ((hopLimit << HOP_LIMIT_OUR_TX_SHIFT) & HOP_LIMIT_OUR_TX_MASK);
}

View File

@ -2,8 +2,11 @@
#include "NodeDB.h" #include "NodeDB.h"
#define NUM_RELAYERS \ // Number of relayers we keep track of. Use 6 to be efficient with memory alignment of PacketRecord to 20 bytes
3 // Number of relayer we keep track of. Use 3 to be efficient with memory alignment of PacketRecord to 16 bytes #define NUM_RELAYERS 6
#define HOP_LIMIT_HIGHEST_MASK 0x07 // Bits 0-2
#define HOP_LIMIT_OUR_TX_MASK 0x38 // Bits 3-5
#define HOP_LIMIT_OUR_TX_SHIFT 3 // Bits 3-5
/** /**
* This is a mixin that adds a record of past packets we have seen * This is a mixin that adds a record of past packets we have seen
@ -16,9 +19,10 @@ class PacketHistory
PacketId id; PacketId id;
uint32_t rxTimeMsec; // Unix time in msecs - the time we received it, 0 means empty uint32_t rxTimeMsec; // Unix time in msecs - the time we received it, 0 means empty
uint8_t next_hop; // The next hop asked for this packet uint8_t next_hop; // The next hop asked for this packet
uint8_t hop_limit; // Highest hop limit observed for this packet uint8_t hop_limit; // bit 0-2: Highest hop limit observed for this packet,
// bit 3-5: our hop limit when we first transmitted it
uint8_t relayed_by[NUM_RELAYERS]; // Array of nodes that relayed this packet uint8_t relayed_by[NUM_RELAYERS]; // Array of nodes that relayed this packet
}; // 4B + 4B + 4B + 1B + 1B + 3B = 17B (will be padded to 20B) }; // 4B + 4B + 4B + 1B + 1B + 6B = 20B
uint32_t recentPacketsCapacity = uint32_t recentPacketsCapacity =
0; // Can be set in constructor, no need to recompile. Used to allocate memory for mx_recentPackets. 0; // Can be set in constructor, no need to recompile. Used to allocate memory for mx_recentPackets.
@ -39,6 +43,11 @@ class PacketHistory
* @return true if node was indeed a relayer, false if not */ * @return true if node was indeed a relayer, false if not */
bool wasRelayer(const uint8_t relayer, const PacketRecord &r, bool *wasSole = nullptr); bool wasRelayer(const uint8_t relayer, const PacketRecord &r, bool *wasSole = nullptr);
uint8_t getHighestHopLimit(PacketRecord &r);
void setHighestHopLimit(PacketRecord &r, uint8_t hopLimit);
uint8_t getOurTxHopLimit(PacketRecord &r);
void setOurTxHopLimit(PacketRecord &r, uint8_t hopLimit);
PacketHistory(const PacketHistory &); // non construction-copyable PacketHistory(const PacketHistory &); // non construction-copyable
PacketHistory &operator=(const PacketHistory &); // non copyable PacketHistory &operator=(const PacketHistory &); // non copyable
public: public:
@ -61,9 +70,6 @@ class PacketHistory
* @return true if node was indeed a relayer, false if not */ * @return true if node was indeed a relayer, false if not */
bool wasRelayer(const uint8_t relayer, const uint32_t id, const NodeNum sender, bool *wasSole = nullptr); bool wasRelayer(const uint8_t relayer, const uint32_t id, const NodeNum sender, bool *wasSole = nullptr);
// Check if a certain node was the *only* relayer of a packet in the history given an ID and sender
bool wasSoleRelayer(const uint8_t relayer, const uint32_t id, const NodeNum sender);
// Remove a relayer from the list of relayers of a packet in the history given an ID and sender // Remove a relayer from the list of relayers of a packet in the history given an ID and sender
void removeRelayer(const uint8_t relayer, const uint32_t id, const NodeNum sender); void removeRelayer(const uint8_t relayer, const uint32_t id, const NodeNum sender);