Path.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2024-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_PATH_HPP
  14. #define ZT_PATH_HPP
  15. #include "Constants.hpp"
  16. #include "InetAddress.hpp"
  17. #include "SharedPtr.hpp"
  18. #include "Utils.hpp"
  19. #include "Mutex.hpp"
  20. #include "Meter.hpp"
  21. #include "Containers.hpp"
  22. namespace ZeroTier {
  23. class RuntimeEnvironment;
  24. template<unsigned int MF, unsigned int MFP, unsigned int GCT, unsigned int GCS, typename P>
  25. class Defragmenter;
  26. /**
  27. * A path across the physical network
  28. */
  29. class Path
  30. {
  31. friend class SharedPtr<Path>;
  32. // Allow defragmenter to access fragment-in-flight info stored in Path for performance reasons.
  33. template<unsigned int MF, unsigned int MFP, unsigned int GCT, unsigned int GCS, typename P>
  34. friend
  35. class Defragmenter;
  36. public:
  37. ZT_INLINE Path(const int64_t l, const InetAddress &r) noexcept:
  38. m_localSocket(l),
  39. m_lastIn(0),
  40. m_lastOut(0),
  41. m_latency(-1),
  42. m_addr(r)
  43. {
  44. }
  45. /**
  46. * Send a packet via this path (last out time is also updated)
  47. *
  48. * @param RR Runtime environment
  49. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  50. * @param data Packet data
  51. * @param len Packet length
  52. * @param now Current time
  53. * @return True if transport reported success
  54. */
  55. bool send(const RuntimeEnvironment *RR, void *tPtr, const void *data, unsigned int len, int64_t now) noexcept;
  56. /**
  57. * Explicitly update last sent time
  58. *
  59. * @param now Time of send
  60. * @param bytes Bytes sent
  61. */
  62. ZT_INLINE void sent(const int64_t now, const unsigned int bytes) noexcept
  63. {
  64. m_lastOut.store(now);
  65. m_outMeter.log(now, bytes);
  66. }
  67. /**
  68. * Called when a packet is received from this remote path, regardless of content
  69. *
  70. * @param now Time of receive
  71. * @param bytes Bytes received
  72. */
  73. ZT_INLINE void received(const int64_t now, const unsigned int bytes) noexcept
  74. {
  75. m_lastIn.store(now);
  76. m_inMeter.log(now, bytes);
  77. }
  78. /**
  79. * Update latency with a new measurement
  80. *
  81. * @param newMeasurement New latency measurement in milliseconds
  82. */
  83. ZT_INLINE void updateLatency(const unsigned int newMeasurement) noexcept
  84. {
  85. int lat = m_latency;
  86. if (lat > 0) {
  87. m_latency = (lat + newMeasurement) / 2;
  88. } else {
  89. m_latency = newMeasurement;
  90. }
  91. }
  92. /**
  93. * @return Latency in milliseconds or -1 if unknown
  94. */
  95. ZT_INLINE int latency() const noexcept
  96. { return m_latency; }
  97. /**
  98. * Check path aliveness
  99. *
  100. * @param now Current time
  101. */
  102. ZT_INLINE bool alive(const int64_t now) const noexcept
  103. { return ((now - m_lastIn.load()) < ZT_PATH_ALIVE_TIMEOUT); }
  104. /**
  105. * @return Physical address
  106. */
  107. ZT_INLINE const InetAddress &address() const noexcept
  108. { return m_addr; }
  109. /**
  110. * @return Local socket as specified by external code
  111. */
  112. ZT_INLINE int64_t localSocket() const noexcept
  113. { return m_localSocket; }
  114. /**
  115. * @return Last time we received anything
  116. */
  117. ZT_INLINE int64_t lastIn() const noexcept
  118. { return m_lastIn.load(); }
  119. /**
  120. * @return Last time we sent something
  121. */
  122. ZT_INLINE int64_t lastOut() const noexcept
  123. { return m_lastOut.load(); }
  124. private:
  125. const int64_t m_localSocket;
  126. std::atomic<int64_t> m_lastIn;
  127. std::atomic<int64_t> m_lastOut;
  128. std::atomic<int> m_latency;
  129. const InetAddress m_addr;
  130. Meter<> m_inMeter;
  131. Meter<> m_outMeter;
  132. // These fields belong to Defragmenter but are kept in Path for performance
  133. // as it's much faster this way than having Defragmenter maintain another
  134. // mapping from paths to inbound message IDs.
  135. Set<uint64_t> m_inboundFragmentedMessages;
  136. Mutex m_inboundFragmentedMessages_l;
  137. std::atomic<int> __refCount;
  138. };
  139. } // namespace ZeroTier
  140. #endif