2020-05-12 08:35:48 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c)2013-2020 ZeroTier, Inc.
|
|
|
|
*
|
|
|
|
* Use of this software is governed by the Business Source License included
|
|
|
|
* in the LICENSE.TXT file in the project's root directory.
|
|
|
|
*
|
2020-08-20 19:51:39 +00:00
|
|
|
* Change Date: 2025-01-01
|
2020-05-12 08:35:48 +00:00
|
|
|
*
|
|
|
|
* On the date above, in accordance with the Business Source License, use
|
|
|
|
* of this software will be governed by version 2.0 of the Apache License.
|
|
|
|
*/
|
|
|
|
/****/
|
|
|
|
|
|
|
|
#ifndef ZT_BOND_HPP
|
|
|
|
#define ZT_BOND_HPP
|
|
|
|
|
2021-05-04 00:59:31 +00:00
|
|
|
#include "Flow.hpp"
|
2020-05-12 08:35:48 +00:00
|
|
|
#include "Path.hpp"
|
|
|
|
#include "Peer.hpp"
|
2021-05-04 00:59:31 +00:00
|
|
|
#include "Packet.hpp"
|
|
|
|
|
|
|
|
#include <list>
|
|
|
|
#include <map>
|
2020-05-12 08:35:48 +00:00
|
|
|
|
|
|
|
namespace ZeroTier {
|
|
|
|
|
|
|
|
class RuntimeEnvironment;
|
2020-06-17 21:54:13 +00:00
|
|
|
class Link;
|
2021-05-04 00:59:31 +00:00
|
|
|
class Peer;
|
2020-05-12 08:35:48 +00:00
|
|
|
|
2021-05-04 00:59:31 +00:00
|
|
|
class Bond {
|
2020-05-12 08:35:48 +00:00
|
|
|
friend class SharedPtr<Bond>;
|
|
|
|
friend class Peer;
|
|
|
|
friend class BondController;
|
|
|
|
|
2021-05-04 00:59:31 +00:00
|
|
|
struct PathQualityComparator {
|
|
|
|
bool operator()(const SharedPtr<Path>& a, const SharedPtr<Path>& b)
|
2020-05-12 08:35:48 +00:00
|
|
|
{
|
2021-05-04 00:59:31 +00:00
|
|
|
if (a->_failoverScore == b->_failoverScore) {
|
2020-05-12 08:35:48 +00:00
|
|
|
return a < b;
|
|
|
|
}
|
|
|
|
return a->_failoverScore > b->_failoverScore;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-05-04 00:59:31 +00:00
|
|
|
public:
|
2020-05-15 03:09:25 +00:00
|
|
|
// TODO: Remove
|
|
|
|
bool _header;
|
|
|
|
int64_t _lastLogTS;
|
|
|
|
int64_t _lastPrintTS;
|
|
|
|
void dumpInfo(const int64_t now);
|
|
|
|
bool relevant();
|
|
|
|
|
2020-06-17 21:54:13 +00:00
|
|
|
SharedPtr<Link> getLink(const SharedPtr<Path>& path);
|
2020-05-15 03:09:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor. Creates a bond based off of ZT defaults
|
|
|
|
*
|
|
|
|
* @param renv Runtime environment
|
|
|
|
* @param policy Bonding policy
|
|
|
|
* @param peer
|
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
Bond(const RuntimeEnvironment* renv, int policy, const SharedPtr<Peer>& peer);
|
2020-05-15 03:09:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor. For use when user intends to manually specify parameters
|
|
|
|
*
|
|
|
|
* @param basePolicy
|
|
|
|
* @param policyAlias
|
|
|
|
* @param peer
|
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
Bond(const RuntimeEnvironment* renv, std::string& basePolicy, std::string& policyAlias, const SharedPtr<Peer>& peer);
|
2020-05-15 03:09:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor. Creates a bond based off of a user-defined bond template
|
|
|
|
*
|
|
|
|
* @param renv Runtime environment
|
|
|
|
* @param original
|
|
|
|
* @param peer
|
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
Bond(const RuntimeEnvironment* renv, SharedPtr<Bond> originalBond, const SharedPtr<Peer>& peer);
|
2020-05-12 08:35:48 +00:00
|
|
|
|
|
|
|
/**
|
2020-05-27 00:57:37 +00:00
|
|
|
* @return The human-readable name of the bonding policy
|
2020-05-12 08:35:48 +00:00
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
std::string policyAlias()
|
|
|
|
{
|
|
|
|
return _policyAlias;
|
|
|
|
}
|
2020-05-12 08:35:48 +00:00
|
|
|
|
|
|
|
/**
|
2020-08-25 01:56:49 +00:00
|
|
|
* Inform the bond about the path that its peer (owning object) just learned about.
|
|
|
|
* If the path is allowed to be used, it will be inducted into the bond on a trial
|
|
|
|
* period where link statistics will be collected to judge its quality.
|
2020-05-12 08:35:48 +00:00
|
|
|
*
|
|
|
|
* @param path Newly-learned Path which should now be handled by the Bond
|
|
|
|
* @param now Current time
|
|
|
|
*/
|
|
|
|
void nominatePath(const SharedPtr<Path>& path, int64_t now);
|
2020-05-15 03:09:25 +00:00
|
|
|
|
2020-05-12 08:35:48 +00:00
|
|
|
/**
|
|
|
|
* Propagate and memoize often-used bonding preferences for each path
|
|
|
|
*/
|
|
|
|
void applyUserPrefs();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check path states and perform bond rebuilds if needed.
|
2020-05-15 03:09:25 +00:00
|
|
|
*
|
2020-05-12 08:35:48 +00:00
|
|
|
* @param now Current time
|
2020-05-15 03:09:25 +00:00
|
|
|
* @param rebuild Whether or not the bond should be reconstructed.
|
2020-05-12 08:35:48 +00:00
|
|
|
*/
|
|
|
|
void curateBond(const int64_t now, bool rebuild);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Periodically perform statistical summaries of quality metrics for all paths.
|
|
|
|
*
|
|
|
|
* @param now Current time
|
|
|
|
*/
|
|
|
|
void estimatePathQuality(int64_t now);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Record an invalid incoming packet. This packet failed
|
|
|
|
* MAC/compression/cipher checks and will now contribute to a
|
|
|
|
* Packet Error Ratio (PER).
|
|
|
|
*
|
|
|
|
* @param path Path over which packet was received
|
|
|
|
*/
|
|
|
|
void recordIncomingInvalidPacket(const SharedPtr<Path>& path);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Record statistics on outbound an packet.
|
|
|
|
*
|
|
|
|
* @param path Path over which packet is being sent
|
|
|
|
* @param packetId Packet ID
|
|
|
|
* @param payloadLength Packet data length
|
|
|
|
* @param verb Packet verb
|
|
|
|
* @param flowId Flow ID
|
|
|
|
* @param now Current time
|
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
void recordOutgoingPacket(const SharedPtr<Path>& path, uint64_t packetId, uint16_t payloadLength, Packet::Verb verb, int32_t flowId, int64_t now);
|
2020-05-12 08:35:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Process the contents of an inbound VERB_QOS_MEASUREMENT to gather path quality observations.
|
|
|
|
*
|
|
|
|
* @param now Current time
|
|
|
|
* @param count Number of records
|
|
|
|
* @param rx_id table of packet IDs
|
|
|
|
* @param rx_ts table of holding times
|
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
void receivedQoS(const SharedPtr<Path>& path, int64_t now, int count, uint64_t* rx_id, uint16_t* rx_ts);
|
2020-05-12 08:35:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Process the contents of an inbound VERB_ACK to gather path quality observations.
|
2020-05-15 03:09:25 +00:00
|
|
|
*
|
2020-05-12 08:35:48 +00:00
|
|
|
* @param path Path over which packet was received
|
|
|
|
* @param now Current time
|
|
|
|
* @param ackedBytes Number of bytes ACKed by this VERB_ACK
|
|
|
|
*/
|
|
|
|
void receivedAck(const SharedPtr<Path>& path, int64_t now, int32_t ackedBytes);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate the contents of a VERB_QOS_MEASUREMENT packet.
|
|
|
|
*
|
|
|
|
* @param now Current time
|
|
|
|
* @param qosBuffer destination buffer
|
|
|
|
* @return Size of payload
|
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
int32_t generateQoSPacket(const SharedPtr<Path>& path, int64_t now, char* qosBuffer);
|
2020-05-12 08:35:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Record statistics for an inbound packet.
|
2020-05-15 03:09:25 +00:00
|
|
|
*
|
2020-05-12 08:35:48 +00:00
|
|
|
* @param path Path over which packet was received
|
|
|
|
* @param packetId Packet ID
|
|
|
|
* @param payloadLength Packet data length
|
|
|
|
* @param verb Packet verb
|
|
|
|
* @param flowId Flow ID
|
|
|
|
* @param now Current time
|
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
void recordIncomingPacket(const SharedPtr<Path>& path, uint64_t packetId, uint16_t payloadLength, Packet::Verb verb, int32_t flowId, int64_t now);
|
2020-05-12 08:35:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines the most appropriate path for packet and flow egress. This decision is made by
|
|
|
|
* the underlying bonding policy as well as QoS-related statistical observations of path quality.
|
|
|
|
*
|
|
|
|
* @param now Current time
|
|
|
|
* @param flowId Flow ID
|
|
|
|
* @return Pointer to suggested Path
|
|
|
|
*/
|
|
|
|
SharedPtr<Path> getAppropriatePath(int64_t now, int32_t flowId);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new flow record
|
2020-05-15 03:09:25 +00:00
|
|
|
*
|
2020-05-12 08:35:48 +00:00
|
|
|
* @param path Path over which flow shall be handled
|
|
|
|
* @param flowId Flow ID
|
|
|
|
* @param entropy A byte of entropy to be used by the bonding algorithm
|
|
|
|
* @param now Current time
|
|
|
|
* @return Pointer to newly-created Flow
|
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
SharedPtr<Flow> createFlow(const SharedPtr<Path>& path, int32_t flowId, unsigned char entropy, int64_t now);
|
2020-05-12 08:35:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes flow records that are past a certain age limit.
|
2020-05-15 03:09:25 +00:00
|
|
|
*
|
2020-05-12 08:35:48 +00:00
|
|
|
* @param age Age threshold to be forgotten
|
|
|
|
* @param oldest Whether only the oldest shall be forgotten
|
|
|
|
* @param now Current time
|
|
|
|
*/
|
|
|
|
void forgetFlowsWhenNecessary(uint64_t age, bool oldest, int64_t now);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assigns a new flow to a bonded path
|
2020-05-15 03:09:25 +00:00
|
|
|
*
|
2020-05-12 08:35:48 +00:00
|
|
|
* @param flow Flow to be assigned
|
|
|
|
* @param now Current time
|
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
bool assignFlowToBondedPath(SharedPtr<Flow>& flow, int64_t now);
|
2020-05-12 08:35:48 +00:00
|
|
|
|
2020-05-15 03:09:25 +00:00
|
|
|
/**
|
2020-05-12 08:35:48 +00:00
|
|
|
* Determine whether a path change should occur given the remote peer's reported utility and our
|
|
|
|
* local peer's known utility. This has the effect of assigning inbound and outbound traffic to
|
2020-05-15 03:09:25 +00:00
|
|
|
* the same path.
|
|
|
|
*
|
2020-05-12 08:35:48 +00:00
|
|
|
* @param now Current time
|
|
|
|
* @param path Path over which the negotiation request was received
|
|
|
|
* @param remoteUtility How much utility the remote peer claims to gain by using the declared path
|
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
void processIncomingPathNegotiationRequest(uint64_t now, SharedPtr<Path>& path, int16_t remoteUtility);
|
2020-05-12 08:35:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine state of path synchronization and whether a negotiation request
|
|
|
|
* shall be sent to the peer.
|
|
|
|
*
|
|
|
|
* @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
|
|
|
|
* @param now Current time
|
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
void pathNegotiationCheck(void* tPtr, const int64_t now);
|
2020-05-12 08:35:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a VERB_ACK to the remote peer.
|
2020-05-15 03:09:25 +00:00
|
|
|
*
|
2020-05-12 08:35:48 +00:00
|
|
|
* @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
|
|
|
|
* @param path Path over which packet should be sent
|
|
|
|
* @param localSocket Local source socket
|
|
|
|
* @param atAddress
|
|
|
|
* @param now Current time
|
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
void sendACK(void* tPtr, const SharedPtr<Path>& path, int64_t localSocket, const InetAddress& atAddress, int64_t now);
|
2020-05-12 08:35:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a VERB_QOS_MEASUREMENT to the remote peer.
|
2020-05-15 03:09:25 +00:00
|
|
|
*
|
2020-05-12 08:35:48 +00:00
|
|
|
* @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
|
|
|
|
* @param path Path over which packet should be sent
|
|
|
|
* @param localSocket Local source socket
|
|
|
|
* @param atAddress
|
|
|
|
* @param now Current time
|
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
void sendQOS_MEASUREMENT(void* tPtr, const SharedPtr<Path>& path, int64_t localSocket, const InetAddress& atAddress, int64_t now);
|
2020-05-12 08:35:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a VERB_PATH_NEGOTIATION_REQUEST to the remote peer.
|
2020-05-15 03:09:25 +00:00
|
|
|
*
|
2020-05-12 08:35:48 +00:00
|
|
|
* @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
|
|
|
|
* @param path Path over which packet should be sent
|
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
void sendPATH_NEGOTIATION_REQUEST(void* tPtr, const SharedPtr<Path>& path);
|
2020-05-12 08:35:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param now Current time
|
|
|
|
*/
|
|
|
|
void processBalanceTasks(int64_t now);
|
2020-05-15 03:09:25 +00:00
|
|
|
|
2020-05-12 08:35:48 +00:00
|
|
|
/**
|
|
|
|
* Perform periodic tasks unique to active-backup
|
2020-05-15 03:09:25 +00:00
|
|
|
*
|
2020-08-07 01:10:40 +00:00
|
|
|
* @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
|
2020-05-12 08:35:48 +00:00
|
|
|
* @param now Current time
|
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
void processActiveBackupTasks(void* tPtr, int64_t now);
|
2020-05-12 08:35:48 +00:00
|
|
|
|
|
|
|
/**
|
2020-06-17 21:54:13 +00:00
|
|
|
* Switches the active link in an active-backup scenario to the next best during
|
2020-05-12 08:35:48 +00:00
|
|
|
* a failover event.
|
|
|
|
*
|
|
|
|
* @param now Current time
|
|
|
|
*/
|
|
|
|
void dequeueNextActiveBackupPath(uint64_t now);
|
|
|
|
|
2020-05-15 03:09:25 +00:00
|
|
|
/**
|
|
|
|
* Set bond parameters to reasonable defaults, these may later be overwritten by
|
2020-05-12 08:35:48 +00:00
|
|
|
* user-specified parameters.
|
2020-05-15 03:09:25 +00:00
|
|
|
*
|
|
|
|
* @param policy Bonding policy
|
2020-06-02 05:58:58 +00:00
|
|
|
* @param templateBond
|
2020-05-15 03:09:25 +00:00
|
|
|
*/
|
2020-06-02 05:58:58 +00:00
|
|
|
void setReasonableDefaults(int policy, SharedPtr<Bond> templateBond, bool useTemplate);
|
2020-05-12 08:35:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check and assign user-specified quality weights to this bond.
|
|
|
|
*
|
|
|
|
* @param weights Set of user-specified weights
|
|
|
|
* @param len Length of weight vector
|
|
|
|
*/
|
|
|
|
void setUserQualityWeights(float weights[], int len);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param latencyInMilliseconds Maximum acceptable latency.
|
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
void setMaxAcceptableLatency(int16_t latencyInMilliseconds)
|
|
|
|
{
|
2020-05-12 08:35:48 +00:00
|
|
|
_maxAcceptableLatency = latencyInMilliseconds;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param latencyInMilliseconds Maximum acceptable (mean) latency.
|
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
void setMaxAcceptableMeanLatency(int16_t latencyInMilliseconds)
|
|
|
|
{
|
2020-05-12 08:35:48 +00:00
|
|
|
_maxAcceptableMeanLatency = latencyInMilliseconds;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param latencyVarianceInMilliseconds Maximum acceptable packet delay variance (jitter).
|
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
void setMaxAcceptablePacketDelayVariance(int16_t latencyVarianceInMilliseconds)
|
|
|
|
{
|
2020-05-12 08:35:48 +00:00
|
|
|
_maxAcceptablePacketDelayVariance = latencyVarianceInMilliseconds;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param lossRatio Maximum acceptable packet loss ratio (PLR).
|
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
void setMaxAcceptablePacketLossRatio(float lossRatio)
|
|
|
|
{
|
2020-05-12 08:35:48 +00:00
|
|
|
_maxAcceptablePacketLossRatio = lossRatio;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param errorRatio Maximum acceptable packet error ratio (PER).
|
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
void setMaxAcceptablePacketErrorRatio(float errorRatio)
|
|
|
|
{
|
2020-05-12 08:35:48 +00:00
|
|
|
_maxAcceptablePacketErrorRatio = errorRatio;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param errorRatio Maximum acceptable packet error ratio (PER).
|
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
void setMinAcceptableAllocation(float minAlloc)
|
|
|
|
{
|
2020-05-12 08:35:48 +00:00
|
|
|
_minAcceptableAllocation = minAlloc * 255;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-06-17 21:54:13 +00:00
|
|
|
* @return Whether the user has defined links for use on this bond
|
2020-05-12 08:35:48 +00:00
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
inline bool userHasSpecifiedLinks()
|
|
|
|
{
|
|
|
|
return _userHasSpecifiedLinks;
|
|
|
|
}
|
2020-05-12 08:35:48 +00:00
|
|
|
|
|
|
|
/**
|
2020-06-17 21:54:13 +00:00
|
|
|
* @return Whether the user has defined a set of failover link(s) for this bond
|
2020-05-12 08:35:48 +00:00
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
inline bool userHasSpecifiedFailoverInstructions()
|
|
|
|
{
|
|
|
|
return _userHasSpecifiedFailoverInstructions;
|
|
|
|
};
|
2020-05-12 08:35:48 +00:00
|
|
|
|
|
|
|
/**
|
2020-06-17 21:54:13 +00:00
|
|
|
* @return Whether the user has specified a primary link
|
2020-05-12 08:35:48 +00:00
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
inline bool userHasSpecifiedPrimaryLink()
|
|
|
|
{
|
|
|
|
return _userHasSpecifiedPrimaryLink;
|
|
|
|
}
|
2020-05-12 08:35:48 +00:00
|
|
|
|
|
|
|
/**
|
2020-06-17 21:54:13 +00:00
|
|
|
* @return Whether the user has specified link speeds
|
2020-05-12 08:35:48 +00:00
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
inline bool userHasSpecifiedLinkSpeeds()
|
|
|
|
{
|
|
|
|
return _userHasSpecifiedLinkSpeeds;
|
|
|
|
}
|
2020-05-12 08:35:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Periodically perform maintenance tasks for each active bond.
|
|
|
|
*
|
|
|
|
* @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
|
|
|
|
* @param now Current time
|
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
void processBackgroundTasks(void* tPtr, int64_t now);
|
2020-05-12 08:35:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Rate limit gate for VERB_ACK
|
|
|
|
*
|
|
|
|
* @param now Current time
|
|
|
|
* @return Whether the incoming packet should be rate-gated
|
|
|
|
*/
|
|
|
|
inline bool rateGateACK(const int64_t now)
|
|
|
|
{
|
|
|
|
_ackCutoffCount++;
|
2021-05-04 00:59:31 +00:00
|
|
|
int numToDrain = _lastAckRateCheck ? (now - _lastAckRateCheck) / ZT_ACK_DRAINAGE_DIVISOR : _ackCutoffCount;
|
2020-05-12 08:35:48 +00:00
|
|
|
_lastAckRateCheck = now;
|
|
|
|
if (_ackCutoffCount > numToDrain) {
|
2021-05-04 00:59:31 +00:00
|
|
|
_ackCutoffCount -= numToDrain;
|
|
|
|
}
|
|
|
|
else {
|
2020-05-12 08:35:48 +00:00
|
|
|
_ackCutoffCount = 0;
|
|
|
|
}
|
|
|
|
return (_ackCutoffCount < ZT_ACK_CUTOFF_LIMIT);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rate limit gate for VERB_QOS_MEASUREMENT
|
|
|
|
*
|
|
|
|
* @param now Current time
|
|
|
|
* @return Whether the incoming packet should be rate-gated
|
|
|
|
*/
|
|
|
|
inline bool rateGateQoS(const int64_t now)
|
|
|
|
{
|
|
|
|
_qosCutoffCount++;
|
|
|
|
int numToDrain = (now - _lastQoSRateCheck) / ZT_QOS_DRAINAGE_DIVISOR;
|
|
|
|
_lastQoSRateCheck = now;
|
|
|
|
if (_qosCutoffCount > numToDrain) {
|
2021-05-04 00:59:31 +00:00
|
|
|
_qosCutoffCount -= numToDrain;
|
|
|
|
}
|
|
|
|
else {
|
2020-05-12 08:35:48 +00:00
|
|
|
_qosCutoffCount = 0;
|
|
|
|
}
|
|
|
|
return (_qosCutoffCount < ZT_QOS_CUTOFF_LIMIT);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rate limit gate for VERB_PATH_NEGOTIATION_REQUEST
|
|
|
|
*
|
|
|
|
* @param now Current time
|
|
|
|
* @return Whether the incoming packet should be rate-gated
|
|
|
|
*/
|
|
|
|
inline bool rateGatePathNegotiation(const int64_t now)
|
|
|
|
{
|
|
|
|
if ((now - _lastPathNegotiationReceived) <= ZT_PATH_NEGOTIATION_CUTOFF_TIME)
|
|
|
|
++_pathNegotiationCutoffCount;
|
2021-05-04 00:59:31 +00:00
|
|
|
else
|
|
|
|
_pathNegotiationCutoffCount = 0;
|
2020-05-12 08:35:48 +00:00
|
|
|
_lastPathNegotiationReceived = now;
|
|
|
|
return (_pathNegotiationCutoffCount < ZT_PATH_NEGOTIATION_CUTOFF_LIMIT);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param interval Maximum amount of time user expects a failover to take on this bond.
|
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
inline void setFailoverInterval(uint32_t interval)
|
|
|
|
{
|
|
|
|
_failoverInterval = interval;
|
|
|
|
}
|
2020-05-12 08:35:48 +00:00
|
|
|
|
2020-11-23 17:59:28 +00:00
|
|
|
/**
|
|
|
|
* @param interval Maximum amount of time user expects a failover to take on this bond.
|
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
inline uint32_t getFailoverInterval()
|
|
|
|
{
|
|
|
|
return _failoverInterval;
|
|
|
|
}
|
2020-11-23 17:59:28 +00:00
|
|
|
|
2020-05-12 08:35:48 +00:00
|
|
|
/**
|
2020-06-16 19:30:21 +00:00
|
|
|
* @param strategy Strategy that the bond uses to re-assign protocol flows.
|
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
inline void setFlowRebalanceStrategy(uint32_t strategy)
|
|
|
|
{
|
|
|
|
_flowRebalanceStrategy = strategy;
|
|
|
|
}
|
2020-06-16 19:30:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param strategy Strategy that the bond uses to prob for path aliveness and quality
|
2020-05-12 08:35:48 +00:00
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
inline void setLinkMonitorStrategy(uint8_t strategy)
|
|
|
|
{
|
|
|
|
_linkMonitorStrategy = strategy;
|
|
|
|
}
|
2020-05-12 08:35:48 +00:00
|
|
|
|
2020-11-23 17:59:28 +00:00
|
|
|
/**
|
|
|
|
* @param abOverflowEnabled Whether "overflow" mode is enabled for this active-backup bond
|
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
inline void setOverflowMode(bool abOverflowEnabled)
|
|
|
|
{
|
|
|
|
_abOverflowEnabled = abOverflowEnabled;
|
|
|
|
}
|
2020-11-23 17:59:28 +00:00
|
|
|
|
2020-05-12 08:35:48 +00:00
|
|
|
/**
|
|
|
|
* @return the current up delay parameter
|
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
inline uint16_t getUpDelay()
|
|
|
|
{
|
|
|
|
return _upDelay;
|
|
|
|
}
|
2020-05-12 08:35:48 +00:00
|
|
|
|
2020-05-15 03:09:25 +00:00
|
|
|
/**
|
|
|
|
* @param upDelay Length of time before a newly-discovered path is admitted to the bond
|
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
inline void setUpDelay(int upDelay)
|
|
|
|
{
|
|
|
|
if (upDelay >= 0) {
|
|
|
|
_upDelay = upDelay;
|
|
|
|
}
|
|
|
|
}
|
2020-05-12 08:35:48 +00:00
|
|
|
|
2020-05-15 03:09:25 +00:00
|
|
|
/**
|
|
|
|
* @return Length of time before a newly-failed path is removed from the bond
|
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
inline uint16_t getDownDelay()
|
|
|
|
{
|
|
|
|
return _downDelay;
|
|
|
|
}
|
2020-05-12 08:35:48 +00:00
|
|
|
|
2020-05-15 03:09:25 +00:00
|
|
|
/**
|
|
|
|
* @param downDelay Length of time before a newly-failed path is removed from the bond
|
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
inline void setDownDelay(int downDelay)
|
|
|
|
{
|
|
|
|
if (downDelay >= 0) {
|
|
|
|
_downDelay = downDelay;
|
|
|
|
}
|
|
|
|
}
|
2020-05-12 08:35:48 +00:00
|
|
|
|
|
|
|
/**
|
2020-06-17 21:54:13 +00:00
|
|
|
* @return the current monitoring interval for the bond (can be overridden with intervals specific to certain links.)
|
2020-05-12 08:35:48 +00:00
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
inline uint16_t getBondMonitorInterval()
|
|
|
|
{
|
|
|
|
return _bondMonitorInterval;
|
|
|
|
}
|
2020-05-12 08:35:48 +00:00
|
|
|
|
2020-05-15 03:09:25 +00:00
|
|
|
/**
|
2020-06-17 21:54:13 +00:00
|
|
|
* Set the current monitoring interval for the bond (can be overridden with intervals specific to certain links.)
|
2020-05-15 03:09:25 +00:00
|
|
|
*
|
|
|
|
* @param monitorInterval How often gratuitous VERB_HELLO(s) are sent to remote peer.
|
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
inline void setBondMonitorInterval(uint16_t interval)
|
|
|
|
{
|
|
|
|
_bondMonitorInterval = interval;
|
|
|
|
}
|
2020-05-12 08:35:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param policy Bonding policy for this bond
|
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
inline void setPolicy(uint8_t policy)
|
|
|
|
{
|
|
|
|
_bondingPolicy = policy;
|
|
|
|
}
|
2020-05-12 08:35:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the current bonding policy
|
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
inline uint8_t getPolicy()
|
|
|
|
{
|
|
|
|
return _bondingPolicy;
|
|
|
|
}
|
2020-05-12 08:35:48 +00:00
|
|
|
|
2020-07-28 06:01:12 +00:00
|
|
|
/**
|
|
|
|
* @return the health status of the bond
|
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
inline bool isHealthy()
|
|
|
|
{
|
|
|
|
return _isHealthy;
|
|
|
|
}
|
2020-07-28 06:01:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the number of links comprising this bond which are considered alive
|
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
inline uint8_t getNumAliveLinks()
|
|
|
|
{
|
|
|
|
return _numAliveLinks;
|
|
|
|
};
|
2020-07-28 06:01:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the number of links comprising this bond
|
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
inline uint8_t getNumTotalLinks()
|
|
|
|
{
|
|
|
|
return _numTotalLinks;
|
|
|
|
}
|
2020-07-28 06:01:12 +00:00
|
|
|
|
2020-05-15 03:09:25 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param allowFlowHashing
|
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
inline void setFlowHashing(bool allowFlowHashing)
|
|
|
|
{
|
|
|
|
_allowFlowHashing = allowFlowHashing;
|
|
|
|
}
|
2020-05-12 08:35:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Whether flow-hashing is currently enabled for this bond.
|
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
bool flowHashingEnabled()
|
|
|
|
{
|
|
|
|
return _allowFlowHashing;
|
|
|
|
}
|
2020-05-12 08:35:48 +00:00
|
|
|
|
2020-05-15 03:09:25 +00:00
|
|
|
/**
|
|
|
|
*
|
2020-06-17 21:54:13 +00:00
|
|
|
* @param packetsPerLink
|
2020-05-15 03:09:25 +00:00
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
inline void setPacketsPerLink(int packetsPerLink)
|
|
|
|
{
|
|
|
|
_packetsPerLink = packetsPerLink;
|
|
|
|
}
|
2020-05-12 08:35:48 +00:00
|
|
|
|
2020-11-23 17:59:28 +00:00
|
|
|
/**
|
|
|
|
* @return Number of packets to be sent on each interface in a balance-rr bond
|
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
inline int getPacketsPerLink()
|
|
|
|
{
|
|
|
|
return _packetsPerLink;
|
|
|
|
}
|
2020-11-23 17:59:28 +00:00
|
|
|
|
2020-05-12 08:35:48 +00:00
|
|
|
/**
|
|
|
|
*
|
2020-06-17 21:54:13 +00:00
|
|
|
* @param linkSelectMethod
|
2020-05-12 08:35:48 +00:00
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
inline void setLinkSelectMethod(uint8_t method)
|
|
|
|
{
|
|
|
|
_abLinkSelectMethod = method;
|
|
|
|
}
|
2020-05-12 08:35:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
inline uint8_t getLinkSelectMethod()
|
|
|
|
{
|
|
|
|
return _abLinkSelectMethod;
|
|
|
|
}
|
2020-05-12 08:35:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param allowPathNegotiation
|
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
inline void setAllowPathNegotiation(bool allowPathNegotiation)
|
|
|
|
{
|
|
|
|
_allowPathNegotiation = allowPathNegotiation;
|
|
|
|
}
|
2020-05-12 08:35:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
inline bool allowPathNegotiation()
|
|
|
|
{
|
|
|
|
return _allowPathNegotiation;
|
|
|
|
}
|
2020-05-12 08:35:48 +00:00
|
|
|
|
2020-11-23 17:59:28 +00:00
|
|
|
/**
|
|
|
|
* Forcibly rotates the currently active link used in an active-backup bond to the next link in the failover queue
|
|
|
|
*
|
|
|
|
* @return True if this operation succeeded, false if otherwise
|
|
|
|
*/
|
|
|
|
bool abForciblyRotateLink();
|
|
|
|
|
2021-05-04 00:59:31 +00:00
|
|
|
SharedPtr<Peer> getPeer()
|
|
|
|
{
|
|
|
|
return _peer;
|
|
|
|
}
|
2020-05-12 08:35:48 +00:00
|
|
|
|
2021-05-04 00:59:31 +00:00
|
|
|
private:
|
|
|
|
const RuntimeEnvironment* RR;
|
2020-05-12 08:35:48 +00:00
|
|
|
AtomicCounter __refCount;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Custom name given by the user to this bond type.
|
|
|
|
*/
|
|
|
|
std::string _policyAlias;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Paths that this bond has been made aware of but that are not necessarily
|
|
|
|
* part of the bond proper.
|
|
|
|
*/
|
|
|
|
SharedPtr<Path> _paths[ZT_MAX_PEER_NETWORK_PATHS];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set of indices corresponding to paths currently included in the bond proper. This
|
|
|
|
* may only be updated during a call to curateBond(). The reason for this is so that
|
|
|
|
* we can simplify the high frequency packet egress logic.
|
|
|
|
*/
|
|
|
|
int _bondedIdx[ZT_MAX_PEER_NETWORK_PATHS];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Number of paths currently included in the _bondedIdx set.
|
|
|
|
*/
|
|
|
|
int _numBondedPaths;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flows hashed according to port and protocol
|
|
|
|
*/
|
2021-05-04 00:59:31 +00:00
|
|
|
std::map<int32_t, SharedPtr<Flow> > _flows;
|
2020-05-12 08:35:48 +00:00
|
|
|
|
2021-05-04 00:59:31 +00:00
|
|
|
float _qualityWeights[ZT_QOS_WEIGHT_SIZE]; // How much each factor contributes to the "quality" score of a path.
|
2020-05-12 08:35:48 +00:00
|
|
|
|
|
|
|
uint8_t _bondingPolicy;
|
|
|
|
uint32_t _upDelay;
|
|
|
|
uint32_t _downDelay;
|
|
|
|
|
|
|
|
// active-backup
|
2021-05-04 00:59:31 +00:00
|
|
|
SharedPtr<Path> _abPath; // current active path
|
2020-05-12 08:35:48 +00:00
|
|
|
std::list<SharedPtr<Path> > _abFailoverQueue;
|
2021-05-04 00:59:31 +00:00
|
|
|
uint8_t _abLinkSelectMethod; // link re-selection policy for the primary link in active-backup
|
2020-11-23 17:59:28 +00:00
|
|
|
bool _abOverflowEnabled;
|
2020-05-12 08:35:48 +00:00
|
|
|
|
|
|
|
// balance-rr
|
2021-05-04 00:59:31 +00:00
|
|
|
uint8_t _rrIdx; // index to path currently in use during Round Robin operation
|
|
|
|
uint16_t _rrPacketsSentOnCurrLink; // number of packets sent on this link since the most recent path switch.
|
2020-05-12 08:35:48 +00:00
|
|
|
/**
|
|
|
|
* How many packets will be sent on a path before moving to the next path
|
|
|
|
* in the round-robin sequence. A value of zero will cause a random path
|
|
|
|
* selection for each outgoing packet.
|
|
|
|
*/
|
2020-06-17 21:54:13 +00:00
|
|
|
int _packetsPerLink;
|
2020-05-12 08:35:48 +00:00
|
|
|
|
|
|
|
// balance-aware
|
|
|
|
uint64_t _totalBondUnderload;
|
2020-06-16 19:30:21 +00:00
|
|
|
uint8_t _flowRebalanceStrategy;
|
2020-05-12 08:35:48 +00:00
|
|
|
|
2020-06-17 21:54:13 +00:00
|
|
|
// dynamic link monitoring
|
|
|
|
uint8_t _linkMonitorStrategy;
|
2020-05-12 08:35:48 +00:00
|
|
|
uint32_t _dynamicPathMonitorInterval;
|
|
|
|
|
|
|
|
// path negotiation
|
|
|
|
int16_t _localUtility;
|
|
|
|
SharedPtr<Path> negotiatedPath;
|
|
|
|
uint8_t _numSentPathNegotiationRequests;
|
|
|
|
unsigned int _pathNegotiationCutoffCount;
|
|
|
|
bool _allowPathNegotiation;
|
|
|
|
|
2020-08-07 01:10:40 +00:00
|
|
|
/**
|
|
|
|
* Timers and intervals
|
|
|
|
*/
|
2020-05-12 08:35:48 +00:00
|
|
|
uint32_t _failoverInterval;
|
|
|
|
uint32_t _qosSendInterval;
|
|
|
|
uint32_t _ackSendInterval;
|
|
|
|
uint32_t throughputMeasurementInterval;
|
|
|
|
uint32_t _qualityEstimationInterval;
|
|
|
|
|
2020-08-07 01:10:40 +00:00
|
|
|
/**
|
|
|
|
* Acceptable quality thresholds
|
|
|
|
*/
|
2020-05-12 08:35:48 +00:00
|
|
|
float _maxAcceptablePacketLossRatio;
|
|
|
|
float _maxAcceptablePacketErrorRatio;
|
|
|
|
uint16_t _maxAcceptableLatency;
|
|
|
|
uint16_t _maxAcceptableMeanLatency;
|
|
|
|
uint16_t _maxAcceptablePacketDelayVariance;
|
|
|
|
uint8_t _minAcceptableAllocation;
|
|
|
|
|
2020-08-07 01:10:40 +00:00
|
|
|
/**
|
|
|
|
* Link state reporting
|
|
|
|
*/
|
2020-07-28 06:01:12 +00:00
|
|
|
bool _isHealthy;
|
|
|
|
uint8_t _numAliveLinks;
|
|
|
|
uint8_t _numTotalLinks;
|
|
|
|
|
2020-05-12 08:35:48 +00:00
|
|
|
/**
|
|
|
|
* Default initial punishment inflicted on misbehaving paths. Punishment slowly
|
|
|
|
* drains linearly. For each eligibility change the remaining punishment is doubled.
|
|
|
|
*/
|
|
|
|
uint32_t _defaultPathRefractoryPeriod;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the current bonding policy requires computation of path statistics
|
|
|
|
*/
|
|
|
|
bool _shouldCollectPathStatistics;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Free byte of entropy that is updated on every packet egress event.
|
|
|
|
*/
|
|
|
|
unsigned char _freeRandomByte;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remote peer that this bond services
|
|
|
|
*/
|
|
|
|
SharedPtr<Peer> _peer;
|
|
|
|
|
2020-08-07 01:10:40 +00:00
|
|
|
/**
|
|
|
|
* Rate-limit cutoffs
|
|
|
|
*/
|
|
|
|
uint16_t _qosCutoffCount;
|
|
|
|
uint16_t _ackCutoffCount;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Recent event timestamps
|
|
|
|
*/
|
|
|
|
uint64_t _lastAckRateCheck;
|
|
|
|
uint64_t _lastQoSRateCheck;
|
|
|
|
uint64_t _lastQualityEstimation;
|
|
|
|
uint64_t _lastCheckUserPreferences;
|
|
|
|
uint64_t _lastBackgroundTaskCheck;
|
|
|
|
uint64_t _lastBondStatusLog;
|
|
|
|
uint64_t _lastPathNegotiationReceived;
|
|
|
|
uint64_t _lastPathNegotiationCheck;
|
|
|
|
uint64_t _lastSentPathNegotiationRequest;
|
|
|
|
uint64_t _lastFlowStatReset;
|
|
|
|
uint64_t _lastFlowExpirationCheck;
|
|
|
|
uint64_t _lastFlowRebalance;
|
|
|
|
uint64_t _lastFrame;
|
|
|
|
uint64_t _lastActiveBackupPathChange;
|
|
|
|
|
2020-05-12 08:35:48 +00:00
|
|
|
Mutex _paths_m;
|
|
|
|
Mutex _flows_m;
|
|
|
|
|
|
|
|
/**
|
2020-06-17 21:54:13 +00:00
|
|
|
* Whether the user has specified links for this bond.
|
2020-05-12 08:35:48 +00:00
|
|
|
*/
|
2020-06-17 21:54:13 +00:00
|
|
|
bool _userHasSpecifiedLinks;
|
2020-05-12 08:35:48 +00:00
|
|
|
|
|
|
|
/**
|
2020-06-17 21:54:13 +00:00
|
|
|
* Whether the user has specified a primary link for this bond.
|
2020-05-12 08:35:48 +00:00
|
|
|
*/
|
2020-06-17 21:54:13 +00:00
|
|
|
bool _userHasSpecifiedPrimaryLink;
|
2020-05-12 08:35:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the user has specified failover instructions for this bond.
|
|
|
|
*/
|
|
|
|
bool _userHasSpecifiedFailoverInstructions;
|
|
|
|
|
|
|
|
/**
|
2020-06-17 21:54:13 +00:00
|
|
|
* Whether the user has specified links speeds for this bond.
|
2020-05-12 08:35:48 +00:00
|
|
|
*/
|
2020-06-17 21:54:13 +00:00
|
|
|
bool _userHasSpecifiedLinkSpeeds;
|
2020-05-12 08:35:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* How frequently (in ms) a VERB_ECHO is sent to a peer to verify that a
|
|
|
|
* path is still active. A value of zero (0) will disable active path
|
|
|
|
* monitoring; as result, all monitoring will be a function of traffic.
|
|
|
|
*/
|
|
|
|
uint16_t _bondMonitorInterval;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether or not flow hashing is allowed.
|
|
|
|
*/
|
|
|
|
bool _allowFlowHashing;
|
|
|
|
};
|
|
|
|
|
2021-05-04 00:59:31 +00:00
|
|
|
} // namespace ZeroTier
|
2020-05-12 08:35:48 +00:00
|
|
|
|
2021-04-07 22:15:35 +00:00
|
|
|
#endif
|