2013-08-08 21:20:35 +00:00
|
|
|
/*
|
|
|
|
* ZeroTier One - Global Peer to Peer Ethernet
|
|
|
|
* Copyright (C) 2012-2013 ZeroTier Networks LLC
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* --
|
|
|
|
*
|
|
|
|
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
|
|
|
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
|
|
|
*
|
|
|
|
* If you would like to embed ZeroTier into a commercial application or
|
|
|
|
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
|
|
|
* LLC. Start here: http://www.zerotier.com/
|
|
|
|
*/
|
|
|
|
|
2013-09-04 13:27:56 +00:00
|
|
|
#ifndef _ZT_BWACCOUNT_HPP
|
|
|
|
#define _ZT_BWACCOUNT_HPP
|
2013-08-08 21:20:35 +00:00
|
|
|
|
2013-09-07 16:23:53 +00:00
|
|
|
#include <stdint.h>
|
2013-08-08 21:20:35 +00:00
|
|
|
#include <math.h>
|
2013-08-13 01:25:36 +00:00
|
|
|
|
2013-09-07 16:23:53 +00:00
|
|
|
#include <algorithm>
|
2013-09-07 19:49:38 +00:00
|
|
|
#include <utility>
|
2013-09-07 16:23:53 +00:00
|
|
|
|
2013-08-13 01:25:36 +00:00
|
|
|
#include "Constants.hpp"
|
2013-08-08 21:20:35 +00:00
|
|
|
#include "Utils.hpp"
|
|
|
|
|
|
|
|
namespace ZeroTier {
|
|
|
|
|
|
|
|
/**
|
2013-09-04 13:27:56 +00:00
|
|
|
* Bandwidth account used for rate limiting multicast groups
|
2013-08-08 21:20:35 +00:00
|
|
|
*
|
2013-08-30 20:47:54 +00:00
|
|
|
* This is used to apply a bank account model to multicast groups. Each
|
|
|
|
* multicast packet counts against a balance, which accrues at a given
|
|
|
|
* rate in bytes per second. Debt is possible. These parameters are
|
|
|
|
* configurable.
|
|
|
|
*
|
|
|
|
* A bank account model permits bursting behavior, which correctly models
|
|
|
|
* how OSes and apps typically use multicast. It's common for things to
|
|
|
|
* spew lots of multicast messages at once, wait a while, then do it
|
|
|
|
* again. A consistent bandwidth limit model doesn't fit.
|
2013-08-08 21:20:35 +00:00
|
|
|
*/
|
2013-09-04 13:27:56 +00:00
|
|
|
class BandwidthAccount
|
2013-08-08 21:20:35 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
2013-09-04 13:27:56 +00:00
|
|
|
* Create an uninitialized account
|
2013-08-08 21:20:35 +00:00
|
|
|
*
|
|
|
|
* init() must be called before this is used.
|
|
|
|
*/
|
2013-09-04 13:27:56 +00:00
|
|
|
BandwidthAccount() throw() {}
|
2013-08-08 21:20:35 +00:00
|
|
|
|
|
|
|
/**
|
2013-09-04 13:27:56 +00:00
|
|
|
* Create and initialize
|
2013-08-30 20:47:54 +00:00
|
|
|
*
|
2013-08-08 21:20:35 +00:00
|
|
|
* @param preload Initial balance to place in account
|
2013-09-07 16:23:53 +00:00
|
|
|
* @param minb Minimum allowed balance (or maximum debt) (<= 0)
|
|
|
|
* @param maxb Maximum allowed balance (> 0)
|
|
|
|
* @param acc Rate of accrual in bytes per second
|
2013-08-08 21:20:35 +00:00
|
|
|
*/
|
2013-09-07 16:23:53 +00:00
|
|
|
BandwidthAccount(int32_t preload,int32_t minb,int32_t maxb,int32_t acc)
|
2013-08-08 21:20:35 +00:00
|
|
|
throw()
|
|
|
|
{
|
2013-09-07 16:23:53 +00:00
|
|
|
init(preload,minb,maxb,acc);
|
2013-08-08 21:20:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-09-04 13:27:56 +00:00
|
|
|
* Initialize or re-initialize account
|
2013-08-08 21:20:35 +00:00
|
|
|
*
|
|
|
|
* @param preload Initial balance to place in account
|
2013-09-07 16:23:53 +00:00
|
|
|
* @param minb Minimum allowed balance (or maximum debt) (<= 0)
|
|
|
|
* @param maxb Maximum allowed balance (> 0)
|
|
|
|
* @param acc Rate of accrual in bytes per second
|
2013-08-08 21:20:35 +00:00
|
|
|
*/
|
2013-09-07 16:23:53 +00:00
|
|
|
inline void init(int32_t preload,int32_t minb,int32_t maxb,int32_t acc)
|
2013-08-08 21:20:35 +00:00
|
|
|
throw()
|
|
|
|
{
|
|
|
|
_lastTime = Utils::nowf();
|
|
|
|
_balance = preload;
|
2013-09-07 16:23:53 +00:00
|
|
|
_minBalance = minb;
|
|
|
|
_maxBalance = maxb;
|
|
|
|
_accrual = acc;
|
2013-08-08 21:20:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-09-04 13:27:56 +00:00
|
|
|
* Update balance by accruing and then deducting
|
2013-08-08 21:20:35 +00:00
|
|
|
*
|
2013-08-30 18:15:24 +00:00
|
|
|
* @param deduct Amount to deduct, or 0.0 to just update
|
2013-09-07 19:49:38 +00:00
|
|
|
* @return New balance with deduction applied, and whether or not deduction fit
|
2013-08-08 21:20:35 +00:00
|
|
|
*/
|
2013-09-07 19:49:38 +00:00
|
|
|
inline std::pair<int32_t,bool> update(int32_t deduct)
|
2013-08-08 21:20:35 +00:00
|
|
|
throw()
|
|
|
|
{
|
2013-08-09 20:36:58 +00:00
|
|
|
double lt = _lastTime;
|
2013-09-07 16:23:53 +00:00
|
|
|
double now = Utils::nowf();
|
|
|
|
_lastTime = now;
|
2013-09-07 19:49:38 +00:00
|
|
|
int32_t newbal = (int32_t)round((double)_balance + ((double)_accrual * (now - lt))) - deduct;
|
|
|
|
bool fits = (newbal > 0);
|
|
|
|
return std::pair<int32_t,bool>((_balance = std::max(_minBalance,std::min(_maxBalance,newbal))),fits);
|
2013-08-13 19:14:03 +00:00
|
|
|
}
|
|
|
|
|
2013-08-08 21:20:35 +00:00
|
|
|
private:
|
|
|
|
double _lastTime;
|
2013-09-07 16:23:53 +00:00
|
|
|
int32_t _balance;
|
|
|
|
int32_t _minBalance;
|
|
|
|
int32_t _maxBalance;
|
|
|
|
int32_t _accrual;
|
2013-08-08 21:20:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|
|
|
|
|
|
|
|
#endif
|