Added VERB_ACK and VERB_QOS_MEASUREMENT, refined notion of path quality

This commit is contained in:
Joseph Henry 2018-05-30 17:45:29 -07:00
parent 8199dbd0dc
commit 46a7a2be2e
18 changed files with 954 additions and 720 deletions

View file

@ -27,8 +27,6 @@
#ifndef ZT_PHY_HPP
#define ZT_PHY_HPP
#include "../osdep/OSUtils.hpp"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -88,22 +86,6 @@ namespace ZeroTier {
*/
typedef void PhySocket;
struct link_test_record
{
link_test_record(PhySocket *_s, uint64_t _id, uint64_t _egress_time, uint32_t _length) :
s(_s),
id(_id),
egress_time(_egress_time),
length(_length)
{
//
}
PhySocket *s;
uint64_t id;
uint64_t egress_time;
uint32_t length;
};
/**
* Simple templated non-blocking sockets implementation
*
@ -170,19 +152,13 @@ private:
ZT_PHY_SOCKET_UNIX_LISTEN = 0x08
};
struct PhySocketImpl
{
PhySocketImpl() :
throughput(0)
{
memset(ifname, 0, sizeof(ifname));
}
struct PhySocketImpl {
PhySocketImpl() { memset(ifname, 0, sizeof(ifname)); }
PhySocketType type;
ZT_PHY_SOCKFD_TYPE sock;
void *uptr; // user-settable pointer
ZT_PHY_SOCKADDR_STORAGE_TYPE saddr; // remote for TCP_OUT and TCP_IN, local for TCP_LISTEN, RAW, and UDP
char ifname[16];
uint64_t throughput;
};
std::list<PhySocketImpl> _socks;
@ -198,7 +174,6 @@ private:
bool _noDelay;
bool _noCheck;
std::vector<struct link_test_record*> link_test_records;
public:
/**
@ -282,7 +257,9 @@ public:
*/
static inline void getIfName(PhySocket *s, char *nameBuf, int buflen)
{
memcpy(nameBuf, reinterpret_cast<PhySocketImpl *>(s)->ifname, buflen);
if (s) {
memcpy(nameBuf, reinterpret_cast<PhySocketImpl *>(s)->ifname, buflen);
}
}
/**
@ -292,18 +269,9 @@ public:
*/
static inline void setIfName(PhySocket *s, char *ifname, int len)
{
memcpy(&(reinterpret_cast<PhySocketImpl *>(s)->ifname), ifname, len);
}
/**
* Get result of most recent throughput test
*
* @param s Socket object
*/
inline uint64_t getThroughput(PhySocket *s)
{
PhySocketImpl *sws = (reinterpret_cast<PhySocketImpl *>(s));
return sws ? sws->throughput : 0;
if (s) {
memcpy(&(reinterpret_cast<PhySocketImpl *>(s)->ifname), ifname, len);
}
}
/**
@ -339,105 +307,9 @@ public:
*/
inline bool isValidState(PhySocket *s)
{
PhySocketImpl *sws = (reinterpret_cast<PhySocketImpl *>(s));
return sws->type >= ZT_PHY_SOCKET_CLOSED && sws->type <= ZT_PHY_SOCKET_UNIX_LISTEN;
}
/**
* Send a datagram of a known size to a selected peer and record egress time. The peer
* shall eventually respond by echoing back a smaller datagram.
*
* @param s Socket object
* @param remoteAddress Address of remote peer to receive link test packet
* @param data Buffer containing random packet data
* @param len Length of packet data buffer
* @return Number of bytes successfully written to socket
*/
inline int test_link_speed(PhySocket *s, const struct sockaddr *to, void *data, uint32_t len) {
if (!reinterpret_cast<PhySocketImpl *>(s)) {
return 0;
}
uint64_t *buf = (uint64_t*)data;
uint64_t id = buf[0];
if (to->sa_family != AF_INET && to->sa_family != AF_INET6) {
return 0;
}
uint64_t egress_time = OSUtils::now();
PhySocketImpl *sws = (reinterpret_cast<PhySocketImpl *>(s));
#if defined(_WIN32) || defined(_WIN64)
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
int w = ::sendto(sws->sock,data,len,0,to,(to->sa_family == AF_INET6) ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in));
#endif
if (w > 0) {
link_test_records.push_back(new link_test_record(s, id, egress_time, len));
}
return w;
}
/**
* Remove link speed test records which have timed-out and record a 0 bits/s measurement
*/
inline void refresh_link_speed_records()
{
for(size_t i=0;i<link_test_records.size();i++) {
if(OSUtils::now() - link_test_records[i]->egress_time > ZT_LINK_TEST_TIMEOUT) {
PhySocketImpl *sws = (reinterpret_cast<PhySocketImpl *>(link_test_records[i]->s));
if (sws) {
sws->throughput = 0;
}
link_test_records.erase(link_test_records.begin() + i);
}
}
}
/**
* Upon receipt of a link speed test datagram we echo back only the identification portion
*
* @param s Socket object
* @param from Address of remote peer that sent this datagram
* @param data Buffer containing datagram's contents
* @param len Length of datagram
* @return Number of bytes successfully written to socket in response
*/
inline int respond_to_link_test(PhySocket *s,const struct sockaddr *from,void *data,unsigned long len) {
PhySocketImpl *sws = (reinterpret_cast<PhySocketImpl *>(s));
uint64_t *id = (uint64_t*)data;
#if defined(_WIN32) || defined(_WIN64)
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
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
return w;
}
/**
* Upon receipt of a response to our original link test datagram, correlate this new datagram with the record
* of the one we sent. Compute the transit time and update the throughput field of the relevant socket. This
* value will later be read by the path quality estimation logic located in Path.hpp.
*
* @param s Socket object
* @param from Address of remote peer that sent this datagram
* @param data Buffer containing datagram contents (ID of original link test datagram)
* @param len Length of datagram
* @return true if datagram correponded to previous record, false if otherwise
*/
inline bool handle_link_test_response(PhySocket *s,const struct sockaddr *from,void *data,unsigned long len) {
uint64_t *id = (uint64_t*)data;
for(size_t i=0;i<link_test_records.size();i++) {
if(link_test_records[i]->id == id[0]) {
float rtt = (OSUtils::now()-link_test_records[i]->egress_time) / (float)1000; // s
uint32_t sz = (link_test_records[i]->length) * 8; // bits
float transit_time = rtt / (float)2.0;
uint64_t raw = (uint64_t)(sz / transit_time);
PhySocketImpl *sws = (reinterpret_cast<PhySocketImpl *>(s));
if (sws) {
sws->throughput = raw;
}
delete link_test_records[i];
link_test_records.erase(link_test_records.begin() + i);
return true;
}
if (s) {
PhySocketImpl *sws = (reinterpret_cast<PhySocketImpl *>(s));
return sws->type >= ZT_PHY_SOCKET_CLOSED && sws->type <= ZT_PHY_SOCKET_UNIX_LISTEN;
}
return false;
}