Merge branch 'dev' of github.com:zerotier/ZeroTierOne into dev

This commit is contained in:
Adam Ierymenko 2021-05-24 22:58:36 -04:00
commit 8bb5bc736d
No known key found for this signature in database
GPG key ID: C8877CF2D7A5D7F3
15 changed files with 1131 additions and 701 deletions

75
.clang-format Normal file
View file

@ -0,0 +1,75 @@
---
BasedOnStyle: LLVM
BreakBeforeBraces: Stroustrup
IndentWidth: 4
TabWidth: 4
AlignAfterOpenBracket: AlwaysBreak
AlignConsecutiveMacros: 'true'
AlignConsecutiveAssignments: 'false'
AlignConsecutiveDeclarations: 'false'
AlignEscapedNewlines: Right
AlignOperands: 'true'
AlignTrailingComments: 'true'
AllowAllArgumentsOnNextLine: 'false'
AllowAllConstructorInitializersOnNextLine: 'false'
AllowAllParametersOfDeclarationOnNextLine: 'false'
AllowShortBlocksOnASingleLine: 'true'
AllowShortCaseLabelsOnASingleLine: 'false'
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: Never
AlwaysBreakAfterReturnType: None
BinPackArguments: 'false'
BinPackParameters: 'false'
BreakBeforeBinaryOperators: NonAssignment
BreakBeforeTernaryOperators: 'true'
BreakConstructorInitializers: BeforeComma
BreakInheritanceList: BeforeComma
CompactNamespaces: 'false'
ConstructorInitializerAllOnOneLineOrOnePerLine: 'true'
ConstructorInitializerIndentWidth: '4'
ContinuationIndentWidth: '4'
Cpp11BracedListStyle: 'false'
FixNamespaceComments: 'true'
IncludeBlocks: Regroup
IndentCaseLabels: 'true'
IndentPPDirectives: None
IndentWrappedFunctionNames: 'false'
KeepEmptyLinesAtTheStartOfBlocks: 'false'
MaxEmptyLinesToKeep: '1'
NamespaceIndentation: None
PointerAlignment: Left
ReflowComments: 'true'
SortIncludes: 'true'
SortUsingDeclarations: 'true'
SpaceAfterCStyleCast: 'false'
SpaceAfterLogicalNot: 'true'
SpaceAfterTemplateKeyword: 'true'
SpaceBeforeAssignmentOperators: 'true'
SpaceBeforeCpp11BracedList: 'true'
SpaceBeforeCtorInitializerColon: 'true'
SpaceBeforeInheritanceColon: 'true'
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: 'true'
SpaceInEmptyParentheses: 'false'
SpacesBeforeTrailingComments: '3'
SpacesInAngles: 'false'
SpacesInCStyleCastParentheses: 'false'
SpacesInContainerLiterals: 'true'
SpacesInParentheses: 'false'
SpacesInSquareBrackets: 'false'
UseTab: 'Always'
---
Language: Cpp
Standard: Cpp03
ColumnLimit: '240'
---
Language: ObjC
ColumnLimit: '240'
---
Language: Java
ColumnLimit: '240'
---
Language: CSharp
ColumnLimit: '240'
...

View file

@ -112,7 +112,7 @@ one: $(CORE_OBJS) $(ONE_OBJS) one.o mac-agent
zerotier-one: one zerotier-one: one
central-controller: central-controller:
make ZT_CONTROLLER=1 one make ARCH_FLAGS="-arch x86_64" ZT_CONTROLLER=1 one
zerotier-idtool: one zerotier-idtool: one

File diff suppressed because it is too large Load diff

View file

