BandwidthAccount.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #ifndef ZT_BWACCOUNT_HPP
  28. #define ZT_BWACCOUNT_HPP
  29. #include "Constants.hpp"
  30. #include <algorithm>
  31. #include <stdint.h>
  32. #include <math.h>
  33. #include "Utils.hpp"
  34. #ifdef __WINDOWS__
  35. #define round(x) ((x-floor(x))>0.5 ? ceil(x) : floor(x))
  36. #endif
  37. namespace ZeroTier {
  38. /**
  39. * Bandwidth account used for rate limiting multicast groups
  40. *
  41. * This is used to apply a bank account model to multicast groups. Each
  42. * multicast packet counts against a balance, which accrues at a given
  43. * rate in bytes per second. Debt is possible. These parameters are
  44. * configurable.
  45. *
  46. * A bank account model permits bursting behavior, which correctly models
  47. * how OSes and apps typically use multicast. It's common for things to
  48. * spew lots of multicast messages at once, wait a while, then do it
  49. * again. A consistent bandwidth limit model doesn't fit.
  50. */
  51. class BandwidthAccount
  52. {
  53. public:
  54. /**
  55. * Create an uninitialized account
  56. *
  57. * init() must be called before this is used.
  58. */
  59. BandwidthAccount() throw() {}
  60. /**
  61. * Create and initialize
  62. *
  63. * @param preload Initial balance to place in account
  64. * @param maxb Maximum allowed balance (> 0)
  65. * @param acc Rate of accrual in bytes per second
  66. * @param now Current time
  67. */
  68. BandwidthAccount(uint32_t preload,uint32_t maxb,uint32_t acc,uint64_t now)
  69. throw()
  70. {
  71. init(preload,maxb,acc,now);
  72. }
  73. /**
  74. * Initialize or re-initialize account
  75. *
  76. * @param preload Initial balance to place in account
  77. * @param maxb Maximum allowed balance (> 0)
  78. * @param acc Rate of accrual in bytes per second
  79. * @param now Current time
  80. */
  81. inline void init(uint32_t preload,uint32_t maxb,uint32_t acc,uint64_t now)
  82. throw()
  83. {
  84. _lastTime = ((double)now / 1000.0);
  85. _balance = preload;
  86. _maxBalance = maxb;
  87. _accrual = acc;
  88. }
  89. /**
  90. * Update and retrieve balance of this account
  91. *
  92. * @param now Current time
  93. * @return New balance updated from current clock
  94. */
  95. inline uint32_t update(uint64_t now)
  96. throw()
  97. {
  98. double lt = _lastTime;
  99. double nowf = ((double)now / 1000.0);
  100. _lastTime = nowf;
  101. return (_balance = std::min(_maxBalance,(uint32_t)round((double)_balance + ((double)_accrual * (nowf - lt)))));
  102. }
  103. /**
  104. * Update balance and conditionally deduct
  105. *
  106. * If the deduction amount fits, it is deducted after update. Otherwise
  107. * balance is updated and false is returned.
  108. *
  109. * @param amt Amount to deduct
  110. * @param now Current time
  111. * @return True if amount fit within balance and was deducted
  112. */
  113. inline bool deduct(uint32_t amt,uint64_t now)
  114. throw()
  115. {
  116. if (update(now) >= amt) {
  117. _balance -= amt;
  118. return true;
  119. }
  120. return false;
  121. }
  122. /**
  123. * @return Most recent balance without update
  124. */
  125. inline uint32_t balance() const
  126. throw()
  127. {
  128. return _balance;
  129. }
  130. private:
  131. double _lastTime;
  132. uint32_t _balance;
  133. uint32_t _maxBalance;
  134. uint32_t _accrual;
  135. };
  136. } // namespace ZeroTier
  137. #endif