From 64e9622270438701d4e351c8b0c415f0be519dd3 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Mon, 1 Jul 2019 09:44:25 -0400 Subject: [PATCH] start seperating tun and endpoint --- llarp/handlers/null.hpp | 16 ++----------- llarp/handlers/tun.cpp | 2 ++ llarp/handlers/tun.hpp | 25 ++++++++++---------- llarp/nodedb.hpp | 1 - llarp/path/pathbuilder.cpp | 9 +++++++- llarp/path/pathset.cpp | 37 ++++++++++++++++++++++++++++++ llarp/path/pathset.hpp | 31 +++++++++++++++++++++++++ llarp/service/context.cpp | 23 ------------------- llarp/service/context.hpp | 6 +---- llarp/service/endpoint.cpp | 29 ++++++++++------------- llarp/service/endpoint.hpp | 29 ++++++++++++----------- llarp/service/outbound_context.cpp | 8 ++++--- llarp/service/protocol.cpp | 4 ++-- llarp/service/protocol.hpp | 3 ++- llarp/util/fs.cpp | 10 ++++---- llarp/utp/session.cpp | 4 ++-- 16 files changed, 137 insertions(+), 100 deletions(-) diff --git a/llarp/handlers/null.hpp b/llarp/handlers/null.hpp index fb71eea3a..3e0ebea89 100644 --- a/llarp/handlers/null.hpp +++ b/llarp/handlers/null.hpp @@ -18,8 +18,8 @@ namespace llarp } bool - HandleWriteIPPacket(const llarp_buffer_t &, - std::function< huint128_t(void) >) override + HandleIPPacket(const AlignedBuffer< 32 >, const llarp_buffer_t &, + bool) override { return true; } @@ -35,18 +35,6 @@ namespace llarp { return false; } - - huint128_t - ObtainIPForAddr(const AlignedBuffer< 32 > &, bool) override - { - return {0}; - } - - bool - HasAddress(const AlignedBuffer< 32 > &) const override - { - return false; - } }; } // namespace handlers } // namespace llarp diff --git a/llarp/handlers/tun.cpp b/llarp/handlers/tun.cpp index b38adb0ca..ec8d9b0b0 100644 --- a/llarp/handlers/tun.cpp +++ b/llarp/handlers/tun.cpp @@ -385,6 +385,8 @@ namespace llarp context->ForEachService( [&](const std::string &, const std::shared_ptr< service::Endpoint > &service) -> bool { + if(!service->HasIfAddr()) + return true; huint128_t ip = service->GetIfAddr(); if(ip.h) { diff --git a/llarp/handlers/tun.hpp b/llarp/handlers/tun.hpp index c26153d87..82b148e9f 100644 --- a/llarp/handlers/tun.hpp +++ b/llarp/handlers/tun.hpp @@ -81,25 +81,27 @@ namespace llarp SetupNetworking() override; /// overrides Endpoint + bool + HandleIPPacket(const AlignedBuffer< 32 > addr, const llarp_buffer_t& buf, + bool serviceNode) override + { + return HandleWriteIPPacket(buf, [=]() -> huint128_t { + return ObtainIPForAddr(addr, serviceNode); + }); + } + /// handle inbound traffic bool HandleWriteIPPacket(const llarp_buffer_t& buf, - std::function< huint128_t(void) > getFromIP) override; + std::function< huint128_t(void) > getFromIP); /// queue outbound packet to the world bool QueueOutboundTraffic(llarp::net::IPPacket&& pkt); - /// we have a resolvable ip address - bool - HasIfAddr() const override - { - return true; - } - /// get the local interface's address huint128_t - GetIfAddr() const override; + GetIfAddr() const; bool HasLocalIP(const huint128_t& ip) const; @@ -142,15 +144,14 @@ namespace llarp } bool - HasAddress(const AlignedBuffer< 32 >& addr) const override + HasAddress(const AlignedBuffer< 32 >& addr) const { return m_AddrToIP.find(addr) != m_AddrToIP.end(); } /// get ip address for key unconditionally huint128_t - ObtainIPForAddr(const AlignedBuffer< 32 >& addr, - bool serviceNode) override; + ObtainIPForAddr(const AlignedBuffer< 32 >& addr, bool serviceNode); /// flush network traffic void diff --git a/llarp/nodedb.hpp b/llarp/nodedb.hpp index 38e62f2ab..d3eb6fe6e 100644 --- a/llarp/nodedb.hpp +++ b/llarp/nodedb.hpp @@ -161,7 +161,6 @@ struct llarp_nodedb void SaveAll() LOCKS_EXCLUDED(access); - }; /// struct for async rc verification diff --git a/llarp/path/pathbuilder.cpp b/llarp/path/pathbuilder.cpp index f19177a9f..cbb897bf4 100644 --- a/llarp/path/pathbuilder.cpp +++ b/llarp/path/pathbuilder.cpp @@ -138,6 +138,7 @@ namespace llarp ctx.router->PersistSessionUntil(remote, ctx.path->ExpireTime()); // add own path ctx.router->pathContext().AddOwnPath(ctx.pathset, ctx.path); + ctx.pathset->PathBuildStarted(ctx.path); } else LogError(ctx.pathset->Name(), " failed to send LRCM to ", remote); @@ -166,12 +167,17 @@ namespace llarp if(ShouldBuildMore(now)) BuildOne(); TickPaths(now, router); + if(m_BuildStats.SuccsessRatio() <= BuildStats::MinGoodRatio) + { + LogWarn(Name(), " has a low path build success. ", m_BuildStats); + } } util::StatusObject Builder::ExtractStatus() const { - util::StatusObject obj{{"numHops", uint64_t(numHops)}, + util::StatusObject obj{{"buildStats", m_BuildStats.ExtractStatus()}, + {"numHops", uint64_t(numHops)}, {"numPaths", uint64_t(m_NumPaths)}}; std::vector< util::StatusObject > pathObjs; std::transform(m_Paths.begin(), m_Paths.end(), @@ -425,6 +431,7 @@ namespace llarp buildIntervalLimit = MIN_PATH_BUILD_INTERVAL; router->routerProfiling().MarkPathSuccess(p.get()); LogInfo(p->Name(), " built latency=", p->intro.latency); + m_BuildStats.success++; } void diff --git a/llarp/path/pathset.cpp b/llarp/path/pathset.cpp index 62fbbb1a7..a326bbde2 100644 --- a/llarp/path/pathset.cpp +++ b/llarp/path/pathset.cpp @@ -288,6 +288,43 @@ namespace llarp PathSet::HandlePathBuildTimeout(Path_ptr p) { LogWarn(Name(), " path build ", p->HopsString(), " timed out"); + m_BuildStats.timeouts++; + } + + void + PathSet::PathBuildStarted(Path_ptr p) + { + LogInfo(Name(), " path build ", p->HopsString(), " started"); + m_BuildStats.attempts++; + } + + util::StatusObject + BuildStats::ExtractStatus() const + { + return util::StatusObject{{"success", success}, + {"attempts", attempts}, + {"timeouts", timeouts}, + {"fails", fails}}; + } + + std::string + BuildStats::ToString() const + { + std::stringstream ss; + ss << SuccsessRatio() << " percent success "; + ss << "(success=" << success << " "; + ss << "attempts=" << attempts << " "; + ss << "timeouts=" << timeouts << " "; + ss << "fails=" << fails << ")"; + return ss.str(); + } + + double + BuildStats::SuccsessRatio() const + { + if(attempts) + return success / attempts; + return 0; } bool diff --git a/llarp/path/pathset.hpp b/llarp/path/pathset.hpp index 795ee998b..00e7d91bb 100644 --- a/llarp/path/pathset.hpp +++ b/llarp/path/pathset.hpp @@ -38,6 +38,32 @@ namespace llarp ePathExpired }; + /// Stats about all our path builds + struct BuildStats + { + static constexpr double MinGoodRatio = 0.25; + + uint64_t attempts = 0; + uint64_t success = 0; + uint64_t fails = 0; + uint64_t timeouts = 0; + + util::StatusObject + ExtractStatus() const; + + double + SuccsessRatio() const; + + std::string + ToString() const; + + friend std::ostream& + operator<<(std::ostream& o, const BuildStats& st) + { + return o << st.ToString(); + } + }; + /// the role of this path can fulfill using PathRole = int; @@ -99,6 +125,9 @@ namespace llarp virtual void HandlePathBuildTimeout(Path_ptr path); + virtual void + PathBuildStarted(Path_ptr path); + /// a path died now what? virtual void HandlePathDied(Path_ptr path) = 0; @@ -243,6 +272,8 @@ namespace llarp protected: size_t m_NumPaths; + BuildStats m_BuildStats; + void TickPaths(llarp_time_t now, AbstractRouter* r); diff --git a/llarp/service/context.cpp b/llarp/service/context.cpp index c595faef0..b99e1b271 100644 --- a/llarp/service/context.cpp +++ b/llarp/service/context.cpp @@ -152,29 +152,6 @@ namespace llarp return m_Endpoints.size() ? true : false; } - bool - Context::FindBestAddressFor(const AlignedBuffer< 32 > &addr, bool isSNode, - huint128_t &ip) - { - auto itr = m_Endpoints.begin(); - while(itr != m_Endpoints.end()) - { - if(itr->second->HasAddress(addr)) - { - ip = itr->second->ObtainIPForAddr(addr, isSNode); - return true; - } - ++itr; - } - itr = m_Endpoints.find("default"); - if(itr != m_Endpoints.end()) - { - ip = itr->second->ObtainIPForAddr(addr, isSNode); - return true; - } - return false; - } - bool Context::AddDefaultEndpoint( const std::unordered_multimap< std::string, std::string > &opts) diff --git a/llarp/service/context.hpp b/llarp/service/context.hpp index 2c9ac57a9..9a860e30b 100644 --- a/llarp/service/context.hpp +++ b/llarp/service/context.hpp @@ -31,10 +31,6 @@ namespace llarp bool hasEndpoints(); - bool - FindBestAddressFor(const AlignedBuffer< 32 > &addr, bool isSNode, - huint128_t &); - /// function visitor returns false to prematurely break iteration void ForEachService( @@ -59,7 +55,7 @@ namespace llarp StartAll(); private: - AbstractRouter *m_Router; + AbstractRouter *const m_Router; std::unordered_map< std::string, std::shared_ptr< Endpoint > > m_Endpoints; std::list< std::shared_ptr< Endpoint > > m_Stopped; diff --git a/llarp/service/endpoint.cpp b/llarp/service/endpoint.cpp index b9f9b9ef5..95e927831 100644 --- a/llarp/service/endpoint.cpp +++ b/llarp/service/endpoint.cpp @@ -402,9 +402,9 @@ namespace llarp auto itr = m_Sessions.find(tag); if(itr == m_Sessions.end()) { - itr = m_Sessions.emplace(tag, Session{}).first; - itr->second.inbound = inbound; - itr->second.remote = info; + itr = m_Sessions.emplace(tag, Session{}).first; + itr->second.inbound = inbound; + itr->second.remote = info; } itr->second.lastUsed = Now(); } @@ -797,12 +797,9 @@ namespace llarp if(path && path->SendRoutingMessage(msg, m_Router)) { - LogInfo(Name(), " looking up ", router); m_PendingRouters.emplace(router, RouterLookupJob(this, handler)); return true; } - else - LogError("failed to send request for router lookup"); } return false; } @@ -839,15 +836,15 @@ namespace llarp PutSenderFor(msg->tag, msg->sender, true); PutReplyIntroFor(msg->tag, path->intro); Introduction intro; - intro.pathID = from; - intro.router = PubKey(path->Endpoint()); + intro.pathID = from; + intro.router = PubKey(path->Endpoint()); intro.expiresAt = std::min(path->ExpireTime(), msg->introReply.expiresAt); PutIntroFor(msg->tag, intro); return ProcessDataMessage(msg); } bool - Endpoint::HasPathToSNode(const RouterID& ident) const + Endpoint::HasPathToSNode(const RouterID ident) const { auto range = m_SNodeSessions.equal_range(ident); auto itr = range.first; @@ -963,7 +960,7 @@ namespace llarp } bool - Endpoint::EnsurePathToService(const Address& remote, PathEnsureHook hook, + Endpoint::EnsurePathToService(const Address remote, PathEnsureHook hook, ABSL_ATTRIBUTE_UNUSED llarp_time_t timeoutMS, bool randomPath) { @@ -1009,16 +1006,16 @@ namespace llarp } void - Endpoint::EnsurePathToSNode(const RouterID& snode, SNodeEnsureHook h) + Endpoint::EnsurePathToSNode(const RouterID snode, SNodeEnsureHook h) { using namespace std::placeholders; if(m_SNodeSessions.count(snode) == 0) { - auto themIP = ObtainIPForAddr(snode, true); auto session = std::make_shared< exit::SNodeSession >( snode, - std::bind(&Endpoint::HandleWriteIPPacket, this, _1, - [themIP]() -> huint128_t { return themIP; }), + [=](const llarp_buffer_t& pkt) -> bool { + return HandleIPPacket(snode, pkt, true); + }, m_Router, m_NumPaths, numHops, false, ShouldBundleRC()); m_SNodeSessions.emplace(snode, session); } @@ -1064,9 +1061,7 @@ namespace llarp { const auto& msg = m_InboundTrafficQueue.top(); llarp_buffer_t buf(msg->payload); - HandleWriteIPPacket(buf, [&]() -> huint128_t { - return ObtainIPForAddr(msg->sender.Addr(), false); - }); + HandleIPPacket(msg->sender.Addr(), buf, false); m_InboundTrafficQueue.pop(); } }); diff --git a/llarp/service/endpoint.hpp b/llarp/service/endpoint.hpp index 8dcc96757..c1a3775e7 100644 --- a/llarp/service/endpoint.hpp +++ b/llarp/service/endpoint.hpp @@ -84,7 +84,7 @@ namespace llarp virtual huint128_t GetIfAddr() const { - return huint128_t{0}; + return {0}; } virtual void @@ -148,11 +148,11 @@ namespace llarp HandleHiddenServiceFrame(path::Path_ptr p, const service::ProtocolFrame& msg); - virtual huint128_t - ObtainIPForAddr(const AlignedBuffer< 32 >& addr, bool serviceNode) = 0; + // virtual huint128_t + // ObtainIPForAddr(const AlignedBuffer< 32 >& addr, bool serviceNode) = 0; - virtual bool - HasAddress(const AlignedBuffer< 32 >& addr) const = 0; + // virtual bool + // HasServiceAddress(const AlignedBuffer< 32 >& addr) const = 0; /// return true if we have a pending job to build to a hidden service but /// it's not done yet @@ -169,8 +169,12 @@ namespace llarp std::shared_ptr< ProtocolMessage > msg) override; virtual bool - HandleWriteIPPacket(const llarp_buffer_t& pkt, - std::function< huint128_t(void) > getFromIP) = 0; + HandleIPPacket(const AlignedBuffer< 32 > addr, const llarp_buffer_t& pkt, + bool serviceNode) = 0; + + // virtual bool + // HandleWriteIPPacket(const llarp_buffer_t& pkt, + // std::function< huint128_t(void) > getFromIP) = 0; bool ProcessDataMessage(std::shared_ptr< ProtocolMessage > msg); @@ -237,22 +241,22 @@ namespace llarp /// return false if we have already called this function before for this /// address bool - EnsurePathToService(const Address& remote, PathEnsureHook h, + EnsurePathToService(const Address remote, PathEnsureHook h, uint64_t timeoutMS, bool lookupOnRandomPath = false); using SNodeEnsureHook = - std::function< void(RouterID, exit::BaseSession_ptr) >; + std::function< void(const RouterID, exit::BaseSession_ptr) >; /// ensure a path to a service node by public key void - EnsurePathToSNode(const RouterID& remote, SNodeEnsureHook h); + EnsurePathToSNode(const RouterID remote, SNodeEnsureHook h); /// return true if this endpoint is trying to lookup this router right now bool HasPendingRouterLookup(const RouterID remote) const; bool - HasPathToSNode(const RouterID& remote) const; + HasPathToSNode(const RouterID remote) const; void PutSenderFor(const ConvoTag& tag, const ServiceInfo& info, @@ -292,9 +296,6 @@ namespace llarp GetConvoTagsForService(const Address& si, std::set< ConvoTag >& tag) const override; - - - void PutNewOutboundContext(const IntroSet& introset); diff --git a/llarp/service/outbound_context.cpp b/llarp/service/outbound_context.cpp index 34cc632c5..fb5888144 100644 --- a/llarp/service/outbound_context.cpp +++ b/llarp/service/outbound_context.cpp @@ -321,15 +321,17 @@ namespace llarp { if(markedBad) return false; - const bool should = (!(path::Builder::BuildCooldownHit(now) ||path::Builder::NumInStatus(path::ePathBuilding) >= m_NumPaths)) && path::Builder::ShouldBuildMore(now); + const bool should = + (!(path::Builder::BuildCooldownHit(now) + || path::Builder::NumInStatus(path::ePathBuilding) >= m_NumPaths)) + && path::Builder::ShouldBuildMore(now); if(!ReadyToSend()) { return should; } llarp_time_t t = 0; - ForEachPath([&t](path::Path_ptr path) - { + ForEachPath([&t](path::Path_ptr path) { if(path->IsReady()) t = std::max(path->ExpireTime(), t); }); diff --git a/llarp/service/protocol.cpp b/llarp/service/protocol.cpp index 5777b8dc3..b38a5c5f7 100644 --- a/llarp/service/protocol.cpp +++ b/llarp/service/protocol.cpp @@ -345,7 +345,7 @@ namespace llarp self->msg->handler = self->handler; std::shared_ptr< ProtocolMessage > msg = std::move(self->msg); path::Path_ptr path = std::move(self->path); - const PathID_t from = self->frame.F; + const PathID_t from = self->frame.F; self->logic->queue_func( [=]() { ProtocolMessage::ProcessAsync(path, from, msg); }); delete self; @@ -407,7 +407,7 @@ namespace llarp LogError("failed to decrypt message"); return false; } - msg->handler = handler; + msg->handler = handler; const PathID_t from = F; logic->queue_func( [=]() { ProtocolMessage::ProcessAsync(recvPath, from, msg); }); diff --git a/llarp/service/protocol.hpp b/llarp/service/protocol.hpp index 4a10d16c1..77fb6b95d 100644 --- a/llarp/service/protocol.hpp +++ b/llarp/service/protocol.hpp @@ -63,7 +63,8 @@ namespace llarp PutBuffer(const llarp_buffer_t& payload); static void - ProcessAsync(path::Path_ptr p, PathID_t from, std::shared_ptr< ProtocolMessage > self); + ProcessAsync(path::Path_ptr p, PathID_t from, + std::shared_ptr< ProtocolMessage > self); bool operator<(const ProtocolMessage& other) const diff --git a/llarp/util/fs.cpp b/llarp/util/fs.cpp index 0d647e3f0..8242cce79 100644 --- a/llarp/util/fs.cpp +++ b/llarp/util/fs.cpp @@ -21,7 +21,7 @@ namespace llarp EnsurePrivateFile(fs::path pathname) { const auto str = pathname.string(); - errno = 0; + errno = 0; error_code_t ec = errno_error(); if(fs::exists(pathname, ec)) // file exists { @@ -31,13 +31,13 @@ namespace llarp auto perms = st.permissions(); if((perms & fs::perms::others_exec) != fs::perms::none) perms ^= fs::perms::others_exec; - if((perms & fs::perms::others_write) != fs::perms::none) + if((perms & fs::perms::others_write) != fs::perms::none) perms ^= fs::perms::others_write; - if((perms & fs::perms::others_write) != fs::perms::none) + if((perms & fs::perms::others_write) != fs::perms::none) perms ^= fs::perms::others_write; - if((perms & fs::perms::group_read) != fs::perms::none) + if((perms & fs::perms::group_read) != fs::perms::none) perms ^= fs::perms::group_read; - if((perms & fs::perms::others_read) != fs::perms::none) + if((perms & fs::perms::others_read) != fs::perms::none) perms ^= fs::perms::others_read; if((perms & fs::perms::owner_exec) != fs::perms::none) perms ^= fs::perms::owner_exec; diff --git a/llarp/utp/session.cpp b/llarp/utp/session.cpp index ec7a52b85..baa23f47b 100644 --- a/llarp/utp/session.cpp +++ b/llarp/utp/session.cpp @@ -82,7 +82,7 @@ namespace llarp { OnLinkEstablished(p); metrics::integerTick("utp.session.open", "to", 1, "id", - RouterID(remoteRC.pubkey).ToString()); + RouterID(remoteRC.pubkey).ToString()); OutboundHandshake(); } @@ -592,7 +592,7 @@ namespace llarp if(remoteRC.IsPublicRouter()) { metrics::integerTick("utp.session.close", "to", 1, "id", - RouterID(remoteRC.pubkey).ToString()); + RouterID(remoteRC.pubkey).ToString()); } } }