Multicaster.cpp 14 KB

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