Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions node/IncomingPacket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,13 @@ bool IncomingPacket::_doOK(const RuntimeEnvironment* RR, void* tPtr, const Share
uint64_t networkId = 0;

if (! RR->node->expectingReplyTo(inRePacketId)) {
// Busy senders can evict probe IDs before their replies arrive.
const bool echo = inReVerb == Packet::VERB_ECHO && size() == ZT_PROTO_VERB_OK_IDX_PAYLOAD + sizeof(uint64_t);
const bool hello = inReVerb == Packet::VERB_HELLO && size() >= ZT_PROTO_VERB_HELLO__OK__IDX_REVISION + 2
&& (*this)[ZT_PROTO_VERB_HELLO__OK__IDX_PROTOCOL_VERSION] >= ZT_PROTO_VERSION_MIN;
if (! hops() && (echo || hello)) {
peer->acknowledgePathProbe(_path, inReVerb, at<uint64_t>(ZT_PROTO_VERB_OK_IDX_PAYLOAD));
}
return true;
}

Expand Down
110 changes: 101 additions & 9 deletions node/Peer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ void Peer::received(
if (_paths[i].p) {
if (_paths[i].p == path) {
_paths[i].lr = now;
_paths[i].probeStarted = 0;
havePath = true;
break;
}
Expand Down Expand Up @@ -167,6 +168,7 @@ void Peer::received(
if (replacePath != ZT_MAX_PEER_NETWORK_PATHS) {
RR->t->peerLearnedNewPath(tPtr, networkId, *this, path, packetId);
_paths[replacePath].lr = now;
_paths[replacePath].probeStarted = 0;
_paths[replacePath].p = path;
_paths[replacePath].priority = 1;
Mutex::Lock _l(_bond_m);
Expand Down Expand Up @@ -258,10 +260,13 @@ void Peer::received(
}
}

SharedPtr<Path> Peer::getAppropriatePath(int64_t now, bool includeExpired, int32_t flowId)
SharedPtr<Path> Peer::getAppropriatePath(int64_t now, bool includeExpired, int32_t flowId, bool* needsCheck)
{
Mutex::Lock _l(_paths_m);
Mutex::Lock _lb(_bond_m);
if (needsCheck) {
*needsCheck = false;
}
if (_bond && _bond->isReady()) {
return _bond->getAppropriatePath(now, flowId);
}
Expand All @@ -286,11 +291,81 @@ SharedPtr<Path> Peer::getAppropriatePath(int64_t now, bool includeExpired, int32
}
}
if (bestPath != ZT_MAX_PEER_NETWORK_PATHS) {
if (needsCheck && ! _bond && ! RR->node->lowBandwidthModeEnabled()) {
*needsCheck = _paths[bestPath].needsProbeCheck(now);
}
return _paths[bestPath].p;
}
return SharedPtr<Path>();
}

SharedPtr<Path> Peer::checkPath(void* tPtr, const SharedPtr<Path>& path, int64_t now)
{
if (RR->node->lowBandwidthModeEnabled() || RR->topology->isUpstream(_id)) {
return path;
}
Mutex::Lock _l(_paths_m);
if (_bond) {
return path;
}
for (unsigned int i = 0; i < ZT_MAX_PEER_NETWORK_PATHS && _paths[i].p; ++i) {
_PeerPath& p = _paths[i];
if (p.p == path) {
if ((now - p.lr) >= ZT_PEER_PATH_EXPIRATION) {
break;
}
if (! p.needsProbeCheck(now)) {
return path;
}
if ((now - _lastEchoCheck) >= ZT_PATH_HEARTBEAT_PERIOD
&& (! p.probeStarted || (now - p.probeStarted) >= ZT_PATH_HEARTBEAT_PERIOD)) {
_lastEchoCheck = now;
const bool retry = p.probeStarted != 0;
if (! p.probeStarted) {
p.probeStarted = now;
}
attemptToContactAt(tPtr, path->localSocket(), path->address(), now, retry, p.probeStarted);
path->sent(now);
}
if ((! p.probeStarted) || ((now - p.probeStarted) < ZT_PATH_HEARTBEAT_PERIOD)) {
return path;
}
break;
}
}
unsigned int bestPath = ZT_MAX_PEER_NETWORK_PATHS;
long bestPathQuality = 2147483647;
for (unsigned int i = 0; i < ZT_MAX_PEER_NETWORK_PATHS && _paths[i].p; ++i) {
if ((now - _paths[i].lr) < ZT_PATH_HEARTBEAT_PERIOD) {
const long q = _paths[i].p->quality(now) / _paths[i].priority;
if (q <= bestPathQuality) {
bestPathQuality = q;
bestPath = i;
}
}
}
return bestPath != ZT_MAX_PEER_NETWORK_PATHS ? _paths[bestPath].p : SharedPtr<Path>();
}

void Peer::acknowledgePathProbe(const SharedPtr<Path>& path, Packet::Verb verb, uint64_t timestamp)
{
const int64_t now = RR->node->now();
Mutex::Lock _l(_paths_m);
for (unsigned int i = 0; i < ZT_MAX_PEER_NETWORK_PATHS && _paths[i].p; ++i) {
_PeerPath& p = _paths[i];
if (p.p == path) {
if (p.probeStarted && timestamp <= (uint64_t)now
&& ((verb == Packet::VERB_ECHO) ? timestamp == (uint64_t)p.probeStarted
: timestamp >= (uint64_t)p.probeStarted)) {
_lastReceive = now;
p.lr = now;
p.probeStarted = 0;
}
return;
}
}
}

void Peer::introduce(void* const tPtr, const int64_t now, const SharedPtr<Peer>& other) const
{
unsigned int myBestV4ByScope[ZT_INETADDRESS_MAX_SCOPE + 1];
Expand Down Expand Up @@ -423,7 +498,7 @@ void Peer::introduce(void* const tPtr, const int64_t now, const SharedPtr<Peer>&
}
}

void Peer::sendHELLO(void* tPtr, const int64_t localSocket, const InetAddress& atAddress, int64_t now)
void Peer::sendHELLO(void* tPtr, const int64_t localSocket, const InetAddress& atAddress, int64_t now, bool forceSpecificSocket)
{
Packet outp(_id.address(), RR->identity.address(), Packet::VERB_HELLO);

Expand Down Expand Up @@ -461,25 +536,34 @@ void Peer::sendHELLO(void* tPtr, const int64_t localSocket, const InetAddress& a
if (atAddress) {
outp.armor(_key, false, RR->node->encryptedHelloEnabled(), nullptr, _id);
RR->node->expectReplyTo(outp.packetId());
RR->node->putPacket(tPtr, RR->node->lowBandwidthModeEnabled() ? localSocket : -1, atAddress, outp.data(), outp.size());
RR->node->putPacket(tPtr, (forceSpecificSocket || RR->node->lowBandwidthModeEnabled()) ? localSocket : -1, atAddress, outp.data(), outp.size());
}
else {
RR->node->expectReplyTo(outp.packetId());
RR->sw->send(tPtr, outp, true, 0, ZT_QOS_NO_FLOW);
}
}

void Peer::attemptToContactAt(void* tPtr, const int64_t localSocket, const InetAddress& atAddress, int64_t now, bool sendFullHello)
void Peer::attemptToContactAt(
void* tPtr,
const int64_t localSocket,
const InetAddress& atAddress,
int64_t now,
bool sendFullHello,
uint64_t echoToken)
{
if ((! sendFullHello) && (_vProto >= 5) && (! ((_vMajor == 1) && (_vMinor == 1) && (_vRevision == 0)))) {
Packet outp(_id.address(), RR->identity.address(), Packet::VERB_ECHO);
if (echoToken) {
outp.append(echoToken);
}
outp.armor(_key, true, false, aesKeysIfSupported(), _id);
Metrics::pkt_echo_out++;
RR->node->expectReplyTo(outp.packetId());
RR->node->putPacket(tPtr, localSocket, atAddress, outp.data(), outp.size());
}
else {
sendHELLO(tPtr, localSocket, atAddress, now);
sendHELLO(tPtr, localSocket, atAddress, now, echoToken != 0);
}
}

Expand Down Expand Up @@ -540,11 +624,13 @@ void Peer::performMultipathStateCheck(void* tPtr, int64_t now)

unsigned int Peer::doPingAndKeepalive(void* tPtr, int64_t now)
{
const bool probePeer = ! RR->node->lowBandwidthModeEnabled() && ! RR->topology->isUpstream(_id);
unsigned int sent = 0;
{
Mutex::Lock _l(_paths_m);

performMultipathStateCheck(tPtr, now);
const bool trackProbes = probePeer && ! _bond;

const bool sendFullHello = ((now - _lastSentFullHello) >= ZT_PEER_PING_PERIOD);
if (sendFullHello) {
Expand All @@ -571,7 +657,13 @@ unsigned int Peer::doPingAndKeepalive(void* tPtr, int64_t now)
// Clean expired and reduced priority paths
if (((now - _paths[i].lr) < ZT_PEER_PATH_EXPIRATION) && (_paths[i].priority == maxPriority)) {
if ((sendFullHello) || (_paths[i].p->needsHeartbeat(now))) {
attemptToContactAt(tPtr, _paths[i].p->localSocket(), _paths[i].p->address(), now, sendFullHello);
// Full HELLOs can use another socket, so they cannot start a path-specific check.
const bool probe = trackProbes && ! sendFullHello;
if (probe && ! _paths[i].probeStarted && (now - _paths[i].lr) >= ZT_PATH_HEARTBEAT_PERIOD) {
_paths[i].probeStarted = now;
}
attemptToContactAt(tPtr, _paths[i].p->localSocket(), _paths[i].p->address(), now, sendFullHello,
probe ? _paths[i].probeStarted : 0);
_paths[i].p->sent(now);
sent |= (_paths[i].p->address().ss_family == AF_INET) ? 0x1 : 0x2;
}
Expand Down Expand Up @@ -654,13 +746,12 @@ void Peer::clusterRedirect(void* tPtr, const SharedPtr<Path>& originatingPath, c
}
if (j < ZT_MAX_PEER_NETWORK_PATHS) {
_paths[j].lr = now;
_paths[j].probeStarted = 0;
_paths[j].p = np;
_paths[j].priority = newPriority;
++j;
while (j < ZT_MAX_PEER_NETWORK_PATHS) {
_paths[j].lr = 0;
_paths[j].p.zero();
_paths[j].priority = 1;
_paths[j] = _PeerPath();
++j;
}
}
Expand All @@ -676,6 +767,7 @@ void Peer::resetWithinScope(void* tPtr, InetAddress::IpScope scope, int inetAddr
attemptToContactAt(tPtr, _paths[i].p->localSocket(), _paths[i].p->address(), now, false);
_paths[i].p->sent(now);
_paths[i].lr = 0; // path will not be used unless it speaks again
_paths[i].probeStarted = 0;
}
}
else {
Expand Down
34 changes: 30 additions & 4 deletions node/Peer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,21 @@ class Peer {
*
* @param now Current time
* @param includeExpired If true, include even expired paths
* @param needsCheck Optional output indicating that the selected path needs a probe before continued use
* @return Best current path or NULL if none
*/
SharedPtr<Path> getAppropriatePath(int64_t now, bool includeExpired, int32_t flowId = -1);
SharedPtr<Path>
getAppropriatePath(int64_t now, bool includeExpired, int32_t flowId = -1, bool* needsCheck = nullptr);

/**
* Probe a quiet direct path and return it, a fresh alternative, or no usable path.
*/
SharedPtr<Path> checkPath(void* tPtr, const SharedPtr<Path>& path, int64_t now);

/**
* Refresh an existing path after an ECHO or HELLO reply matches its pending probe.
*/
void acknowledgePathProbe(const SharedPtr<Path>& path, Packet::Verb verb, uint64_t timestamp);

/**
* Send VERB_RENDEZVOUS to this and another peer via the best common IP scope and path
Expand All @@ -203,8 +215,9 @@ class Peer {
* @param localSocket Local source socket
* @param atAddress Destination address
* @param now Current time
* @param forceSpecificSocket Use only the given socket when checking a retained path
*/
void sendHELLO(void* tPtr, const int64_t localSocket, const InetAddress& atAddress, int64_t now);
void sendHELLO(void* tPtr, const int64_t localSocket, const InetAddress& atAddress, int64_t now, bool forceSpecificSocket = false);

/**
* Send ECHO (or HELLO for older peers) to this peer at the given address
Expand All @@ -216,8 +229,15 @@ class Peer {
* @param atAddress Destination address
* @param now Current time
* @param sendFullHello If true, always send a full HELLO instead of just an ECHO
* @param echoToken Optional token for a pending path probe
*/
void attemptToContactAt(void* tPtr, const int64_t localSocket, const InetAddress& atAddress, int64_t now, bool sendFullHello);
void attemptToContactAt(
void* tPtr,
const int64_t localSocket,
const InetAddress& atAddress,
int64_t now,
bool sendFullHello,
uint64_t echoToken = 0);

/**
* Try a memorized or statically defined path if any are known
Expand Down Expand Up @@ -666,10 +686,16 @@ class Peer {

private:
struct _PeerPath {
_PeerPath() : lr(0), p(), priority(1)
_PeerPath() : lr(0), probeStarted(0), p(), priority(1)
{
}
bool needsProbeCheck(int64_t now) const
{
// Give remote heartbeats one background tick before starting another check.
return (now - lr) >= ZT_PATH_HEARTBEAT_PERIOD + (probeStarted ? 0 : ZT_PING_CHECK_INTERVAL);
}
int64_t lr; // time of last valid ZeroTier packet
int64_t probeStarted; // First unanswered probe; retries share its timestamp.
SharedPtr<Path> p;
long priority; // >= 1, higher is better
};
Expand Down
6 changes: 5 additions & 1 deletion node/Switch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1078,7 +1078,11 @@ bool Switch::_trySend(void* tPtr, Packet& packet, bool encrypt, const uint64_t n
return true;
}
else {
viaPath = peer->getAppropriatePath(now, false, flowId);
bool needsCheck;
viaPath = peer->getAppropriatePath(now, false, flowId, &needsCheck);
if (viaPath && needsCheck) {
viaPath = peer->checkPath(tPtr, viaPath, now);
}
if (! viaPath) {
peer->tryMemorizedPath(tPtr, now); // periodically attempt memorized or statically defined paths, if any are known
const SharedPtr<Peer> relay(RR->topology->getUpstreamPeer(nwid));
Expand Down
Loading