2021-03-09 22:24:35 +00:00
|
|
|
#pragma once
|
2018-12-10 16:26:46 +00:00
|
|
|
|
2021-03-09 22:24:35 +00:00
|
|
|
#include "path_types.hpp"
|
2021-03-16 11:56:27 +00:00
|
|
|
#include "service/protocol_type.hpp"
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/router_id.hpp>
|
|
|
|
#include <llarp/routing/message.hpp>
|
|
|
|
#include <llarp/service/intro_set.hpp>
|
|
|
|
#include <llarp/util/status.hpp>
|
|
|
|
#include <llarp/util/thread/threading.hpp>
|
|
|
|
#include <llarp/util/time.hpp>
|
2018-12-12 00:48:54 +00:00
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <list>
|
2018-06-25 15:12:08 +00:00
|
|
|
#include <map>
|
|
|
|
#include <tuple>
|
2021-06-02 19:32:05 +00:00
|
|
|
#include <unordered_set>
|
2018-06-25 15:12:08 +00:00
|
|
|
|
2021-03-09 18:39:40 +00:00
|
|
|
namespace std
|
|
|
|
{
|
|
|
|
template <>
|
|
|
|
struct hash<std::pair<llarp::RouterID, llarp::PathID_t>>
|
|
|
|
{
|
|
|
|
size_t
|
|
|
|
operator()(const std::pair<llarp::RouterID, llarp::PathID_t>& i) const
|
|
|
|
{
|
|
|
|
return hash<llarp::RouterID>{}(i.first) ^ hash<llarp::PathID_t>{}(i.second);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace std
|
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
2019-01-16 00:24:16 +00:00
|
|
|
struct RouterContact;
|
2021-02-02 14:35:40 +00:00
|
|
|
class NodeDB;
|
2019-01-16 00:24:16 +00:00
|
|
|
|
2018-07-09 17:32:11 +00:00
|
|
|
namespace dht
|
|
|
|
{
|
|
|
|
struct GotIntroMessage;
|
2019-01-16 00:24:16 +00:00
|
|
|
struct GotRouterMessage;
|
2020-09-17 19:18:08 +00:00
|
|
|
struct GotNameMessage;
|
2019-01-16 00:24:16 +00:00
|
|
|
} // namespace dht
|
2018-07-09 17:32:11 +00:00
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
namespace path
|
|
|
|
{
|
2018-11-14 18:02:27 +00:00
|
|
|
/// status of a path
|
2018-06-25 15:12:08 +00:00
|
|
|
enum PathStatus
|
|
|
|
{
|
|
|
|
ePathBuilding,
|
|
|
|
ePathEstablished,
|
|
|
|
ePathTimeout,
|
2019-06-04 18:31:17 +00:00
|
|
|
ePathFailed,
|
2019-05-31 10:57:41 +00:00
|
|
|
ePathIgnore,
|
2018-06-25 15:12:08 +00:00
|
|
|
ePathExpired
|
|
|
|
};
|
2018-11-14 18:02:27 +00:00
|
|
|
|
2019-07-01 13:44:25 +00:00
|
|
|
/// Stats about all our path builds
|
|
|
|
struct BuildStats
|
|
|
|
{
|
|
|
|
static constexpr double MinGoodRatio = 0.25;
|
|
|
|
|
|
|
|
uint64_t attempts = 0;
|
2020-04-07 18:38:56 +00:00
|
|
|
uint64_t success = 0;
|
|
|
|
uint64_t fails = 0;
|
2019-07-01 13:44:25 +00:00
|
|
|
uint64_t timeouts = 0;
|
|
|
|
|
|
|
|
util::StatusObject
|
|
|
|
ExtractStatus() const;
|
|
|
|
|
|
|
|
double
|
2020-02-26 02:27:34 +00:00
|
|
|
SuccessRatio() const;
|
2019-07-01 13:44:25 +00:00
|
|
|
|
|
|
|
std::string
|
|
|
|
ToString() const;
|
|
|
|
|
|
|
|
friend std::ostream&
|
|
|
|
operator<<(std::ostream& o, const BuildStats& st)
|
|
|
|
{
|
|
|
|
return o << st.ToString();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-01-16 00:24:16 +00:00
|
|
|
/// the role of this path can fulfill
|
2018-11-14 18:02:27 +00:00
|
|
|
using PathRole = int;
|
|
|
|
|
|
|
|
/// capable of any role
|
|
|
|
constexpr PathRole ePathRoleAny = 0;
|
2019-01-16 00:24:16 +00:00
|
|
|
/// outbound hs traffic capable
|
2018-11-14 18:02:27 +00:00
|
|
|
constexpr PathRole ePathRoleOutboundHS = (1 << 0);
|
|
|
|
/// inbound hs traffic capable
|
|
|
|
constexpr PathRole ePathRoleInboundHS = (1 << 1);
|
|
|
|
/// exit traffic capable
|
|
|
|
constexpr PathRole ePathRoleExit = (1 << 2);
|
2018-11-21 12:31:36 +00:00
|
|
|
/// service node capable
|
|
|
|
constexpr PathRole ePathRoleSVC = (1 << 3);
|
2018-11-14 18:02:27 +00:00
|
|
|
/// dht message capable
|
2018-11-21 12:31:36 +00:00
|
|
|
constexpr PathRole ePathRoleDHT = (1 << 4);
|
2018-11-14 18:02:27 +00:00
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
// forward declare
|
|
|
|
struct Path;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
using Path_ptr = std::shared_ptr<Path>;
|
2019-04-23 16:13:22 +00:00
|
|
|
|
|
|
|
struct PathSet;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
using PathSet_ptr = std::shared_ptr<PathSet>;
|
2019-04-23 14:28:59 +00:00
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
/// a set of paths owned by an entity
|
|
|
|
struct PathSet
|
|
|
|
{
|
2019-07-18 16:32:26 +00:00
|
|
|
/// maximum number of paths a path set can maintain
|
|
|
|
static constexpr size_t max_paths = 32;
|
2018-06-25 15:12:08 +00:00
|
|
|
/// construct
|
2020-10-27 20:27:14 +00:00
|
|
|
/// @params numDesiredPaths the number of paths to maintain
|
|
|
|
PathSet(size_t numDesiredPaths);
|
2018-06-25 15:12:08 +00:00
|
|
|
|
2019-04-23 16:13:22 +00:00
|
|
|
/// get a shared_ptr of ourself
|
|
|
|
virtual PathSet_ptr
|
|
|
|
GetSelf() = 0;
|
|
|
|
|
2021-06-04 12:34:41 +00:00
|
|
|
/// get a weak_ptr of ourself
|
|
|
|
virtual std::weak_ptr<PathSet>
|
|
|
|
GetWeak() = 0;
|
|
|
|
|
2019-04-23 16:13:22 +00:00
|
|
|
virtual void
|
|
|
|
BuildOne(PathRole roles = ePathRoleAny) = 0;
|
|
|
|
|
2019-05-06 14:54:05 +00:00
|
|
|
/// manual build on these hops
|
|
|
|
virtual void
|
2021-02-02 14:35:40 +00:00
|
|
|
Build(std::vector<RouterContact> hops, PathRole roles = ePathRoleAny) = 0;
|
2019-05-06 14:54:05 +00:00
|
|
|
|
2018-06-29 16:02:39 +00:00
|
|
|
/// tick owned paths
|
2019-04-23 16:13:22 +00:00
|
|
|
virtual void
|
2022-03-28 22:11:47 +00:00
|
|
|
Tick(llarp_time_t now);
|
2018-06-29 16:02:39 +00:00
|
|
|
|
2018-11-25 16:58:27 +00:00
|
|
|
/// count the number of paths that will exist at this timestamp in future
|
|
|
|
size_t
|
|
|
|
NumPathsExistingAt(llarp_time_t futureTime) const;
|
|
|
|
|
2018-08-02 00:48:43 +00:00
|
|
|
virtual void
|
2019-04-23 14:28:59 +00:00
|
|
|
HandlePathBuilt(Path_ptr path) = 0;
|
2018-06-25 15:12:08 +00:00
|
|
|
|
2018-09-26 13:04:25 +00:00
|
|
|
virtual void
|
2019-06-20 16:22:29 +00:00
|
|
|
HandlePathBuildTimeout(Path_ptr path);
|
2018-09-26 13:04:25 +00:00
|
|
|
|
2019-07-31 12:51:24 +00:00
|
|
|
virtual void
|
2021-03-31 10:54:28 +00:00
|
|
|
HandlePathBuildFailedAt(Path_ptr path, RouterID hop);
|
2019-07-31 12:51:24 +00:00
|
|
|
|
2019-07-01 13:44:25 +00:00
|
|
|
virtual void
|
|
|
|
PathBuildStarted(Path_ptr path);
|
|
|
|
|
2019-03-30 13:02:10 +00:00
|
|
|
/// a path died now what?
|
|
|
|
virtual void
|
2020-11-04 16:08:29 +00:00
|
|
|
HandlePathDied(Path_ptr path);
|
2019-03-30 13:02:10 +00:00
|
|
|
|
2018-09-27 10:51:30 +00:00
|
|
|
bool
|
|
|
|
GetNewestIntro(service::Introduction& intro) const;
|
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
void
|
2019-04-23 14:28:59 +00:00
|
|
|
AddPath(Path_ptr path);
|
2018-06-25 15:12:08 +00:00
|
|
|
|
2019-04-23 14:28:59 +00:00
|
|
|
Path_ptr
|
2018-12-18 18:36:19 +00:00
|
|
|
GetByUpstream(RouterID remote, PathID_t rxid) const;
|
2018-06-25 15:12:08 +00:00
|
|
|
|
|
|
|
void
|
2019-11-07 18:23:06 +00:00
|
|
|
ExpirePaths(llarp_time_t now, AbstractRouter* router);
|
2018-06-25 15:12:08 +00:00
|
|
|
|
2018-11-14 18:02:27 +00:00
|
|
|
/// get the number of paths in this status
|
2018-06-25 15:12:08 +00:00
|
|
|
size_t
|
|
|
|
NumInStatus(PathStatus st) const;
|
|
|
|
|
2018-11-14 18:02:27 +00:00
|
|
|
/// get the number of paths that match the role that are available
|
|
|
|
size_t
|
|
|
|
AvailablePaths(PathRole role) const;
|
|
|
|
|
2018-10-29 16:48:36 +00:00
|
|
|
/// get time from event loop
|
|
|
|
virtual llarp_time_t
|
|
|
|
Now() const = 0;
|
|
|
|
|
2018-12-24 16:09:05 +00:00
|
|
|
/// stop this pathset and mark it as to be removed
|
|
|
|
virtual bool
|
|
|
|
Stop() = 0;
|
|
|
|
|
2019-02-05 14:50:33 +00:00
|
|
|
/// return true if we are stopped
|
|
|
|
virtual bool
|
|
|
|
IsStopped() const = 0;
|
|
|
|
|
2019-03-22 14:10:30 +00:00
|
|
|
/// get the "name" of this pathset
|
|
|
|
virtual std::string
|
|
|
|
Name() const = 0;
|
|
|
|
|
2018-12-24 16:09:05 +00:00
|
|
|
/// return true if we can and should remove this pathset and underlying
|
|
|
|
/// resources from its parent context
|
|
|
|
virtual bool
|
|
|
|
ShouldRemove() const = 0;
|
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
/// return true if we should build another path
|
2018-08-23 15:19:16 +00:00
|
|
|
virtual bool
|
2018-10-29 16:48:36 +00:00
|
|
|
ShouldBuildMore(llarp_time_t now) const;
|
2018-06-25 15:12:08 +00:00
|
|
|
|
2018-11-14 18:02:27 +00:00
|
|
|
/// return true if we need another path with the given path roles
|
|
|
|
virtual bool
|
|
|
|
ShouldBuildMoreForRoles(llarp_time_t now, PathRole roles) const;
|
|
|
|
|
|
|
|
/// return the minimum number of paths we want for given roles
|
|
|
|
virtual size_t
|
|
|
|
MinRequiredForRoles(PathRole roles) const;
|
|
|
|
|
2018-07-09 17:32:11 +00:00
|
|
|
/// return true if we should publish a new hidden service descriptor
|
2018-07-18 03:10:21 +00:00
|
|
|
virtual bool
|
2021-03-03 20:44:32 +00:00
|
|
|
ShouldPublishDescriptors([[maybe_unused]] llarp_time_t now) const
|
2018-07-18 03:10:21 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2018-07-09 17:32:11 +00:00
|
|
|
|
2020-05-21 14:18:23 +00:00
|
|
|
virtual void
|
|
|
|
BlacklistSNode(const RouterID) = 0;
|
|
|
|
|
2018-07-17 06:17:13 +00:00
|
|
|
/// override me in subtype
|
2020-04-07 18:38:56 +00:00
|
|
|
virtual bool HandleGotIntroMessage(std::shared_ptr<const dht::GotIntroMessage>)
|
2018-07-11 16:11:19 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-10 21:34:11 +00:00
|
|
|
/// override me in subtype
|
2020-04-07 18:38:56 +00:00
|
|
|
virtual bool HandleGotRouterMessage(std::shared_ptr<const dht::GotRouterMessage>)
|
2018-08-10 21:34:11 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-17 19:18:08 +00:00
|
|
|
/// override me in subtype
|
|
|
|
virtual bool HandleGotNameMessage(std::shared_ptr<const dht::GotNameMessage>)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-10 21:34:11 +00:00
|
|
|
virtual routing::IMessageHandler*
|
|
|
|
GetDHTHandler()
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-04-23 14:28:59 +00:00
|
|
|
Path_ptr
|
2021-06-02 19:32:05 +00:00
|
|
|
GetEstablishedPathClosestTo(
|
|
|
|
RouterID router,
|
|
|
|
std::unordered_set<RouterID> excluding = {},
|
|
|
|
PathRole roles = ePathRoleAny) const;
|
2021-04-06 18:32:14 +00:00
|
|
|
|
|
|
|
Path_ptr
|
|
|
|
PickEstablishedPath(PathRole roles = ePathRoleAny) const;
|
2018-07-11 16:11:19 +00:00
|
|
|
|
2019-04-23 14:28:59 +00:00
|
|
|
Path_ptr
|
2018-11-14 18:02:27 +00:00
|
|
|
PickRandomEstablishedPath(PathRole roles = ePathRoleAny) const;
|
2018-08-01 22:10:38 +00:00
|
|
|
|
2019-04-23 14:28:59 +00:00
|
|
|
Path_ptr
|
2018-12-18 18:36:19 +00:00
|
|
|
GetPathByRouter(RouterID router, PathRole roles = ePathRoleAny) const;
|
2018-07-22 23:14:29 +00:00
|
|
|
|
2019-04-23 14:28:59 +00:00
|
|
|
Path_ptr
|
2020-04-07 18:38:56 +00:00
|
|
|
GetNewestPathByRouter(RouterID router, PathRole roles = ePathRoleAny) const;
|
2018-09-18 23:56:26 +00:00
|
|
|
|
2020-05-21 14:25:46 +00:00
|
|
|
Path_ptr
|
|
|
|
GetRandomPathByRouter(RouterID router, PathRole roles = ePathRoleAny) const;
|
|
|
|
|
2019-04-23 14:28:59 +00:00
|
|
|
Path_ptr
|
2018-12-18 18:36:19 +00:00
|
|
|
GetPathByID(PathID_t id) const;
|
2018-08-10 21:34:11 +00:00
|
|
|
|
2019-04-23 14:28:59 +00:00
|
|
|
Path_ptr
|
2019-03-08 17:26:29 +00:00
|
|
|
GetByEndpointWithID(RouterID router, PathID_t id) const;
|
|
|
|
|
2021-06-02 19:32:05 +00:00
|
|
|
std::optional<std::set<service::Introduction>>
|
2018-10-01 14:18:17 +00:00
|
|
|
GetCurrentIntroductionsWithFilter(
|
2020-04-07 18:38:56 +00:00
|
|
|
std::function<bool(const service::Introduction&)> filter) const;
|
2018-10-01 14:18:17 +00:00
|
|
|
|
2018-07-18 03:10:21 +00:00
|
|
|
virtual bool
|
2020-01-27 21:30:41 +00:00
|
|
|
PublishIntroSet(const service::EncryptedIntroSet&, AbstractRouter*)
|
2018-07-18 03:10:21 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2018-07-17 06:17:13 +00:00
|
|
|
|
2019-05-07 17:46:38 +00:00
|
|
|
/// reset all cooldown timers
|
|
|
|
virtual void
|
|
|
|
ResetInternalState() = 0;
|
|
|
|
|
2019-05-07 12:31:34 +00:00
|
|
|
virtual bool
|
|
|
|
BuildOneAlignedTo(const RouterID endpoint) = 0;
|
2019-04-23 16:13:22 +00:00
|
|
|
|
2020-05-21 14:18:23 +00:00
|
|
|
virtual void
|
2021-03-16 11:56:27 +00:00
|
|
|
SendPacketToRemote(const llarp_buffer_t& pkt, service::ProtocolType t) = 0;
|
2020-05-21 14:18:23 +00:00
|
|
|
|
2021-02-02 14:35:40 +00:00
|
|
|
virtual std::optional<std::vector<RouterContact>>
|
|
|
|
GetHopsForBuild() = 0;
|
|
|
|
|
2019-04-23 16:13:22 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
ForEachPath(std::function<void(const Path_ptr&)> visit) const
|
2019-02-08 19:43:25 +00:00
|
|
|
{
|
De-abseil, part 2: mutex, locks, (most) time
- util::Mutex is now a std::shared_timed_mutex, which is capable of
exclusive and shared locks.
- util::Lock is still present as a std::lock_guard<util::Mutex>.
- the locking annotations are preserved, but updated to the latest
supported by clang rather than using abseil's older/deprecated ones.
- ACQUIRE_LOCK macro is gone since we don't pass mutexes by pointer into
locks anymore (WTF abseil).
- ReleasableLock is gone. Instead there are now some llarp::util helper
methods to obtain unique and/or shared locks:
- `auto lock = util::unique_lock(mutex);` gets an RAII-but-also
unlockable object (std::unique_lock<T>, with T inferred from
`mutex`).
- `auto lock = util::shared_lock(mutex);` gets an RAII shared (i.e.
"reader") lock of the mutex.
- `auto lock = util::unique_locks(mutex1, mutex2, mutex3);` can be
used to atomically lock multiple mutexes at once (returning a
tuple of the locks).
This are templated on the mutex which makes them a bit more flexible
than using a concrete type: they can be used for any type of lockable
mutex, not only util::Mutex. (Some of the code here uses them for
getting locks around a std::mutex). Until C++17, using the RAII types
is painfully verbose:
```C++
// pre-C++17 - needing to figure out the mutex type here is annoying:
std::unique_lock<util::Mutex> lock(mutex);
// pre-C++17 and even more verbose (but at least the type isn't needed):
std::unique_lock<decltype(mutex)> lock(mutex);
// our compromise:
auto lock = util::unique_lock(mutex);
// C++17:
std::unique_lock lock(mutex);
```
All of these functions will also warn (under gcc or clang) if you
discard the return value. You can also do fancy things like
`auto l = util::unique_lock(mutex, std::adopt_lock)` (which lets a
lock take over an already-locked mutex).
- metrics code is gone, which also removes a big pile of code that was
only used by metrics:
- llarp::util::Scheduler
- llarp::thread::TimerQueue
- llarp::util::Stopwatch
2020-02-21 17:21:11 +00:00
|
|
|
Lock_t lock(m_PathsMutex);
|
2019-02-08 19:43:25 +00:00
|
|
|
auto itr = m_Paths.begin();
|
2020-04-07 18:38:56 +00:00
|
|
|
while (itr != m_Paths.end())
|
2019-02-08 19:43:25 +00:00
|
|
|
{
|
|
|
|
visit(itr->second);
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-05 17:39:09 +00:00
|
|
|
void
|
2019-09-16 16:12:05 +00:00
|
|
|
UpstreamFlush(AbstractRouter* r);
|
2019-09-16 10:21:12 +00:00
|
|
|
|
|
|
|
void
|
2019-09-16 16:12:05 +00:00
|
|
|
DownstreamFlush(AbstractRouter* r);
|
2019-09-05 17:39:09 +00:00
|
|
|
|
2020-10-27 20:27:14 +00:00
|
|
|
size_t numDesiredPaths;
|
2019-05-07 12:31:34 +00:00
|
|
|
|
2019-07-18 16:28:17 +00:00
|
|
|
protected:
|
2019-07-01 13:44:25 +00:00
|
|
|
BuildStats m_BuildStats;
|
|
|
|
|
2019-05-07 12:31:34 +00:00
|
|
|
void
|
2019-11-05 16:58:53 +00:00
|
|
|
TickPaths(AbstractRouter* r);
|
2019-05-07 12:31:34 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
using Mtx_t = util::NullMutex;
|
|
|
|
using Lock_t = util::NullLock;
|
2021-03-09 18:39:40 +00:00
|
|
|
using PathMap_t = std::unordered_map<std::pair<RouterID, PathID_t>, Path_ptr>;
|
2018-11-27 00:02:32 +00:00
|
|
|
mutable Mtx_t m_PathsMutex;
|
2018-06-26 14:52:19 +00:00
|
|
|
PathMap_t m_Paths;
|
2022-03-28 22:11:47 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::unordered_map<RouterID, std::weak_ptr<path::Path>> m_PathCache;
|
2018-06-25 15:12:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace path
|
|
|
|
} // namespace llarp
|