From b004b9e2a18bf6425d25e41257fd38954d82bf6e Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Wed, 28 Nov 2018 07:32:38 -0500 Subject: [PATCH] fix up exit logic --- include/llarp/aligned.hpp | 2 +- include/llarp/exit/endpoint.hpp | 5 +++++ include/llarp/handlers/exit.hpp | 3 +++ llarp/exit/endpoint.cpp | 11 +++++++++++ llarp/handlers/exit.cpp | 26 ++++++++++++++++++-------- 5 files changed, 38 insertions(+), 9 deletions(-) diff --git a/include/llarp/aligned.hpp b/include/llarp/aligned.hpp index dc63e5635..f5ee817d7 100644 --- a/include/llarp/aligned.hpp +++ b/include/llarp/aligned.hpp @@ -19,7 +19,7 @@ extern "C" } namespace llarp { - /// aligned buffer, aligns to the nears Long_t + /// aligned buffer that is sz bytes long and aligns to the nears Long_t template < size_t sz, bool randomize = false, typename Long_t = uint64_t > struct AlignedBuffer { diff --git a/include/llarp/exit/endpoint.hpp b/include/llarp/exit/endpoint.hpp index fd101cfab..251dd5851 100644 --- a/include/llarp/exit/endpoint.hpp +++ b/include/llarp/exit/endpoint.hpp @@ -35,6 +35,10 @@ namespace llarp bool ExpiresSoon(llarp_time_t now, llarp_time_t dlt = 5000) const; + /// return true if this endpoint looks dead right now + bool + LooksDead(llarp_time_t now, llarp_time_t timeout = 10000) const; + /// tick ourself, reset tx/rx rates void Tick(llarp_time_t now); @@ -92,6 +96,7 @@ namespace llarp llarp::PathID_t m_CurrentPath; llarp::huint32_t m_IP; uint64_t m_TxRate, m_RxRate; + llarp_time_t m_LastActive; bool m_RewriteSource; }; } // namespace exit diff --git a/include/llarp/handlers/exit.hpp b/include/llarp/handlers/exit.hpp index 14f78f381..94613956c 100644 --- a/include/llarp/handlers/exit.hpp +++ b/include/llarp/handlers/exit.hpp @@ -44,6 +44,9 @@ namespace llarp llarp_router* Router(); + llarp_time_t + Now() const; + llarp_crypto* Crypto(); diff --git a/llarp/exit/endpoint.cpp b/llarp/exit/endpoint.cpp index c50b81bf7..c990964cd 100644 --- a/llarp/exit/endpoint.cpp +++ b/llarp/exit/endpoint.cpp @@ -14,6 +14,7 @@ namespace llarp , m_IP(ip) , m_RewriteSource(rewriteIP) { + m_LastActive = parent->Now(); } Endpoint::~Endpoint() @@ -65,6 +66,15 @@ namespace llarp return true; } + bool Endpoint::LooksDead(llarp_time_t now, llarp_time_t timeout) const + { + if(ExpiresSoon(now, timeout)) + return true; + if (now > m_LastActive) + return now - m_LastActive > timeout; + return true; + } + bool Endpoint::SendOutboundTraffic(llarp_buffer_t buf) { @@ -83,6 +93,7 @@ namespace llarp return false; } m_TxRate += buf.sz; + m_LastActive = m_Parent->Now(); return true; } diff --git a/llarp/handlers/exit.cpp b/llarp/handlers/exit.cpp index ff42293c4..b9a2dec8f 100644 --- a/llarp/handlers/exit.cpp +++ b/llarp/handlers/exit.cpp @@ -37,10 +37,16 @@ namespace llarp { } + llarp_time_t + ExitEndpoint::Now() const + { + return m_Router->Now(); + } + void ExitEndpoint::FlushInbound() { - auto now = Router()->Now(); + auto now = Now(); m_InetToNetwork.Process([&](Pkt_t &pkt) { llarp::PubKey pk; { @@ -58,23 +64,27 @@ namespace llarp auto range = m_ActiveExits.equal_range(pk); auto itr = range.first; uint64_t min = std::numeric_limits< uint64_t >::max(); - /// pick path with lowest rx rate + /// pick non dead looking path with lowest tx rate while(itr != range.second) { - if(ep == nullptr) - ep = itr->second.get(); - else if(itr->second->RxRate() < min && !itr->second->ExpiresSoon(now)) + if(itr->second->TxRate() < min && !itr->second->LooksDead(now)) { - min = ep->RxRate(); ep = itr->second.get(); + min = ep->TxRate(); } ++itr; } - if(ep) + + if(ep == nullptr) + { + // we may have all dead sessions, wtf now? + llarp::LogWarn(Name(), " dropped inbound traffic for session ", pk, " as we have no working endpoints"); + } + else { if(!ep->SendInboundTraffic(pkt.Buffer())) { - llarp::LogWarn(Name(), " dropped inbound traffic for session ", pk); + llarp::LogWarn(Name(), " dropped inbound traffic for session ", pk, " as we are overloaded (probably)"); } } });