More multicast work...

This commit is contained in:
Adam Ierymenko 2019-09-10 16:20:28 -07:00
parent 592e743349
commit bccb86a401
No known key found for this signature in database
GPG key ID: C8877CF2D7A5D7F3
7 changed files with 410 additions and 230 deletions

View file

@ -30,8 +30,11 @@
#include "SharedPtr.hpp"
#include "Packet.hpp"
// Size in bits -- this is pretty close to the maximum allowed by the protocol
#define ZT_MULTICAST_BLOOM_FILTER_SIZE_BITS 16384
// Size in bits -- do not change as this is about as large as we can support
// This leaves room for up to 10000 MTU data (max supported MTU) and header
// information in a maximum supported size packet. Note that data compression
// will practically reduce this size in transit for sparse or saturated fields.
#define ZT_MULTICAST_BLOOM_FILTER_SIZE_BITS 50048
namespace ZeroTier {
@ -45,6 +48,44 @@ class Network;
*/
class Multicaster
{
private:
// Composite key of network ID and multicast group
struct _K
{
uint64_t nwid;
MulticastGroup mg;
ZT_ALWAYS_INLINE _K() : nwid(0),mg() {}
ZT_ALWAYS_INLINE _K(const uint64_t n,const MulticastGroup &g) : nwid(n),mg(g) {}
ZT_ALWAYS_INLINE bool operator==(const _K &k) const { return ((nwid == k.nwid)&&(mg == k.mg)); }
ZT_ALWAYS_INLINE bool operator!=(const _K &k) const { return ((nwid != k.nwid)||(mg != k.mg)); }
ZT_ALWAYS_INLINE unsigned long hashCode() const { return (mg.hashCode() ^ (unsigned long)(nwid ^ (nwid >> 32))); }
};
// Multicast group info
struct _G
{
ZT_ALWAYS_INLINE _G() : lastGather(0),members(16) {}
int64_t lastGather;
Hashtable< Address,int64_t > members;
};
// Outbound multicast
struct _OM
{
uint64_t nwid;
MAC src;
MulticastGroup mg;
unsigned int etherType;
unsigned int dataSize;
unsigned int count;
unsigned int limit;
unsigned int bloomFilterMultiplier;
uint64_t bloomFilter[ZT_MULTICAST_BLOOM_FILTER_SIZE_BITS / 64];
uint8_t data[ZT_MAX_MTU];
Mutex lock;
};
public:
Multicaster(const RuntimeEnvironment *renv);
~Multicaster();
@ -60,7 +101,7 @@ public:
ZT_ALWAYS_INLINE void add(const int64_t now,const uint64_t nwid,const MulticastGroup &mg,const Address &member)
{
Mutex::Lock l(_groups_l);
_groups[_K(nwid,mg)].set(member,now);
_groups[_K(nwid,mg)].members.set(member,now);
}
/**
@ -80,9 +121,9 @@ public:
{
Mutex::Lock l(_groups_l);
const uint8_t *a = (const uint8_t *)addresses;
Hashtable< Address,int64_t > &members = _groups[_K(nwid,mg)];
_G &g = _groups[_K(nwid,mg)];
while (count--) {
members.set(Address(a,ZT_ADDRESS_LENGTH),now);
g.members.set(Address(a,ZT_ADDRESS_LENGTH),now);
a += ZT_ADDRESS_LENGTH;
}
}
@ -98,10 +139,10 @@ public:
{
Mutex::Lock l(_groups_l);
const _K gk(nwid,mg);
Hashtable< Address,int64_t > *const members = _groups.get(gk);
if (members) {
members->erase(member);
if (members->empty())
_G *const g = _groups.get(gk);
if (g) {
g->members.erase(member);
if (g->members.empty())
_groups.erase(gk);
}
}
@ -121,8 +162,21 @@ public:
ZT_ALWAYS_INLINE unsigned long eachMember(const uint64_t nwid,const MulticastGroup &mg,F func) const
{
std::vector< std::pair<int64_t,Address> > sortedByTime;
Mutex::Lock l(_groups_l);
_getMembersByTime(nwid,mg,sortedByTime);
{
Mutex::Lock l(_groups_l);
const _K gk(nwid,mg);
const _G *const g = _groups.get(gk);
if (g) {
sortedByTime.reserve(g->members.size());
{
Hashtable< Address,int64_t >::Iterator mi(const_cast<_G *>(g)->members);
Address *mik = nullptr;
int64_t *miv = nullptr;
while (mi.next(mik,miv))
sortedByTime.push_back(std::pair<int64_t,Address>(*miv,*mik));
}
}
}
std::sort(sortedByTime.begin(),sortedByTime.end());
for(std::vector< std::pair<int64_t,Address> >::const_reverse_iterator i(sortedByTime.begin());i!=sortedByTime.end();++i) {
if (!func(i->second))
@ -144,8 +198,9 @@ public:
* @param existingBloom Existing bloom filter or NULL if none
* @param data Packet data
* @param len Length of packet data
* @return Number of known recipients for multicast (including bridges and replicators)
*/
void send(
unsigned int send(
void *tPtr,
int64_t now,
const SharedPtr<Network> &network,
@ -166,74 +221,13 @@ public:
void clean(int64_t now);
private:
ZT_ALWAYS_INLINE void _getMembersByTime(const uint64_t nwid,const MulticastGroup &mg,std::vector< std::pair<int64_t,Address> > &byTime)
{
// assumes _groups_l is locked
const _K gk(nwid,mg);
const Hashtable< Address,int64_t > *const members = _groups.get(gk);
if (members) {
byTime.reserve(members->size());
{
Hashtable< Address,int64_t >::Iterator mi(*const_cast<Hashtable< Address,int64_t > *>(members));
Address *mik = nullptr;
int64_t *miv = nullptr;
while (mi.next(mik,miv))
byTime.push_back(std::pair<int64_t,Address>(*miv,*mik));
}
}
}
struct _K
{
uint64_t nwid;
MulticastGroup mg;
ZT_ALWAYS_INLINE _K() : nwid(0),mg() {}
ZT_ALWAYS_INLINE _K(const uint64_t n,const MulticastGroup &g) : nwid(n),mg(g) {}
ZT_ALWAYS_INLINE bool operator==(const _K &k) const { return ((nwid == k.nwid)&&(mg == k.mg)); }
ZT_ALWAYS_INLINE bool operator!=(const _K &k) const { return ((nwid != k.nwid)||(mg != k.mg)); }
ZT_ALWAYS_INLINE unsigned long hashCode() const { return (mg.hashCode() ^ (unsigned long)(nwid ^ (nwid >> 32))); }
};
/*
* Multicast frame:
* <[8] 64-bit network ID>
* <[1] flags>
* [<[...] network certificate of membership (DEPRECATED)>]
* [<[4] 32-bit implicit gather limit (DEPRECATED)>]
* [<[5] ZeroTier address of originating sender (including w/0x08)>]
* [<[2] 16-bit bloom filter multiplier>]
* [<[2] 16-bit length of propagation bloom filter in bytes]
* [<[...] propagation bloom filter>]
* [<[6] source MAC>]
* <[6] destination MAC (multicast address)>
* <[4] 32-bit multicast ADI (multicast address extension)>
* <[2] 16-bit ethertype>
* <[...] ethernet payload>
* [<[2] 16-bit length of signature>]
* [<[...] signature (algorithm depends on sender identity)>]
*/
struct _OM
{
uint64_t nwid;
MAC src;
MulticastGroup mg;
unsigned int etherType;
unsigned int dataSize;
unsigned int bloomFilterMultiplier;
uint8_t bloomFilter[ZT_MULTICAST_BLOOM_FILTER_SIZE_BITS / 8];
uint8_t data[ZT_MAX_MTU];
Mutex lock;
};
const RuntimeEnvironment *const RR;
_OM _txQueue[ZT_TX_QUEUE_SIZE];
unsigned int _txQueuePtr;
Mutex _txQueue_l;
Hashtable< _K,Hashtable< Address,int64_t > > _groups;
Hashtable< _K,_G > _groups;
Mutex _groups_l;
};