Multicaster.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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: 2025-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. {
  30. }
  31. Multicaster::~Multicaster()
  32. {
  33. }
  34. void Multicaster::addMultiple(void *tPtr,int64_t now,uint64_t nwid,const MulticastGroup &mg,const void *addresses,unsigned int count,unsigned int totalKnown)
  35. {
  36. const unsigned char *p = (const unsigned char *)addresses;
  37. const unsigned char *e = p + (5 * count);
  38. Mutex::Lock _l(_groups_m);
  39. MulticastGroupStatus &gs = _groups[Multicaster::Key(nwid,mg)];
  40. while (p != e) {
  41. _add(tPtr,now,nwid,mg,gs,Address(p,5));
  42. p += 5;
  43. }
  44. }
  45. void Multicaster::remove(uint64_t nwid,const MulticastGroup &mg,const Address &member)
  46. {
  47. Mutex::Lock _l(_groups_m);
  48. MulticastGroupStatus *s = _groups.get(Multicaster::Key(nwid,mg));
  49. if (s) {
  50. for(std::vector<MulticastGroupMember>::iterator m(s->members.begin());m!=s->members.end();++m) {
  51. if (m->address == member) {
  52. s->members.erase(m);
  53. break;
  54. }
  55. }
  56. }
  57. }
  58. unsigned int Multicaster::gather(const Address &queryingPeer,uint64_t nwid,const MulticastGroup &mg,Buffer<ZT_PROTO_MAX_PACKET_LENGTH> &appendTo,unsigned int limit) const
  59. {
  60. unsigned char *p;
  61. unsigned int added = 0,i,k,rptr,totalKnown = 0;
  62. uint64_t a,picked[(ZT_PROTO_MAX_PACKET_LENGTH / 5) + 2];
  63. if (!limit) {
  64. return 0;
  65. } else if (limit > 0xffff) {
  66. limit = 0xffff;
  67. }
  68. const unsigned int totalAt = appendTo.size();
  69. appendTo.addSize(4); // sizeof(uint32_t)
  70. const unsigned int addedAt = appendTo.size();
  71. appendTo.addSize(2); // sizeof(uint16_t)
  72. { // Return myself if I am a member of this group
  73. SharedPtr<Network> network(RR->node->network(nwid));
  74. if ((network)&&(network->subscribedToMulticastGroup(mg,true))) {
  75. RR->identity.address().appendTo(appendTo);
  76. ++totalKnown;
  77. ++added;
  78. }
  79. }
  80. Mutex::Lock _l(_groups_m);
  81. const MulticastGroupStatus *s = _groups.get(Multicaster::Key(nwid,mg));
  82. if ((s)&&(!s->members.empty())) {
  83. totalKnown += (unsigned int)s->members.size();
  84. // Members are returned in random order so that repeated gather queries
  85. // will return different subsets of a large multicast group.
  86. k = 0;
  87. while ((added < limit)&&(k < s->members.size())&&((appendTo.size() + ZT_ADDRESS_LENGTH) <= ZT_PROTO_MAX_PACKET_LENGTH)) {
  88. rptr = (unsigned int)RR->node->prng();
  89. restart_member_scan:
  90. a = s->members[rptr % (unsigned int)s->members.size()].address.toInt();
  91. for(i=0;i<k;++i) {
  92. if (picked[i] == a) {
  93. ++rptr;
  94. goto restart_member_scan;
  95. }
  96. }
  97. picked[k++] = a;
  98. if (queryingPeer.toInt() != a) { // do not return the peer that is making the request as a result
  99. p = (unsigned char *)appendTo.appendField(ZT_ADDRESS_LENGTH);
  100. *(p++) = (unsigned char)((a >> 32) & 0xff);
  101. *(p++) = (unsigned char)((a >> 24) & 0xff);
  102. *(p++) = (unsigned char)((a >> 16) & 0xff);
  103. *(p++) = (unsigned char)((a >> 8) & 0xff);
  104. *p = (unsigned char)(a & 0xff);
  105. ++added;
  106. }
  107. }
  108. }
  109. appendTo.setAt(totalAt,(uint32_t)totalKnown);
  110. appendTo.setAt(addedAt,(uint16_t)added);
  111. return added;
  112. }
  113. std::vector<Address> Multicaster::getMembers(uint64_t nwid,const MulticastGroup &mg,unsigned int limit) const
  114. {
  115. std::vector<Address> ls;
  116. Mutex::Lock _l(_groups_m);
  117. const MulticastGroupStatus *s = _groups.get(Multicaster::Key(nwid,mg));
  118. if (!s) {
  119. return ls;
  120. }
  121. for(std::vector<MulticastGroupMember>::const_reverse_iterator m(s->members.rbegin());m!=s->members.rend();++m) {
  122. ls.push_back(m->address);
  123. if (ls.size() >= limit) {
  124. break;
  125. }
  126. }
  127. return ls;
  128. }
  129. void Multicaster::send(
  130. void *tPtr,
  131. int64_t now,
  132. const SharedPtr<Network> &network,
  133. const Address &origin,
  134. const MulticastGroup &mg,
  135. const MAC &src,
  136. unsigned int etherType,
  137. const void *data,
  138. unsigned int len)
  139. {
  140. unsigned long idxbuf[4096];
  141. unsigned long *indexes = idxbuf;
  142. // If we're in hub-and-spoke designated multicast replication mode, see if we
  143. // have a multicast replicator active. If so, pick the best and send it
  144. // there. If we are a multicast replicator or if none are alive, fall back
  145. // to sender replication. Note that bridges do not do this since this would
  146. // break bridge route learning. This is sort of an edge case limitation of
  147. // the current protocol and could be fixed, but fixing it would add more
  148. // complexity than the fix is probably worth. Bridges are generally high
  149. // bandwidth nodes.
  150. if (!network->config().isActiveBridge(RR->identity.address())) {
  151. Address multicastReplicators[ZT_MAX_NETWORK_SPECIALISTS];
  152. const unsigned int multicastReplicatorCount = network->config().multicastReplicators(multicastReplicators);
  153. if (multicastReplicatorCount) {
  154. if (std::find(multicastReplicators,multicastReplicators + multicastReplicatorCount,RR->identity.address()) == (multicastReplicators + multicastReplicatorCount)) {
  155. SharedPtr<Peer> bestMulticastReplicator;
  156. SharedPtr<Path> bestMulticastReplicatorPath;
  157. unsigned int bestMulticastReplicatorLatency = 0xffff;
  158. for(unsigned int i=0;i<multicastReplicatorCount;++i) {
  159. const SharedPtr<Peer> p(RR->topology->getPeerNoCache(multicastReplicators[i]));
  160. if ((p)&&(p->isAlive(now))) {
  161. const SharedPtr<Path> pp(p->getAppropriatePath(now,false));
  162. if ((pp)&&(pp->latency() < bestMulticastReplicatorLatency)) {
  163. bestMulticastReplicatorLatency = pp->latency();
  164. bestMulticastReplicatorPath = pp;
  165. bestMulticastReplicator = p;
  166. }
  167. }
  168. }
  169. if (bestMulticastReplicator) {
  170. Packet outp(bestMulticastReplicator->address(),RR->identity.address(),Packet::VERB_MULTICAST_FRAME);
  171. outp.append((uint64_t)network->id());
  172. outp.append((uint8_t)0x0c); // includes source MAC | please replicate
  173. ((src) ? src : MAC(RR->identity.address(),network->id())).appendTo(outp);
  174. mg.mac().appendTo(outp);
  175. outp.append((uint32_t)mg.adi());
  176. outp.append((uint16_t)etherType);
  177. outp.append(data,len);
  178. if (!network->config().disableCompression()) {
  179. outp.compress();
  180. }
  181. outp.armor(bestMulticastReplicator->key(),true,bestMulticastReplicator->aesKeysIfSupported());
  182. Metrics::pkt_multicast_frame_out++;
  183. bestMulticastReplicatorPath->send(RR,tPtr,outp.data(),outp.size(),now);
  184. return;
  185. }
  186. }
  187. }
  188. }
  189. try {
  190. Mutex::Lock _l(_groups_m);
  191. MulticastGroupStatus &gs = _groups[Multicaster::Key(network->id(),mg)];
  192. if (!gs.members.empty()) {
  193. // Allocate a memory buffer if group is monstrous
  194. if (gs.members.size() > (sizeof(idxbuf) / sizeof(unsigned long))) {
  195. indexes = new unsigned long[gs.members.size()];
  196. }
  197. // Generate a random permutation of member indexes
  198. for(unsigned long i=0;i<gs.members.size();++i) {
  199. indexes[i] = i;
  200. }
  201. for(unsigned long i=(unsigned long)gs.members.size()-1;i>0;--i) {
  202. unsigned long j = (unsigned long)RR->node->prng() % (i + 1);
  203. unsigned long tmp = indexes[j];
  204. indexes[j] = indexes[i];
  205. indexes[i] = tmp;
  206. }
  207. }
  208. Address activeBridges[ZT_MAX_NETWORK_SPECIALISTS];
  209. const unsigned int activeBridgeCount = network->config().activeBridges(activeBridges);
  210. const unsigned int limit = network->config().multicastLimit;
  211. if (gs.members.size() >= limit) {
  212. // Skip queue if we already have enough members to complete the send operation
  213. OutboundMulticast out;
  214. out.init(
  215. RR,
  216. now,
  217. network->id(),
  218. network->config().disableCompression(),
  219. limit,
  220. 1, // we'll still gather a little from peers to keep multicast list fresh
  221. src,
  222. mg,
  223. etherType,
  224. data,
  225. len);
  226. unsigned int count = 0;
  227. for(unsigned int i=0;i<activeBridgeCount;++i) {
  228. if ((activeBridges[i] != RR->identity.address())&&(activeBridges[i] != origin)) {
  229. out.sendOnly(RR,tPtr,activeBridges[i]); // optimization: don't use dedup log if it's a one-pass send
  230. if (++count >= limit) {
  231. break;
  232. }
  233. }
  234. }
  235. unsigned long idx = 0;
  236. while ((count < limit)&&(idx < gs.members.size())) {
  237. const Address ma(gs.members[indexes[idx++]].address);
  238. if ((std::find(activeBridges,activeBridges + activeBridgeCount,ma) == (activeBridges + activeBridgeCount))&&(ma != origin)) {
  239. out.sendOnly(RR,tPtr,ma); // optimization: don't use dedup log if it's a one-pass send
  240. ++count;
  241. }
  242. }
  243. } else {
  244. while (gs.txQueue.size() >= ZT_TX_QUEUE_SIZE) {
  245. gs.txQueue.pop_front();
  246. }
  247. const unsigned int gatherLimit = (limit - (unsigned int)gs.members.size()) + 1;
  248. int timerScale = RR->node->lowBandwidthModeEnabled() ? 3 : 1;
  249. if ((gs.members.empty())||((now - gs.lastExplicitGather) >= (ZT_MULTICAST_EXPLICIT_GATHER_DELAY * timerScale))) {
  250. gs.lastExplicitGather = now;
  251. Address explicitGatherPeers[16];
  252. unsigned int numExplicitGatherPeers = 0;
  253. SharedPtr<Peer> bestRoot(RR->topology->getUpstreamPeer());
  254. if (bestRoot) {
  255. explicitGatherPeers[numExplicitGatherPeers++] = bestRoot->address();
  256. }
  257. explicitGatherPeers[numExplicitGatherPeers++] = network->controller();
  258. Address ac[ZT_MAX_NETWORK_SPECIALISTS];
  259. const unsigned int accnt = network->config().alwaysContactAddresses(ac);
  260. unsigned int shuffled[ZT_MAX_NETWORK_SPECIALISTS];
  261. for(unsigned int i=0;i<accnt;++i) {
  262. shuffled[i] = i;
  263. }
  264. for(unsigned int i=0,k=accnt>>1;i<k;++i) {
  265. const uint64_t x = RR->node->prng();
  266. const unsigned int x1 = shuffled[(unsigned int)x % accnt];
  267. const unsigned int x2 = shuffled[(unsigned int)(x >> 32) % accnt];
  268. const unsigned int tmp = shuffled[x1];
  269. shuffled[x1] = shuffled[x2];
  270. shuffled[x2] = tmp;
  271. }
  272. for(unsigned int i=0;i<accnt;++i) {
  273. explicitGatherPeers[numExplicitGatherPeers++] = ac[shuffled[i]];
  274. if (numExplicitGatherPeers == 16) {
  275. break;
  276. }
  277. }
  278. std::vector<Address> anchors(network->config().anchors());
  279. for(std::vector<Address>::const_iterator a(anchors.begin());a!=anchors.end();++a) {
  280. if (*a != RR->identity.address()) {
  281. explicitGatherPeers[numExplicitGatherPeers++] = *a;
  282. if (numExplicitGatherPeers == 16) {
  283. break;
  284. }
  285. }
  286. }
  287. for(unsigned int k=0;k<numExplicitGatherPeers;++k) {
  288. const CertificateOfMembership *com = (network) ? ((network->config().com) ? &(network->config().com) : (const CertificateOfMembership *)0) : (const CertificateOfMembership *)0;
  289. Packet outp(explicitGatherPeers[k],RR->identity.address(),Packet::VERB_MULTICAST_GATHER);
  290. outp.append(network->id());
  291. outp.append((uint8_t)((com) ? 0x01 : 0x00));
  292. mg.mac().appendTo(outp);
  293. outp.append((uint32_t)mg.adi());
  294. outp.append((uint32_t)gatherLimit);
  295. if (com) {
  296. com->serialize(outp);
  297. }
  298. RR->node->expectReplyTo(outp.packetId());
  299. RR->sw->send(tPtr,outp,true);
  300. Metrics::pkt_multicast_gather_out++;
  301. }
  302. }
  303. gs.txQueue.push_back(OutboundMulticast());
  304. OutboundMulticast &out = gs.txQueue.back();
  305. out.init(
  306. RR,
  307. now,
  308. network->id(),
  309. network->config().disableCompression(),
  310. limit,
  311. gatherLimit,
  312. src,
  313. mg,
  314. etherType,
  315. data,
  316. len);
  317. if (origin) {
  318. out.logAsSent(origin);
  319. }
  320. unsigned int count = 0;
  321. for(unsigned int i=0;i<activeBridgeCount;++i) {
  322. if (activeBridges[i] != RR->identity.address()) {
  323. out.sendAndLog(RR,tPtr,activeBridges[i]);
  324. if (++count >= limit) {
  325. break;
  326. }
  327. }
  328. }
  329. unsigned long idx = 0;
  330. while ((count < limit)&&(idx < gs.members.size())) {
  331. Address ma(gs.members[indexes[idx++]].address);
  332. if (std::find(activeBridges,activeBridges + activeBridgeCount,ma) == (activeBridges + activeBridgeCount)) {
  333. out.sendAndLog(RR,tPtr,ma);
  334. ++count;
  335. }
  336. }
  337. }
  338. } catch ( ... ) {} // this is a sanity check to catch any failures and make sure indexes[] still gets deleted
  339. // Free allocated memory buffer if any
  340. if (indexes != idxbuf) {
  341. delete [] indexes;
  342. }
  343. }
  344. void Multicaster::clean(int64_t now)
  345. {
  346. Mutex::Lock _l(_groups_m);
  347. Multicaster::Key *k = (Multicaster::Key *)0;
  348. MulticastGroupStatus *s = (MulticastGroupStatus *)0;
  349. Hashtable<Multicaster::Key,MulticastGroupStatus>::Iterator mm(_groups);
  350. while (mm.next(k,s)) {
  351. for(std::list<OutboundMulticast>::iterator tx(s->txQueue.begin());tx!=s->txQueue.end();) {
  352. if ((tx->expired(now))||(tx->atLimit())) {
  353. s->txQueue.erase(tx++);
  354. } else {
  355. ++tx;
  356. }
  357. }
  358. unsigned long count = 0;
  359. {
  360. std::vector<MulticastGroupMember>::iterator reader(s->members.begin());
  361. std::vector<MulticastGroupMember>::iterator writer(reader);
  362. while (reader != s->members.end()) {
  363. if ((now - reader->timestamp) < ZT_MULTICAST_LIKE_EXPIRE) {
  364. *writer = *reader;
  365. ++writer;
  366. ++count;
  367. }
  368. ++reader;
  369. }
  370. }
  371. if (count) {
  372. s->members.resize(count);
  373. } else if (s->txQueue.empty()) {
  374. _groups.erase(*k);
  375. } else {
  376. s->members.clear();
  377. }
  378. }
  379. }
  380. void Multicaster::_add(void *tPtr,int64_t now,uint64_t nwid,const MulticastGroup &mg,MulticastGroupStatus &gs,const Address &member)
  381. {
  382. // assumes _groups_m is locked
  383. // Do not add self -- even if someone else returns it
  384. if (member == RR->identity.address()) {
  385. return;
  386. }
  387. std::vector<MulticastGroupMember>::iterator m(std::lower_bound(gs.members.begin(),gs.members.end(),member));
  388. if (m != gs.members.end()) {
  389. if (m->address == member) {
  390. m->timestamp = now;
  391. return;
  392. }
  393. gs.members.insert(m,MulticastGroupMember(member,now));
  394. } else {
  395. gs.members.push_back(MulticastGroupMember(member,now));
  396. }
  397. for(std::list<OutboundMulticast>::iterator tx(gs.txQueue.begin());tx!=gs.txQueue.end();) {
  398. if (tx->atLimit()) {
  399. gs.txQueue.erase(tx++);
  400. } else {
  401. tx->sendIfNew(RR,tPtr,member);
  402. if (tx->atLimit()) {
  403. gs.txQueue.erase(tx++);
  404. } else {
  405. ++tx;
  406. }
  407. }
  408. }
  409. }
  410. } // namespace ZeroTier