Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Next Hop-based routing with fallback to flooding #2856

Open
wants to merge 53 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
0d6729b
Initial version of NextHopRouter
GUVWAF Oct 2, 2023
3776064
Merge branch 'master' into NextHopRouter
thebentern Oct 3, 2023
44dc270
Set original hop limit in header flags
GUVWAF Oct 13, 2023
9b1dd75
Short-circuit to FloodingRouter for broadcasts
GUVWAF Oct 14, 2023
27a492a
If packet traveled 1 hop, set `relay_node` as `next_hop` for the orig…
GUVWAF Oct 14, 2023
c7293cf
Merge branch 'master' into NextHopRouter
caveman99 Oct 31, 2023
42757d8
Merge branch 'master' into NextHopRouter
caveman99 Nov 16, 2023
81f57b6
Merge branch 'master' into NextHopRouter
thebentern Nov 26, 2023
ef2c6ee
Set last byte to 0xFF if it ended at 0x00
GUVWAF Nov 27, 2023
3ba9ecb
Also update next-hop based on received DM for us
GUVWAF Nov 28, 2023
25ec051
Resolve conflicts (needs testing)
GUVWAF Apr 20, 2024
b456e34
Merge branch 'master' into NextHopRouter
caveman99 Apr 23, 2024
d4ef0cd
Merge branch 'master' into NextHopRouter
caveman99 May 3, 2024
b8e01b4
Merge branch 'master' into NextHopRouter
caveman99 Jun 19, 2024
e91dcb4
Merge branch 'master' into NextHopRouter
caveman99 Sep 4, 2024
913268b
temp
GUVWAF Aug 9, 2024
2e303a3
Add 1 retransmission for intermediate hops when using NextHopRouter
GUVWAF Aug 10, 2024
6fe42ed
Add next_hop and relayed_by in PacketHistory for setting next-hop and…
GUVWAF Nov 1, 2024
aae4443
Merge remote-tracking branch 'origin/master' into NextHopRouter
GUVWAF Nov 1, 2024
ba4220f
Update protos, store multiple relayers
GUVWAF Nov 1, 2024
9de8d5a
Remove next-hop update logic from NeighborInfoModule
GUVWAF Nov 1, 2024
0134483
Fix retransmissions
GUVWAF Nov 1, 2024
e4c9818
Improve ACKs for repeated packets and responses
GUVWAF Nov 2, 2024
aab973e
Stop retransmission even if there's not relay node
GUVWAF Nov 2, 2024
28944ad
Merge remote-tracking branch 'origin/master' into NextHopRouter
GUVWAF Nov 5, 2024
790801f
Revert perhapsRebroadcast()
GUVWAF Nov 5, 2024
bb64b14
Remove relayer if we cancel a transmission
GUVWAF Nov 5, 2024
24ff7c0
Better checking for fallback to flooding
GUVWAF Nov 5, 2024
69f88b9
Fix newlines in traceroute print logs
GUVWAF Nov 5, 2024
fbefce7
Merge remote-tracking branch 'origin/master' into NextHopRouter
GUVWAF Nov 8, 2024
70aa28c
Stop retransmission for original packet
GUVWAF Nov 8, 2024
78bf1e1
Use relayID
GUVWAF Nov 8, 2024
f37abe8
Also when want_ack is set, we should try to retransmit
GUVWAF Nov 8, 2024
be73b09
Fix cppcheck error
GUVWAF Nov 8, 2024
71a90b3
Fix 'router' not in scope error
GUVWAF Nov 8, 2024
93bcee3
Fix another cppcheck error
GUVWAF Nov 9, 2024
17495e7
Check for hop_limit and also update next hop when `hop_start == hop_l…
GUVWAF Nov 11, 2024
3725319
Merge remote-tracking branch 'origin/master' into NextHopRouter
GUVWAF Nov 11, 2024
42d17b3
Formatting and correct NUM_RETRANSMISSIONS
GUVWAF Nov 11, 2024
dbe520c
Merge remote-tracking branch 'origin/master' into NextHopRouter
GUVWAF Nov 14, 2024
b229abc
Update protos
GUVWAF Nov 16, 2024
3ea2918
Merge remote-tracking branch 'origin/master' into NextHopRouter
GUVWAF Nov 16, 2024
360637c
Start retransmissions in NextHopRouter if ReliableRouter didn't do it
GUVWAF Nov 16, 2024
bfc6a19
Handle repeated/fallback to flooding packets properly
GUVWAF Nov 16, 2024
98719e4
Merge branch 'master' into NextHopRouter
fifieldt Nov 17, 2024
47116f6
Guard against clients setting `next_hop`/`relay_node`
GUVWAF Nov 18, 2024
6a29793
Merge remote-tracking branch 'origin/master' into NextHopRouter
GUVWAF Nov 18, 2024
e593d54
Merge remote-tracking branch 'origin/master' into NextHopRouter
GUVWAF Nov 19, 2024
017bff8
Merge remote-tracking branch 'origin/master' into NextHopRouter
GUVWAF Nov 23, 2024
0952c86
Don't cancel relay if we were the assigned next-hop
GUVWAF Nov 27, 2024
41d0a39
Merge remote-tracking branch 'origin/master' into NextHopRouter
GUVWAF Nov 27, 2024
98b4a29
Merge branch 'master' into NextHopRouter
GUVWAF Dec 10, 2024
4915a07
Merge branch 'master' into NextHopRouter
GUVWAF Dec 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,9 @@ void setup()
}
nodeStatus->observe(&nodeDB.newStatus);

config.lora.next_hop_routing = true; // FIXME - remove this before merging
LOG_INFO("USING NEXT-HOP ROUTING\n");

service.init();

// Now that the mesh service is created, create any modules
Expand Down
2 changes: 1 addition & 1 deletion src/mesh/FloodingRouter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,4 @@ void FloodingRouter::sniffReceived(const meshtastic_MeshPacket *p, const meshtas
}
// handle the packet as normal
Router::sniffReceived(p, c);
}
}
101 changes: 101 additions & 0 deletions src/mesh/NextHopRouter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#include "NextHopRouter.h"

NextHopRouter::NextHopRouter() {}

/**
* Send a packet
*/
ErrorCode NextHopRouter::send(meshtastic_MeshPacket *p)
{
// Add any messages _we_ send to the seen message list (so we will ignore all retransmissions we see)
wasSeenRecently(p); // FIXME, move this to a sniffSent method

p->next_hop = getNextHop(p->to, p->relay_node); // set the next hop
LOG_DEBUG("Setting next hop for packet with dest %x to %x\n", p->to, p->next_hop);

return Router::send(p);
}

bool NextHopRouter::shouldFilterReceived(const meshtastic_MeshPacket *p)
{
if (wasSeenRecently(p)) { // Note: this will also add a recent packet record
if (p->next_hop == nodeDB.getLastByteOfNodeNum(getNodeNum())) {
LOG_DEBUG("Ignoring incoming msg, because we've already seen it.\n");
} else {
LOG_DEBUG("Ignoring incoming msg, because we've already seen it and cancel any outgoing packets.\n");
Router::cancelSending(p->from, p->id);
}
return true;
}

return Router::shouldFilterReceived(p);
}

void NextHopRouter::sniffReceived(const meshtastic_MeshPacket *p, const meshtastic_Routing *c)
{
bool isAck =
((c && c->error_reason == meshtastic_Routing_Error_NONE)); // consider only ROUTING_APP message without error as ACK
if (isAck) {
// Update next-hop of this successful transmission to the relay node, but ONLY if "from" is not 0 or ourselves (means
// implicit ACK or someone is relaying our ACK)
if (p->from != 0 && p->from != getNodeNum()) {
if (p->relay_node) {
meshtastic_NodeInfoLite *sentTo = nodeDB.getMeshNode(p->from);
if (sentTo) {
LOG_DEBUG("Update next hop of 0x%x to 0x%x based on received ACK.\n", p->from, p->relay_node);
sentTo->next_hop = p->relay_node;
}
}
}
}

if (config.device.role != meshtastic_Config_DeviceConfig_Role_CLIENT_MUTE) {
if ((p->to != getNodeNum()) && (getFrom(p) != getNodeNum())) {
if (p->next_hop == nodeDB.getLastByteOfNodeNum(getNodeNum())) {
meshtastic_MeshPacket *tosend = packetPool.allocCopy(*p); // keep a copy because we will be sending it
LOG_INFO("Relaying received next-hop message coming from %x\n", p->relay_node);

if (p->which_payload_variant == meshtastic_MeshPacket_decoded_tag) {
// If it is a traceRoute request, update the route that it went via me
if (traceRouteModule && traceRouteModule->wantPacket(tosend))
traceRouteModule->updateRoute(tosend);
// If it is a neighborInfo packet, update last_sent_by_id
if (neighborInfoModule && neighborInfoModule->wantPacket(tosend))
neighborInfoModule->updateLastSentById(tosend);
}

tosend->hop_limit--; // bump down the hop count
NextHopRouter::send(tosend);
} else if (p->next_hop == NO_NEXT_HOP_PREFERENCE) {
// No preference for next hop, use FloodingRouter
LOG_DEBUG("No preference for next hop, using FloodingRouter\n");
FloodingRouter::sniffReceived(p, c);
} // else don't relay
}
} else {
LOG_DEBUG("Not rebroadcasting. Role = Role_ClientMute\n");
}
// handle the packet as normal
Router::sniffReceived(p, c);
}

/**
* Get the next hop for a destination, given the relay node
* @return the node number of the next hop, 0 if no preference (fallback to FloodingRouter)
*/
uint8_t NextHopRouter::getNextHop(NodeNum to, uint8_t relay_node)
{
meshtastic_NodeInfoLite *node = nodeDB.getMeshNode(to);
if (node) {
// We are careful not to return the relay node as the next hop
if (node->next_hop != relay_node) {
LOG_DEBUG("Next hop for 0x%x is 0x%x\n", to, node->next_hop);
return node->next_hop;
} else {
LOG_WARN("Next hop for 0x%x is 0x%x, same as relayer; setting as no preference\n", to, node->next_hop);
return NO_NEXT_HOP_PREFERENCE;
}
} else {
return NO_NEXT_HOP_PREFERENCE;
}
}
51 changes: 51 additions & 0 deletions src/mesh/NextHopRouter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#pragma once

#include "FloodingRouter.h"

/*
Router which only relays if it is the next hop for a packet.
The next hop is set by the relay node of a packet, which bases this on information from either the NeighborInfoModule, or a
previous successful delivery via flooding. It is only used for DMs and not used for broadcasts. Using the NeighborInfoModule, it
can derive the next hop of neighbors and that of neighbors of neighbors. For others, it has no information in the beginning,
which results into falling back to the FloodingRouter. Upon successful delivery via flooding, it updates the next hop of the
recipient to the node that last relayed the ACK to us. When the ReliableRouter is doing retransmissions, at the last retry, it
will reset the next hop, in order to fall back to the FloodingRouter.
*/
class NextHopRouter : public FloodingRouter
{
public:
/**
* Constructor
*
*/
NextHopRouter();

/**
* Send a packet
* @return an error code
*/
virtual ErrorCode send(meshtastic_MeshPacket *p) override;

protected:
/**
* Should this incoming filter be dropped?
*
* Called immediately on reception, before any further processing.
* @return true to abandon the packet
*/
virtual bool shouldFilterReceived(const meshtastic_MeshPacket *p) override;

/**
* Look for packets we need to relay
*/
virtual void sniffReceived(const meshtastic_MeshPacket *p, const meshtastic_Routing *c) override;

constexpr static uint8_t NO_NEXT_HOP_PREFERENCE = 0;

private:
/**
* Get the next hop for a destination, given the relay node
* @return the node number of the next hop, 0 if no preference (fallback to FloodingRouter)
*/
uint8_t getNextHop(NodeNum to, uint8_t relay_node);
};
8 changes: 8 additions & 0 deletions src/mesh/NodeDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,14 @@ void NodeDB::updateFrom(const meshtastic_MeshPacket &mp)
if (mp.decoded.portnum == meshtastic_PortNum_NODEINFO_APP) {
info->channel = mp.channel;
}

// If this packet didn't travel any hops, then it was sent directly to us, so we know what to use as next hop to this node
if (mp.original_hop_limit == mp.hop_limit) {
info->next_hop = getLastByteOfNodeNum(mp.from);
} else if (mp.relay_node && (mp.original_hop_limit - mp.hop_limit == 1)) {
// This packet traveled one hop, so we can use the relay_node as next_hop
info->next_hop = getLastByteOfNodeNum(mp.relay_node);
}
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/mesh/NodeDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ class NodeDB
/// @return our node number
NodeNum getNodeNum() { return myNodeInfo.my_node_num; }

// @return last byte of a NodeNum, 0xFF if it ended at 0x00
uint8_t getLastByteOfNodeNum(NodeNum num) { return (uint8_t)((num & 0xFF) ? (num & 0xFF) : 0xFF); }

/// if returns false, that means our node should send a DenyNodeNum response. If true, we think the number is okay for use
// bool handleWantNodeNum(NodeNum n);

Expand Down Expand Up @@ -250,4 +253,4 @@ extern uint32_t error_address;
#define Module_Config_size \
(ModuleConfig_CannedMessageConfig_size + ModuleConfig_ExternalNotificationConfig_size + ModuleConfig_MQTTConfig_size + \
ModuleConfig_RangeTestConfig_size + ModuleConfig_SerialConfig_size + ModuleConfig_StoreForwardConfig_size + \
ModuleConfig_TelemetryConfig_size + ModuleConfig_size)
ModuleConfig_TelemetryConfig_size + ModuleConfig_size)
5 changes: 4 additions & 1 deletion src/mesh/RadioInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,9 @@ size_t RadioInterface::beginSending(meshtastic_MeshPacket *p)
p->hop_limit = HOP_RELIABLE;
}
h->flags = p->hop_limit | (p->want_ack ? PACKET_FLAGS_WANT_ACK_MASK : 0);
h->flags |= (p->original_hop_limit & PACKET_FLAGS_HOP_MASK) << PACKET_FLAGS_ORIG_HOP_SHIFT;
h->next_hop = p->next_hop;
h->relay_node = p->relay_node;

// if the sender nodenum is zero, that means uninitialized
assert(h->from);
Expand All @@ -548,4 +551,4 @@ size_t RadioInterface::beginSending(meshtastic_MeshPacket *p)

sendingPacket = p;
return p->encrypted.size + sizeof(PacketHeader);
}
}
8 changes: 8 additions & 0 deletions src/mesh/RadioInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

#define PACKET_FLAGS_HOP_MASK 0x07
#define PACKET_FLAGS_WANT_ACK_MASK 0x08
#define PACKET_FLAGS_ORIG_HOP_MASK 0x70
#define PACKET_FLAGS_ORIG_HOP_SHIFT 4

/**
* This structure has to exactly match the wire layout when sent over the radio link. Used to keep compatibility
Expand All @@ -31,6 +33,12 @@ typedef struct {

/** The channel hash - used as a hint for the decoder to limit which channels we consider */
uint8_t channel;

// Last byte of the NodeNum of the next-hop for this packet
uint8_t next_hop;

// Last byte of the NodeNum of the node that will relay/relayed this packet
uint8_t relay_node;
} PacketHeader;

/**
Expand Down
3 changes: 3 additions & 0 deletions src/mesh/RadioLibInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,10 @@ void RadioLibInterface::handleReceiveInterrupt()
mp->channel = h->channel;
assert(HOP_MAX <= PACKET_FLAGS_HOP_MASK); // If hopmax changes, carefully check this code
mp->hop_limit = h->flags & PACKET_FLAGS_HOP_MASK;
mp->original_hop_limit = (h->flags & PACKET_FLAGS_ORIG_HOP_MASK) >> PACKET_FLAGS_ORIG_HOP_SHIFT;
mp->want_ack = !!(h->flags & PACKET_FLAGS_WANT_ACK_MASK);
mp->next_hop = h->next_hop;
mp->relay_node = h->relay_node;

addReceiveMetadata(mp);

Expand Down
41 changes: 28 additions & 13 deletions src/mesh/ReliableRouter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ ErrorCode ReliableRouter::send(meshtastic_MeshPacket *p)
}
}

return FloodingRouter::send(p);
return (config.lora.next_hop_routing && p->to != NODENUM_BROADCAST) ? NextHopRouter::send(p) : FloodingRouter::send(p);
}

bool ReliableRouter::shouldFilterReceived(const meshtastic_MeshPacket *p)
Expand Down Expand Up @@ -71,19 +71,19 @@ bool ReliableRouter::shouldFilterReceived(const meshtastic_MeshPacket *p)
i->second.nextTxMsec += iface->getPacketTime(p);
}

/* Resend implicit ACKs for repeated packets (assuming the original packet was sent with HOP_RELIABLE)
* this way if an implicit ACK is dropped and a packet is resent we'll rebroadcast again.
* Resending real ACKs is omitted, as you might receive a packet multiple times due to flooding and
* flooding this ACK back to the original sender already adds redundancy. */
if (wasSeenRecently(p, false) && p->hop_limit == HOP_RELIABLE && !MeshModule::currentReply && p->to != nodeDB.getNodeNum()) {
/* Resend implicit ACKs for repeated packets (hop limit is the original setting) this way if an implicit ACK is dropped and a
* packet is resent we'll rebroadcast again. Resending real ACKs is omitted, as you might receive a packet multiple times due
* to flooding and flooding this ACK back to the original sender already adds redundancy. */
if (wasSeenRecently(p, false) && (p->original_hop_limit == p->hop_limit) && !MeshModule::currentReply &&
p->to != nodeDB.getNodeNum()) {
// retransmission on broadcast has hop_limit still equal to HOP_RELIABLE
LOG_DEBUG("Resending implicit ack for a repeated floodmsg\n");
meshtastic_MeshPacket *tosend = packetPool.allocCopy(*p);
tosend->hop_limit--; // bump down the hop count
Router::send(tosend);
}

return FloodingRouter::shouldFilterReceived(p);
return config.lora.next_hop_routing ? NextHopRouter::shouldFilterReceived(p) : FloodingRouter::shouldFilterReceived(p);
}

/**
Expand Down Expand Up @@ -132,8 +132,9 @@ void ReliableRouter::sniffReceived(const meshtastic_MeshPacket *p, const meshtas
}
}

// handle the packet as normal
FloodingRouter::sniffReceived(p, c);
// For DMs, we use the NextHopRouter, whereas for broadcasts, we use the FloodingRouter
config.lora.next_hop_routing && (p->to != NODENUM_BROADCAST) ? NextHopRouter::sniffReceived(p, c)
: FloodingRouter::sniffReceived(p, c);
}

#define NUM_RETRANSMISSIONS 3
Expand Down Expand Up @@ -222,9 +223,23 @@ int32_t ReliableRouter::doRetransmissions()
LOG_DEBUG("Sending reliable retransmission fr=0x%x,to=0x%x,id=0x%x, tries left=%d\n", p.packet->from,
p.packet->to, p.packet->id, p.numRetransmissions);

// Note: we call the superclass version because we don't want to have our version of send() add a new
// retransmission record
FloodingRouter::send(packetPool.allocCopy(*p.packet));
if (config.lora.next_hop_routing && p.packet->to != NODENUM_BROADCAST) {
if (p.numRetransmissions == 1) {
// Last retransmission, reset next_hop (fallback to FloodingRouter)
p.packet->next_hop = NO_NEXT_HOP_PREFERENCE;
// Also reset it in the nodeDB
meshtastic_NodeInfoLite *sentTo = nodeDB.getMeshNode(p.packet->to);
if (sentTo) {
LOG_DEBUG("Resetting next hop for packet with dest 0x%x\n", p.packet->to);
sentTo->next_hop = NO_NEXT_HOP_PREFERENCE;
}
}
NextHopRouter::send(packetPool.allocCopy(*p.packet));
} else {
// Note: we call the superclass version because we don't want to have our version of send() add a new
// retransmission record
FloodingRouter::send(packetPool.allocCopy(*p.packet));
}

// Queue again
--p.numRetransmissions;
Expand All @@ -251,4 +266,4 @@ void ReliableRouter::setNextTx(PendingPacket *pending)
LOG_DEBUG("Setting next retransmission in %u msecs: ", d);
printPacket("", pending->packet);
setReceivedMessage(); // Run ASAP, so we can figure out our correct sleep time
}
}
6 changes: 3 additions & 3 deletions src/mesh/ReliableRouter.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "FloodingRouter.h"
#include "NextHopRouter.h"
#include <unordered_map>

/**
Expand Down Expand Up @@ -51,7 +51,7 @@ class GlobalPacketIdHashFunction
/**
* This is a mixin that extends Router with the ability to do (one hop only) reliable message sends.
*/
class ReliableRouter : public FloodingRouter
class ReliableRouter : public NextHopRouter
{
private:
std::unordered_map<GlobalPacketId, PendingPacket, GlobalPacketIdHashFunction> pending;
Expand Down Expand Up @@ -120,4 +120,4 @@ class ReliableRouter : public FloodingRouter
int32_t doRetransmissions();

void setNextTx(PendingPacket *pending);
};
};
6 changes: 6 additions & 0 deletions src/mesh/Router.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,12 @@ ErrorCode Router::send(meshtastic_MeshPacket *p)
// the lora we need to make sure we have replaced it with our local address
p->from = getFrom(p);

// If we are the original transmitter, set the original hop limit
if (p->from == getNodeNum())
GUVWAF marked this conversation as resolved.
Show resolved Hide resolved
p->original_hop_limit = config.lora.hop_limit ? config.lora.hop_limit : HOP_RELIABLE;

p->relay_node = nodeDB.getLastByteOfNodeNum(getNodeNum()); // set the relayer to us

// If the packet hasn't yet been encrypted, do so now (it might already be encrypted if we are just forwarding it)

assert(p->which_payload_variant == meshtastic_MeshPacket_encrypted_tag ||
Expand Down
2 changes: 1 addition & 1 deletion src/mesh/generated/meshtastic/apponly.pb.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ extern const pb_msgdesc_t meshtastic_ChannelSet_msg;
#define meshtastic_ChannelSet_fields &meshtastic_ChannelSet_msg

/* Maximum encoded size of messages (where known) */
#define meshtastic_ChannelSet_size 591
#define meshtastic_ChannelSet_size 593

#ifdef __cplusplus
} /* extern "C" */
Expand Down
Loading