Flow.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. /**
  24. * @param flowId Given flow ID
  25. * @param now Current time
  26. */
  27. Flow(int32_t flowId, int64_t now) :
  28. _flowId(flowId),
  29. _bytesInPerUnitTime(0),
  30. _bytesOutPerUnitTime(0),
  31. _lastActivity(now),
  32. _lastPathReassignment(0),
  33. _assignedPath(SharedPtr<Path>())
  34. {}
  35. /**
  36. * Reset flow statistics
  37. */
  38. void resetByteCounts()
  39. {
  40. _bytesInPerUnitTime = 0;
  41. _bytesOutPerUnitTime = 0;
  42. }
  43. /**
  44. * @return The Flow's ID
  45. */
  46. int32_t id() { return _flowId; }
  47. /**
  48. * @return Number of incoming bytes processed on this flow per unit time
  49. */
  50. int64_t bytesInPerUnitTime() { return _bytesInPerUnitTime; }
  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) { _bytesInPerUnitTime += bytes; }
  57. /**
  58. * @return Number of outgoing bytes processed on this flow per unit time
  59. */
  60. int64_t bytesOutPerUnitTime() { return _bytesOutPerUnitTime; }
  61. /**
  62. * Record number of outgoing bytes on this flow
  63. *
  64. * @param bytes
  65. */
  66. void recordOutgoingBytes(uint64_t bytes) { _bytesOutPerUnitTime += bytes; }
  67. /**
  68. * @return The total number of bytes processed on this flow
  69. */
  70. uint64_t totalBytes() { return _bytesInPerUnitTime + _bytesOutPerUnitTime; }
  71. /**
  72. * How long since a packet was sent or received in this flow
  73. *
  74. * @param now Current time
  75. * @return The age of the flow in terms of last recorded activity
  76. */
  77. int64_t age(int64_t now) { return now - _lastActivity; }
  78. /**
  79. * Record that traffic was processed on this flow at the given time.
  80. *
  81. * @param now Current time
  82. */
  83. void updateActivity(int64_t now) { _lastActivity = now; }
  84. /**
  85. * @return Path assigned to this flow
  86. */
  87. SharedPtr<Path> assignedPath() { return _assignedPath; }
  88. /**
  89. * @param path Assigned path over which this flow should be handled
  90. */
  91. void assignPath(const SharedPtr<Path> &path, int64_t now) {
  92. _assignedPath = path;
  93. _lastPathReassignment = now;
  94. }
  95. AtomicCounter __refCount;
  96. int32_t _flowId;
  97. uint64_t _bytesInPerUnitTime;
  98. uint64_t _bytesOutPerUnitTime;
  99. int64_t _lastActivity;
  100. int64_t _lastPathReassignment;
  101. SharedPtr<Path> _assignedPath;
  102. SharedPtr<Path> _previouslyAssignedPath;
  103. };
  104. } // namespace ZeroTier
  105. #endif