Path.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  4. *
  5. * (c) ZeroTier, Inc.
  6. * https://www.zerotier.com/
  7. */
  8. #ifndef ZT_PATH_HPP
  9. #define ZT_PATH_HPP
  10. #include "AtomicCounter.hpp"
  11. #include "Constants.hpp"
  12. #include "InetAddress.hpp"
  13. #include "Packet.hpp"
  14. #include "RingBuffer.hpp"
  15. #include "SharedPtr.hpp"
  16. #include "Utils.hpp"
  17. #include <algorithm>
  18. #include <stdexcept>
  19. #include <stdint.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. /**
  23. * Maximum return value of preferenceRank()
  24. */
  25. #define ZT_PATH_MAX_PREFERENCE_RANK ((ZT_INETADDRESS_MAX_SCOPE << 1) | 1)
  26. namespace ZeroTier {
  27. class RuntimeEnvironment;
  28. /**
  29. * A path across the physical network
  30. */
  31. class Path {
  32. friend class SharedPtr<Path>;
  33. friend class Bond;
  34. public:
  35. /**
  36. * Efficient unique key for paths in a Hashtable
  37. */
  38. class HashKey {
  39. public:
  40. HashKey()
  41. {
  42. }
  43. HashKey(const int64_t l, const InetAddress& r)
  44. {
  45. if (r.ss_family == AF_INET) {
  46. _k[0] = (uint64_t)reinterpret_cast<const struct sockaddr_in*>(&r)->sin_addr.s_addr;
  47. _k[1] = (uint64_t)reinterpret_cast<const struct sockaddr_in*>(&r)->sin_port;
  48. _k[2] = (uint64_t)l;
  49. }
  50. else if (r.ss_family == AF_INET6) {
  51. memcpy(_k, reinterpret_cast<const struct sockaddr_in6*>(&r)->sin6_addr.s6_addr, 16);
  52. _k[2] = ((uint64_t)reinterpret_cast<const struct sockaddr_in6*>(&r)->sin6_port << 32) ^ (uint64_t)l;
  53. }
  54. else {
  55. memcpy(_k, &r, std::min(sizeof(_k), sizeof(InetAddress)));
  56. _k[2] += (uint64_t)l;
  57. }
  58. }
  59. inline unsigned long hashCode() const
  60. {
  61. return (unsigned long)(_k[0] + _k[1] + _k[2]);
  62. }
  63. inline bool operator==(const HashKey& k) const
  64. {
  65. return ((_k[0] == k._k[0]) && (_k[1] == k._k[1]) && (_k[2] == k._k[2]));
  66. }
  67. inline bool operator!=(const HashKey& k) const
  68. {
  69. return (! (*this == k));
  70. }
  71. private:
  72. uint64_t _k[3];
  73. };
  74. Path()
  75. : _lastOut(0)
  76. , _lastIn(0)
  77. , _lastTrustEstablishedPacketReceived(0)
  78. , _lastEchoRequestReceived(0)
  79. , _localPort(0)
  80. , _localSocket(-1)
  81. , _latencyMean(0.0)
  82. , _latencyVariance(0.0)
  83. , _packetLossRatio(0.0)
  84. , _packetErrorRatio(0.0)
  85. , _assignedFlowCount(0)
  86. , _valid(true)
  87. , _eligible(false)
  88. , _bonded(false)
  89. , _mtu(0)
  90. , _givenLinkSpeed(0)
  91. , _relativeQuality(0)
  92. , _latency(0xffff)
  93. , _addr()
  94. , _ipScope(InetAddress::IP_SCOPE_NONE)
  95. {
  96. }
  97. Path(const int64_t localSocket, const InetAddress& addr)
  98. : _lastOut(0)
  99. , _lastIn(0)
  100. , _lastTrustEstablishedPacketReceived(0)
  101. , _lastEchoRequestReceived(0)
  102. , _localPort(0)
  103. , _localSocket(localSocket)
  104. , _latencyMean(0.0)
  105. , _latencyVariance(0.0)
  106. , _packetLossRatio(0.0)
  107. , _packetErrorRatio(0.0)
  108. , _assignedFlowCount(0)
  109. , _valid(true)
  110. , _eligible(false)
  111. , _bonded(false)
  112. , _mtu(0)
  113. , _givenLinkSpeed(0)
  114. , _relativeQuality(0)
  115. , _latency(0xffff)
  116. , _addr(addr)
  117. , _ipScope(addr.ipScope())
  118. {
  119. }
  120. /**
  121. * Called when a packet is received from this remote path, regardless of content
  122. *
  123. * @param t Time of receive
  124. */
  125. inline void received(const uint64_t t)
  126. {
  127. _lastIn = t;
  128. }
  129. /**
  130. * Set time last trusted packet was received (done in Peer::received())
  131. */
  132. inline void trustedPacketReceived(const uint64_t t)
  133. {
  134. _lastTrustEstablishedPacketReceived = t;
  135. }
  136. /**
  137. * Send a packet via this path (last out time is also updated)
  138. *
  139. * @param RR Runtime environment
  140. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  141. * @param data Packet data
  142. * @param len Packet length
  143. * @param now Current time
  144. * @return True if transport reported success
  145. */
  146. bool send(const RuntimeEnvironment* RR, void* tPtr, const void* data, unsigned int len, int64_t now);
  147. /**
  148. * Manually update last sent time
  149. *
  150. * @param t Time of send
  151. */
  152. inline void sent(const int64_t t)
  153. {
  154. _lastOut = t;
  155. }
  156. /**
  157. * Update path latency with a new measurement
  158. *
  159. * @param l Measured latency
  160. */
  161. inline void updateLatency(const unsigned int l, int64_t now)
  162. {
  163. unsigned int pl = _latency;
  164. if (pl < 0xffff) {
  165. _latency = (pl + l) / 2;
  166. }
  167. else {
  168. _latency = l;
  169. }
  170. }
  171. /**
  172. * @return Local socket as specified by external code
  173. */
  174. inline int64_t localSocket() const
  175. {
  176. return _localSocket;
  177. }
  178. /**
  179. * @return Local port corresponding to the localSocket
  180. */
  181. inline int64_t localPort() const
  182. {
  183. return _localPort;
  184. }
  185. /**
  186. * @return Physical address
  187. */
  188. inline const InetAddress& address() const
  189. {
  190. return _addr;
  191. }
  192. /**
  193. * @return IP scope -- faster shortcut for address().ipScope()
  194. */
  195. inline InetAddress::IpScope ipScope() const
  196. {
  197. return _ipScope;
  198. }
  199. /**
  200. * @return True if path has received a trust established packet (e.g. common network membership) in the past ZT_TRUST_EXPIRATION ms
  201. */
  202. inline bool trustEstablished(const int64_t now) const
  203. {
  204. return ((now - _lastTrustEstablishedPacketReceived) < ZT_TRUST_EXPIRATION);
  205. }
  206. /**
  207. * @return Preference rank, higher == better
  208. */
  209. inline unsigned int preferenceRank() const
  210. {
  211. // This causes us to rank paths in order of IP scope rank (see InetAddress.hpp) but
  212. // within each IP scope class to prefer IPv6 over IPv4.
  213. return (((unsigned int)_ipScope << 1) | (unsigned int)(_addr.ss_family == AF_INET6));
  214. }
  215. /**
  216. * Check whether this address is valid for a ZeroTier path
  217. *
  218. * This checks the address type and scope against address types and scopes
  219. * that we currently support for ZeroTier communication.
  220. *
  221. * @param a Address to check
  222. * @return True if address is good for ZeroTier path use
  223. */
  224. static inline bool isAddressValidForPath(const InetAddress& a)
  225. {
  226. if ((a.ss_family == AF_INET) || (a.ss_family == AF_INET6)) {
  227. switch (a.ipScope()) {
  228. /* Note: we don't do link-local at the moment. Unfortunately these
  229. * cause several issues. The first is that they usually require a
  230. * device qualifier, which we don't handle yet and can't portably
  231. * push in PUSH_DIRECT_PATHS. The second is that some OSes assign
  232. * these very ephemerally or otherwise strangely. So we'll use
  233. * private, pseudo-private, shared (e.g. carrier grade NAT), or
  234. * global IP addresses. */
  235. case InetAddress::IP_SCOPE_PRIVATE:
  236. case InetAddress::IP_SCOPE_PSEUDOPRIVATE:
  237. case InetAddress::IP_SCOPE_SHARED:
  238. case InetAddress::IP_SCOPE_GLOBAL:
  239. if (a.ss_family == AF_INET6) {
  240. // TEMPORARY HACK: for now, we are going to blacklist he.net IPv6
  241. // tunnels due to very spotty performance and low MTU issues over
  242. // these IPv6 tunnel links.
  243. const uint8_t* ipd = reinterpret_cast<const uint8_t*>(reinterpret_cast<const struct sockaddr_in6*>(&a)->sin6_addr.s6_addr);
  244. if ((ipd[0] == 0x20) && (ipd[1] == 0x01) && (ipd[2] == 0x04) && (ipd[3] == 0x70)) {
  245. return false;
  246. }
  247. }
  248. return true;
  249. default:
  250. return false;
  251. }
  252. }
  253. return false;
  254. }
  255. /**
  256. * @return Latency or 0xffff if unknown
  257. */
  258. inline unsigned int latency() const
  259. {
  260. return _latency;
  261. }
  262. /**
  263. * @return Path quality -- lower is better
  264. */
  265. inline long quality(const int64_t now) const
  266. {
  267. const int l = (long)_latency;
  268. const int age = (long)std::min((now - _lastIn), (int64_t)(ZT_PATH_HEARTBEAT_PERIOD * 10)); // set an upper sanity limit to avoid overflow
  269. return (((age < (ZT_PATH_HEARTBEAT_PERIOD + 5000)) ? l : (l + 0xffff + age)) * (long)((ZT_INETADDRESS_MAX_SCOPE - _ipScope) + 1));
  270. }
  271. /**
  272. * @return True if this path is alive (receiving heartbeats)
  273. */
  274. inline bool alive(const int64_t now) const
  275. {
  276. return (now - _lastIn) < (ZT_PATH_HEARTBEAT_PERIOD + 5000);
  277. }
  278. /**
  279. * @return True if this path needs a heartbeat
  280. */
  281. inline bool needsHeartbeat(const int64_t now) const
  282. {
  283. return ((now - _lastOut) >= ZT_PATH_HEARTBEAT_PERIOD);
  284. }
  285. /**
  286. * @return Last time we sent something
  287. */
  288. inline int64_t lastOut() const
  289. {
  290. return _lastOut;
  291. }
  292. /**
  293. * @return Last time we received anything
  294. */
  295. inline int64_t lastIn() const
  296. {
  297. return _lastIn;
  298. }
  299. /**
  300. * @return the age of the path in terms of receiving packets
  301. */
  302. inline int64_t age(int64_t now)
  303. {
  304. return (now - _lastIn);
  305. }
  306. /**
  307. * @return Time last trust-established packet was received
  308. */
  309. inline int64_t lastTrustEstablishedPacketReceived() const
  310. {
  311. return _lastTrustEstablishedPacketReceived;
  312. }
  313. /**
  314. * Rate limit gate for inbound ECHO requests
  315. */
  316. inline bool rateGateEchoRequest(const int64_t now)
  317. {
  318. if ((now - _lastEchoRequestReceived) >= (ZT_PEER_GENERAL_RATE_LIMIT / 6)) {
  319. _lastEchoRequestReceived = now;
  320. return true;
  321. }
  322. return false;
  323. }
  324. /**
  325. * @return Mean latency as reported by the bonding layer
  326. */
  327. inline float latencyMean() const
  328. {
  329. return _latencyMean;
  330. }
  331. /**
  332. * @return Latency variance as reported by the bonding layer
  333. */
  334. inline float latencyVariance() const
  335. {
  336. return _latencyVariance;
  337. }
  338. /**
  339. * @return Packet Loss Ratio as reported by the bonding layer
  340. */
  341. inline float packetLossRatio() const
  342. {
  343. return _packetLossRatio;
  344. }
  345. /**
  346. * @return Packet Error Ratio as reported by the bonding layer
  347. */
  348. inline float packetErrorRatio() const
  349. {
  350. return _packetErrorRatio;
  351. }
  352. /**
  353. * @return Number of flows assigned to this path
  354. */
  355. inline unsigned int assignedFlowCount() const
  356. {
  357. return _assignedFlowCount;
  358. }
  359. /**
  360. * @return Whether this path is valid as reported by the bonding layer. The bonding layer
  361. * actually checks with Phy to see if the interface is still up
  362. */
  363. inline bool valid() const
  364. {
  365. return _valid;
  366. }
  367. /**
  368. * @return Whether this path is eligible for use in a bond as reported by the bonding layer
  369. */
  370. inline bool eligible() const
  371. {
  372. return _eligible;
  373. }
  374. /**
  375. * @return Whether this path is bonded as reported by the bonding layer
  376. */
  377. inline bool bonded() const
  378. {
  379. return _bonded;
  380. }
  381. /**
  382. * @return Whether the user-specified MTU for this path (determined by MTU for parent link)
  383. */
  384. inline uint16_t mtu() const
  385. {
  386. return _mtu;
  387. }
  388. /**
  389. * @return Given link capacity as reported by the bonding layer
  390. */
  391. inline uint32_t givenLinkSpeed() const
  392. {
  393. return _givenLinkSpeed;
  394. }
  395. /**
  396. * @return Path's quality as reported by the bonding layer
  397. */
  398. inline float relativeQuality() const
  399. {
  400. return _relativeQuality;
  401. }
  402. /**
  403. * @return Physical interface name that this path lives on
  404. */
  405. char* ifname()
  406. {
  407. return _ifname;
  408. }
  409. private:
  410. char _ifname[ZT_MAX_PHYSIFNAME] = {};
  411. volatile int64_t _lastOut;
  412. volatile int64_t _lastIn;
  413. volatile int64_t _lastTrustEstablishedPacketReceived;
  414. int64_t _lastEchoRequestReceived;
  415. uint16_t _localPort;
  416. int64_t _localSocket;
  417. volatile float _latencyMean;
  418. volatile float _latencyVariance;
  419. volatile float _packetLossRatio;
  420. volatile float _packetErrorRatio;
  421. volatile uint16_t _assignedFlowCount;
  422. volatile bool _valid;
  423. volatile bool _eligible;
  424. volatile bool _bonded;
  425. volatile uint16_t _mtu;
  426. volatile uint32_t _givenLinkSpeed;
  427. volatile float _relativeQuality;
  428. volatile unsigned int _latency;
  429. InetAddress _addr;
  430. InetAddress::IpScope _ipScope; // memoize this since it's a computed value checked often
  431. AtomicCounter __refCount;
  432. };
  433. } // namespace ZeroTier
  434. #endif