|
@@ -16,14 +16,9 @@
|
|
|
#include "Constants.hpp"
|
|
|
#include "RuntimeEnvironment.hpp"
|
|
|
#include "Multicaster.hpp"
|
|
|
+#include "Network.hpp"
|
|
|
#include "Topology.hpp"
|
|
|
#include "Switch.hpp"
|
|
|
-#include "Packet.hpp"
|
|
|
-#include "Peer.hpp"
|
|
|
-#include "C25519.hpp"
|
|
|
-#include "CertificateOfMembership.hpp"
|
|
|
-#include "Node.hpp"
|
|
|
-#include "Network.hpp"
|
|
|
|
|
|
namespace ZeroTier {
|
|
|
|
|
@@ -37,224 +32,140 @@ void Multicaster::send(
|
|
|
void *tPtr,
|
|
|
int64_t now,
|
|
|
const SharedPtr<Network> &network,
|
|
|
- const Address &origin,
|
|
|
const MulticastGroup &mg,
|
|
|
const MAC &src,
|
|
|
unsigned int etherType,
|
|
|
- const void *data,
|
|
|
+ const unsigned int existingBloomMultiplier,
|
|
|
+ const uint8_t existingBloom[ZT_MULTICAST_BLOOM_FILTER_SIZE_BITS / 8],
|
|
|
+ const void *const data,
|
|
|
unsigned int len)
|
|
|
{
|
|
|
-#if 0
|
|
|
- unsigned long idxbuf[4096];
|
|
|
- unsigned long *indexes = idxbuf;
|
|
|
-
|
|
|
- try {
|
|
|
- Mutex::Lock _l(_groups_m);
|
|
|
- MulticastGroupStatus &gs = _groups[Multicaster::Key(network->id(),mg)];
|
|
|
-
|
|
|
- if (!gs.members.empty()) {
|
|
|
- // Allocate a memory buffer if group is monstrous
|
|
|
- if (gs.members.size() > (sizeof(idxbuf) / sizeof(unsigned long)))
|
|
|
- indexes = new unsigned long[gs.members.size()];
|
|
|
-
|
|
|
- // Generate a random permutation of member indexes
|
|
|
- for(unsigned long i=0;i<gs.members.size();++i)
|
|
|
- indexes[i] = i;
|
|
|
- for(unsigned long i=(unsigned long)gs.members.size()-1;i>0;--i) {
|
|
|
- unsigned long j = (unsigned long)Utils::random() % (i + 1);
|
|
|
- unsigned long tmp = indexes[j];
|
|
|
- indexes[j] = indexes[i];
|
|
|
- indexes[i] = tmp;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- Address activeBridges[ZT_MAX_NETWORK_SPECIALISTS];
|
|
|
- const unsigned int activeBridgeCount = network->config().activeBridges(activeBridges);
|
|
|
- const unsigned int limit = network->config().multicastLimit;
|
|
|
-
|
|
|
- if (gs.members.size() >= limit) {
|
|
|
- // Skip queue if we already have enough members to complete the send operation
|
|
|
- OutboundMulticast out;
|
|
|
+ static const unsigned int PRIMES[16] = { 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53 };
|
|
|
+
|
|
|
+ if (unlikely(len > ZT_MAX_MTU)) return; // sanity check
|
|
|
+
|
|
|
+ const NetworkConfig &config = network->config();
|
|
|
+ if (config.multicastLimit == 0) return; // multicast disabled
|
|
|
+ Address bridges[ZT_MAX_NETWORK_SPECIALISTS],multicastReplicators[ZT_MAX_NETWORK_SPECIALISTS];
|
|
|
+ unsigned int bridgeCount = 0,multicastReplicatorCount = 0;
|
|
|
+ for(unsigned int i=0;i<config.specialistCount;++i) {
|
|
|
+ if ((config.specialists[i] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_ACTIVE_BRIDGE) != 0)
|
|
|
+ bridges[bridgeCount++] = config.specialists[i];
|
|
|
+ if ((config.specialists[i] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_MULTICAST_REPLICATOR) != 0)
|
|
|
+ multicastReplicators[multicastReplicatorCount++] = config.specialists[i];
|
|
|
+ }
|
|
|
|
|
|
- out.init(
|
|
|
- RR,
|
|
|
- now,
|
|
|
- network->id(),
|
|
|
- network->config().disableCompression(),
|
|
|
- limit,
|
|
|
- 1, // we'll still gather a little from peers to keep multicast list fresh
|
|
|
- src,
|
|
|
- mg,
|
|
|
- etherType,
|
|
|
- data,
|
|
|
- len);
|
|
|
+ std::vector< std::pair<int64_t,Address> > recipients;
|
|
|
+ bool needMoar = false;
|
|
|
+ for(unsigned int i=0;i<bridgeCount;++i)
|
|
|
+ recipients.push_back(std::pair<int64_t,Address>(9223372036854775807LL,bridges[i]));
|
|
|
+ {
|
|
|
+ Mutex::Lock l2(_groups_l);
|
|
|
+ _getMembersByTime(network->id(),mg,recipients);
|
|
|
+ }
|
|
|
+ std::sort(recipients.begin() + bridgeCount,recipients.end(),std::greater< std::pair<int64_t,Address> >());
|
|
|
+ recipients.erase(std::unique(recipients.begin(),recipients.end()),recipients.end());
|
|
|
+ if (recipients.size() > config.multicastLimit) {
|
|
|
+ recipients.resize(config.multicastLimit);
|
|
|
+ } else if (recipients.size() < config.multicastLimit) {
|
|
|
+ needMoar = true;
|
|
|
+ }
|
|
|
|
|
|
- unsigned int count = 0;
|
|
|
+ _txQueue_l.lock();
|
|
|
+ _OM *om = &(_txQueue[_txQueuePtr++ % ZT_TX_QUEUE_SIZE]);
|
|
|
+ Mutex::Lock ql(om->lock);
|
|
|
+ _txQueue_l.unlock();
|
|
|
+
|
|
|
+ om->nwid = network->id();
|
|
|
+ om->src = src;
|
|
|
+ om->mg = mg;
|
|
|
+ om->etherType = etherType;
|
|
|
+ om->dataSize = len;
|
|
|
+ memcpy(om->data,data,len);
|
|
|
+
|
|
|
+ if (existingBloom) {
|
|
|
+ om->bloomFilterMultiplier = existingBloomMultiplier;
|
|
|
+ memcpy(om->bloomFilter,existingBloom,sizeof(om->bloomFilter));
|
|
|
+ } else {
|
|
|
+ om->bloomFilterMultiplier = 1;
|
|
|
+ memset(om->bloomFilter,0,sizeof(om->bloomFilter));
|
|
|
+
|
|
|
+ if (recipients.size() > 1) {
|
|
|
+ unsigned int mult = 1;
|
|
|
+ unsigned int bestMultColl = 0xffffffff;
|
|
|
+ for(int k=0;k<16;++k) { // 16 == arbitrary limit on iterations for this search, also must be <= size of PRIMES
|
|
|
+ unsigned int coll = 0;
|
|
|
+ for(std::vector< std::pair<int64_t,Address> >::const_iterator r(recipients.begin());r!=recipients.end();++r) {
|
|
|
+ const unsigned int bfi = mult * (unsigned int)r->second.toInt();
|
|
|
+ const unsigned int byte = (bfi >> 3) % sizeof(om->bloomFilter);
|
|
|
+ const uint8_t bit = 1 << (bfi & 7);
|
|
|
+ coll += ((om->bloomFilter[byte] & bit) != 0);
|
|
|
+ om->bloomFilter[byte] |= bit;
|
|
|
+ }
|
|
|
+ memset(om->bloomFilter,0,sizeof(om->bloomFilter));
|
|
|
|
|
|
- for(unsigned int i=0;i<activeBridgeCount;++i) {
|
|
|
- if ((activeBridges[i] != RR->identity.address())&&(activeBridges[i] != origin)) {
|
|
|
- out.sendOnly(RR,tPtr,activeBridges[i]); // optimization: don't use dedup log if it's a one-pass send
|
|
|
- if (++count >= limit)
|
|
|
+ if (coll <= bestMultColl) {
|
|
|
+ om->bloomFilterMultiplier = mult;
|
|
|
+ if (coll == 0) // perfect score, no need to continue searching
|
|
|
break;
|
|
|
+ bestMultColl = coll;
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- unsigned long idx = 0;
|
|
|
- while ((count < limit)&&(idx < gs.members.size())) {
|
|
|
- const Address ma(gs.members[indexes[idx++]].address);
|
|
|
- if ((std::find(activeBridges,activeBridges + activeBridgeCount,ma) == (activeBridges + activeBridgeCount))&&(ma != origin)) {
|
|
|
- out.sendOnly(RR,tPtr,ma); // optimization: don't use dedup log if it's a one-pass send
|
|
|
- ++count;
|
|
|
- }
|
|
|
- }
|
|
|
- } else {
|
|
|
- if (gs.txQueue.size() >= ZT_TX_QUEUE_SIZE) {
|
|
|
- RR->t->outgoingNetworkFrameDropped(tPtr,network,src,mg.mac(),etherType,0,len,"multicast TX queue is full");
|
|
|
- return;
|
|
|
+ mult = PRIMES[k];
|
|
|
}
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- const unsigned int gatherLimit = (limit - (unsigned int)gs.members.size()) + 1;
|
|
|
-
|
|
|
- if ((gs.members.empty())||((now - gs.lastExplicitGather) >= ZT_MULTICAST_EXPLICIT_GATHER_DELAY)) {
|
|
|
- gs.lastExplicitGather = now;
|
|
|
-
|
|
|
- Address explicitGatherPeers[16];
|
|
|
- unsigned int numExplicitGatherPeers = 0;
|
|
|
-
|
|
|
- explicitGatherPeers[numExplicitGatherPeers++] = network->controller();
|
|
|
-
|
|
|
- /*
|
|
|
- Address ac[ZT_MAX_NETWORK_SPECIALISTS];
|
|
|
- const unsigned int accnt = network->config().alwaysContactAddresses(ac);
|
|
|
- unsigned int shuffled[ZT_MAX_NETWORK_SPECIALISTS];
|
|
|
- for(unsigned int i=0;i<accnt;++i)
|
|
|
- shuffled[i] = i;
|
|
|
- for(unsigned int i=0,k=accnt>>1;i<k;++i) {
|
|
|
- const uint64_t x = Utils::random();
|
|
|
- const unsigned int x1 = shuffled[(unsigned int)x % accnt];
|
|
|
- const unsigned int x2 = shuffled[(unsigned int)(x >> 32) % accnt];
|
|
|
- const unsigned int tmp = shuffled[x1];
|
|
|
- shuffled[x1] = shuffled[x2];
|
|
|
- shuffled[x2] = tmp;
|
|
|
- }
|
|
|
- for(unsigned int i=0;i<accnt;++i) {
|
|
|
- explicitGatherPeers[numExplicitGatherPeers++] = ac[shuffled[i]];
|
|
|
- if (numExplicitGatherPeers == 16)
|
|
|
- break;
|
|
|
- }
|
|
|
- */
|
|
|
+ if (multicastReplicatorCount > 0) {
|
|
|
+ // SEND
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
- /*
|
|
|
- std::vector<Address> anchors(network->config().anchors());
|
|
|
- for(std::vector<Address>::const_iterator a(anchors.begin());a!=anchors.end();++a) {
|
|
|
- if (*a != RR->identity.address()) {
|
|
|
- explicitGatherPeers[numExplicitGatherPeers++] = *a;
|
|
|
- if (numExplicitGatherPeers == 16)
|
|
|
+ SharedPtr<Peer> nextHops[2]; // these by definition are protocol version >= 11
|
|
|
+ unsigned int nextHopsBestLatency[2] = { 0xffff,0xffff };
|
|
|
+ for(std::vector< std::pair<int64_t,Address> >::const_iterator r(recipients.begin());r!=recipients.end();++r) {
|
|
|
+ const unsigned int bfi = om->bloomFilterMultiplier * (unsigned int)r->second.toInt();
|
|
|
+ const unsigned int bfbyte = (bfi >> 3) % sizeof(om->bloomFilter);
|
|
|
+ const uint8_t bfbit = 1 << (bfi & 7);
|
|
|
+ if ((om->bloomFilter[bfbyte] & bfbit) != 0) {
|
|
|
+ continue;
|
|
|
+ } else {
|
|
|
+ SharedPtr<Peer> peer(RR->topology->get(r->second));
|
|
|
+ if (peer) {
|
|
|
+ if (peer->remoteVersionProtocol() < 11) {
|
|
|
+ // SEND
|
|
|
+
|
|
|
+ om->bloomFilter[bfbyte] |= bfbit;
|
|
|
+ continue;
|
|
|
+ } else {
|
|
|
+ const unsigned int lat = peer->latency(now);
|
|
|
+ for(unsigned int nh=0;nh<2;++nh) {
|
|
|
+ if (lat <= nextHopsBestLatency[nh]) {
|
|
|
+ nextHopsBestLatency[nh] = lat;
|
|
|
+ nextHops[nh] = peer;
|
|
|
break;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
- */
|
|
|
-
|
|
|
- for(unsigned int k=0;k<numExplicitGatherPeers;++k) {
|
|
|
- const CertificateOfMembership *com = (network) ? ((network->config().com) ? &(network->config().com) : (const CertificateOfMembership *)0) : (const CertificateOfMembership *)0;
|
|
|
- Packet outp(explicitGatherPeers[k],RR->identity.address(),Packet::VERB_MULTICAST_GATHER);
|
|
|
- outp.append(network->id());
|
|
|
- outp.append((uint8_t)((com) ? 0x01 : 0x00));
|
|
|
- mg.mac().appendTo(outp);
|
|
|
- outp.append((uint32_t)mg.adi());
|
|
|
- outp.append((uint32_t)gatherLimit);
|
|
|
- if (com)
|
|
|
- com->serialize(outp);
|
|
|
- RR->node->expectReplyTo(outp.packetId());
|
|
|
- RR->sw->send(tPtr,outp,true);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- gs.txQueue.push_back(OutboundMulticast());
|
|
|
- OutboundMulticast &out = gs.txQueue.back();
|
|
|
-
|
|
|
- out.init(
|
|
|
- RR,
|
|
|
- now,
|
|
|
- network->id(),
|
|
|
- network->config().disableCompression(),
|
|
|
- limit,
|
|
|
- gatherLimit,
|
|
|
- src,
|
|
|
- mg,
|
|
|
- etherType,
|
|
|
- data,
|
|
|
- len);
|
|
|
-
|
|
|
- if (origin)
|
|
|
- out.logAsSent(origin);
|
|
|
-
|
|
|
- unsigned int count = 0;
|
|
|
-
|
|
|
- for(unsigned int i=0;i<activeBridgeCount;++i) {
|
|
|
- if (activeBridges[i] != RR->identity.address()) {
|
|
|
- out.sendAndLog(RR,tPtr,activeBridges[i]);
|
|
|
- if (++count >= limit)
|
|
|
- break;
|
|
|
- }
|
|
|
}
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- unsigned long idx = 0;
|
|
|
- while ((count < limit)&&(idx < gs.members.size())) {
|
|
|
- Address ma(gs.members[indexes[idx++]].address);
|
|
|
- if (std::find(activeBridges,activeBridges + activeBridgeCount,ma) == (activeBridges + activeBridgeCount)) {
|
|
|
- out.sendAndLog(RR,tPtr,ma);
|
|
|
- ++count;
|
|
|
- }
|
|
|
- }
|
|
|
+ for(unsigned int nh=0;nh<2;++nh) {
|
|
|
+ if (nextHops[nh]) {
|
|
|
+ const unsigned int bfi = om->bloomFilterMultiplier * (unsigned int)nextHops[nh]->address().toInt();
|
|
|
+ om->bloomFilter[(bfi >> 3) % sizeof(om->bloomFilter)] |= 1 << (bfi & 7);
|
|
|
}
|
|
|
- } catch ( ... ) {} // this is a sanity check to catch any failures and make sure indexes[] still gets deleted
|
|
|
+ }
|
|
|
|
|
|
- // Free allocated memory buffer if any
|
|
|
- if (indexes != idxbuf)
|
|
|
- delete [] indexes;
|
|
|
-#endif
|
|
|
+ for(unsigned int nh=0;nh<2;++nh) {
|
|
|
+ if (nextHops[nh]) {
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
void Multicaster::clean(int64_t now)
|
|
|
{
|
|
|
-#if 0
|
|
|
- {
|
|
|
- Mutex::Lock _l(_groups_m);
|
|
|
- Multicaster::Key *k = (Multicaster::Key *)0;
|
|
|
- MulticastGroupStatus *s = (MulticastGroupStatus *)0;
|
|
|
- Hashtable<Multicaster::Key,MulticastGroupStatus>::Iterator mm(_groups);
|
|
|
- while (mm.next(k,s)) {
|
|
|
- for(std::list<OutboundMulticast>::iterator tx(s->txQueue.begin());tx!=s->txQueue.end();) {
|
|
|
- if ((tx->expired(now))||(tx->atLimit()))
|
|
|
- s->txQueue.erase(tx++);
|
|
|
- else ++tx;
|
|
|
- }
|
|
|
-
|
|
|
- unsigned long count = 0;
|
|
|
- {
|
|
|
- std::vector<MulticastGroupMember>::iterator reader(s->members.begin());
|
|
|
- std::vector<MulticastGroupMember>::iterator writer(reader);
|
|
|
- while (reader != s->members.end()) {
|
|
|
- if ((now - reader->timestamp) < ZT_MULTICAST_LIKE_EXPIRE) {
|
|
|
- *writer = *reader;
|
|
|
- ++writer;
|
|
|
- ++count;
|
|
|
- }
|
|
|
- ++reader;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (count) {
|
|
|
- s->members.resize(count);
|
|
|
- } else if (s->txQueue.empty()) {
|
|
|
- _groups.erase(*k);
|
|
|
- } else {
|
|
|
- s->members.clear();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-#endif
|
|
|
}
|
|
|
|
|
|
} // namespace ZeroTier
|