Multicaster.hpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Copyright (c)2019 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2023-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_MULTICASTER_HPP
  14. #define ZT_MULTICASTER_HPP
  15. #include <stdint.h>
  16. #include <string.h>
  17. #include <map>
  18. #include <vector>
  19. #include "Constants.hpp"
  20. #include "Hashtable.hpp"
  21. #include "Address.hpp"
  22. #include "MAC.hpp"
  23. #include "MulticastGroup.hpp"
  24. #include "Utils.hpp"
  25. #include "Mutex.hpp"
  26. #include "SharedPtr.hpp"
  27. #include "Packet.hpp"
  28. // Size in bits -- do not change as this is about as large as we can support
  29. // This leaves room for up to 10000 MTU data (max supported MTU) and header
  30. // information in a maximum supported size packet. Note that data compression
  31. // will practically reduce this size in transit for sparse or saturated fields.
  32. #define ZT_MULTICAST_BLOOM_FILTER_SIZE_BITS 50048
  33. namespace ZeroTier {
  34. class RuntimeEnvironment;
  35. class CertificateOfMembership;
  36. class Packet;
  37. class Network;
  38. /**
  39. * Multicast database and outbound multicast logic
  40. */
  41. class Multicaster
  42. {
  43. private:
  44. // Composite key of network ID and multicast group
  45. struct _K
  46. {
  47. uint64_t nwid;
  48. MulticastGroup mg;
  49. ZT_ALWAYS_INLINE _K() : nwid(0),mg() {}
  50. ZT_ALWAYS_INLINE _K(const uint64_t n,const MulticastGroup &g) : nwid(n),mg(g) {}
  51. ZT_ALWAYS_INLINE bool operator==(const _K &k) const { return ((nwid == k.nwid)&&(mg == k.mg)); }
  52. ZT_ALWAYS_INLINE bool operator!=(const _K &k) const { return ((nwid != k.nwid)||(mg != k.mg)); }
  53. ZT_ALWAYS_INLINE unsigned long hashCode() const { return (mg.hashCode() ^ (unsigned long)(nwid ^ (nwid >> 32))); }
  54. };
  55. // Multicast group info
  56. struct _G
  57. {
  58. ZT_ALWAYS_INLINE _G() : lastGather(0),members(16) {}
  59. int64_t lastGather;
  60. Hashtable< Address,int64_t > members;
  61. };
  62. // Outbound multicast
  63. struct _OM
  64. {
  65. uint64_t nwid;
  66. MAC src;
  67. MulticastGroup mg;
  68. unsigned int etherType;
  69. unsigned int dataSize;
  70. unsigned int count;
  71. unsigned int limit;
  72. unsigned int bloomFilterMultiplier;
  73. uint64_t bloomFilter[ZT_MULTICAST_BLOOM_FILTER_SIZE_BITS / 64];
  74. uint8_t data[ZT_MAX_MTU];
  75. Mutex lock;
  76. };
  77. public:
  78. Multicaster(const RuntimeEnvironment *renv);
  79. ~Multicaster();
  80. /**
  81. * Add or update a member in a multicast group
  82. *
  83. * @param now Current time
  84. * @param nwid Network ID
  85. * @param mg Multicast group
  86. * @param member New member address
  87. */
  88. ZT_ALWAYS_INLINE void add(const int64_t now,const uint64_t nwid,const MulticastGroup &mg,const Address &member)
  89. {
  90. Mutex::Lock l(_groups_l);
  91. _groups[_K(nwid,mg)].members.set(member,now);
  92. }
  93. /**
  94. * Add multiple addresses from a binary array of 5-byte address fields
  95. *
  96. * It's up to the caller to check bounds on the array before calling this.
  97. *
  98. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  99. * @param now Current time
  100. * @param nwid Network ID
  101. * @param mg Multicast group
  102. * @param addresses Raw binary addresses in big-endian format, as a series of 5-byte fields
  103. * @param count Number of addresses
  104. * @param totalKnown Total number of known addresses as reported by peer
  105. */
  106. ZT_ALWAYS_INLINE void addMultiple(const int64_t now,const uint64_t nwid,const MulticastGroup &mg,const void *addresses,unsigned int count,const unsigned int totalKnown)
  107. {
  108. Mutex::Lock l(_groups_l);
  109. const uint8_t *a = (const uint8_t *)addresses;
  110. _G &g = _groups[_K(nwid,mg)];
  111. while (count--) {
  112. g.members.set(Address(a,ZT_ADDRESS_LENGTH),now);
  113. a += ZT_ADDRESS_LENGTH;
  114. }
  115. }
  116. /**
  117. * Remove a multicast group member (if present)
  118. *
  119. * @param nwid Network ID
  120. * @param mg Multicast group
  121. * @param member Member to unsubscribe
  122. */
  123. ZT_ALWAYS_INLINE void remove(const uint64_t nwid,const MulticastGroup &mg,const Address &member)
  124. {
  125. Mutex::Lock l(_groups_l);
  126. const _K gk(nwid,mg);
  127. _G *const g = _groups.get(gk);
  128. if (g) {
  129. g->members.erase(member);
  130. if (g->members.empty())
  131. _groups.erase(gk);
  132. }
  133. }
  134. /**
  135. * Iterate over members of a multicast group until function returns false
  136. *
  137. * Iteration order is in inverse order of most recent receipt of a LIKE
  138. * for a given membership.
  139. *
  140. * @param nwid Network ID
  141. * @param mg Multicast group
  142. * @param func f(Address)
  143. * @return Total number of known members (regardless of when function aborted)
  144. */
  145. template<typename F>
  146. ZT_ALWAYS_INLINE unsigned long eachMember(const uint64_t nwid,const MulticastGroup &mg,F func) const
  147. {
  148. std::vector< std::pair<int64_t,Address> > sortedByTime;
  149. {
  150. Mutex::Lock l(_groups_l);
  151. const _K gk(nwid,mg);
  152. const _G *const g = _groups.get(gk);
  153. if (g) {
  154. sortedByTime.reserve(g->members.size());
  155. {
  156. Hashtable< Address,int64_t >::Iterator mi(const_cast<_G *>(g)->members);
  157. Address *mik = nullptr;
  158. int64_t *miv = nullptr;
  159. while (mi.next(mik,miv))
  160. sortedByTime.push_back(std::pair<int64_t,Address>(*miv,*mik));
  161. }
  162. }
  163. }
  164. std::sort(sortedByTime.begin(),sortedByTime.end());
  165. for(std::vector< std::pair<int64_t,Address> >::const_reverse_iterator i(sortedByTime.rbegin());i!=sortedByTime.rend();++i) {
  166. if (!func(i->second))
  167. break;
  168. }
  169. return sortedByTime.size();
  170. }
  171. /**
  172. * Send a multicast
  173. *
  174. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  175. * @param now Current time
  176. * @param network Network
  177. * @param mg Multicast group
  178. * @param src Source Ethernet MAC address or NULL to skip in packet and compute from ZT address (non-bridged mode)
  179. * @param etherType Ethernet frame type
  180. * @param existingBloomMultiplier Existing bloom filter multiplier or 0 if none
  181. * @param existingBloom Existing bloom filter or NULL if none
  182. * @param data Packet data
  183. * @param len Length of packet data
  184. * @return Number of known recipients for multicast (including bridges and replicators)
  185. */
  186. unsigned int send(
  187. void *tPtr,
  188. int64_t now,
  189. const SharedPtr<Network> &network,
  190. const MulticastGroup &mg,
  191. const MAC &src,
  192. unsigned int etherType,
  193. const unsigned int existingBloomMultiplier,
  194. const uint8_t existingBloom[ZT_MULTICAST_BLOOM_FILTER_SIZE_BITS / 8],
  195. const void *const data,
  196. unsigned int len);
  197. /**
  198. * Clean up database
  199. *
  200. * @param RR Runtime environment
  201. * @param now Current time
  202. */
  203. void clean(int64_t now);
  204. private:
  205. const RuntimeEnvironment *const RR;
  206. _OM _txQueue[ZT_TX_QUEUE_SIZE];
  207. unsigned int _txQueuePtr;
  208. Mutex _txQueue_l;
  209. Hashtable< _K,_G > _groups;
  210. Mutex _groups_l;
  211. };
  212. } // namespace ZeroTier
  213. #endif