Flow.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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: 2025-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_FLOW_HPP
  14. #define ZT_FLOW_HPP
  15. #include "Path.hpp"
  16. #include "SharedPtr.hpp"
  17. namespace ZeroTier {
  18. /**
  19. * A protocol flow that is identified by the origin and destination port.
  20. */
  21. struct Flow {
  22. /**
  23. * @param flowId Given flow ID
  24. * @param now Current time
  25. */
  26. Flow(int32_t flowId, int64_t now) : _flowId(flowId), _bytesInPerUnitTime(0), _bytesOutPerUnitTime(0), _lastActivity(now), _lastPathReassignment(0), _assignedPath(SharedPtr<Path>())
  27. {
  28. }
  29. /**
  30. * Reset flow statistics
  31. */
  32. void resetByteCounts()
  33. {
  34. _bytesInPerUnitTime = 0;
  35. _bytesOutPerUnitTime = 0;
  36. }
  37. /**
  38. * @return The Flow's ID
  39. */
  40. int32_t id()
  41. {
  42. return _flowId;
  43. }
  44. /**
  45. * @return Number of incoming bytes processed on this flow per unit time
  46. */
  47. int64_t bytesInPerUnitTime()
  48. {
  49. return _bytesInPerUnitTime;
  50. }
  51. /**
  52. * Record number of incoming bytes on this flow
  53. *
  54. * @param bytes Number of incoming bytes
  55. */
  56. void recordIncomingBytes(uint64_t bytes)
  57. {
  58. _bytesInPerUnitTime += bytes;
  59. }
  60. /**
  61. * @return Number of outgoing bytes processed on this flow per unit time
  62. */
  63. int64_t bytesOutPerUnitTime()
  64. {
  65. return _bytesOutPerUnitTime;
  66. }
  67. /**
  68. * Record number of outgoing bytes on this flow
  69. *
  70. * @param bytes
  71. */
  72. void recordOutgoingBytes(uint64_t bytes)
  73. {
  74. _bytesOutPerUnitTime += bytes;
  75. }
  76. /**
  77. * @return The total number of bytes processed on this flow
  78. */
  79. uint64_t totalBytes()
  80. {
  81. return _bytesInPerUnitTime + _bytesOutPerUnitTime;
  82. }
  83. /**
  84. * How long since a packet was sent or received in this flow
  85. *
  86. * @param now Current time
  87. * @return The age of the flow in terms of last recorded activity
  88. */
  89. int64_t age(int64_t now)
  90. {
  91. return now - _lastActivity;
  92. }
  93. /**
  94. * Record that traffic was processed on this flow at the given time.
  95. *
  96. * @param now Current time
  97. */
  98. void updateActivity(int64_t now)
  99. {
  100. _lastActivity = now;
  101. }
  102. /**
  103. * @return Path assigned to this flow
  104. */
  105. SharedPtr<Path> assignedPath()
  106. {
  107. return _assignedPath;
  108. }
  109. /**
  110. * @param path Assigned path over which this flow should be handled
  111. */
  112. void assignPath(const SharedPtr<Path>& path, int64_t now)
  113. {
  114. _assignedPath = path;
  115. _lastPathReassignment = now;
  116. }
  117. AtomicCounter __refCount;
  118. int32_t _flowId;
  119. uint64_t _bytesInPerUnitTime;
  120. uint64_t _bytesOutPerUnitTime;
  121. int64_t _lastActivity;
  122. int64_t _lastPathReassignment;
  123. SharedPtr<Path> _assignedPath;
  124. SharedPtr<Path> _previouslyAssignedPath;
  125. };
  126. } // namespace ZeroTier
  127. #endif