2021-03-09 22:24:35 +00:00
|
|
|
#pragma once
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/ev/ev.hpp>
|
|
|
|
#include <llarp/util/thread/queue.hpp>
|
2021-04-29 22:54:43 +00:00
|
|
|
#include <llarp/util/decaying_hashset.hpp>
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/path/path_types.hpp>
|
2022-04-04 22:12:27 +00:00
|
|
|
#include <llarp/util/priority_queue.hpp>
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/router_id.hpp>
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
#include <list>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
struct llarp_buffer_t;
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
2023-09-13 19:57:18 +00:00
|
|
|
enum class SendStatus
|
|
|
|
{
|
|
|
|
Success,
|
|
|
|
Timeout,
|
|
|
|
NoLink,
|
|
|
|
InvalidRouter,
|
|
|
|
RouterNotFound,
|
|
|
|
Congestion
|
|
|
|
};
|
|
|
|
|
2023-09-15 14:55:32 +00:00
|
|
|
struct Router;
|
2019-06-26 21:39:29 +00:00
|
|
|
enum class SessionResult;
|
2023-09-13 19:57:18 +00:00
|
|
|
struct AbstractLinkMessage;
|
|
|
|
struct RouterID;
|
|
|
|
struct PathID_t;
|
|
|
|
|
|
|
|
static const size_t MAX_PATH_QUEUE_SIZE = 100;
|
|
|
|
static const size_t MAX_OUTBOUND_QUEUE_SIZE = 1000;
|
|
|
|
static const size_t MAX_OUTBOUND_MESSAGES_PER_TICK = 500;
|
|
|
|
|
|
|
|
using SendStatusHandler = std::function<void(SendStatus)>;
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2023-09-13 19:57:18 +00:00
|
|
|
struct OutboundMessageHandler final
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
|
|
|
public:
|
2023-09-13 19:57:18 +00:00
|
|
|
~OutboundMessageHandler() = default;
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2019-10-25 21:13:11 +00:00
|
|
|
OutboundMessageHandler(size_t maxQueueSize = MAX_OUTBOUND_QUEUE_SIZE);
|
|
|
|
|
2021-04-29 22:54:43 +00:00
|
|
|
/* Called to queue a message to be sent to a router.
|
|
|
|
*
|
|
|
|
* If there is no session with the destination router, the message is added to a
|
|
|
|
* pending session queue for that router. If there is no pending session to that
|
|
|
|
* router, one is created.
|
|
|
|
*
|
|
|
|
* If there is a session to the destination router, the message is placed on the shared
|
2021-11-11 14:17:48 +00:00
|
|
|
* outbound message queue to be processed on Pump().
|
2021-04-29 22:54:43 +00:00
|
|
|
*
|
2021-11-11 14:17:48 +00:00
|
|
|
* When this class' Pump() is called, that queue is emptied and the messages there
|
2021-04-29 22:54:43 +00:00
|
|
|
* are placed in their paths' respective individual queues.
|
|
|
|
*
|
|
|
|
* Returns false if encoding the message into a buffer fails, true otherwise.
|
|
|
|
* A return value of true merely means we successfully processed the queue request,
|
|
|
|
* so for example an invalid destination still yields a true return.
|
|
|
|
*/
|
2019-06-26 21:39:29 +00:00
|
|
|
bool
|
2023-08-29 14:26:59 +00:00
|
|
|
QueueMessage(const RouterID& remote, const AbstractLinkMessage& msg, SendStatusHandler callback)
|
2023-09-13 19:57:18 +00:00
|
|
|
EXCLUDES(_mutex);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2021-11-12 13:51:39 +00:00
|
|
|
/* Called when pumping output queues, typically scheduled via a call to Router::TriggerPump().
|
2021-04-29 22:54:43 +00:00
|
|
|
*
|
|
|
|
* Processes messages on the shared message queue into their paths' respective
|
|
|
|
* individual queues.
|
|
|
|
*
|
|
|
|
* Removes the individual queues for paths which have died / expired, as informed by
|
|
|
|
* QueueRemoveEmptyPath.
|
|
|
|
*
|
|
|
|
* Sends all routing messages that have been queued, indicated by pathid 0 when queued.
|
|
|
|
* Sends messages from path queues until all are empty or a set cap has been reached.
|
|
|
|
*/
|
2019-10-25 21:13:11 +00:00
|
|
|
void
|
2023-09-13 19:57:18 +00:00
|
|
|
Pump();
|
2019-10-25 21:13:11 +00:00
|
|
|
|
2021-04-29 22:54:43 +00:00
|
|
|
/* Called from outside this class to inform it that a path has died / expired
|
|
|
|
* and its queue should be discarded.
|
|
|
|
*/
|
2019-10-25 21:13:11 +00:00
|
|
|
void
|
2023-09-13 19:57:18 +00:00
|
|
|
RemovePath(const PathID_t& pathid);
|
2019-10-25 21:13:11 +00:00
|
|
|
|
2019-06-26 21:39:29 +00:00
|
|
|
util::StatusObject
|
2023-09-13 19:57:18 +00:00
|
|
|
ExtractStatus() const;
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
void
|
2023-09-15 14:55:32 +00:00
|
|
|
Init(Router* router);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
private:
|
2021-04-29 22:54:43 +00:00
|
|
|
/* A message that has been queued for sending, but not yet
|
|
|
|
* processed into an individual path's message queue.
|
|
|
|
*/
|
2019-10-25 21:13:11 +00:00
|
|
|
struct MessageQueueEntry
|
|
|
|
{
|
2020-01-17 16:33:37 +00:00
|
|
|
uint16_t priority;
|
2022-03-30 20:21:57 +00:00
|
|
|
std::vector<byte_t> message;
|
|
|
|
SendStatusHandler inform;
|
2019-10-25 21:13:11 +00:00
|
|
|
PathID_t pathid;
|
|
|
|
RouterID router;
|
2020-01-17 16:33:37 +00:00
|
|
|
|
|
|
|
bool
|
2022-04-04 22:12:27 +00:00
|
|
|
operator>(const MessageQueueEntry& other) const
|
2020-01-17 16:33:37 +00:00
|
|
|
{
|
2022-04-04 22:12:27 +00:00
|
|
|
return priority > other.priority;
|
2020-01-17 16:33:37 +00:00
|
|
|
}
|
2019-10-25 21:13:11 +00:00
|
|
|
};
|
|
|
|
|
2020-01-17 17:14:24 +00:00
|
|
|
struct MessageQueueStats
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
uint64_t queued = 0;
|
|
|
|
uint64_t dropped = 0;
|
|
|
|
uint64_t sent = 0;
|
2020-01-17 17:14:24 +00:00
|
|
|
uint32_t queueWatermark = 0;
|
|
|
|
|
|
|
|
uint32_t perTickMax = 0;
|
2020-04-07 18:38:56 +00:00
|
|
|
uint32_t numTicks = 0;
|
2020-01-17 17:14:24 +00:00
|
|
|
};
|
|
|
|
|
2022-04-04 22:12:27 +00:00
|
|
|
using MessageQueue = util::ascending_priority_queue<MessageQueueEntry>;
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2021-04-29 22:54:43 +00:00
|
|
|
/* If a session is not yet created with the destination router for a message,
|
|
|
|
* a special queue is created for that router and an attempt is made to
|
|
|
|
* establish a session. When this establish attempt concludes, either
|
|
|
|
* the messages are then sent to that router immediately, on success, or
|
|
|
|
* the messages are dropped and their send status callbacks are invoked with
|
|
|
|
* the appropriate send status.
|
|
|
|
*/
|
2019-06-26 21:39:29 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
OnSessionResult(const RouterID& router, const SessionResult result);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2021-04-29 22:54:43 +00:00
|
|
|
/* queues a message's send result callback onto the event loop */
|
2019-06-26 21:39:29 +00:00
|
|
|
void
|
|
|
|
DoCallback(SendStatusHandler callback, SendStatus status);
|
|
|
|
|
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
QueueSessionCreation(const RouterID& remote);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
bool
|
2023-09-13 15:57:29 +00:00
|
|
|
EncodeBuffer(const AbstractLinkMessage& msg, std::string& buf);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2021-04-29 22:54:43 +00:00
|
|
|
/* sends the message along to the link layer, and hopefully out to the network
|
|
|
|
*
|
|
|
|
* returns the result of the call to LinkManager::SendTo()
|
|
|
|
*/
|
2019-06-26 21:39:29 +00:00
|
|
|
bool
|
2022-03-30 20:21:57 +00:00
|
|
|
Send(const MessageQueueEntry& ent);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2021-04-29 22:54:43 +00:00
|
|
|
/* Sends the message along to the link layer if we have a session to the remote
|
|
|
|
*
|
|
|
|
* returns the result of the Send() call, or false if no session.
|
|
|
|
*/
|
2019-06-26 21:39:29 +00:00
|
|
|
bool
|
2022-03-30 20:21:57 +00:00
|
|
|
SendIfSession(const MessageQueueEntry& ent);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2021-04-29 22:54:43 +00:00
|
|
|
/* queues a message to the shared outbound message queue.
|
|
|
|
*
|
|
|
|
* If the queue is full, the message is dropped and the message's status
|
|
|
|
* callback is invoked with a congestion status.
|
|
|
|
*
|
2021-11-11 14:17:48 +00:00
|
|
|
* When this class' Pump() is called, that queue is emptied and the messages there
|
2021-04-29 22:54:43 +00:00
|
|
|
* are placed in their paths' respective individual queues.
|
|
|
|
*/
|
2019-10-25 21:13:11 +00:00
|
|
|
bool
|
2022-03-30 20:21:57 +00:00
|
|
|
QueueOutboundMessage(MessageQueueEntry entry);
|
2019-10-25 21:13:11 +00:00
|
|
|
|
2021-04-29 22:54:43 +00:00
|
|
|
/* Processes messages on the shared message queue into their paths' respective
|
|
|
|
* individual queues.
|
|
|
|
*/
|
2019-06-26 21:39:29 +00:00
|
|
|
void
|
2019-10-25 21:13:11 +00:00
|
|
|
ProcessOutboundQueue();
|
|
|
|
|
2021-04-29 22:54:43 +00:00
|
|
|
/*
|
2021-11-09 23:46:09 +00:00
|
|
|
* Sends routing messages that have been queued, indicated by pathid 0 when queued.
|
2021-04-29 22:54:43 +00:00
|
|
|
*
|
|
|
|
* Sends messages from path queues until all are empty or a set cap has been reached.
|
|
|
|
* This will send one message from each queue in a round-robin fashion such that they
|
|
|
|
* all have roughly equal access to bandwidth. A notion of priority may be introduced
|
|
|
|
* at a later time, but for now only routing messages get priority.
|
2021-11-09 23:46:09 +00:00
|
|
|
*
|
|
|
|
* Returns true if there is more to send (i.e. we hit the limit before emptying all path
|
|
|
|
* queues), false if all queues were drained.
|
2021-04-29 22:54:43 +00:00
|
|
|
*/
|
2021-11-09 23:46:09 +00:00
|
|
|
bool
|
2019-10-25 21:13:11 +00:00
|
|
|
SendRoundRobin();
|
|
|
|
|
2021-04-29 22:54:43 +00:00
|
|
|
/* Invoked when an outbound session establish attempt has concluded.
|
|
|
|
*
|
|
|
|
* If the outbound session was successfully created, sends any messages queued
|
|
|
|
* for that destination along to it.
|
|
|
|
*
|
|
|
|
* If the session was unsuccessful, invokes the send status callbacks of those
|
|
|
|
* queued messages and drops them.
|
|
|
|
*/
|
2019-10-25 21:13:11 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
FinalizeSessionRequest(const RouterID& router, SendStatus status) EXCLUDES(_mutex);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp::thread::Queue<MessageQueueEntry> outboundQueue;
|
2021-04-29 22:54:43 +00:00
|
|
|
llarp::util::DecayingHashSet<PathID_t> recentlyRemovedPaths;
|
2019-10-25 21:13:11 +00:00
|
|
|
bool removedSomePaths;
|
|
|
|
|
|
|
|
mutable util::Mutex _mutex; // protects pendingSessionMessageQueues
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2021-03-09 18:39:40 +00:00
|
|
|
std::unordered_map<RouterID, MessageQueue> pendingSessionMessageQueues GUARDED_BY(_mutex);
|
2019-10-25 21:13:11 +00:00
|
|
|
|
2021-03-09 18:39:40 +00:00
|
|
|
std::unordered_map<PathID_t, MessageQueue> outboundMessageQueues;
|
2019-10-25 21:13:11 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
std::queue<PathID_t> roundRobinOrder;
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2023-09-15 14:55:32 +00:00
|
|
|
Router* _router;
|
2019-10-25 21:13:11 +00:00
|
|
|
|
2019-11-14 18:50:45 +00:00
|
|
|
util::ContentionKiller m_Killer;
|
|
|
|
|
2019-10-29 02:28:59 +00:00
|
|
|
// paths cannot have pathid "0", so it can be used as the "pathid"
|
|
|
|
// for non-traffic (control) messages, so they can be prioritized.
|
|
|
|
static const PathID_t zeroID;
|
2020-01-17 17:14:24 +00:00
|
|
|
|
|
|
|
MessageQueueStats m_queueStats;
|
2019-06-26 21:39:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace llarp
|