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

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -684,6 +684,9 @@ void setup()
audioThread = new AudioThread();
#endif

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 @@ -61,4 +61,4 @@ void FloodingRouter::sniffReceived(const meshtastic_MeshPacket *p, const meshtas
}
// handle the packet as normal
Router::sniffReceived(p, c);
}
}
93 changes: 93 additions & 0 deletions src/mesh/NextHopRouter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#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)
{
NodeNum ourNodeNum = getNodeNum();
bool isAck =
((c && c->error_reason == meshtastic_Routing_Error_NONE)); // consider only ROUTING_APP message without error as ACK
if (isAck || (p->to == ourNodeNum)) {
// Update next-hop for the original transmitter 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 != ourNodeNum) {
if (p->relay_node) {
meshtastic_NodeInfoLite *origTx = nodeDB->getMeshNode(p->from);
if (origTx) {
LOG_DEBUG("Update next hop of 0x%x to 0x%x based on received DM or ACK.\n", p->from, p->relay_node);
origTx->next_hop = p->relay_node;
}
}
}
}

if (config.device.role != meshtastic_Config_DeviceConfig_Role_CLIENT_MUTE) {
if ((p->to != ourNodeNum) && (getFrom(p) != ourNodeNum)) {
if (p->next_hop == nodeDB->getLastByteOfNodeNum(ourNodeNum)) {
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);

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: 7 additions & 1 deletion src/mesh/NodeDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -938,8 +938,14 @@ void NodeDB::updateFrom(const meshtastic_MeshPacket &mp)
info->via_mqtt = mp.via_mqtt; // Store if we received this packet via MQTT

// If hopStart was set and there wasn't someone messing with the limit in the middle, add hopsAway
if (mp.hop_start != 0 && mp.hop_limit <= mp.hop_start)
if (mp.hop_start != 0 && mp.hop_limit <= mp.hop_start) {
info->hops_away = mp.hop_start - mp.hop_limit;
if (info->hops_away == 0) {
info->next_hop = getLastByteOfNodeNum(mp.from);
} else if (info->hops_away == 1) {
info->next_hop = getLastByteOfNodeNum(mp.relay_node);
}
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/mesh/NodeDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,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
4 changes: 2 additions & 2 deletions src/mesh/RadioInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -573,14 +573,14 @@ size_t RadioInterface::beginSending(meshtastic_MeshPacket *p)
h->to = p->to;
h->id = p->id;
h->channel = p->channel;
h->next_hop = 0; // *** For future use ***
h->relay_node = 0; // *** For future use ***
if (p->hop_limit > HOP_MAX) {
LOG_WARN("hop limit %d is too high, setting to %d\n", p->hop_limit, HOP_RELIABLE);
p->hop_limit = HOP_RELIABLE;
}
h->flags = p->hop_limit | (p->want_ack ? PACKET_FLAGS_WANT_ACK_MASK : 0) | (p->via_mqtt ? PACKET_FLAGS_VIA_MQTT_MASK : 0);
h->flags |= (p->hop_start << PACKET_FLAGS_HOP_START_SHIFT) & PACKET_FLAGS_HOP_START_MASK;
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 Down
4 changes: 2 additions & 2 deletions src/mesh/RadioInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ typedef struct {
/** The channel hash - used as a hint for the decoder to limit which channels we consider */
uint8_t channel;

// ***For future use*** Last byte of the NodeNum of the next-hop for this packet
// Last byte of the NodeNum of the next-hop for this packet
uint8_t next_hop;

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

Expand Down
2 changes: 2 additions & 0 deletions src/mesh/RadioLibInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,8 @@ void RadioLibInterface::handleReceiveInterrupt()
mp->hop_start = (h->flags & PACKET_FLAGS_HOP_START_MASK) >> PACKET_FLAGS_HOP_START_SHIFT;
mp->want_ack = !!(h->flags & PACKET_FLAGS_WANT_ACK_MASK);
mp->via_mqtt = !!(h->flags & PACKET_FLAGS_VIA_MQTT_MASK);
mp->next_hop = h->next_hop;
mp->relay_node = h->relay_node;

addReceiveMetadata(mp);

Expand Down
29 changes: 22 additions & 7 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 @@ -83,7 +83,7 @@ bool ReliableRouter::shouldFilterReceived(const meshtastic_MeshPacket *p)
Router::send(tosend);
}

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

/**
Expand Down Expand Up @@ -133,8 +133,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 @@ -227,9 +228,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;
}
GUVWAF marked this conversation as resolved.
Show resolved Hide resolved
}
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 Down
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);
};
};
1 change: 1 addition & 0 deletions src/mesh/Router.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ 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);

p->relay_node = nodeDB->getLastByteOfNodeNum(getNodeNum()); // set the relayer to us
// If we are the original transmitter, set the hop limit with which we start
if (p->from == getNodeNum())
GUVWAF marked this conversation as resolved.
Show resolved Hide resolved
p->hop_start = p->hop_limit;
Expand Down
5 changes: 5 additions & 0 deletions src/mesh/generated/meshtastic/config.pb.h
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,8 @@ typedef struct _meshtastic_Config_LoRaConfig {
Please respect your local laws and regulations. If you are a HAM, make sure you
enable HAM mode and turn off encryption. */
float override_frequency;
/* If the NeighborInfo Module is enabled, use its information for next hop-based routing */
bool next_hop_routing;
/* For testing it is useful sometimes to force a node to never listen to
particular other nodes (simulating radio out of range). All nodenums listed
in ignore_incoming will have packets they send dropped on receive (by router.cpp) */
Expand Down Expand Up @@ -697,6 +699,7 @@ extern "C" {
#define meshtastic_Config_LoRaConfig_override_duty_cycle_tag 12
#define meshtastic_Config_LoRaConfig_sx126x_rx_boosted_gain_tag 13
#define meshtastic_Config_LoRaConfig_override_frequency_tag 14
#define meshtastic_Config_LoRaConfig_next_hop_routing_tag 15
#define meshtastic_Config_LoRaConfig_ignore_incoming_tag 103
#define meshtastic_Config_LoRaConfig_ignore_mqtt_tag 104
#define meshtastic_Config_BluetoothConfig_enabled_tag 1
Expand Down Expand Up @@ -825,6 +828,7 @@ X(a, STATIC, SINGULAR, UINT32, channel_num, 11) \
X(a, STATIC, SINGULAR, BOOL, override_duty_cycle, 12) \
X(a, STATIC, SINGULAR, BOOL, sx126x_rx_boosted_gain, 13) \
X(a, STATIC, SINGULAR, FLOAT, override_frequency, 14) \
X(a, STATIC, SINGULAR, BOOL, next_hop_routing, 15) \
X(a, STATIC, REPEATED, UINT32, ignore_incoming, 103) \
X(a, STATIC, SINGULAR, BOOL, ignore_mqtt, 104)
#define meshtastic_Config_LoRaConfig_CALLBACK NULL
Expand Down Expand Up @@ -863,6 +867,7 @@ extern const pb_msgdesc_t meshtastic_Config_BluetoothConfig_msg;
#define meshtastic_Config_BluetoothConfig_size 10
#define meshtastic_Config_DeviceConfig_size 100
#define meshtastic_Config_DisplayConfig_size 30
#define meshtastic_Config_LoRaConfig_size 82
#define meshtastic_Config_LoRaConfig_size 80
#define meshtastic_Config_NetworkConfig_IpV4Config_size 20
#define meshtastic_Config_NetworkConfig_size 196
Expand Down
Loading
Loading