Match formatting of Bond-related sources to ZeroTier standard (no functional changes)

This commit is contained in:
Joseph Henry 2021-05-03 17:59:31 -07:00
parent 63fd2cbaeb
commit 29e5880d8b
No known key found for this signature in database
GPG Key ID: C45B33FF5EBC9344
6 changed files with 971 additions and 654 deletions

File diff suppressed because it is too large Load Diff

View File

@ -14,37 +14,36 @@
#ifndef ZT_BOND_HPP
#define ZT_BOND_HPP
#include <map>
#include "Flow.hpp"
#include "Path.hpp"
#include "Peer.hpp"
#include "../osdep/Link.hpp"
#include "Flow.hpp"
#include "Packet.hpp"
#include <list>
#include <map>
namespace ZeroTier {
class RuntimeEnvironment;
class Link;
class Peer;
class Bond
{
class Bond {
friend class SharedPtr<Bond>;
friend class Peer;
friend class BondController;
struct PathQualityComparator
{
bool operator ()(const SharedPtr<Path> & a, const SharedPtr<Path> & b)
struct PathQualityComparator {
bool operator()(const SharedPtr<Path>& a, const SharedPtr<Path>& b)
{
if(a->_failoverScore == b->_failoverScore) {
if (a->_failoverScore == b->_failoverScore) {
return a < b;
}
return a->_failoverScore > b->_failoverScore;
}
};
public:
public:
// TODO: Remove
bool _header;
int64_t _lastLogTS;
@ -61,7 +60,7 @@ public:
* @param policy Bonding policy
* @param peer
*/
Bond(const RuntimeEnvironment *renv, int policy, const SharedPtr<Peer>& peer);
Bond(const RuntimeEnvironment* renv, int policy, const SharedPtr<Peer>& peer);
/**
* Constructor. For use when user intends to manually specify parameters
@ -70,7 +69,7 @@ public:
* @param policyAlias
* @param peer
*/
Bond(const RuntimeEnvironment *renv, std::string& basePolicy, std::string& policyAlias, const SharedPtr<Peer>& peer);
Bond(const RuntimeEnvironment* renv, std::string& basePolicy, std::string& policyAlias, const SharedPtr<Peer>& peer);
/**
* Constructor. Creates a bond based off of a user-defined bond template
@ -79,12 +78,15 @@ public:
* @param original
* @param peer
*/
Bond(const RuntimeEnvironment *renv, SharedPtr<Bond> originalBond, const SharedPtr<Peer>& peer);
Bond(const RuntimeEnvironment* renv, SharedPtr<Bond> originalBond, const SharedPtr<Peer>& peer);
/**
* @return The human-readable name of the bonding policy
*/
std::string policyAlias() { return _policyAlias; }
std::string policyAlias()
{
return _policyAlias;
}
/**
* Inform the bond about the path that its peer (owning object) just learned about.
@ -135,8 +137,7 @@ public:
* @param flowId Flow ID
* @param now Current time
*/
void recordOutgoingPacket(const SharedPtr<Path> &path, uint64_t packetId,
uint16_t payloadLength, Packet::Verb verb, int32_t flowId, int64_t now);
void recordOutgoingPacket(const SharedPtr<Path>& path, uint64_t packetId, uint16_t payloadLength, Packet::Verb verb, int32_t flowId, int64_t now);
/**
* Process the contents of an inbound VERB_QOS_MEASUREMENT to gather path quality observations.
@ -146,7 +147,7 @@ public:
* @param rx_id table of packet IDs
* @param rx_ts table of holding times
*/
void receivedQoS(const SharedPtr<Path>& path, int64_t now, int count, uint64_t *rx_id, uint16_t *rx_ts);
void receivedQoS(const SharedPtr<Path>& path, int64_t now, int count, uint64_t* rx_id, uint16_t* rx_ts);
/**
* Process the contents of an inbound VERB_ACK to gather path quality observations.
@ -164,7 +165,7 @@ public:
* @param qosBuffer destination buffer
* @return Size of payload
*/
int32_t generateQoSPacket(const SharedPtr<Path>& path, int64_t now, char *qosBuffer);
int32_t generateQoSPacket(const SharedPtr<Path>& path, int64_t now, char* qosBuffer);
/**
* Record statistics for an inbound packet.
@ -176,8 +177,7 @@ public:
* @param flowId Flow ID
* @param now Current time
*/
void recordIncomingPacket(const SharedPtr<Path>& path, uint64_t packetId, uint16_t payloadLength,
Packet::Verb verb, int32_t flowId, int64_t now);
void recordIncomingPacket(const SharedPtr<Path>& path, uint64_t packetId, uint16_t payloadLength, Packet::Verb verb, int32_t flowId, int64_t now);
/**
* Determines the most appropriate path for packet and flow egress. This decision is made by
@ -198,7 +198,7 @@ public:
* @param now Current time
* @return Pointer to newly-created Flow
*/
SharedPtr<Flow> createFlow(const SharedPtr<Path> &path, int32_t flowId, unsigned char entropy, int64_t now);
SharedPtr<Flow> createFlow(const SharedPtr<Path>& path, int32_t flowId, unsigned char entropy, int64_t now);
/**
* Removes flow records that are past a certain age limit.
@ -215,7 +215,7 @@ public:
* @param flow Flow to be assigned
* @param now Current time
*/
bool assignFlowToBondedPath(SharedPtr<Flow> &flow, int64_t now);
bool assignFlowToBondedPath(SharedPtr<Flow>& flow, int64_t now);
/**
* Determine whether a path change should occur given the remote peer's reported utility and our
@ -226,7 +226,7 @@ public:
* @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
*/
void processIncomingPathNegotiationRequest(uint64_t now, SharedPtr<Path> &path, int16_t remoteUtility);
void processIncomingPathNegotiationRequest(uint64_t now, SharedPtr<Path>& path, int16_t remoteUtility);
/**
* Determine state of path synchronization and whether a negotiation request
@ -235,7 +235,7 @@ public:
* @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
* @param now Current time
*/
void pathNegotiationCheck(void *tPtr, const int64_t now);
void pathNegotiationCheck(void* tPtr, const int64_t now);
/**
* Sends a VERB_ACK to the remote peer.
@ -246,8 +246,7 @@ public:
* @param atAddress
* @param now Current time
*/
void sendACK(void *tPtr, const SharedPtr<Path> &path,int64_t localSocket,
const InetAddress &atAddress,int64_t now);
void sendACK(void* tPtr, const SharedPtr<Path>& path, int64_t localSocket, const InetAddress& atAddress, int64_t now);
/**
* Sends a VERB_QOS_MEASUREMENT to the remote peer.
@ -258,8 +257,7 @@ public:
* @param atAddress
* @param now Current time
*/
void sendQOS_MEASUREMENT(void *tPtr,const SharedPtr<Path> &path,int64_t localSocket,
const InetAddress &atAddress,int64_t now);
void sendQOS_MEASUREMENT(void* tPtr, const SharedPtr<Path>& path, int64_t localSocket, const InetAddress& atAddress, int64_t now);
/**
* Sends a VERB_PATH_NEGOTIATION_REQUEST to the remote peer.
@ -267,7 +265,7 @@ public:
* @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
*/
void sendPATH_NEGOTIATION_REQUEST(void *tPtr, const SharedPtr<Path> &path);
void sendPATH_NEGOTIATION_REQUEST(void* tPtr, const SharedPtr<Path>& path);
/**
*
@ -281,7 +279,7 @@ public:
* @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
* @param now Current time
*/
void processActiveBackupTasks(void *tPtr, int64_t now);
void processActiveBackupTasks(void* tPtr, int64_t now);
/**
* Switches the active link in an active-backup scenario to the next best during
@ -311,64 +309,82 @@ public:
/**
* @param latencyInMilliseconds Maximum acceptable latency.
*/
void setMaxAcceptableLatency(int16_t latencyInMilliseconds) {
void setMaxAcceptableLatency(int16_t latencyInMilliseconds)
{
_maxAcceptableLatency = latencyInMilliseconds;
}
/**
* @param latencyInMilliseconds Maximum acceptable (mean) latency.
*/
void setMaxAcceptableMeanLatency(int16_t latencyInMilliseconds) {
void setMaxAcceptableMeanLatency(int16_t latencyInMilliseconds)
{
_maxAcceptableMeanLatency = latencyInMilliseconds;
}
/**
* @param latencyVarianceInMilliseconds Maximum acceptable packet delay variance (jitter).
*/
void setMaxAcceptablePacketDelayVariance(int16_t latencyVarianceInMilliseconds) {
void setMaxAcceptablePacketDelayVariance(int16_t latencyVarianceInMilliseconds)
{
_maxAcceptablePacketDelayVariance = latencyVarianceInMilliseconds;
}
/**
* @param lossRatio Maximum acceptable packet loss ratio (PLR).
*/
void setMaxAcceptablePacketLossRatio(float lossRatio) {
void setMaxAcceptablePacketLossRatio(float lossRatio)
{
_maxAcceptablePacketLossRatio = lossRatio;
}
/**
* @param errorRatio Maximum acceptable packet error ratio (PER).
*/
void setMaxAcceptablePacketErrorRatio(float errorRatio) {
void setMaxAcceptablePacketErrorRatio(float errorRatio)
{
_maxAcceptablePacketErrorRatio = errorRatio;
}
/**
* @param errorRatio Maximum acceptable packet error ratio (PER).
*/
void setMinAcceptableAllocation(float minAlloc) {
void setMinAcceptableAllocation(float minAlloc)
{
_minAcceptableAllocation = minAlloc * 255;
}
/**
* @return Whether the user has defined links for use on this bond
*/
inline bool userHasSpecifiedLinks() { return _userHasSpecifiedLinks; }
inline bool userHasSpecifiedLinks()
{
return _userHasSpecifiedLinks;
}
/**
* @return Whether the user has defined a set of failover link(s) for this bond
*/
inline bool userHasSpecifiedFailoverInstructions() { return _userHasSpecifiedFailoverInstructions; };
inline bool userHasSpecifiedFailoverInstructions()
{
return _userHasSpecifiedFailoverInstructions;
};
/**
* @return Whether the user has specified a primary link
*/
inline bool userHasSpecifiedPrimaryLink() { return _userHasSpecifiedPrimaryLink; }
inline bool userHasSpecifiedPrimaryLink()
{
return _userHasSpecifiedPrimaryLink;
}
/**
* @return Whether the user has specified link speeds
*/
inline bool userHasSpecifiedLinkSpeeds() { return _userHasSpecifiedLinkSpeeds; }
inline bool userHasSpecifiedLinkSpeeds()
{
return _userHasSpecifiedLinkSpeeds;
}
/**
* Periodically perform maintenance tasks for each active bond.
@ -376,7 +392,7 @@ public:
* @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
* @param now Current time
*/
void processBackgroundTasks(void *tPtr, int64_t now);
void processBackgroundTasks(void* tPtr, int64_t now);
/**
* Rate limit gate for VERB_ACK
@ -387,11 +403,12 @@ public:
inline bool rateGateACK(const int64_t now)
{
_ackCutoffCount++;
int numToDrain = _lastAckRateCheck ? (now - _lastAckRateCheck) / ZT_ACK_DRAINAGE_DIVISOR : _ackCutoffCount;
int numToDrain = _lastAckRateCheck ? (now - _lastAckRateCheck) / ZT_ACK_DRAINAGE_DIVISOR : _ackCutoffCount;
_lastAckRateCheck = now;
if (_ackCutoffCount > numToDrain) {
_ackCutoffCount-=numToDrain;
} else {
_ackCutoffCount -= numToDrain;
}
else {
_ackCutoffCount = 0;
}
return (_ackCutoffCount < ZT_ACK_CUTOFF_LIMIT);
@ -409,8 +426,9 @@ public:
int numToDrain = (now - _lastQoSRateCheck) / ZT_QOS_DRAINAGE_DIVISOR;
_lastQoSRateCheck = now;
if (_qosCutoffCount > numToDrain) {
_qosCutoffCount-=numToDrain;
} else {
_qosCutoffCount -= numToDrain;
}
else {
_qosCutoffCount = 0;
}
return (_qosCutoffCount < ZT_QOS_CUTOFF_LIMIT);
@ -426,7 +444,8 @@ public:
{
if ((now - _lastPathNegotiationReceived) <= ZT_PATH_NEGOTIATION_CUTOFF_TIME)
++_pathNegotiationCutoffCount;
else _pathNegotiationCutoffCount = 0;
else
_pathNegotiationCutoffCount = 0;
_lastPathNegotiationReceived = now;
return (_pathNegotiationCutoffCount < ZT_PATH_NEGOTIATION_CUTOFF_LIMIT);
}
@ -434,130 +453,206 @@ public:
/**
* @param interval Maximum amount of time user expects a failover to take on this bond.
*/
inline void setFailoverInterval(uint32_t interval) { _failoverInterval = interval; }
inline void setFailoverInterval(uint32_t interval)
{
_failoverInterval = interval;
}
/**
* @param interval Maximum amount of time user expects a failover to take on this bond.
*/
inline uint32_t getFailoverInterval() { return _failoverInterval; }
inline uint32_t getFailoverInterval()
{
return _failoverInterval;
}
/**
* @param strategy Strategy that the bond uses to re-assign protocol flows.
*/
inline void setFlowRebalanceStrategy(uint32_t strategy) { _flowRebalanceStrategy = strategy; }
inline void setFlowRebalanceStrategy(uint32_t strategy)
{
_flowRebalanceStrategy = strategy;
}
/**
* @param strategy Strategy that the bond uses to prob for path aliveness and quality
*/
inline void setLinkMonitorStrategy(uint8_t strategy) { _linkMonitorStrategy = strategy; }
inline void setLinkMonitorStrategy(uint8_t strategy)
{
_linkMonitorStrategy = strategy;
}
/**
* @param abOverflowEnabled Whether "overflow" mode is enabled for this active-backup bond
*/
inline void setOverflowMode(bool abOverflowEnabled) { _abOverflowEnabled = abOverflowEnabled; }
inline void setOverflowMode(bool abOverflowEnabled)
{
_abOverflowEnabled = abOverflowEnabled;
}
/**
* @return the current up delay parameter
*/
inline uint16_t getUpDelay() { return _upDelay; }
inline uint16_t getUpDelay()
{
return _upDelay;
}
/**
* @param upDelay Length of time before a newly-discovered path is admitted to the bond
*/
inline void setUpDelay(int upDelay) { if (upDelay >= 0) { _upDelay = upDelay; } }
inline void setUpDelay(int upDelay)
{
if (upDelay >= 0) {
_upDelay = upDelay;
}
}
/**
* @return Length of time before a newly-failed path is removed from the bond
*/
inline uint16_t getDownDelay() { return _downDelay; }
inline uint16_t getDownDelay()
{
return _downDelay;
}
/**
* @param downDelay Length of time before a newly-failed path is removed from the bond
*/
inline void setDownDelay(int downDelay) { if (downDelay >= 0) { _downDelay = downDelay; } }
inline void setDownDelay(int downDelay)
{
if (downDelay >= 0) {
_downDelay = downDelay;
}
}
/**
* @return the current monitoring interval for the bond (can be overridden with intervals specific to certain links.)
*/
inline uint16_t getBondMonitorInterval() { return _bondMonitorInterval; }
inline uint16_t getBondMonitorInterval()
{
return _bondMonitorInterval;
}
/**
* Set the current monitoring interval for the bond (can be overridden with intervals specific to certain links.)
*
* @param monitorInterval How often gratuitous VERB_HELLO(s) are sent to remote peer.
*/
inline void setBondMonitorInterval(uint16_t interval) { _bondMonitorInterval = interval; }
inline void setBondMonitorInterval(uint16_t interval)
{
_bondMonitorInterval = interval;
}
/**
* @param policy Bonding policy for this bond
*/
inline void setPolicy(uint8_t policy) { _bondingPolicy = policy; }
inline void setPolicy(uint8_t policy)
{
_bondingPolicy = policy;
}
/**
* @return the current bonding policy
*/
inline uint8_t getPolicy() { return _bondingPolicy; }
inline uint8_t getPolicy()
{
return _bondingPolicy;
}
/**
* @return the health status of the bond
*/
inline bool isHealthy() { return _isHealthy; }
inline bool isHealthy()
{
return _isHealthy;
}
/**
* @return the number of links comprising this bond which are considered alive
*/
inline uint8_t getNumAliveLinks() { return _numAliveLinks; };
inline uint8_t getNumAliveLinks()
{
return _numAliveLinks;
};
/**
* @return the number of links comprising this bond
*/
inline uint8_t getNumTotalLinks() { return _numTotalLinks; }
inline uint8_t getNumTotalLinks()
{
return _numTotalLinks;
}
/**
*
* @param allowFlowHashing
*/
inline void setFlowHashing(bool allowFlowHashing) { _allowFlowHashing = allowFlowHashing; }
inline void setFlowHashing(bool allowFlowHashing)
{
_allowFlowHashing = allowFlowHashing;
}
/**
* @return Whether flow-hashing is currently enabled for this bond.
*/
bool flowHashingEnabled() { return _allowFlowHashing; }
bool flowHashingEnabled()
{
return _allowFlowHashing;
}
/**
*
* @param packetsPerLink
*/
inline void setPacketsPerLink(int packetsPerLink) { _packetsPerLink = packetsPerLink; }
inline void setPacketsPerLink(int packetsPerLink)
{
_packetsPerLink = packetsPerLink;
}
/**
* @return Number of packets to be sent on each interface in a balance-rr bond
*/
inline int getPacketsPerLink() { return _packetsPerLink; }
inline int getPacketsPerLink()
{
return _packetsPerLink;
}
/**
*
* @param linkSelectMethod
*/
inline void setLinkSelectMethod(uint8_t method) { _abLinkSelectMethod = method; }
inline void setLinkSelectMethod(uint8_t method)
{
_abLinkSelectMethod = method;
}
/**
*
* @return
*/
inline uint8_t getLinkSelectMethod() { return _abLinkSelectMethod; }
inline uint8_t getLinkSelectMethod()
{
return _abLinkSelectMethod;
}
/**
*
* @param allowPathNegotiation
*/
inline void setAllowPathNegotiation(bool allowPathNegotiation) { _allowPathNegotiation = allowPathNegotiation; }
inline void setAllowPathNegotiation(bool allowPathNegotiation)
{
_allowPathNegotiation = allowPathNegotiation;
}
/**
*
* @return
*/
inline bool allowPathNegotiation() { return _allowPathNegotiation; }
inline bool allowPathNegotiation()
{
return _allowPathNegotiation;
}
/**
* Forcibly rotates the currently active link used in an active-backup bond to the next link in the failover queue
@ -566,11 +661,13 @@ public:
*/
bool abForciblyRotateLink();
SharedPtr<Peer> getPeer() { return _peer; }
SharedPtr<Peer> getPeer()
{
return _peer;
}
private:
const RuntimeEnvironment *RR;
private:
const RuntimeEnvironment* RR;
AtomicCounter __refCount;
/**
@ -599,23 +696,23 @@ private:
/**
* Flows hashed according to port and protocol
*/
std::map<int32_t,SharedPtr<Flow> > _flows;
std::map<int32_t, SharedPtr<Flow> > _flows;
float _qualityWeights[ZT_QOS_WEIGHT_SIZE]; // How much each factor contributes to the "quality" score of a path.
float _qualityWeights[ZT_QOS_WEIGHT_SIZE]; // How much each factor contributes to the "quality" score of a path.
uint8_t _bondingPolicy;
uint32_t _upDelay;
uint32_t _downDelay;
// active-backup
SharedPtr<Path> _abPath; // current active path
SharedPtr<Path> _abPath; // current active path
std::list<SharedPtr<Path> > _abFailoverQueue;
uint8_t _abLinkSelectMethod; // link re-selection policy for the primary link in active-backup
uint8_t _abLinkSelectMethod; // link re-selection policy for the primary link in active-backup
bool _abOverflowEnabled;
// balance-rr
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.
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.
/**
* 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
@ -745,6 +842,6 @@ private:
bool _allowFlowHashing;
};
} // namespace ZeroTier
} // namespace ZeroTier
#endif

View File

@ -11,25 +11,25 @@
*/
/****/
#include "../osdep/OSUtils.hpp"
#include "Constants.hpp"
#include "BondController.hpp"
#include "Peer.hpp"
#include "../osdep/OSUtils.hpp"
#include "Bond.hpp"
#include "Node.hpp"
#include "RuntimeEnvironment.hpp"
namespace ZeroTier {
int BondController::_minReqPathMonitorInterval;
uint8_t BondController::_defaultBondingPolicy;
BondController::BondController(const RuntimeEnvironment *renv) :
RR(renv)
BondController::BondController(const RuntimeEnvironment* renv) : RR(renv)
{
bondStartTime = RR->node->now();
_defaultBondingPolicy = ZT_BONDING_POLICY_NONE;
}
bool BondController::linkAllowed(std::string &policyAlias, SharedPtr<Link> link)
bool BondController::linkAllowed(std::string& policyAlias, SharedPtr<Link> link)
{
bool foundInDefinitions = false;
if (_linkDefinitions.count(policyAlias)) {
@ -52,14 +52,14 @@ void BondController::addCustomLink(std::string& policyAlias, SharedPtr<Link> lin
auto search = _interfaceToLinkMap[policyAlias].find(link->ifname());
if (search == _interfaceToLinkMap[policyAlias].end()) {
link->setAsUserSpecified(true);
_interfaceToLinkMap[policyAlias].insert(std::pair<std::string, SharedPtr<Link>>(link->ifname(), link));
_interfaceToLinkMap[policyAlias].insert(std::pair<std::string, SharedPtr<Link> >(link->ifname(), link));
}
}
bool BondController::addCustomPolicy(const SharedPtr<Bond>& newBond)
{
Mutex::Lock _l(_bonds_m);
if (!_bondPolicyTemplates.count(newBond->policyAlias())) {
if (! _bondPolicyTemplates.count(newBond->policyAlias())) {
_bondPolicyTemplates[newBond->policyAlias()] = newBond;
return true;
}
@ -69,7 +69,7 @@ bool BondController::addCustomPolicy(const SharedPtr<Bond>& newBond)
bool BondController::assignBondingPolicyToPeer(int64_t identity, const std::string& policyAlias)
{
Mutex::Lock _l(_bonds_m);
if (!_policyTemplateAssignments.count(identity)) {
if (! _policyTemplateAssignments.count(identity)) {
_policyTemplateAssignments[identity] = policyAlias;
return true;
}
@ -82,37 +82,40 @@ SharedPtr<Bond> BondController::getBondByPeerId(int64_t identity)
return _bonds.count(identity) ? _bonds[identity] : SharedPtr<Bond>();
}
SharedPtr<Bond> BondController::createTransportTriggeredBond(const RuntimeEnvironment *renv, const SharedPtr<Peer>& peer)
SharedPtr<Bond> BondController::createTransportTriggeredBond(const RuntimeEnvironment* renv, const SharedPtr<Peer>& peer)
{
Mutex::Lock _l(_bonds_m);
int64_t identity = peer->identity().address().toInt();
Bond *bond = nullptr;
Bond* bond = nullptr;
char traceMsg[128];
if (!_bonds.count(identity)) {
if (! _bonds.count(identity)) {
std::string policyAlias;
if (!_policyTemplateAssignments.count(identity)) {
if (! _policyTemplateAssignments.count(identity)) {
if (_defaultBondingPolicy) {
sprintf(traceMsg, "%s (bond) Creating new default %s bond to peer %llx",
OSUtils::humanReadableTimestamp().c_str(), getPolicyStrByCode(_defaultBondingPolicy).c_str(), identity); RR->t->bondStateMessage(NULL, traceMsg);
sprintf(traceMsg, "%s (bond) Creating new default %s bond to peer %llx", OSUtils::humanReadableTimestamp().c_str(), getPolicyStrByCode(_defaultBondingPolicy).c_str(), (unsigned long long)identity);
RR->t->bondStateMessage(NULL, traceMsg);
bond = new Bond(renv, _defaultBondingPolicy, peer);
}
if (!_defaultBondingPolicy && _defaultBondingPolicyStr.length()) {
sprintf(traceMsg, "%s (bond) Creating new default custom %s bond to peer %llx",
OSUtils::humanReadableTimestamp().c_str(), _defaultBondingPolicyStr.c_str(), identity);
if (! _defaultBondingPolicy && _defaultBondingPolicyStr.length()) {
sprintf(traceMsg, "%s (bond) Creating new default custom %s bond to peer %llx", OSUtils::humanReadableTimestamp().c_str(), _defaultBondingPolicyStr.c_str(), (unsigned long long)identity);
RR->t->bondStateMessage(NULL, traceMsg);
bond = new Bond(renv, _bondPolicyTemplates[_defaultBondingPolicyStr].ptr(), peer);
}
}
else {
if (!_bondPolicyTemplates[_policyTemplateAssignments[identity]]) {
sprintf(traceMsg, "%s (bond) Creating new bond. Assignment for peer %llx was specified as %s but the bond definition was not found. Using default %s",
OSUtils::humanReadableTimestamp().c_str(), identity, _policyTemplateAssignments[identity].c_str(), getPolicyStrByCode(_defaultBondingPolicy).c_str());
if (! _bondPolicyTemplates[_policyTemplateAssignments[identity]]) {
sprintf(
traceMsg,
"%s (bond) Creating new bond. Assignment for peer %llx was specified as %s but the bond definition was not found. Using default %s",
OSUtils::humanReadableTimestamp().c_str(),
(unsigned long long)identity,
_policyTemplateAssignments[identity].c_str(),
getPolicyStrByCode(_defaultBondingPolicy).c_str());
RR->t->bondStateMessage(NULL, traceMsg);
bond = new Bond(renv, _defaultBondingPolicy, peer);
}
else {
sprintf(traceMsg, "%s (bond) Creating new default bond %s to peer %llx",
OSUtils::humanReadableTimestamp().c_str(), _defaultBondingPolicyStr.c_str(), identity);
sprintf(traceMsg, "%s (bond) Creating new default bond %s to peer %llx", OSUtils::humanReadableTimestamp().c_str(), _defaultBondingPolicyStr.c_str(), (unsigned long long)identity);
RR->t->bondStateMessage(NULL, traceMsg);
bond = new Bond(renv, _bondPolicyTemplates[_policyTemplateAssignments[identity]].ptr(), peer);
}
@ -150,12 +153,12 @@ SharedPtr<Link> BondController::getLinkBySocket(const std::string& policyAlias,
{
Mutex::Lock _l(_links_m);
char ifname[16];
_phy->getIfName((PhySocket *) ((uintptr_t)localSocket), ifname, 16);
_phy->getIfName((PhySocket*)((uintptr_t)localSocket), ifname, 16);
std::string ifnameStr(ifname);
auto search = _interfaceToLinkMap[policyAlias].find(ifnameStr);
if (search == _interfaceToLinkMap[policyAlias].end()) {
SharedPtr<Link> s = new Link(ifnameStr, 0, 0, 0, 0, 0, true, ZT_MULTIPATH_SLAVE_MODE_SPARE, "", 0.0);
_interfaceToLinkMap[policyAlias].insert(std::pair<std::string,SharedPtr<Link> >(ifnameStr, s));
_interfaceToLinkMap[policyAlias].insert(std::pair<std::string, SharedPtr<Link> >(ifnameStr, s));
return s;
}
else {
@ -199,14 +202,14 @@ bool BondController::allowedToBind(const std::string& ifname)
*/
}
void BondController::processBackgroundTasks(void *tPtr, const int64_t now)
void BondController::processBackgroundTasks(void* tPtr, const int64_t now)
{
Mutex::Lock _l(_bonds_m);
std::map<int64_t,SharedPtr<Bond> >::iterator bondItr = _bonds.begin();
std::map<int64_t, SharedPtr<Bond> >::iterator bondItr = _bonds.begin();
while (bondItr != _bonds.end()) {
bondItr->second->processBackgroundTasks(tPtr, now);
++bondItr;
}
}
} // namespace ZeroTier
} // namespace ZeroTier

View File

@ -14,46 +14,54 @@
#ifndef ZT_BONDCONTROLLER_HPP
#define ZT_BONDCONTROLLER_HPP
#include "../osdep/Link.hpp"
#include "../osdep/Phy.hpp"
#include "SharedPtr.hpp"
#include <map>
#include <vector>
#include "SharedPtr.hpp"
#include "../osdep/Phy.hpp"
#include "../osdep/Link.hpp"
namespace ZeroTier {
class RuntimeEnvironment;
class Bond;
class Peer;
class Mutex;
class BondController
{
class BondController {
friend class Bond;
public:
BondController(const RuntimeEnvironment *renv);
public:
BondController(const RuntimeEnvironment* renv);
/**
* @return Whether this link is permitted to become a member of a bond.
*/
bool linkAllowed(std::string &policyAlias, SharedPtr<Link> link);
bool linkAllowed(std::string& policyAlias, SharedPtr<Link> link);
/**
* @return The minimum interval required to poll the active bonds to fulfill all active monitoring timing requirements.
*/
int minReqPathMonitorInterval() { return _minReqPathMonitorInterval; }
int minReqPathMonitorInterval()
{
return _minReqPathMonitorInterval;
}
/**
* @param minReqPathMonitorInterval The minimum interval required to poll the active bonds to fulfill all active monitoring timing requirements.
*/
static void setMinReqPathMonitorInterval(int minReqPathMonitorInterval) { _minReqPathMonitorInterval = minReqPathMonitorInterval; }
static void setMinReqPathMonitorInterval(int minReqPathMonitorInterval)
{
_minReqPathMonitorInterval = minReqPathMonitorInterval;
}
/**
* @return Whether the bonding layer is currently set up to be used.
*/
bool inUse() { return !_bondPolicyTemplates.empty() || _defaultBondingPolicy; }
bool inUse()
{
return ! _bondPolicyTemplates.empty() || _defaultBondingPolicy;
}
/**
* @param basePolicyName Bonding policy name (See ZeroTierOne.h)
@ -61,12 +69,22 @@ public:
*/
static int getPolicyCodeByStr(const std::string& basePolicyName)
{
if (basePolicyName == "active-backup") { return 1; }
if (basePolicyName == "broadcast") { return 2; }
if (basePolicyName == "balance-rr") { return 3; }
if (basePolicyName == "balance-xor") { return 4; }
if (basePolicyName == "balance-aware") { return 5; }
return 0; // "none"
if (basePolicyName == "active-backup") {
return 1;
}
if (basePolicyName == "broadcast") {
return 2;
}
if (basePolicyName == "balance-rr") {
return 3;
}
if (basePolicyName == "balance-xor") {
return 4;
}
if (basePolicyName == "balance-aware") {
return 5;
}
return 0; // "none"
}
/**
@ -75,11 +93,21 @@ public:
*/
static std::string getPolicyStrByCode(int policy)
{
if (policy == 1) { return "active-backup"; }
if (policy == 2) { return "broadcast"; }
if (policy == 3) { return "balance-rr"; }
if (policy == 4) { return "balance-xor"; }
if (policy == 5) { return "balance-aware"; }
if (policy == 1) {
return "active-backup";
}
if (policy == 2) {
return "broadcast";
}
if (policy == 3) {
return "balance-rr";
}
if (policy == 4) {
return "balance-xor";
}
if (policy == 5) {
return "balance-aware";
}
return "none";
}
@ -88,19 +116,28 @@ public:
*
* @param bp Bonding policy
*/
void setBondingLayerDefaultPolicy(uint8_t bp) { _defaultBondingPolicy = bp; }
void setBondingLayerDefaultPolicy(uint8_t bp)
{
_defaultBondingPolicy = bp;
}
/**
* Sets the default (custom) bonding policy for new or undefined bonds.
*
* @param alias Human-readable string alias for bonding policy
*/
void setBondingLayerDefaultPolicyStr(std::string alias) { _defaultBondingPolicyStr = alias; }
void setBondingLayerDefaultPolicyStr(std::string alias)
{
_defaultBondingPolicyStr = alias;
}
/**
* @return The default bonding policy
*/
static int defaultBondingPolicy() { return _defaultBondingPolicy; }
static int defaultBondingPolicy()
{
return _defaultBondingPolicy;
}
/**
* Add a user-defined link to a given bonding policy.
@ -142,7 +179,7 @@ public:
* @param peer Remote peer that this bond services
* @return A pointer to the newly created Bond
*/
SharedPtr<Bond> createTransportTriggeredBond(const RuntimeEnvironment *renv, const SharedPtr<Peer>& peer);
SharedPtr<Bond> createTransportTriggeredBond(const RuntimeEnvironment* renv, const SharedPtr<Peer>& peer);
/**
* Periodically perform maintenance tasks for the bonding layer.
@ -150,7 +187,7 @@ public:
* @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
* @param now Current time
*/
void processBackgroundTasks(void *tPtr, int64_t now);
void processBackgroundTasks(void* tPtr, int64_t now);
/**
* Gets a reference to a physical link definition given a policy alias and a local socket.
@ -175,12 +212,14 @@ public:
*/
bool allowedToBind(const std::string& ifname);
uint64_t getBondStartTime() { return bondStartTime; }
uint64_t getBondStartTime()
{
return bondStartTime;
}
private:
Phy<BondController *> *_phy;
const RuntimeEnvironment *RR;
private:
Phy<BondController*>* _phy;
const RuntimeEnvironment* RR;
Mutex _bonds_m;
Mutex _links_m;
@ -208,22 +247,22 @@ private:
/**
* All currently active bonds.
*/
std::map<int64_t,SharedPtr<Bond> > _bonds;
std::map<int64_t, SharedPtr<Bond> > _bonds;
/**
* Map of peers to custom bonding policies
*/
std::map<int64_t,std::string> _policyTemplateAssignments;
std::map<int64_t, std::string> _policyTemplateAssignments;
/**
* User-defined bonding policies (can be assigned to a peer)
*/
std::map<std::string,SharedPtr<Bond> > _bondPolicyTemplates;
std::map<std::string, SharedPtr<Bond> > _bondPolicyTemplates;
/**
* Set of links defined for a given bonding policy
*/
std::map<std::string,std::vector<SharedPtr<Link> > > _linkDefinitions;
std::map<std::string, std::vector<SharedPtr<Link> > > _linkDefinitions;
/**
* Set of link objects mapped to their physical interfaces
@ -234,6 +273,6 @@ private:
uint64_t bondStartTime;
};
} // namespace ZeroTier
} // namespace ZeroTier
#endif

View File

@ -22,20 +22,14 @@ namespace ZeroTier {
/**
* A protocol flow that is identified by the origin and destination port.
*/
struct Flow
{
struct Flow {
/**
* @param flowId Given flow ID
* @param now Current time
*/
Flow(int32_t flowId, int64_t now) :
_flowId(flowId),
_bytesInPerUnitTime(0),
_bytesOutPerUnitTime(0),
_lastActivity(now),
_lastPathReassignment(0),
_assignedPath(SharedPtr<Path>())
{}
Flow(int32_t flowId, int64_t now) : _flowId(flowId), _bytesInPerUnitTime(0), _bytesOutPerUnitTime(0), _lastActivity(now), _lastPathReassignment(0), _assignedPath(SharedPtr<Path>())
{
}
/**
* Reset flow statistics
@ -49,36 +43,54 @@ struct Flow
/**
* @return The Flow's ID
*/
int32_t id() { return _flowId; }
int32_t id()
{
return _flowId;
}
/**
* @return Number of incoming bytes processed on this flow per unit time
*/
int64_t bytesInPerUnitTime() { return _bytesInPerUnitTime; }
int64_t bytesInPerUnitTime()
{
return _bytesInPerUnitTime;
}
/**
* Record number of incoming bytes on this flow
*
* @param bytes Number of incoming bytes
*/
void recordIncomingBytes(uint64_t bytes) { _bytesInPerUnitTime += bytes; }
void recordIncomingBytes(uint64_t bytes)
{
_bytesInPerUnitTime += bytes;
}
/**
* @return Number of outgoing bytes processed on this flow per unit time
*/
int64_t bytesOutPerUnitTime() { return _bytesOutPerUnitTime; }
int64_t bytesOutPerUnitTime()
{
return _bytesOutPerUnitTime;
}
/**
* Record number of outgoing bytes on this flow
*
* @param bytes
*/
void recordOutgoingBytes(uint64_t bytes) { _bytesOutPerUnitTime += bytes; }
void recordOutgoingBytes(uint64_t bytes)
{
_bytesOutPerUnitTime += bytes;
}
/**
* @return The total number of bytes processed on this flow
*/
uint64_t totalBytes() { return _bytesInPerUnitTime + _bytesOutPerUnitTime; }
uint64_t totalBytes()
{
return _bytesInPerUnitTime + _bytesOutPerUnitTime;
}
/**
* How long since a packet was sent or received in this flow
@ -86,24 +98,34 @@ struct Flow
* @param now Current time
* @return The age of the flow in terms of last recorded activity
*/
int64_t age(int64_t now) { return now - _lastActivity; }
int64_t age(int64_t now)
{
return now - _lastActivity;
}
/**
* Record that traffic was processed on this flow at the given time.
*
* @param now Current time
*/
void updateActivity(int64_t now) { _lastActivity = now; }
void updateActivity(int64_t now)
{
_lastActivity = now;
}
/**
* @return Path assigned to this flow
*/
SharedPtr<Path> assignedPath() { return _assignedPath; }
SharedPtr<Path> assignedPath()
{
return _assignedPath;
}
/**
* @param path Assigned path over which this flow should be handled
*/
void assignPath(const SharedPtr<Path> &path, int64_t now) {
void assignPath(const SharedPtr<Path>& path, int64_t now)
{
_assignedPath = path;
_lastPathReassignment = now;
}
@ -119,6 +141,6 @@ struct Flow
SharedPtr<Path> _previouslyAssignedPath;
};
} // namespace ZeroTier
} // namespace ZeroTier
#endif

View File

@ -14,19 +14,20 @@
#ifndef ZT_LINK_HPP
#define ZT_LINK_HPP
#include <string>
#include "../node/AtomicCounter.hpp"
#include "../node/SharedPtr.hpp"
#include <string>
namespace ZeroTier {
class Link
{
class Link {
friend class SharedPtr<Link>;
public:
Link() {}
public:
Link()
{
}
/**
*
@ -38,123 +39,165 @@ public:
* @param failoverToLinkStr
* @param userSpecifiedAlloc
*/
Link(std::string& ifnameStr,
uint8_t ipvPref,
uint32_t speed,
uint32_t linkMonitorInterval,
uint32_t upDelay,
uint32_t downDelay,
bool enabled,
uint8_t mode,
std::string failoverToLinkStr,
float userSpecifiedAlloc) :
_ifnameStr(ifnameStr),
_ipvPref(ipvPref),
_speed(speed),
_relativeSpeed(0),
_linkMonitorInterval(linkMonitorInterval),
_upDelay(upDelay),
_downDelay(downDelay),
_enabled(enabled),
_mode(mode),
_failoverToLinkStr(failoverToLinkStr),
_userSpecifiedAlloc(userSpecifiedAlloc),
_isUserSpecified(false)
{}
Link(std::string& ifnameStr, uint8_t ipvPref, uint32_t speed, uint32_t linkMonitorInterval, uint32_t upDelay, uint32_t downDelay, bool enabled, uint8_t mode, std::string failoverToLinkStr, float userSpecifiedAlloc)
: _ifnameStr(ifnameStr)
, _ipvPref(ipvPref)
, _speed(speed)
, _relativeSpeed(0)
, _linkMonitorInterval(linkMonitorInterval)
, _upDelay(upDelay)
, _downDelay(downDelay)
, _enabled(enabled)
, _mode(mode)
, _failoverToLinkStr(failoverToLinkStr)
, _userSpecifiedAlloc(userSpecifiedAlloc)
, _isUserSpecified(false)
{
}
/**
* @return The string representation of this link's underlying interface's system name.
*/
inline std::string ifname() { return _ifnameStr; }
inline std::string ifname()
{
return _ifnameStr;
}
/**
* @return Whether this link is designated as a primary.
*/
inline bool primary() { return _mode == ZT_MULTIPATH_SLAVE_MODE_PRIMARY; }
inline bool primary()
{
return _mode == ZT_MULTIPATH_SLAVE_MODE_PRIMARY;
}
/**
* @return Whether this link is designated as a spare.
*/
inline bool spare() { return _mode == ZT_MULTIPATH_SLAVE_MODE_SPARE; }
inline bool spare()
{
return _mode == ZT_MULTIPATH_SLAVE_MODE_SPARE;
}
/**
* @return The name of the link interface that should be used in the event of a failure.
*/
inline std::string failoverToLink() { return _failoverToLinkStr; }
inline std::string failoverToLink()
{
return _failoverToLinkStr;
}
/**
* @return Whether this link interface was specified by the user or auto-detected.
*/
inline bool isUserSpecified() { return _isUserSpecified; }
inline bool isUserSpecified()
{
return _isUserSpecified;
}
/**
* Signify that this link was specified by the user and not the result of auto-detection.
*
* @param isUserSpecified
*/
inline void setAsUserSpecified(bool isUserSpecified) { _isUserSpecified = isUserSpecified; }
inline void setAsUserSpecified(bool isUserSpecified)
{
_isUserSpecified = isUserSpecified;
}
/**
* @return Whether or not the user has specified failover instructions.
*/
inline bool userHasSpecifiedFailoverInstructions() { return _failoverToLinkStr.length(); }
inline bool userHasSpecifiedFailoverInstructions()
{
return _failoverToLinkStr.length();
}
/**
* @return The speed of the link relative to others in the bond.
*/
inline uint8_t relativeSpeed() { return _relativeSpeed; }
inline uint8_t relativeSpeed()
{
return _relativeSpeed;
}
/**
* Sets the speed of the link relative to others in the bond.
*
* @param relativeSpeed The speed relative to the rest of the link.
*/
inline void setRelativeSpeed(uint8_t relativeSpeed) { _relativeSpeed = relativeSpeed; }
inline void setRelativeSpeed(uint8_t relativeSpeed)
{
_relativeSpeed = relativeSpeed;
}
/**
* Sets the speed of the link relative to others in the bond.
*
* @param relativeSpeed
*/
inline void setMonitorInterval(uint32_t interval) { _linkMonitorInterval = interval; }
inline void setMonitorInterval(uint32_t interval)
{
_linkMonitorInterval = interval;
}
/**
* @return The absolute speed of the link (as specified by the user.)
*/
inline uint32_t monitorInterval() { return _linkMonitorInterval; }
inline uint32_t monitorInterval()
{
return _linkMonitorInterval;
}
/**
* @return The absolute speed of the link (as specified by the user.)
*/
inline uint32_t speed() { return _speed; }
inline uint32_t speed()
{
return _speed;
}
/**
* @return The address preference for this link (as specified by the user.)
*/
inline uint8_t ipvPref() { return _ipvPref; }
inline uint8_t ipvPref()
{
return _ipvPref;
}
/**
* @return The mode (e.g. primary/spare) for this link (as specified by the user.)
*/
inline uint8_t mode() { return _mode; }
inline uint8_t mode()
{
return _mode;
}
/**
* @return The upDelay parameter for all paths on this link.
*/
inline uint32_t upDelay() { return _upDelay; }
inline uint32_t upDelay()
{
return _upDelay;
}
/**
* @return The downDelay parameter for all paths on this link.
*/
inline uint32_t downDelay() { return _downDelay; }
inline uint32_t downDelay()
{
return _downDelay;
}
/**
* @return Whether this link is enabled or disabled
*/
inline uint8_t enabled() { return _enabled; }
private:
inline uint8_t enabled()
{
return _enabled;
}
private:
/**
* String representation of underlying interface's system name
*/
@ -223,15 +266,15 @@ private:
float _userSpecifiedAlloc;
/**
* Whether or not this link was created as a result of manual user specification. This is
* important to know because certain policy decisions are dependent on whether the user
* intents to use a specific set of interfaces.
*/
* Whether or not this link was created as a result of manual user specification. This is
* important to know because certain policy decisions are dependent on whether the user
* intents to use a specific set of interfaces.
*/
bool _isUserSpecified;
AtomicCounter __refCount;
};
} // namespace ZeroTier
} // namespace ZeroTier
#endif
#endif