mirror of
https://github.com/zerotier/ZeroTierOne.git
synced 2025-06-05 20:13:44 +02:00
Cleanup. Misc type conversion and signedness fixes
This commit is contained in:
parent
6a2ba4baca
commit
1debe2292d
5 changed files with 30 additions and 33 deletions
|
@ -303,11 +303,11 @@ public:
|
||||||
*/
|
*/
|
||||||
inline float computeQuality(const int64_t now)
|
inline float computeQuality(const int64_t now)
|
||||||
{
|
{
|
||||||
float latency_contrib = _meanLatency ? 1.0 / _meanLatency : 0;
|
float latency_contrib = _meanLatency ? (float)1.0 / _meanLatency : 0;
|
||||||
float jitter_contrib = _jitter ? 1.0 / _jitter : 0;
|
float jitter_contrib = _jitter ? (float)1.0 / _jitter : 0;
|
||||||
float throughput_contrib = _meanThroughput ? _meanThroughput / 1000000 : 0; // in Mbps
|
float throughput_contrib = _meanThroughput ? _meanThroughput / 1000000 : 0; // in Mbps
|
||||||
float age_contrib = _meanAge > 0 ? (float)sqrt(_meanAge) : 1;
|
float age_contrib = _meanAge > 0 ? (float)sqrt(_meanAge) : 1;
|
||||||
float error_contrib = 1.0 - _meanPacketErrorRatio;
|
float error_contrib = (float)1.0 - _meanPacketErrorRatio;
|
||||||
float sum = (latency_contrib + jitter_contrib + throughput_contrib + error_contrib) / age_contrib;
|
float sum = (latency_contrib + jitter_contrib + throughput_contrib + error_contrib) / age_contrib;
|
||||||
_lastComputedQuality = sum * (long)((_ipScope) + 1);
|
_lastComputedQuality = sum * (long)((_ipScope) + 1);
|
||||||
return _lastComputedQuality;
|
return _lastComputedQuality;
|
||||||
|
|
|
@ -320,12 +320,12 @@ SharedPtr<Path> Peer::getAppropriatePath(int64_t now, bool includeExpired)
|
||||||
Utils::getSecureRandom(&r, 1);
|
Utils::getSecureRandom(&r, 1);
|
||||||
if (numAlivePaths > 0) {
|
if (numAlivePaths > 0) {
|
||||||
// pick a random out of the set deemed "alive"
|
// pick a random out of the set deemed "alive"
|
||||||
int rf = (float)(r %= numAlivePaths);
|
int rf = r % numAlivePaths;
|
||||||
return _paths[alivePaths[rf]].p;
|
return _paths[alivePaths[rf]].p;
|
||||||
}
|
}
|
||||||
else if(numStalePaths > 0) {
|
else if(numStalePaths > 0) {
|
||||||
// resort to trying any non-expired path
|
// resort to trying any non-expired path
|
||||||
int rf = (float)(r %= numStalePaths);
|
int rf = r % numStalePaths;
|
||||||
return _paths[stalePaths[rf]].p;
|
return _paths[stalePaths[rf]].p;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -366,11 +366,9 @@ SharedPtr<Path> Peer::getAppropriatePath(int64_t now, bool includeExpired)
|
||||||
// Compute quality here, going forward we will use lastComputedQuality()
|
// Compute quality here, going forward we will use lastComputedQuality()
|
||||||
currQuality = _paths[i].p->computeQuality(now);
|
currQuality = _paths[i].p->computeQuality(now);
|
||||||
if (!_paths[i].p->stale(now)) {
|
if (!_paths[i].p->stale(now)) {
|
||||||
alivePaths[i] = currQuality;
|
|
||||||
numAlivePaths++;
|
numAlivePaths++;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
stalePaths[i] = currQuality;
|
|
||||||
numStalePaths++;
|
numStalePaths++;
|
||||||
}
|
}
|
||||||
if (currQuality > maxQuality) {
|
if (currQuality > maxQuality) {
|
||||||
|
@ -412,10 +410,10 @@ SharedPtr<Path> Peer::getAppropriatePath(int64_t now, bool includeExpired)
|
||||||
// randomly selected for the next packet. If however the path
|
// randomly selected for the next packet. If however the path
|
||||||
// needs to contribute more to the flow, we should record
|
// needs to contribute more to the flow, we should record
|
||||||
float imbalance = 0;
|
float imbalance = 0;
|
||||||
float qualityScalingFactor = 1.0 / totalRelativeQuality;
|
float qualityScalingFactor = (float)1.0 / totalRelativeQuality;
|
||||||
for(uint16_t i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
|
for(uint16_t i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
|
||||||
// Out of the last N packets to this peer, how many were sent by this path?
|
// Out of the last N packets to this peer, how many were sent by this path?
|
||||||
int numPktSentWithinWin = (int)_pathChoiceHist->countValue((float)i);
|
int numPktSentWithinWin = (int)_pathChoiceHist->countValue(i);
|
||||||
// Compute traffic allocation for each path in the flow
|
// Compute traffic allocation for each path in the flow
|
||||||
if (_paths[i].p && _paths[i].p->isValidState()) {
|
if (_paths[i].p && _paths[i].p->isValidState()) {
|
||||||
// Allocation
|
// Allocation
|
||||||
|
@ -442,7 +440,7 @@ SharedPtr<Path> Peer::getAppropriatePath(int64_t now, bool includeExpired)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compute and record current flow balance
|
// Compute and record current flow balance
|
||||||
float balance = 1.0 - imbalance;
|
float balance = (float)1.0 - imbalance;
|
||||||
if (balance >= ZT_MULTIPATH_FLOW_BALANCE_THESHOLD) {
|
if (balance >= ZT_MULTIPATH_FLOW_BALANCE_THESHOLD) {
|
||||||
if (!_linkBalanceStatus) {
|
if (!_linkBalanceStatus) {
|
||||||
_linkBalanceStatus = true;
|
_linkBalanceStatus = true;
|
||||||
|
|
|
@ -226,34 +226,34 @@ public:
|
||||||
/**
|
/**
|
||||||
* @return The arithmetic mean of the contents of the buffer
|
* @return The arithmetic mean of the contents of the buffer
|
||||||
*/
|
*/
|
||||||
T mean()
|
float mean()
|
||||||
{
|
{
|
||||||
size_t iterator = begin;
|
size_t iterator = begin;
|
||||||
T mean = 0;
|
float mean = 0;
|
||||||
for (int i=0; i<size; i++) {
|
for (size_t i=0; i<size; i++) {
|
||||||
iterator = (iterator + size - 1) % size;
|
iterator = (iterator + size - 1) % size;
|
||||||
mean += *(buf + iterator);
|
mean += *(buf + iterator);
|
||||||
}
|
}
|
||||||
return count() ? mean / (T)count() : 0;
|
return count() ? mean / (float)count() : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return The sample standard deviation of the contents of the ring buffer
|
* @return The sample standard deviation of the contents of the ring buffer
|
||||||
*/
|
*/
|
||||||
T stddev()
|
float stddev()
|
||||||
{
|
{
|
||||||
size_t iterator = begin;
|
size_t iterator = begin;
|
||||||
T cached_mean = mean();
|
float cached_mean = mean();
|
||||||
if (size) {
|
if (size) {
|
||||||
T sum_of_squared_deviations = 0;
|
T sum_of_squared_deviations = 0;
|
||||||
for (int i=0; i<size; i++) {
|
for (size_t i=0; i<size; i++) {
|
||||||
iterator = (iterator + size - 1) % size;
|
iterator = (iterator + size - 1) % size;
|
||||||
T deviation = (buf[i] - cached_mean);
|
float deviation = (buf[i] - cached_mean);
|
||||||
T sdev = deviation*deviation;
|
float sdev = deviation*deviation;
|
||||||
sum_of_squared_deviations += sdev;
|
sum_of_squared_deviations += sdev;
|
||||||
}
|
}
|
||||||
T variance = sum_of_squared_deviations / (size - 1);
|
float variance = sum_of_squared_deviations / (size - 1);
|
||||||
T sd = sqrt(variance);
|
float sd = sqrt(variance);
|
||||||
return sd;
|
return sd;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -266,7 +266,7 @@ public:
|
||||||
{
|
{
|
||||||
size_t iterator = begin;
|
size_t iterator = begin;
|
||||||
size_t zeros = 0;
|
size_t zeros = 0;
|
||||||
for (int i=0; i<size; i++) {
|
for (size_t i=0; i<size; i++) {
|
||||||
iterator = (iterator + size - 1) % size;
|
iterator = (iterator + size - 1) % size;
|
||||||
if (*(buf + iterator) == 0) {
|
if (*(buf + iterator) == 0) {
|
||||||
zeros++;
|
zeros++;
|
||||||
|
@ -283,7 +283,7 @@ public:
|
||||||
{
|
{
|
||||||
size_t iterator = begin;
|
size_t iterator = begin;
|
||||||
size_t count = 0;
|
size_t count = 0;
|
||||||
for (int i=0; i<size; i++) {
|
for (size_t i=0; i<size; i++) {
|
||||||
iterator = (iterator + size - 1) % size;
|
iterator = (iterator + size - 1) % size;
|
||||||
if (*(buf + iterator) == value) {
|
if (*(buf + iterator) == value) {
|
||||||
count++;
|
count++;
|
||||||
|
@ -298,7 +298,7 @@ public:
|
||||||
void dump()
|
void dump()
|
||||||
{
|
{
|
||||||
size_t iterator = begin;
|
size_t iterator = begin;
|
||||||
for (int i=0; i<size; i++) {
|
for (size_t i=0; i<size; i++) {
|
||||||
iterator = (iterator + size - 1) % size;
|
iterator = (iterator + size - 1) % size;
|
||||||
if (typeid(T) == typeid(int)) {
|
if (typeid(T) == typeid(int)) {
|
||||||
// DEBUG_INFO("buf[%2zu]=%2d", iterator, (int)*(buf + iterator));
|
// DEBUG_INFO("buf[%2zu]=%2d", iterator, (int)*(buf + iterator));
|
||||||
|
|
|
@ -365,7 +365,7 @@ public:
|
||||||
uint64_t egress_time = OSUtils::now();
|
uint64_t egress_time = OSUtils::now();
|
||||||
PhySocketImpl *sws = (reinterpret_cast<PhySocketImpl *>(s));
|
PhySocketImpl *sws = (reinterpret_cast<PhySocketImpl *>(s));
|
||||||
#if defined(_WIN32) || defined(_WIN64)
|
#if defined(_WIN32) || defined(_WIN64)
|
||||||
return ((long)::sendto(sws->sock,reinterpret_cast<const char *>(data),len,0,to,(to->sa_family == AF_INET6) ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in)) == (long)len);
|
int w = ::sendto(sws->sock,reinterpret_cast<const char *>(data),len,0,to,(to->sa_family == AF_INET6) ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in))
|
||||||
#else
|
#else
|
||||||
int w = ::sendto(sws->sock,data,len,0,to,(to->sa_family == AF_INET6) ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in));
|
int w = ::sendto(sws->sock,data,len,0,to,(to->sa_family == AF_INET6) ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in));
|
||||||
#endif
|
#endif
|
||||||
|
@ -380,7 +380,7 @@ public:
|
||||||
*/
|
*/
|
||||||
inline void refresh_link_speed_records()
|
inline void refresh_link_speed_records()
|
||||||
{
|
{
|
||||||
for(int i=0;i<link_test_records.size();i++) {
|
for(size_t i=0;i<link_test_records.size();i++) {
|
||||||
if(OSUtils::now() - link_test_records[i]->egress_time > ZT_LINK_TEST_TIMEOUT) {
|
if(OSUtils::now() - link_test_records[i]->egress_time > ZT_LINK_TEST_TIMEOUT) {
|
||||||
PhySocketImpl *sws = (reinterpret_cast<PhySocketImpl *>(link_test_records[i]->s));
|
PhySocketImpl *sws = (reinterpret_cast<PhySocketImpl *>(link_test_records[i]->s));
|
||||||
if (sws) {
|
if (sws) {
|
||||||
|
@ -404,7 +404,7 @@ public:
|
||||||
PhySocketImpl *sws = (reinterpret_cast<PhySocketImpl *>(s));
|
PhySocketImpl *sws = (reinterpret_cast<PhySocketImpl *>(s));
|
||||||
uint64_t *id = (uint64_t*)data;
|
uint64_t *id = (uint64_t*)data;
|
||||||
#if defined(_WIN32) || defined(_WIN64)
|
#if defined(_WIN32) || defined(_WIN64)
|
||||||
return ((long)::sendto(sws->sock,reinterpret_cast<const char *>(data),len,0,from,(from->sa_family == AF_INET6) ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in)) == (long)len);
|
int w = ::sendto(sws->sock,reinterpret_cast<const char *>(id),sizeof(id[0]),0,from,(from->sa_family == AF_INET6) ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in));
|
||||||
#else
|
#else
|
||||||
int w = ::sendto(sws->sock,id,sizeof(id[0]),0,from,(from->sa_family == AF_INET6) ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in));
|
int w = ::sendto(sws->sock,id,sizeof(id[0]),0,from,(from->sa_family == AF_INET6) ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in));
|
||||||
#endif
|
#endif
|
||||||
|
@ -424,12 +424,12 @@ public:
|
||||||
*/
|
*/
|
||||||
inline bool handle_link_test_response(PhySocket *s,const struct sockaddr *from,void *data,unsigned long len) {
|
inline bool handle_link_test_response(PhySocket *s,const struct sockaddr *from,void *data,unsigned long len) {
|
||||||
uint64_t *id = (uint64_t*)data;
|
uint64_t *id = (uint64_t*)data;
|
||||||
for(int i=0;i<link_test_records.size();i++) {
|
for(size_t i=0;i<link_test_records.size();i++) {
|
||||||
if(link_test_records[i]->id == id[0]) {
|
if(link_test_records[i]->id == id[0]) {
|
||||||
float rtt = (OSUtils::now()-link_test_records[i]->egress_time) / (float)1000; // s
|
float rtt = (OSUtils::now()-link_test_records[i]->egress_time) / (float)1000; // s
|
||||||
uint32_t sz = (link_test_records[i]->length) * 8; // bits
|
uint32_t sz = (link_test_records[i]->length) * 8; // bits
|
||||||
float transit_time = rtt / 2.0;
|
float transit_time = rtt / (float)2.0;
|
||||||
int64_t raw = sz / transit_time;
|
uint64_t raw = (uint64_t)(sz / transit_time);
|
||||||
PhySocketImpl *sws = (reinterpret_cast<PhySocketImpl *>(s));
|
PhySocketImpl *sws = (reinterpret_cast<PhySocketImpl *>(s));
|
||||||
if (sws) {
|
if (sws) {
|
||||||
sws->throughput = raw;
|
sws->throughput = raw;
|
||||||
|
|
|
@ -879,7 +879,6 @@ public:
|
||||||
lastMultipathModeUpdate = now;
|
lastMultipathModeUpdate = now;
|
||||||
_node->setMultipathMode(_multipathMode);
|
_node->setMultipathMode(_multipathMode);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test link speeds
|
// Test link speeds
|
||||||
// TODO: This logic should eventually find its way into the core or as part of a passive
|
// TODO: This logic should eventually find its way into the core or as part of a passive
|
||||||
// measure within the protocol.
|
// measure within the protocol.
|
||||||
|
@ -894,7 +893,7 @@ public:
|
||||||
std::vector<PhySocket*> sockets = _binder.getBoundSockets();
|
std::vector<PhySocket*> sockets = _binder.getBoundSockets();
|
||||||
// interfaces
|
// interfaces
|
||||||
for (int i=0; i<ZT_BINDER_MAX_BINDINGS; i++) {
|
for (int i=0; i<ZT_BINDER_MAX_BINDINGS; i++) {
|
||||||
for(int j=0;j<pl->peerCount;++j) {
|
for(size_t j=0;j<pl->peerCount;++j) {
|
||||||
for (int k=0; k<(ZT_MAX_PEER_NETWORK_PATHS/4); k++) {
|
for (int k=0; k<(ZT_MAX_PEER_NETWORK_PATHS/4); k++) {
|
||||||
Utils::getSecureRandom(pktBuf, 8); // generate one random integer for unique id
|
Utils::getSecureRandom(pktBuf, 8); // generate one random integer for unique id
|
||||||
_phy.test_link_speed(sockets[i], (struct sockaddr*)&(pl->peers[j].paths[k].address), pktBuf, ZT_LINK_TEST_DATAGRAM_SZ);
|
_phy.test_link_speed(sockets[i], (struct sockaddr*)&(pl->peers[j].paths[k].address), pktBuf, ZT_LINK_TEST_DATAGRAM_SZ);
|
||||||
|
@ -1554,7 +1553,7 @@ public:
|
||||||
|
|
||||||
_primaryPort = (unsigned int)OSUtils::jsonInt(settings["primaryPort"],(uint64_t)_primaryPort) & 0xffff;
|
_primaryPort = (unsigned int)OSUtils::jsonInt(settings["primaryPort"],(uint64_t)_primaryPort) & 0xffff;
|
||||||
_allowTcpFallbackRelay = OSUtils::jsonBool(settings["allowTcpFallbackRelay"],true);
|
_allowTcpFallbackRelay = OSUtils::jsonBool(settings["allowTcpFallbackRelay"],true);
|
||||||
_multipathMode = OSUtils::jsonInt(settings["multipathMode"],0);
|
_multipathMode = (unsigned int)OSUtils::jsonInt(settings["multipathMode"],0);
|
||||||
if (_multipathMode != 0 && _allowTcpFallbackRelay) {
|
if (_multipathMode != 0 && _allowTcpFallbackRelay) {
|
||||||
fprintf(stderr,"WARNING: multipathMode cannot be used with allowTcpFallbackRelay. Disabling allowTcpFallbackRelay");
|
fprintf(stderr,"WARNING: multipathMode cannot be used with allowTcpFallbackRelay. Disabling allowTcpFallbackRelay");
|
||||||
_allowTcpFallbackRelay = false;
|
_allowTcpFallbackRelay = false;
|
||||||
|
|
Loading…
Add table
Reference in a new issue