From a40b8c07f49bd9ad2748430eb9e79680059458fd Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Sat, 7 Sep 2013 15:49:38 -0400 Subject: [PATCH] Apply multicast rate limits to my own multicasts. Will run locally and on a variety of system types to test the result of this. --- node/BandwidthAccount.hpp | 9 ++++++--- node/Network.cpp | 3 ++- node/Network.hpp | 6 +++--- node/Switch.cpp | 9 +++++++-- 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/node/BandwidthAccount.hpp b/node/BandwidthAccount.hpp index 927037f8f..42e68bfe8 100644 --- a/node/BandwidthAccount.hpp +++ b/node/BandwidthAccount.hpp @@ -32,6 +32,7 @@ #include #include +#include #include "Constants.hpp" #include "Utils.hpp" @@ -97,15 +98,17 @@ public: * Update balance by accruing and then deducting * * @param deduct Amount to deduct, or 0.0 to just update - * @return New balance with deduction applied + * @return New balance with deduction applied, and whether or not deduction fit */ - inline int32_t update(int32_t deduct) + inline std::pair update(int32_t deduct) throw() { double lt = _lastTime; double now = Utils::nowf(); _lastTime = now; - return (_balance = std::max(_minBalance,std::min(_maxBalance,(int32_t)round(((double)_balance) + (((double)_accrual) * (now - lt))) - deduct))); + int32_t newbal = (int32_t)round((double)_balance + ((double)_accrual * (now - lt))) - deduct; + bool fits = (newbal > 0); + return std::pair((_balance = std::max(_minBalance,std::min(_maxBalance,newbal))),fits); } private: diff --git a/node/Network.cpp b/node/Network.cpp index 60f87f927..b16daaf3e 100644 --- a/node/Network.cpp +++ b/node/Network.cpp @@ -91,7 +91,7 @@ bool Network::Certificate::qualifyMembership(const Network::Certificate &mc) con } // A low default global rate, fast enough for something like ARP -const Network::MulticastRates::Rate Network::MulticastRates::GLOBAL_DEFAULT_RATE(256.0,-32.0,256.0,64.0); +const Network::MulticastRates::Rate Network::MulticastRates::GLOBAL_DEFAULT_RATE(128,-32,128,64); const char *Network::statusString(const Status s) throw() @@ -154,6 +154,7 @@ void Network::setConfiguration(const Network::Config &conf) //TRACE("network %.16llx got netconf:\n%s",(unsigned long long)_id,conf.toString().c_str()); _configuration = conf; _myCertificate = conf.certificateOfMembership(); + _mcRates = conf.multicastRates(); _lastConfigUpdate = Utils::now(); _tap->setIps(conf.staticAddresses()); diff --git a/node/Network.hpp b/node/Network.hpp index 880355866..2d90dac77 100644 --- a/node/Network.hpp +++ b/node/Network.hpp @@ -553,7 +553,7 @@ public: MulticastRates::Rate r(_mcRates.get(mg)); bal = _multicastRateAccounts.insert(std::make_pair(k,BandwidthAccount(r.preload,r.minBalance,r.maxBalance,r.accrual))).first; } - return (bal->second.update((int32_t)bytes) < (int32_t)bytes); + return bal->second.update((int32_t)bytes).second; } private: @@ -574,8 +574,8 @@ private: // Configuration from network master node Config _configuration; - Certificate _myCertificate; - MulticastRates _mcRates; + Certificate _myCertificate; // memoized from _configuration + MulticastRates _mcRates; // memoized from _configuration // Ethertype whitelist bit field, set from config, for really fast lookup unsigned char _etWhitelist[65536 / 8]; diff --git a/node/Switch.cpp b/node/Switch.cpp index 157ecec8d..5ba56555a 100644 --- a/node/Switch.cpp +++ b/node/Switch.cpp @@ -100,13 +100,18 @@ void Switch::onLocalEthernet(const SharedPtr &network,const MAC &from,c MulticastGroup mg(to,0); if (to.isBroadcast()) { - // Handle broadcast special cases - // Cram IPv4 IP into ADI field to make IPv4 ARP broadcast channel specific and scalable if ((etherType == ZT_ETHERTYPE_ARP)&&(data.size() == 28)&&(data[2] == 0x08)&&(data[3] == 0x00)&&(data[4] == 6)&&(data[5] == 4)&&(data[7] == 0x01)) mg = MulticastGroup::deriveMulticastGroupForAddressResolution(InetAddress(data.field(24,4),4,0)); } + // Check our own multicasts against the global rate for this network + // just to be polite. + if (!network->updateAndCheckMulticastBalance(_r->identity.address(),mg,data.size())) { + LOG("didn't send local multicast %u byte multicast packet to network %.16llx: not within budget for multicast group %s",(unsigned int)data.size(),(unsigned long long)network->id(),mg.toString().c_str()); + return; + } + Multicaster::MulticastBloomFilter bloom; SharedPtr propPeers[ZT_MULTICAST_PROPAGATION_BREADTH]; unsigned int np = _r->multicaster->pickSocialPropagationPeers(