@ -14,37 +14,36 @@
#ifndef ZT_BOND_HPP #ifndef ZT_BOND_HPP
#define ZT_BOND_HPP #define ZT_BOND_HPP
#include <map> #include "Flow.hpp"
#include "Packet.hpp"
#include "Path.hpp" #include "Path.hpp"
#include "Peer.hpp" #include "Peer.hpp"
#include "../osdep/Link.hpp"
#include "Flow.hpp" #include <list>
#include <map>
namespace ZeroTier { namespace ZeroTier {
class RuntimeEnvironment; class RuntimeEnvironment;
class Link; class Link;
class Peer;
class Bond class Bond {
{
friend class SharedPtr<Bond>; friend class SharedPtr<Bond>;
friend class Peer; friend class Peer;
friend class BondController; friend class BondController;
struct PathQualityComparator struct PathQualityComparator {
{ bool operator()(const SharedPtr<Path>& a, const SharedPtr<Path>& b)
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 < b;
} }
return a->_failoverScore > b->_failoverScore; return a->_failoverScore > b->_failoverScore;
} }
}; };
public: public:
// TODO: Remove // TODO: Remove
bool _header; bool _header;
int64_t _lastLogTS; int64_t _lastLogTS;
@ -61,7 +60,7 @@ public:
* @param policy Bonding policy * @param policy Bonding policy
* @param peer * @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 * Constructor. For use when user intends to manually specify parameters
@ -70,7 +69,7 @@ public:
* @param policyAlias * @param policyAlias
* @param peer * @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 * Constructor. Creates a bond based off of a user-defined bond template
@ -79,12 +78,15 @@ public:
* @param original * @param original
* @param peer * @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 * @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. * Inform the bond about the path that its peer (owning object) just learned about.
@ -135,8 +137,7 @@ public:
* @param flowId Flow ID * @param flowId Flow ID
* @param now Current time * @param now Current time
*/ */
void recordOutgoingPacket(const SharedPtr<Path> &path, uint64_t packetId, void recordOutgoingPacket(const SharedPtr<Path>& path, uint64_t packetId, uint16_t payloadLength, Packet::Verb verb, int32_t flowId, int64_t now);
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. * 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_id table of packet IDs
* @param rx_ts table of holding times * @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. * Process the contents of an inbound VERB_ACK to gather path quality observations.
@ -164,7 +165,7 @@ public:
* @param qosBuffer destination buffer * @param qosBuffer destination buffer
* @return Size of payload * @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. * Record statistics for an inbound packet.
@ -176,8 +177,7 @@ public:
* @param flowId Flow ID * @param flowId Flow ID
* @param now Current time * @param now Current time
*/ */
void recordIncomingPacket(const SharedPtr<Path>& path, uint64_t packetId, uint16_t payloadLength, void recordIncomingPacket(const SharedPtr<Path>& path, uint64_t packetId, uint16_t payloadLength, Packet::Verb verb, int32_t flowId, int64_t now);
Packet::Verb verb, int32_t flowId, int64_t now);
/** /**
* Determines the most appropriate path for packet and flow egress. This decision is made by * Determines the most appropriate path for packet and flow egress. This decision is made by
@ -198,7 +198,7 @@ public:
* @param now Current time * @param now Current time
* @return Pointer to newly-created Flow * @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. * Removes flow records that are past a certain age limit.
@ -215,7 +215,7 @@ public:
* @param flow Flow to be assigned * @param flow Flow to be assigned
* @param now Current time * @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 * 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 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 * @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 * 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 tPtr Thread pointer to be handed through to any callbacks called as a result of this call
* @param now Current time * @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. * Sends a VERB_ACK to the remote peer.
@ -246,8 +246,7 @@ public:
* @param atAddress * @param atAddress
* @param now Current time * @param now Current time
*/ */
void sendACK(void *tPtr, const SharedPtr<Path> &path,int64_t localSocket, void sendACK(void* tPtr, const SharedPtr<Path>& path, int64_t localSocket, const InetAddress& atAddress, int64_t now);
const InetAddress &atAddress,int64_t now);
/** /**
* Sends a VERB_QOS_MEASUREMENT to the remote peer. * Sends a VERB_QOS_MEASUREMENT to the remote peer.
@ -258,8 +257,7 @@ public:
* @param atAddress * @param atAddress
* @param now Current time * @param now Current time
*/ */
void sendQOS_MEASUREMENT(void *tPtr,const SharedPtr<Path> &path,int64_t localSocket, void sendQOS_MEASUREMENT(void* tPtr, const SharedPtr<Path>& path, int64_t localSocket, const InetAddress& atAddress, int64_t now);
const InetAddress &atAddress,int64_t now);
/** /**
* Sends a VERB_PATH_NEGOTIATION_REQUEST to the remote peer. * 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 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 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 tPtr Thread pointer to be handed through to any callbacks called as a result of this call
* @param now Current time * @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 * Switches the active link in an active-backup scenario to the next best during
@ -311,64 +309,82 @@ public:
/** /**
* @param latencyInMilliseconds Maximum acceptable latency. * @param latencyInMilliseconds Maximum acceptable latency.
*/ */
void setMaxAcceptableLatency(int16_t latencyInMilliseconds) { void setMaxAcceptableLatency(int16_t latencyInMilliseconds)
{
_maxAcceptableLatency = latencyInMilliseconds; _maxAcceptableLatency = latencyInMilliseconds;
} }
/** /**
* @param latencyInMilliseconds Maximum acceptable (mean) latency. * @param latencyInMilliseconds Maximum acceptable (mean) latency.
*/ */
void setMaxAcceptableMeanLatency(int16_t latencyInMilliseconds) { void setMaxAcceptableMeanLatency(int16_t latencyInMilliseconds)
{
_maxAcceptableMeanLatency = latencyInMilliseconds; _maxAcceptableMeanLatency = latencyInMilliseconds;
} }
/** /**
* @param latencyVarianceInMilliseconds Maximum acceptable packet delay variance (jitter). * @param latencyVarianceInMilliseconds Maximum acceptable packet delay variance (jitter).
*/ */
void setMaxAcceptablePacketDelayVariance(int16_t latencyVarianceInMilliseconds) { void setMaxAcceptablePacketDelayVariance(int16_t latencyVarianceInMilliseconds)
{
_maxAcceptablePacketDelayVariance = latencyVarianceInMilliseconds; _maxAcceptablePacketDelayVariance = latencyVarianceInMilliseconds;
} }
/** /**
* @param lossRatio Maximum acceptable packet loss ratio (PLR). * @param lossRatio Maximum acceptable packet loss ratio (PLR).
*/ */
void setMaxAcceptablePacketLossRatio(float lossRatio) { void setMaxAcceptablePacketLossRatio(float lossRatio)
{
_maxAcceptablePacketLossRatio = lossRatio; _maxAcceptablePacketLossRatio = lossRatio;
} }
/** /**
* @param errorRatio Maximum acceptable packet error ratio (PER). * @param errorRatio Maximum acceptable packet error ratio (PER).
*/ */
void setMaxAcceptablePacketErrorRatio(float errorRatio) { void setMaxAcceptablePacketErrorRatio(float errorRatio)
{
_maxAcceptablePacketErrorRatio = errorRatio; _maxAcceptablePacketErrorRatio = errorRatio;
} }
/** /**
* @param errorRatio Maximum acceptable packet error ratio (PER). * @param errorRatio Maximum acceptable packet error ratio (PER).
*/ */
void setMinAcceptableAllocation(float minAlloc) { void setMinAcceptableAllocation(float minAlloc)
_minAcceptableAllocation = minAlloc * 255; {
_minAcceptableAllocation = (uint8_t)(minAlloc * 255);
} }
/** /**
* @return Whether the user has defined links for use on this bond * @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 * @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 * @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 * @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. * 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 tPtr Thread pointer to be handed through to any callbacks called as a result of this call
* @param now Current time * @param now Current time
*/ */
void processBackgroundTasks(void *tPtr, int64_t now); void processBackgroundTasks(void* tPtr, int64_t now);
/** /**
* Rate limit gate for VERB_ACK * Rate limit gate for VERB_ACK
@ -387,11 +403,12 @@ public:
inline bool rateGateACK(const int64_t now) inline bool rateGateACK(const int64_t now)
{ {
_ackCutoffCount++; _ackCutoffCount++;
int numToDrain = _lastAckRateCheck ? (now - _lastAckRateCheck) / ZT_ACK_DRAINAGE_DIVISOR : _ackCutoffCount; int numToDrain = _lastAckRateCheck ? (now - _lastAckRateCheck) / ZT_ACK_DRAINAGE_DIVISOR : _ackCutoffCount;
_lastAckRateCheck = now; _lastAckRateCheck = now;
if (_ackCutoffCount > numToDrain) { if (_ackCutoffCount > numToDrain) {
_ackCutoffCount-=numToDrain; _ackCutoffCount -= numToDrain;
} else { }
else {
_ackCutoffCount = 0; _ackCutoffCount = 0;
} }
return (_ackCutoffCount < ZT_ACK_CUTOFF_LIMIT); return (_ackCutoffCount < ZT_ACK_CUTOFF_LIMIT);
@ -409,8 +426,9 @@ public:
int numToDrain = (now - _lastQoSRateCheck) / ZT_QOS_DRAINAGE_DIVISOR; int numToDrain = (now - _lastQoSRateCheck) / ZT_QOS_DRAINAGE_DIVISOR;
_lastQoSRateCheck = now; _lastQoSRateCheck = now;
if (_qosCutoffCount > numToDrain) { if (_qosCutoffCount > numToDrain) {
_qosCutoffCount-=numToDrain; _qosCutoffCount -= numToDrain;
} else { }
else {
_qosCutoffCount = 0; _qosCutoffCount = 0;
} }
return (_qosCutoffCount < ZT_QOS_CUTOFF_LIMIT); return (_qosCutoffCount < ZT_QOS_CUTOFF_LIMIT);
@ -426,7 +444,8 @@ public:
{ {
if ((now - _lastPathNegotiationReceived) <= ZT_PATH_NEGOTIATION_CUTOFF_TIME) if ((now - _lastPathNegotiationReceived) <= ZT_PATH_NEGOTIATION_CUTOFF_TIME)
++_pathNegotiationCutoffCount; ++_pathNegotiationCutoffCount;
else _pathNegotiationCutoffCount = 0; else
_pathNegotiationCutoffCount = 0;
_lastPathNegotiationReceived = now; _lastPathNegotiationReceived = now;
return (_pathNegotiationCutoffCount < ZT_PATH_NEGOTIATION_CUTOFF_LIMIT); 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. * @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. * @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. * @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 * @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 * @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 * @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 * @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 * @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 * @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.) * @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.) * 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. * @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 * @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 * @return the current bonding policy
*/ */
inline uint8_t getPolicy() { return _bondingPolicy; } inline uint8_t getPolicy()
{
return _bondingPolicy;
}
/** /**
* @return the health status of the bond * @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 * @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 * @return the number of links comprising this bond
*/ */
inline uint8_t getNumTotalLinks() { return _numTotalLinks; } inline uint8_t getNumTotalLinks()
{
return _numTotalLinks;
}
/** /**
* *
* @param allowFlowHashing * @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. * @return Whether flow-hashing is currently enabled for this bond.
*/ */
bool flowHashingEnabled() { return _allowFlowHashing; } bool flowHashingEnabled()
{
return _allowFlowHashing;
}
/** /**
* *
* @param packetsPerLink * @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 * @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 * @param linkSelectMethod
*/ */
inline void setLinkSelectMethod(uint8_t method) { _abLinkSelectMethod = method; } inline void setLinkSelectMethod(uint8_t method)
{
_abLinkSelectMethod = method;
}
/** /**
* *
* @return * @return
*/ */
inline uint8_t getLinkSelectMethod() { return _abLinkSelectMethod; } inline uint8_t getLinkSelectMethod()
{
return _abLinkSelectMethod;
}
/** /**
* *
* @param allowPathNegotiation * @param allowPathNegotiation
*/ */
inline void setAllowPathNegotiation(bool allowPathNegotiation) { _allowPathNegotiation = allowPathNegotiation; } inline void setAllowPathNegotiation(bool allowPathNegotiation)
{
_allowPathNegotiation = allowPathNegotiation;
}
/** /**
* *
* @return * @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 * 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(); bool abForciblyRotateLink();
SharedPtr<Peer> getPeer() { return _peer; } SharedPtr<Peer> getPeer()
{
return _peer;
}
private: private:
const RuntimeEnvironment* RR;
const RuntimeEnvironment *RR;
AtomicCounter __refCount; AtomicCounter __refCount;
/** /**
@ -599,23 +696,23 @@ private:
/** /**
* Flows hashed according to port and protocol * 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; uint8_t _bondingPolicy;
uint32_t _upDelay; uint32_t _upDelay;
uint32_t _downDelay; uint32_t _downDelay;
// active-backup // active-backup
SharedPtr<Path> _abPath; // current active path SharedPtr<Path> _abPath; // current active path
std::list<SharedPtr<Path> > _abFailoverQueue; 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; bool _abOverflowEnabled;
// balance-rr // balance-rr
uint8_t _rrIdx; // index to path currently in use during Round Robin operation 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. 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 * 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 * in the round-robin sequence. A value of zero will cause a random path
@ -745,6 +842,6 @@ private:
bool _allowFlowHashing; bool _allowFlowHashing;
}; };
} // namespace ZeroTier } // namespace ZeroTier
#endif #endif

View file

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

View file

@ -14,46 +14,54 @@
#ifndef ZT_BONDCONTROLLER_HPP #ifndef ZT_BONDCONTROLLER_HPP
#define ZT_BONDCONTROLLER_HPP #define ZT_BONDCONTROLLER_HPP
#include "../osdep/Link.hpp"
#include "../osdep/Phy.hpp"
#include "SharedPtr.hpp"
#include <map> #include <map>
#include <vector> #include <vector>
#include "SharedPtr.hpp"
#include "../osdep/Phy.hpp"
#include "../osdep/Link.hpp"
namespace ZeroTier { namespace ZeroTier {
class RuntimeEnvironment; class RuntimeEnvironment;
class Bond; class Bond;
class Peer; class Peer;
class Mutex;
class BondController class BondController {
{
friend class Bond; friend class Bond;
public: public:
BondController(const RuntimeEnvironment* renv);
BondController(const RuntimeEnvironment *renv);
/** /**
* @return Whether this link is permitted to become a member of a bond. * @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. * @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. * @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. * @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) * @param basePolicyName Bonding policy name (See ZeroTierOne.h)
@ -61,12 +69,22 @@ public:
*/ */
static int getPolicyCodeByStr(const std::string& basePolicyName) static int getPolicyCodeByStr(const std::string& basePolicyName)
{ {
if (basePolicyName == "active-backup") { return 1; } if (basePolicyName == "active-backup") {
if (basePolicyName == "broadcast") { return 2; } return 1;
if (basePolicyName == "balance-rr") { return 3; } }
if (basePolicyName == "balance-xor") { return 4; } if (basePolicyName == "broadcast") {
if (basePolicyName == "balance-aware") { return 5; } return 2;
return 0; // "none" }
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) static std::string getPolicyStrByCode(int policy)
{ {
if (policy == 1) { return "active-backup"; } if (policy == 1) {
if (policy == 2) { return "broadcast"; } return "active-backup";
if (policy == 3) { return "balance-rr"; } }
if (policy == 4) { return "balance-xor"; } if (policy == 2) {
if (policy == 5) { return "balance-aware"; } return "broadcast";
}
if (policy == 3) {
return "balance-rr";
}
if (policy == 4) {
return "balance-xor";
}
if (policy == 5) {
return "balance-aware";
}
return "none"; return "none";
} }
@ -88,19 +116,28 @@ public:
* *
* @param bp Bonding policy * @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. * Sets the default (custom) bonding policy for new or undefined bonds.
* *
* @param alias Human-readable string alias for bonding policy * @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 * @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. * Add a user-defined link to a given bonding policy.
@ -142,7 +179,7 @@ public:
* @param peer Remote peer that this bond services * @param peer Remote peer that this bond services
* @return A pointer to the newly created Bond * @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. * 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 tPtr Thread pointer to be handed through to any callbacks called as a result of this call
* @param now Current time * @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. * 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); bool allowedToBind(const std::string& ifname);
uint64_t getBondStartTime() { return bondStartTime; } uint64_t getBondStartTime()
{
return bondStartTime;
}
private: private:
Phy<BondController*>* _phy;
Phy<BondController *> *_phy; const RuntimeEnvironment* RR;
const RuntimeEnvironment *RR;
Mutex _bonds_m; Mutex _bonds_m;
Mutex _links_m; Mutex _links_m;
@ -208,22 +247,22 @@ private:
/** /**
* All currently active bonds. * 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 * 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) * 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 * 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 * Set of link objects mapped to their physical interfaces
@ -234,6 +273,6 @@ private:
uint64_t bondStartTime; uint64_t bondStartTime;
}; };
} // namespace ZeroTier } // namespace ZeroTier
#endif #endif

View file

@ -22,20 +22,14 @@ namespace ZeroTier {
/** /**
* A protocol flow that is identified by the origin and destination port. * A protocol flow that is identified by the origin and destination port.
*/ */
struct Flow struct Flow {
{
/** /**
* @param flowId Given flow ID * @param flowId Given flow ID
* @param now Current time * @param now Current time
*/ */
Flow(int32_t flowId, int64_t now) : Flow(int32_t flowId, int64_t now) : _flowId(flowId), _bytesInPerUnitTime(0), _bytesOutPerUnitTime(0), _lastActivity(now), _lastPathReassignment(0), _assignedPath(SharedPtr<Path>())
_flowId(flowId), {
_bytesInPerUnitTime(0), }
_bytesOutPerUnitTime(0),
_lastActivity(now),
_lastPathReassignment(0),
_assignedPath(SharedPtr<Path>())
{}
/** /**
* Reset flow statistics * Reset flow statistics
@ -49,36 +43,54 @@ struct Flow
/** /**
* @return The Flow's ID * @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 * @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 * Record number of incoming bytes on this flow
* *
* @param bytes Number of incoming bytes * @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 * @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 * Record number of outgoing bytes on this flow
* *
* @param bytes * @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 * @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 * How long since a packet was sent or received in this flow
@ -86,24 +98,34 @@ struct Flow
* @param now Current time * @param now Current time
* @return The age of the flow in terms of last recorded activity * @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. * Record that traffic was processed on this flow at the given time.
* *
* @param now Current 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 * @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 * @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; _assignedPath = path;
_lastPathReassignment = now; _lastPathReassignment = now;
} }
@ -119,6 +141,6 @@ struct Flow
SharedPtr<Path> _previouslyAssignedPath; SharedPtr<Path> _previouslyAssignedPath;
}; };
} // namespace ZeroTier } // namespace ZeroTier
#endif #endif

View file

@ -1047,7 +1047,6 @@ void Switch::_sendViaSpecificPath(void *tPtr,SharedPtr<Peer> peer,SharedPtr<Path
if (trustedPathId) { if (trustedPathId) {
packet.setTrusted(trustedPathId); packet.setTrusted(trustedPathId);
} else { } else {
Packet::Verb v = packet.verb();
packet.armor(peer->key(),encrypt,peer->aesKeysIfSupported()); packet.armor(peer->key(),encrypt,peer->aesKeysIfSupported());
RR->node->expectReplyTo(packet.packetId()); RR->node->expectReplyTo(packet.packetId());
} }

View file

@ -69,7 +69,7 @@ void Trace::peerConfirmingUnknownPath(void *const tPtr,const uint64_t networkId,
char tmp[128]; char tmp[128];
if (!path) return; // sanity check if (!path) return; // sanity check
ZT_LOCAL_TRACE(tPtr,RR,"trying unknown path %s to %.10llx (packet %.16llx verb %d local socket %lld network %.16llx)",path->address().toString(tmp),peer.address().toInt(),packetId,(double)verb,path->localSocket(),networkId); ZT_LOCAL_TRACE(tPtr,RR,"trying unknown path %s to %.10llx (packet %.16llx verb %d local socket %lld network %.16llx)",path->address().toString(tmp),peer.address().toInt(),packetId,verb,path->localSocket(),networkId);
std::pair<Address,Trace::Level> byn; std::pair<Address,Trace::Level> byn;
if (networkId) { Mutex::Lock l(_byNet_m); _byNet.get(networkId,byn); } if (networkId) { Mutex::Lock l(_byNet_m); _byNet.get(networkId,byn); }

View file

@ -28,10 +28,12 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/uio.h> #include <sys/uio.h>
#include <dirent.h> #include <dirent.h>
#ifdef ZT_ARCH_ARM_HAS_NEON
#ifdef __LINUX__ #ifdef __LINUX__
#include <sys/auxv.h> #include <sys/auxv.h>
#endif #endif
#endif #endif
#endif
#ifdef __WINDOWS__ #ifdef __WINDOWS__
#include <wincrypt.h> #include <wincrypt.h>

42
one.cpp
View file

@ -494,12 +494,11 @@ static int cli(int argc,char **argv)
} else if (command == "bond") { } else if (command == "bond") {
/* zerotier-cli bond */ /* zerotier-cli bond */
if (arg1.empty()) { if (arg1.empty()) {
printf("(bond) command is missing required arugments" ZT_EOL_S); printf("(bond) command is missing required arguments" ZT_EOL_S);
return 2; return 2;
} }
/* zerotier-cli bond list */ /* zerotier-cli bond list */
if (arg1 == "list") { if (arg1 == "list") {
fprintf(stderr, "zerotier-cli bond list\n");
const unsigned int scode = Http::GET(1024 * 1024 * 16,60000,(const struct sockaddr *)&addr,"/bonds",requestHeaders,responseHeaders,responseBody); const unsigned int scode = Http::GET(1024 * 1024 * 16,60000,(const struct sockaddr *)&addr,"/bonds",requestHeaders,responseHeaders,responseBody);
if (scode == 0) { if (scode == 0) {
printf("Error connecting to the ZeroTier service: %s\n\nPlease check that the service is running and that TCP port 9993 can be contacted via 127.0.0.1." ZT_EOL_S, responseBody.c_str()); printf("Error connecting to the ZeroTier service: %s\n\nPlease check that the service is running and that TCP port 9993 can be contacted via 127.0.0.1." ZT_EOL_S, responseBody.c_str());
@ -525,11 +524,11 @@ static int cli(int argc,char **argv)
for(unsigned long k=0;k<j.size();++k) { for(unsigned long k=0;k<j.size();++k) {
nlohmann::json &p = j[k]; nlohmann::json &p = j[k];
bool isBonded = p["isBonded"]; bool isBonded = p["isBonded"];
int8_t bondingPolicy = p["bondingPolicy"];
bool isHealthy = p["isHealthy"];
int8_t numAliveLinks = p["numAliveLinks"];
int8_t numTotalLinks = p["numTotalLinks"];
if (isBonded) { if (isBonded) {
int8_t bondingPolicy = p["bondingPolicy"];
bool isHealthy = p["isHealthy"];
int8_t numAliveLinks = p["numAliveLinks"];
int8_t numTotalLinks = p["numTotalLinks"];
bFoundBond = true; bFoundBond = true;
std::string healthStr; std::string healthStr;
if (isHealthy) { if (isHealthy) {
@ -618,7 +617,6 @@ static int cli(int argc,char **argv)
if (json) { if (json) {
printf("%s" ZT_EOL_S,OSUtils::jsonDump(j).c_str()); printf("%s" ZT_EOL_S,OSUtils::jsonDump(j).c_str());
} else { } else {
bool bFoundBond = false;
std::string healthStr; std::string healthStr;
if (OSUtils::jsonInt(j["isHealthy"],0)) { if (OSUtils::jsonInt(j["isHealthy"],0)) {
healthStr = "Healthy"; healthStr = "Healthy";
@ -630,14 +628,14 @@ static int cli(int argc,char **argv)
printf("Peer : %s\n", arg1.c_str()); printf("Peer : %s\n", arg1.c_str());
printf("Bond : %s\n", OSUtils::jsonString(j["bondingPolicy"],"-").c_str()); printf("Bond : %s\n", OSUtils::jsonString(j["bondingPolicy"],"-").c_str());
//if (bondingPolicy == ZT_BONDING_POLICY_ACTIVE_BACKUP) { //if (bondingPolicy == ZT_BONDING_POLICY_ACTIVE_BACKUP) {
printf("Link Select Method : %d\n", OSUtils::jsonInt(j["linkSelectMethod"],0)); printf("Link Select Method : %d\n", (int)OSUtils::jsonInt(j["linkSelectMethod"],0));
//} //}
printf("Status : %s\n", healthStr.c_str()); printf("Status : %s\n", healthStr.c_str());
printf("Links : %d/%d\n", numAliveLinks, numTotalLinks); printf("Links : %d/%d\n", numAliveLinks, numTotalLinks);
printf("Failover Interval : %d (ms)\n", OSUtils::jsonInt(j["failoverInterval"],0)); printf("Failover Interval : %d (ms)\n", (int)OSUtils::jsonInt(j["failoverInterval"],0));
printf("Up Delay : %d (ms)\n", OSUtils::jsonInt(j["upDelay"],0)); printf("Up Delay : %d (ms)\n", (int)OSUtils::jsonInt(j["upDelay"],0));
printf("Down Delay : %d (ms)\n", OSUtils::jsonInt(j["downDelay"],0)); printf("Down Delay : %d (ms)\n", (int)OSUtils::jsonInt(j["downDelay"],0));
printf("Packets Per Link : %d (ms)\n", OSUtils::jsonInt(j["packetsPerLink"],0)); printf("Packets Per Link : %d (ms)\n", (int)OSUtils::jsonInt(j["packetsPerLink"],0));
nlohmann::json &p = j["links"]; nlohmann::json &p = j["links"];
if (p.is_array()) { if (p.is_array()) {
printf("\n Interface Name\t\t\t\t\t Path\t Alive\n"); printf("\n Interface Name\t\t\t\t\t Path\t Alive\n");
@ -649,7 +647,7 @@ static int cli(int argc,char **argv)
i, i,
OSUtils::jsonString(p[i]["ifname"],"-").c_str(), OSUtils::jsonString(p[i]["ifname"],"-").c_str(),
OSUtils::jsonString(p[i]["path"],"-").c_str(), OSUtils::jsonString(p[i]["path"],"-").c_str(),
OSUtils::jsonInt(p[i]["alive"],0)); (int)OSUtils::jsonInt(p[i]["alive"],0));
} }
printf("\n Latency Jitter Loss Error Speed Alloc\n"); printf("\n Latency Jitter Loss Error Speed Alloc\n");
for(int i=0; i<80; i++) { printf("-"); } for(int i=0; i<80; i++) { printf("-"); }
@ -662,8 +660,8 @@ static int cli(int argc,char **argv)
OSUtils::jsonDouble(p[i]["latencyVariance"], 0), OSUtils::jsonDouble(p[i]["latencyVariance"], 0),
OSUtils::jsonDouble(p[i]["packetLossRatio"], 0), OSUtils::jsonDouble(p[i]["packetLossRatio"], 0),
OSUtils::jsonDouble(p[i]["packetErrorRatio"], 0), OSUtils::jsonDouble(p[i]["packetErrorRatio"], 0),
OSUtils::jsonInt(p[i]["givenLinkSpeed"], 0), (int)OSUtils::jsonInt(p[i]["givenLinkSpeed"], 0),
OSUtils::jsonInt(p[i]["allocation"], 0)); (int)OSUtils::jsonInt(p[i]["allocation"], 0));
} }
} }
} }
@ -676,7 +674,7 @@ static int cli(int argc,char **argv)
} }
} }
/* zerotier-cli bond command was malformed in some way */ /* zerotier-cli bond command was malformed in some way */
printf("(bond) command is missing required arugments" ZT_EOL_S); printf("(bond) command is missing required arguments" ZT_EOL_S);
return 2; return 2;
const unsigned int scode = Http::GET(1024 * 1024 * 16,60000,(const struct sockaddr *)&addr,"/bonds",requestHeaders,responseHeaders,responseBody); const unsigned int scode = Http::GET(1024 * 1024 * 16,60000,(const struct sockaddr *)&addr,"/bonds",requestHeaders,responseHeaders,responseBody);
if (scode == 0) { if (scode == 0) {
@ -711,14 +709,13 @@ static int cli(int argc,char **argv)
if (j.is_array()) { if (j.is_array()) {
for(unsigned long k=0;k<j.size();++k) { for(unsigned long k=0;k<j.size();++k) {
nlohmann::json &p = j[k]; nlohmann::json &p = j[k];
bool isBonded = p["isBonded"]; bool isBonded = p["isBonded"];
int8_t bondingPolicy = p["bondingPolicy"];
bool isHealthy = p["isHealthy"];
int8_t numAliveLinks = p["numAliveLinks"];
int8_t numTotalLinks = p["numTotalLinks"];
if (isBonded) { if (isBonded) {
int8_t bondingPolicy = p["bondingPolicy"];
bool isHealthy = p["isHealthy"];
int8_t numAliveLinks = p["numAliveLinks"];
int8_t numTotalLinks = p["numTotalLinks"];
bFoundBond = true; bFoundBond = true;
std::string healthStr; std::string healthStr;
if (isHealthy) { if (isHealthy) {
@ -1310,7 +1307,6 @@ static int cli(int argc,char **argv)
struct ifconf ifc; struct ifconf ifc;
char buf[1024]; char buf[1024];
char stringBuffer[128]; char stringBuffer[128];
int success = 0;
int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);

View file

@ -14,20 +14,17 @@
#ifndef ZT_LINK_HPP #ifndef ZT_LINK_HPP
#define ZT_LINK_HPP #define ZT_LINK_HPP
#include <string>
#include "../node/AtomicCounter.hpp" #include "../node/AtomicCounter.hpp"
#include "../node/SharedPtr.hpp"
#include <string>
namespace ZeroTier { namespace ZeroTier {
class Link class Link {
{
friend class SharedPtr<Link>; friend class SharedPtr<Link>;
public: public:
Link() {}
/** /**
* *
* @param ifnameStr * @param ifnameStr
@ -38,123 +35,165 @@ public:
* @param failoverToLinkStr * @param failoverToLinkStr
* @param userSpecifiedAlloc * @param userSpecifiedAlloc
*/ */
Link(std::string& ifnameStr, 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)
uint8_t ipvPref, : _ifnameStr(ifnameStr)
uint32_t speed, , _ipvPref(ipvPref)
uint32_t linkMonitorInterval, , _speed(speed)
uint32_t upDelay, , _relativeSpeed(0)
uint32_t downDelay, , _linkMonitorInterval(linkMonitorInterval)
bool enabled, , _upDelay(upDelay)
uint8_t mode, , _downDelay(downDelay)
std::string failoverToLinkStr, , _enabled(enabled)
float userSpecifiedAlloc) : , _mode(mode)
_ifnameStr(ifnameStr), , _failoverToLinkStr(failoverToLinkStr)
_ipvPref(ipvPref), , _userSpecifiedAlloc(userSpecifiedAlloc)
_speed(speed), , _isUserSpecified(false)
_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. * @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. * @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. * @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. * @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. * @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. * Signify that this link was specified by the user and not the result of auto-detection.
* *
* @param isUserSpecified * @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. * @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. * @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. * Sets the speed of the link relative to others in the bond.
* *
* @param relativeSpeed The speed relative to the rest of the link. * @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. * Sets the speed of the link relative to others in the bond.
* *
* @param relativeSpeed * @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.) * @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.) * @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.) * @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.) * @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. * @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. * @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 * @return Whether this link is enabled or disabled
*/ */
inline uint8_t enabled() { return _enabled; } inline uint8_t enabled()
{
private: return _enabled;
}
private:
/** /**
* String representation of underlying interface's system name * String representation of underlying interface's system name
*/ */
@ -223,15 +262,15 @@ private:
float _userSpecifiedAlloc; float _userSpecifiedAlloc;
/** /**
* Whether or not this link was created as a result of manual user specification. This is * 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 * important to know because certain policy decisions are dependent on whether the user
* intents to use a specific set of interfaces. * intents to use a specific set of interfaces.
*/ */
bool _isUserSpecified; bool _isUserSpecified;
AtomicCounter __refCount; AtomicCounter __refCount;
}; };
} // namespace ZeroTier } // namespace ZeroTier
#endif #endif

View file

@ -302,7 +302,6 @@ static void _peerToJson(nlohmann::json &pj,const ZT_Peer *peer)
static void _bondToJson(nlohmann::json &pj, SharedPtr<Bond> &bond) static void _bondToJson(nlohmann::json &pj, SharedPtr<Bond> &bond)
{ {
char tmp[256];
uint64_t now = OSUtils::now(); uint64_t now = OSUtils::now();
int bondingPolicy = bond->getPolicy(); int bondingPolicy = bond->getPolicy();
@ -1261,7 +1260,7 @@ public:
_bondToJson(res,bond); _bondToJson(res,bond);
scode = 200; scode = 200;
} else { } else {
fprintf(stderr, "unable to find bond to peer %llx\n", id); fprintf(stderr, "unable to find bond to peer %llx\n", (unsigned long long)id);
scode = 400; scode = 400;
} }
} }
@ -1468,7 +1467,7 @@ public:
if (bond) { if (bond) {
scode = bond->abForciblyRotateLink() ? 200 : 400; scode = bond->abForciblyRotateLink() ? 200 : 400;
} else { } else {
fprintf(stderr, "unable to find bond to peer %llx\n", id); fprintf(stderr, "unable to find bond to peer %llx\n", (unsigned long long)id);
scode = 400; scode = 400;
} }
} }
@ -2393,7 +2392,7 @@ public:
Dictionary<4096> nc; Dictionary<4096> nc;
nc.load(nlcbuf.c_str()); nc.load(nlcbuf.c_str());
Buffer<1024> allowManaged; Buffer<1024> allowManaged;
if (nc.get("allowManaged", allowManaged) && !allowManaged.size() == 0) { if (nc.get("allowManaged", allowManaged) && allowManaged.size() > 0) {
std::string addresses (allowManaged.begin(), allowManaged.size()); std::string addresses (allowManaged.begin(), allowManaged.size());
if (allowManaged.size() <= 5) { // untidy parsing for backward compatibility if (allowManaged.size() <= 5) { // untidy parsing for backward compatibility
if (allowManaged[0] == '1' || allowManaged[0] == 't' || allowManaged[0] == 'T') { if (allowManaged[0] == '1' || allowManaged[0] == 't' || allowManaged[0] == 'T') {