2021-03-09 22:24:35 +00:00
|
|
|
#include "link_manager.hpp"
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/router/i_outbound_session_maker.hpp>
|
|
|
|
#include <llarp/crypto/crypto.hpp>
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <set>
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
LinkLayer_ptr
|
2020-04-07 18:38:56 +00:00
|
|
|
LinkManager::GetCompatibleLink(const RouterContact& rc) const
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (stopping)
|
2019-06-26 21:39:29 +00:00
|
|
|
return nullptr;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
for (auto& link : outboundLinks)
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
|
|
|
// TODO: may want to add some memory of session failures for a given
|
|
|
|
// router on a given link and not return that link here for a
|
|
|
|
// duration
|
2022-03-30 20:21:57 +00:00
|
|
|
if (not link->IsCompatable(rc))
|
2019-06-26 21:39:29 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
return link;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
IOutboundSessionMaker*
|
2019-06-26 21:39:29 +00:00
|
|
|
LinkManager::GetSessionMaker() const
|
|
|
|
{
|
|
|
|
return _sessionMaker;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
LinkManager::SendTo(
|
2022-03-30 20:21:57 +00:00
|
|
|
const RouterID& remote,
|
|
|
|
const llarp_buffer_t& buf,
|
|
|
|
ILinkSession::CompletionHandler completed,
|
|
|
|
uint16_t priority)
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (stopping)
|
2019-06-26 21:39:29 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
auto link = GetLinkWithSessionTo(remote);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (link == nullptr)
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (completed)
|
2019-07-26 16:19:31 +00:00
|
|
|
{
|
|
|
|
completed(ILinkSession::DeliveryStatus::eDeliveryDropped);
|
|
|
|
}
|
2019-06-26 21:39:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-03-30 20:21:57 +00:00
|
|
|
return link->SendTo(remote, buf, completed, priority);
|
2019-06-26 21:39:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
LinkManager::HasSessionTo(const RouterID& remote) const
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
|
|
|
return GetLinkWithSessionTo(remote) != nullptr;
|
|
|
|
}
|
|
|
|
|
2021-06-07 22:31:57 +00:00
|
|
|
bool
|
|
|
|
LinkManager::HasOutboundSessionTo(const RouterID& remote) const
|
|
|
|
{
|
|
|
|
for (const auto& link : outboundLinks)
|
|
|
|
{
|
|
|
|
if (link->HasSessionTo(remote))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-11-10 14:24:58 +00:00
|
|
|
std::optional<bool>
|
|
|
|
LinkManager::SessionIsClient(RouterID remote) const
|
|
|
|
{
|
|
|
|
for (const auto& link : inboundLinks)
|
|
|
|
{
|
|
|
|
const auto session = link->FindSessionByPubkey(remote);
|
|
|
|
if (session)
|
|
|
|
return not session->IsRelay();
|
|
|
|
}
|
2021-06-07 22:31:57 +00:00
|
|
|
if (HasOutboundSessionTo(remote))
|
|
|
|
return false;
|
2020-11-10 14:24:58 +00:00
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
LinkManager::DeregisterPeer(RouterID remote)
|
|
|
|
{
|
|
|
|
m_PersistingSessions.erase(remote);
|
|
|
|
for (const auto& link : inboundLinks)
|
|
|
|
{
|
|
|
|
link->CloseSessionTo(remote);
|
|
|
|
}
|
|
|
|
for (const auto& link : outboundLinks)
|
|
|
|
{
|
|
|
|
link->CloseSessionTo(remote);
|
|
|
|
}
|
|
|
|
LogInfo(remote, " has been de-registered");
|
|
|
|
}
|
|
|
|
|
2019-06-26 21:39:29 +00:00
|
|
|
void
|
|
|
|
LinkManager::PumpLinks()
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
for (const auto& link : inboundLinks)
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
|
|
|
link->Pump();
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
for (const auto& link : outboundLinks)
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
|
|
|
link->Pump();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
LinkManager::AddLink(LinkLayer_ptr link, bool inbound)
|
|
|
|
{
|
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
|
|
|
util::Lock l(_mutex);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (inbound)
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
|
|
|
inboundLinks.emplace(link);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
outboundLinks.emplace(link);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2021-03-02 22:27:35 +00:00
|
|
|
LinkManager::StartLinks()
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
|
|
|
LogInfo("starting ", outboundLinks.size(), " outbound links");
|
2020-04-07 18:38:56 +00:00
|
|
|
for (const auto& link : outboundLinks)
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
2021-03-02 22:27:35 +00:00
|
|
|
if (!link->Start())
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
|
|
|
LogWarn("outbound link '", link->Name(), "' failed to start");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
LogDebug("Outbound Link ", link->Name(), " started");
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (inboundLinks.size())
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
|
|
|
LogInfo("starting ", inboundLinks.size(), " inbound links");
|
2020-04-07 18:38:56 +00:00
|
|
|
for (const auto& link : inboundLinks)
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
2021-03-02 22:27:35 +00:00
|
|
|
if (!link->Start())
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
|
|
|
LogWarn("Link ", link->Name(), " failed to start");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
LogDebug("Inbound Link ", link->Name(), " started");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
LinkManager::Stop()
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (stopping)
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
util::Lock l(_mutex);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
LogInfo("stopping links");
|
|
|
|
stopping = true;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
for (const auto& link : outboundLinks)
|
2019-06-26 21:39:29 +00:00
|
|
|
link->Stop();
|
2020-04-07 18:38:56 +00:00
|
|
|
for (const auto& link : inboundLinks)
|
2019-06-26 21:39:29 +00:00
|
|
|
link->Stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
LinkManager::PersistSessionUntil(const RouterID& remote, llarp_time_t until)
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (stopping)
|
2019-06-26 21:39:29 +00:00
|
|
|
return;
|
|
|
|
|
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
|
|
|
util::Lock l(_mutex);
|
2021-06-06 14:51:29 +00:00
|
|
|
|
|
|
|
m_PersistingSessions[remote] = std::max(until, m_PersistingSessions[remote]);
|
2021-06-07 12:39:38 +00:00
|
|
|
if (auto maybe = SessionIsClient(remote))
|
|
|
|
{
|
|
|
|
if (*maybe)
|
|
|
|
{
|
|
|
|
// mark this as a client so we don't try to back connect
|
|
|
|
m_Clients.Upsert(remote);
|
|
|
|
}
|
|
|
|
}
|
2019-06-26 21:39:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
LinkManager::ForEachPeer(
|
2020-04-07 18:38:56 +00:00
|
|
|
std::function<void(const ILinkSession*, bool)> visit, bool randomize) const
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (stopping)
|
2019-06-26 21:39:29 +00:00
|
|
|
return;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
for (const auto& link : outboundLinks)
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
link->ForEachSession([visit](const ILinkSession* peer) { visit(peer, true); }, randomize);
|
2019-06-26 21:39:29 +00:00
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
for (const auto& link : inboundLinks)
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
link->ForEachSession([visit](const ILinkSession* peer) { visit(peer, false); }, randomize);
|
2019-06-26 21:39:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
LinkManager::ForEachPeer(std::function<void(ILinkSession*)> visit)
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (stopping)
|
2019-06-26 21:39:29 +00:00
|
|
|
return;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
for (const auto& link : outboundLinks)
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
link->ForEachSession([visit](ILinkSession* peer) { visit(peer); });
|
2019-06-26 21:39:29 +00:00
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
for (const auto& link : inboundLinks)
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
link->ForEachSession([visit](ILinkSession* peer) { visit(peer); });
|
2019-06-26 21:39:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
LinkManager::ForEachInboundLink(std::function<void(LinkLayer_ptr)> visit) const
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
for (const auto& link : inboundLinks)
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
|
|
|
visit(link);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-08 11:35:24 +00:00
|
|
|
void
|
|
|
|
LinkManager::ForEachOutboundLink(std::function<void(LinkLayer_ptr)> visit) const
|
|
|
|
{
|
|
|
|
for (const auto& link : outboundLinks)
|
|
|
|
{
|
|
|
|
visit(link);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-26 21:39:29 +00:00
|
|
|
size_t
|
|
|
|
LinkManager::NumberOfConnectedRouters() const
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
std::set<RouterID> connectedRouters;
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
auto fn = [&connectedRouters](const ILinkSession* session, bool) {
|
|
|
|
if (session->IsEstablished())
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
|
|
|
const RouterContact rc(session->GetRemoteRC());
|
2020-04-07 18:38:56 +00:00
|
|
|
if (rc.IsPublicRouter())
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
|
|
|
connectedRouters.insert(rc.pubkey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ForEachPeer(fn);
|
|
|
|
|
|
|
|
return connectedRouters.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
LinkManager::NumberOfConnectedClients() const
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
std::set<RouterID> connectedClients;
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
auto fn = [&connectedClients](const ILinkSession* session, bool) {
|
|
|
|
if (session->IsEstablished())
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
|
|
|
const RouterContact rc(session->GetRemoteRC());
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!rc.IsPublicRouter())
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
|
|
|
connectedClients.insert(rc.pubkey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ForEachPeer(fn);
|
|
|
|
|
|
|
|
return connectedClients.size();
|
|
|
|
}
|
|
|
|
|
2019-12-03 17:03:19 +00:00
|
|
|
size_t
|
|
|
|
LinkManager::NumberOfPendingConnections() const
|
|
|
|
{
|
|
|
|
size_t pending = 0;
|
2020-04-07 18:38:56 +00:00
|
|
|
for (const auto& link : inboundLinks)
|
2019-12-03 17:03:19 +00:00
|
|
|
{
|
|
|
|
pending += link->NumberOfPendingSessions();
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
for (const auto& link : outboundLinks)
|
2019-12-03 17:03:19 +00:00
|
|
|
{
|
|
|
|
pending += link->NumberOfPendingSessions();
|
|
|
|
}
|
|
|
|
|
|
|
|
return pending;
|
|
|
|
}
|
|
|
|
|
2019-06-26 21:39:29 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
LinkManager::GetRandomConnectedRouter(RouterContact& router) const
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
2021-03-09 18:39:40 +00:00
|
|
|
std::unordered_map<RouterID, RouterContact> connectedRouters;
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
ForEachPeer(
|
2020-04-07 18:38:56 +00:00
|
|
|
[&connectedRouters](const ILinkSession* peer, bool unused) {
|
2019-06-26 21:39:29 +00:00
|
|
|
(void)unused;
|
|
|
|
connectedRouters[peer->GetPubKey()] = peer->GetRemoteRC();
|
|
|
|
},
|
|
|
|
false);
|
|
|
|
|
|
|
|
const auto sz = connectedRouters.size();
|
2020-04-07 18:38:56 +00:00
|
|
|
if (sz)
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
|
|
|
auto itr = connectedRouters.begin();
|
2020-04-07 18:38:56 +00:00
|
|
|
if (sz > 1)
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
|
|
|
std::advance(itr, randint() % sz);
|
|
|
|
}
|
|
|
|
|
|
|
|
router = itr->second;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
LinkManager::CheckPersistingSessions(llarp_time_t now)
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (stopping)
|
2019-06-26 21:39:29 +00:00
|
|
|
return;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
std::vector<RouterID> sessionsNeeded;
|
2021-06-07 12:39:38 +00:00
|
|
|
std::vector<RouterID> sessionsClosed;
|
2019-06-26 21:39:29 +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
|
|
|
util::Lock l(_mutex);
|
2021-06-07 12:39:38 +00:00
|
|
|
for (auto [remote, until] : m_PersistingSessions)
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
2021-06-07 12:39:38 +00:00
|
|
|
if (now < until)
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
2021-06-07 12:39:38 +00:00
|
|
|
auto link = GetLinkWithSessionTo(remote);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (link)
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
2021-06-07 12:39:38 +00:00
|
|
|
link->KeepAliveSessionTo(remote);
|
2019-06-26 21:39:29 +00:00
|
|
|
}
|
2021-06-07 12:39:38 +00:00
|
|
|
else if (not m_Clients.Contains(remote))
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
2021-06-07 12:39:38 +00:00
|
|
|
sessionsNeeded.push_back(remote);
|
2019-06-26 21:39:29 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-07 12:39:38 +00:00
|
|
|
else if (not m_Clients.Contains(remote))
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
2021-06-07 12:39:38 +00:00
|
|
|
sessionsClosed.push_back(remote);
|
2019-06-26 21:39:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
for (const auto& router : sessionsNeeded)
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
2021-06-10 00:15:06 +00:00
|
|
|
LogDebug("ensuring session to ", router, " for previously made commitment");
|
2019-06-26 21:39:29 +00:00
|
|
|
_sessionMaker->CreateSessionTo(router, nullptr);
|
|
|
|
}
|
2021-06-07 12:39:38 +00:00
|
|
|
|
2021-06-08 13:01:01 +00:00
|
|
|
for (const auto& router : sessionsClosed)
|
|
|
|
{
|
|
|
|
m_PersistingSessions.erase(router);
|
|
|
|
ForEachOutboundLink([router](auto link) { link->CloseSessionTo(router); });
|
|
|
|
}
|
2019-06-26 21:39:29 +00:00
|
|
|
}
|
|
|
|
|
2020-06-04 16:00:30 +00:00
|
|
|
void
|
|
|
|
LinkManager::updatePeerDb(std::shared_ptr<PeerDb> peerDb)
|
|
|
|
{
|
|
|
|
std::vector<std::pair<RouterID, SessionStats>> statsToUpdate;
|
|
|
|
|
|
|
|
int64_t diffTotalTX = 0;
|
|
|
|
|
|
|
|
ForEachPeer([&](ILinkSession* session) {
|
|
|
|
// derive RouterID
|
|
|
|
RouterID id = RouterID(session->GetRemoteRC().pubkey);
|
|
|
|
|
|
|
|
SessionStats sessionStats = session->GetSessionStats();
|
|
|
|
SessionStats diff;
|
|
|
|
SessionStats& lastStats = m_lastRouterStats[id];
|
|
|
|
|
|
|
|
// TODO: operator overloads / member func for diff
|
|
|
|
diff.currentRateRX = std::max(sessionStats.currentRateRX, lastStats.currentRateRX);
|
|
|
|
diff.currentRateTX = std::max(sessionStats.currentRateTX, lastStats.currentRateTX);
|
|
|
|
diff.totalPacketsRX = sessionStats.totalPacketsRX - lastStats.totalPacketsRX;
|
|
|
|
diff.totalAckedTX = sessionStats.totalAckedTX - lastStats.totalAckedTX;
|
|
|
|
diff.totalDroppedTX = sessionStats.totalDroppedTX - lastStats.totalDroppedTX;
|
|
|
|
|
|
|
|
diffTotalTX = diff.totalAckedTX + diff.totalDroppedTX + diff.totalInFlightTX;
|
|
|
|
|
|
|
|
lastStats = sessionStats;
|
|
|
|
|
|
|
|
// TODO: if we have both inbound and outbound session, this will overwrite
|
|
|
|
statsToUpdate.push_back({id, diff});
|
|
|
|
});
|
|
|
|
|
|
|
|
for (auto& routerStats : statsToUpdate)
|
|
|
|
{
|
|
|
|
peerDb->modifyPeerStats(routerStats.first, [&](PeerStats& stats) {
|
|
|
|
// TODO: store separate stats for up vs down
|
|
|
|
const auto& diff = routerStats.second;
|
|
|
|
|
|
|
|
// note that 'currentRateRX' and 'currentRateTX' are per-second
|
|
|
|
stats.peakBandwidthBytesPerSec = std::max(
|
|
|
|
stats.peakBandwidthBytesPerSec,
|
|
|
|
(double)std::max(diff.currentRateRX, diff.currentRateTX));
|
|
|
|
stats.numPacketsDropped += diff.totalDroppedTX;
|
|
|
|
stats.numPacketsSent = diff.totalAckedTX;
|
|
|
|
stats.numPacketsAttempted = diffTotalTX;
|
|
|
|
|
|
|
|
// TODO: others -- we have slight mismatch on what we store
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-26 21:39:29 +00:00
|
|
|
util::StatusObject
|
|
|
|
LinkManager::ExtractStatus() const
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
std::vector<util::StatusObject> ob_links, ib_links;
|
|
|
|
std::transform(
|
|
|
|
inboundLinks.begin(),
|
|
|
|
inboundLinks.end(),
|
|
|
|
std::back_inserter(ib_links),
|
|
|
|
[](const auto& link) -> util::StatusObject { return link->ExtractStatus(); });
|
|
|
|
std::transform(
|
|
|
|
outboundLinks.begin(),
|
|
|
|
outboundLinks.end(),
|
|
|
|
std::back_inserter(ob_links),
|
|
|
|
[](const auto& link) -> util::StatusObject { return link->ExtractStatus(); });
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
util::StatusObject obj{{"outbound", ob_links}, {"inbound", ib_links}};
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
LinkManager::Init(IOutboundSessionMaker* sessionMaker)
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
stopping = false;
|
2019-06-26 21:39:29 +00:00
|
|
|
_sessionMaker = sessionMaker;
|
|
|
|
}
|
|
|
|
|
|
|
|
LinkLayer_ptr
|
2020-04-07 18:38:56 +00:00
|
|
|
LinkManager::GetLinkWithSessionTo(const RouterID& remote) const
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (stopping)
|
2019-06-26 21:39:29 +00:00
|
|
|
return nullptr;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
for (const auto& link : outboundLinks)
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (link->HasSessionTo(remote))
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
|
|
|
return link;
|
|
|
|
}
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
for (const auto& link : inboundLinks)
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (link->HasSessionTo(remote))
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
|
|
|
return link;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace llarp
|