From 5a65a48e36d2aeb2029e151b3e80d59f28ac1121 Mon Sep 17 00:00:00 2001 From: iczero Date: Wed, 26 Feb 2020 01:54:45 -0500 Subject: [PATCH 1/2] Prefer routes with lowest rtt --- src/subnet.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 50 insertions(+), 6 deletions(-) diff --git a/src/subnet.c b/src/subnet.c index 94000cc0e..e6984564e 100644 --- a/src/subnet.c +++ b/src/subnet.c @@ -264,17 +264,39 @@ subnet_t *lookup_subnet_ipv4(const ipv4_t *address) { // Search all subnets for a matching one + int last_rtt = INT_MAX; // current smallest rtt seen + for splay_each(subnet_t, p, &subnet_tree) { if(!p || p->type != SUBNET_IPV4) { continue; } if(!maskcmp(address, &p->net.ipv4.address, p->net.ipv4.prefixlength)) { - r = p; - - if(!p->owner || p->owner->status.reachable) { + if(!p->owner) { + // this is a broadcast subnet + r = p; break; } + + if(p->owner->status.reachable) { + int rtt = INT_MAX - 1; // use INT_MAX - 1 as rtt for nodes not directly reachable + + if(p->owner->udp_ping_rtt != -1) { + rtt = p->owner->udp_ping_rtt; + } else if(p->owner == myself) { + // we have this subnet, don't route it somewhere else + r = p; + break; + } + + if(rtt < last_rtt) { + r = p; + last_rtt = rtt; + } + } + } else if(r) { + // no more matching subnets + break; } } @@ -298,17 +320,39 @@ subnet_t *lookup_subnet_ipv6(const ipv6_t *address) { // Search all subnets for a matching one + int last_rtt = INT_MAX; // current smallest rtt seen + for splay_each(subnet_t, p, &subnet_tree) { if(!p || p->type != SUBNET_IPV6) { continue; } if(!maskcmp(address, &p->net.ipv6.address, p->net.ipv6.prefixlength)) { - r = p; - - if(!p->owner || p->owner->status.reachable) { + if(!p->owner) { + // this is a broadcast subnet + r = p; break; } + + if(p->owner->status.reachable) { + int rtt = INT_MAX - 1; // use INT_MAX - 1 as rtt for nodes not directly reachable + + if(p->owner->udp_ping_rtt != -1) { + rtt = p->owner->udp_ping_rtt; + } else if(p->owner == myself) { + // we have this subnet, don't route it somewhere else + r = p; + break; + } + + if(rtt < last_rtt) { + r = p; + last_rtt = rtt; + } + } + } else if(r) { + // no more matching subnets + break; } } From 2b885a0e84b6ae2cfd2611ab6e0e5e95d360331c Mon Sep 17 00:00:00 2001 From: iczero Date: Thu, 16 Sep 2021 02:08:37 -0400 Subject: [PATCH 2/2] Fix bug where less specific subnet may be selected --- src/subnet.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/subnet.c b/src/subnet.c index e6984564e..b5c3b5fb6 100644 --- a/src/subnet.c +++ b/src/subnet.c @@ -265,12 +265,18 @@ subnet_t *lookup_subnet_ipv4(const ipv4_t *address) { // Search all subnets for a matching one int last_rtt = INT_MAX; // current smallest rtt seen + int last_prefix_length = -1; // most specific prefix length seen for splay_each(subnet_t, p, &subnet_tree) { if(!p || p->type != SUBNET_IPV4) { continue; } + // we are on a route less specific than the one we found, break + if(p->net.ipv4.prefixlength < last_prefix_length) { + break; + } + if(!maskcmp(address, &p->net.ipv4.address, p->net.ipv4.prefixlength)) { if(!p->owner) { // this is a broadcast subnet @@ -292,10 +298,11 @@ subnet_t *lookup_subnet_ipv4(const ipv4_t *address) { if(rtt < last_rtt) { r = p; last_rtt = rtt; + last_prefix_length = p->net.ipv4.prefixlength; } } } else if(r) { - // no more matching subnets + // no more matching subnets with equal or greater specificity break; } } @@ -321,12 +328,18 @@ subnet_t *lookup_subnet_ipv6(const ipv6_t *address) { // Search all subnets for a matching one int last_rtt = INT_MAX; // current smallest rtt seen + int last_prefix_length = -1; // most specific prefix length seen for splay_each(subnet_t, p, &subnet_tree) { if(!p || p->type != SUBNET_IPV6) { continue; } + // we are on a route less specific than the one we found, break + if(p->net.ipv6.prefixlength < last_prefix_length) { + break; + } + if(!maskcmp(address, &p->net.ipv6.address, p->net.ipv6.prefixlength)) { if(!p->owner) { // this is a broadcast subnet @@ -348,10 +361,11 @@ subnet_t *lookup_subnet_ipv6(const ipv6_t *address) { if(rtt < last_rtt) { r = p; last_rtt = rtt; + last_prefix_length = p->net.ipv6.prefixlength; } } } else if(r) { - // no more matching subnets + // no more matching subnets with equal or greater specificity break; } }