Multicaster.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. #include <algorithm>
  14. #include "Constants.hpp"
  15. #include "RuntimeEnvironment.hpp"
  16. #include "Multicaster.hpp"
  17. #include "Topology.hpp"
  18. #include "Switch.hpp"
  19. #include "Packet.hpp"
  20. #include "Peer.hpp"
  21. #include "C25519.hpp"
  22. #include "CertificateOfMembership.hpp"
  23. #include "Node.hpp"
  24. #include "Network.hpp"
  25. namespace ZeroTier {
  26. Multicaster::Multicaster(const RuntimeEnvironment *renv) :
  27. RR(renv),
  28. _groups(32) {}
  29. Multicaster::~Multicaster() {}
  30. void Multicaster::add(const int64_t now,const uint64_t nwid,const MulticastGroup &mg,const Address &member)
  31. {
  32. Mutex::Lock l(_groups_l);
  33. _groups[Multicaster::Key(nwid,mg)].set(member,now);
  34. }
  35. void Multicaster::addMultiple(const int64_t now,const uint64_t nwid,const MulticastGroup &mg,const void *addresses,unsigned int count,const unsigned int totalKnown)
  36. {
  37. Mutex::Lock l(_groups_l);
  38. const uint8_t *a = (const uint8_t *)addresses;
  39. Hashtable< Address,int64_t > &members = _groups[Multicaster::Key(nwid,mg)];
  40. while (count--) {
  41. members.set(Address(a,ZT_ADDRESS_LENGTH),now);
  42. a += ZT_ADDRESS_LENGTH;
  43. }
  44. }
  45. void Multicaster::remove(const uint64_t nwid,const MulticastGroup &mg,const Address &member)
  46. {
  47. Mutex::Lock l(_groups_l);
  48. const Multicaster::Key gk(nwid,mg);
  49. Hashtable< Address,int64_t > *const members = _groups.get(gk);
  50. if (members) {
  51. members->erase(member);
  52. if (members->empty())
  53. _groups.erase(gk);
  54. }
  55. }
  56. void Multicaster::send(
  57. void *tPtr,
  58. int64_t now,
  59. const SharedPtr<Network> &network,
  60. const Address &origin,
  61. const MulticastGroup &mg,
  62. const MAC &src,
  63. unsigned int etherType,
  64. const void *data,
  65. unsigned int len)
  66. {
  67. #if 0
  68. unsigned long idxbuf[4096];
  69. unsigned long *indexes = idxbuf;
  70. try {
  71. Mutex::Lock _l(_groups_m);
  72. MulticastGroupStatus &gs = _groups[Multicaster::Key(network->id(),mg)];
  73. if (!gs.members.empty()) {
  74. // Allocate a memory buffer if group is monstrous
  75. if (gs.members.size() > (sizeof(idxbuf) / sizeof(unsigned long)))
  76. indexes = new unsigned long[gs.members.size()];
  77. // Generate a random permutation of member indexes
  78. for(unsigned long i=0;i<gs.members.size();++i)
  79. indexes[i] = i;
  80. for(unsigned long i=(unsigned long)gs.members.size()-1;i>0;--i) {
  81. unsigned long j = (unsigned long)Utils::random() % (i + 1);
  82. unsigned long tmp = indexes[j];
  83. indexes[j] = indexes[i];
  84. indexes[i] = tmp;
  85. }
  86. }
  87. Address activeBridges[ZT_MAX_NETWORK_SPECIALISTS];
  88. const unsigned int activeBridgeCount = network->config().activeBridges(activeBridges);
  89. const unsigned int limit = network->config().multicastLimit;
  90. if (gs.members.size() >= limit) {
  91. // Skip queue if we already have enough members to complete the send operation
  92. OutboundMulticast out;
  93. out.init(
  94. RR,
  95. now,
  96. network->id(),
  97. network->config().disableCompression(),
  98. limit,
  99. 1, // we'll still gather a little from peers to keep multicast list fresh
  100. src,
  101. mg,
  102. etherType,
  103. data,
  104. len);
  105. unsigned int count = 0;
  106. for(unsigned int i=0;i<activeBridgeCount;++i) {
  107. if ((activeBridges[i] != RR->identity.address())&&(activeBridges[i] != origin)) {
  108. out.sendOnly(RR,tPtr,activeBridges[i]); // optimization: don't use dedup log if it's a one-pass send
  109. if (++count >= limit)
  110. break;
  111. }
  112. }
  113. unsigned long idx = 0;
  114. while ((count < limit)&&(idx < gs.members.size())) {
  115. const Address ma(gs.members[indexes[idx++]].address);
  116. if ((std::find(activeBridges,activeBridges + activeBridgeCount,ma) == (activeBridges + activeBridgeCount))&&(ma != origin)) {
  117. out.sendOnly(RR,tPtr,ma); // optimization: don't use dedup log if it's a one-pass send
  118. ++count;
  119. }
  120. }
  121. } else {
  122. if (gs.txQueue.size() >= ZT_TX_QUEUE_SIZE) {
  123. RR->t->outgoingNetworkFrameDropped(tPtr,network,src,mg.mac(),etherType,0,len,"multicast TX queue is full");
  124. return;
  125. }
  126. const unsigned int gatherLimit = (limit - (unsigned int)gs.members.size()) + 1;
  127. if ((gs.members.empty())||((now - gs.lastExplicitGather) >= ZT_MULTICAST_EXPLICIT_GATHER_DELAY)) {
  128. gs.lastExplicitGather = now;
  129. Address explicitGatherPeers[16];
  130. unsigned int numExplicitGatherPeers = 0;
  131. explicitGatherPeers[numExplicitGatherPeers++] = network->controller();
  132. /*
  133. Address ac[ZT_MAX_NETWORK_SPECIALISTS];
  134. const unsigned int accnt = network->config().alwaysContactAddresses(ac);
  135. unsigned int shuffled[ZT_MAX_NETWORK_SPECIALISTS];
  136. for(unsigned int i=0;i<accnt;++i)
  137. shuffled[i] = i;
  138. for(unsigned int i=0,k=accnt>>1;i<k;++i) {
  139. const uint64_t x = Utils::random();
  140. const unsigned int x1 = shuffled[(unsigned int)x % accnt];
  141. const unsigned int x2 = shuffled[(unsigned int)(x >> 32) % accnt];
  142. const unsigned int tmp = shuffled[x1];
  143. shuffled[x1] = shuffled[x2];
  144. shuffled[x2] = tmp;
  145. }
  146. for(unsigned int i=0;i<accnt;++i) {
  147. explicitGatherPeers[numExplicitGatherPeers++] = ac[shuffled[i]];
  148. if (numExplicitGatherPeers == 16)
  149. break;
  150. }
  151. */
  152. /*
  153. std::vector<Address> anchors(network->config().anchors());
  154. for(std::vector<Address>::const_iterator a(anchors.begin());a!=anchors.end();++a) {
  155. if (*a != RR->identity.address()) {
  156. explicitGatherPeers[numExplicitGatherPeers++] = *a;
  157. if (numExplicitGatherPeers == 16)
  158. break;
  159. }
  160. }
  161. */
  162. for(unsigned int k=0;k<numExplicitGatherPeers;++k) {
  163. const CertificateOfMembership *com = (network) ? ((network->config().com) ? &(network->config().com) : (const CertificateOfMembership *)0) : (const CertificateOfMembership *)0;
  164. Packet outp(explicitGatherPeers[k],RR->identity.address(),Packet::VERB_MULTICAST_GATHER);
  165. outp.append(network->id());
  166. outp.append((uint8_t)((com) ? 0x01 : 0x00));
  167. mg.mac().appendTo(outp);
  168. outp.append((uint32_t)mg.adi());
  169. outp.append((uint32_t)gatherLimit);
  170. if (com)
  171. com->serialize(outp);
  172. RR->node->expectReplyTo(outp.packetId());
  173. RR->sw->send(tPtr,outp,true);
  174. }
  175. }
  176. gs.txQueue.push_back(OutboundMulticast());
  177. OutboundMulticast &out = gs.txQueue.back();
  178. out.init(
  179. RR,
  180. now,
  181. network->id(),
  182. network->config().disableCompression(),
  183. limit,
  184. gatherLimit,
  185. src,
  186. mg,
  187. etherType,
  188. data,
  189. len);
  190. if (origin)
  191. out.logAsSent(origin);
  192. unsigned int count = 0;
  193. for(unsigned int i=0;i<activeBridgeCount;++i) {
  194. if (activeBridges[i] != RR->identity.address()) {
  195. out.sendAndLog(RR,tPtr,activeBridges[i]);
  196. if (++count >= limit)
  197. break;
  198. }
  199. }
  200. unsigned long idx = 0;
  201. while ((count < limit)&&(idx < gs.members.size())) {
  202. Address ma(gs.members[indexes[idx++]].address);
  203. if (std::find(activeBridges,activeBridges + activeBridgeCount,ma) == (activeBridges + activeBridgeCount)) {
  204. out.sendAndLog(RR,tPtr,ma);
  205. ++count;
  206. }
  207. }
  208. }
  209. } catch ( ... ) {} // this is a sanity check to catch any failures and make sure indexes[] still gets deleted
  210. // Free allocated memory buffer if any
  211. if (indexes != idxbuf)
  212. delete [] indexes;
  213. #endif
  214. }
  215. void Multicaster::clean(int64_t now)
  216. {
  217. #if 0
  218. {
  219. Mutex::Lock _l(_groups_m);
  220. Multicaster::Key *k = (Multicaster::Key *)0;
  221. MulticastGroupStatus *s = (MulticastGroupStatus *)0;
  222. Hashtable<Multicaster::Key,MulticastGroupStatus>::Iterator mm(_groups);
  223. while (mm.next(k,s)) {
  224. for(std::list<OutboundMulticast>::iterator tx(s->txQueue.begin());tx!=s->txQueue.end();) {
  225. if ((tx->expired(now))||(tx->atLimit()))
  226. s->txQueue.erase(tx++);
  227. else ++tx;
  228. }
  229. unsigned long count = 0;
  230. {
  231. std::vector<MulticastGroupMember>::iterator reader(s->members.begin());
  232. std::vector<MulticastGroupMember>::iterator writer(reader);
  233. while (reader != s->members.end()) {
  234. if ((now - reader->timestamp) < ZT_MULTICAST_LIKE_EXPIRE) {
  235. *writer = *reader;
  236. ++writer;
  237. ++count;
  238. }
  239. ++reader;
  240. }
  241. }
  242. if (count) {
  243. s->members.resize(count);
  244. } else if (s->txQueue.empty()) {
  245. _groups.erase(*k);
  246. } else {
  247. s->members.clear();
  248. }
  249. }
  250. }
  251. #endif
  252. }
  253. } // namespace ZeroTier