Multicaster.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. if (gs.txQueue.size() >= ZT_TX_QUEUE_SIZE) {
  249. RR->t->outgoingNetworkFrameDropped(tPtr,network,src,mg.mac(),etherType,0,len,"multicast TX queue is full");
  250. return;
  251. }
  252. const unsigned int gatherLimit = (limit - (unsigned int)gs.members.size()) + 1;
  253. if ((gs.members.empty())||((now - gs.lastExplicitGather) >= ZT_MULTICAST_EXPLICIT_GATHER_DELAY)) {
  254. gs.lastExplicitGather = now;
  255. Address explicitGatherPeers[16];
  256. unsigned int numExplicitGatherPeers = 0;
  257. SharedPtr<Peer> bestRoot(RR->topology->getUpstreamPeer());
  258. if (bestRoot)
  259. explicitGatherPeers[numExplicitGatherPeers++] = bestRoot->address();
  260. explicitGatherPeers[numExplicitGatherPeers++] = network->controller();
  261. Address ac[ZT_MAX_NETWORK_SPECIALISTS];
  262. const unsigned int accnt = network->config().alwaysContactAddresses(ac);
  263. unsigned int shuffled[ZT_MAX_NETWORK_SPECIALISTS];
  264. for(unsigned int i=0;i<accnt;++i)
  265. shuffled[i] = i;
  266. for(unsigned int i=0,k=accnt>>1;i<k;++i) {
  267. const uint64_t x = RR->node->prng();
  268. const unsigned int x1 = shuffled[(unsigned int)x % accnt];
  269. const unsigned int x2 = shuffled[(unsigned int)(x >> 32) % accnt];
  270. const unsigned int tmp = shuffled[x1];
  271. shuffled[x1] = shuffled[x2];
  272. shuffled[x2] = tmp;
  273. }
  274. for(unsigned int i=0;i<accnt;++i) {
  275. explicitGatherPeers[numExplicitGatherPeers++] = ac[shuffled[i]];
  276. if (numExplicitGatherPeers == 16)
  277. break;
  278. }
  279. std::vector<Address> anchors(network->config().anchors());
  280. for(std::vector<Address>::const_iterator a(anchors.begin());a!=anchors.end();++a) {
  281. if (*a != RR->identity.address()) {
  282. explicitGatherPeers[numExplicitGatherPeers++] = *a;
  283. if (numExplicitGatherPeers == 16)
  284. break;
  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. RR->node->expectReplyTo(outp.packetId());
  298. RR->sw->send(tPtr,outp,true);
  299. }
  300. }
  301. gs.txQueue.push_back(OutboundMulticast());
  302. OutboundMulticast &out = gs.txQueue.back();
  303. out.init(
  304. RR,
  305. now,
  306. network->id(),
  307. network->config().disableCompression(),
  308. limit,
  309. gatherLimit,
  310. src,
  311. mg,
  312. etherType,
  313. data,
  314. len);
  315. if (origin)
  316. out.logAsSent(origin);
  317. unsigned int count = 0;
  318. for(unsigned int i=0;i<activeBridgeCount;++i) {
  319. if (activeBridges[i] != RR->identity.address()) {
  320. out.sendAndLog(RR,tPtr,activeBridges[i]);
  321. if (++count >= limit)
  322. break;
  323. }
  324. }
  325. unsigned long idx = 0;
  326. while ((count < limit)&&(idx < gs.members.size())) {
  327. Address ma(gs.members[indexes[idx++]].address);
  328. if (std::find(activeBridges,activeBridges + activeBridgeCount,ma) == (activeBridges + activeBridgeCount)) {
  329. out.sendAndLog(RR,tPtr,ma);
  330. ++count;
  331. }
  332. }
  333. }
  334. } catch ( ... ) {} // this is a sanity check to catch any failures and make sure indexes[] still gets deleted
  335. // Free allocated memory buffer if any
  336. if (indexes != idxbuf)
  337. delete [] indexes;
  338. }
  339. void Multicaster::clean(int64_t now)
  340. {
  341. {
  342. Mutex::Lock _l(_groups_m);
  343. Multicaster::Key *k = (Multicaster::Key *)0;
  344. MulticastGroupStatus *s = (MulticastGroupStatus *)0;
  345. Hashtable<Multicaster::Key,MulticastGroupStatus>::Iterator mm(_groups);
  346. while (mm.next(k,s)) {
  347. for(std::list<OutboundMulticast>::iterator tx(s->txQueue.begin());tx!=s->txQueue.end();) {
  348. if ((tx->expired(now))||(tx->atLimit()))
  349. s->txQueue.erase(tx++);
  350. else ++tx;
  351. }
  352. unsigned long count = 0;
  353. {
  354. std::vector<MulticastGroupMember>::iterator reader(s->members.begin());
  355. std::vector<MulticastGroupMember>::iterator writer(reader);
  356. while (reader != s->members.end()) {
  357. if ((now - reader->timestamp) < ZT_MULTICAST_LIKE_EXPIRE) {
  358. *writer = *reader;
  359. ++writer;
  360. ++count;
  361. }
  362. ++reader;
  363. }
  364. }
  365. if (count) {
  366. s->members.resize(count);
  367. } else if (s->txQueue.empty()) {
  368. _groups.erase(*k);
  369. } else {
  370. s->members.clear();
  371. }
  372. }
  373. }
  374. }
  375. void Multicaster::_add(void *tPtr,int64_t now,uint64_t nwid,const MulticastGroup &mg,MulticastGroupStatus &gs,const Address &member)
  376. {
  377. // assumes _groups_m is locked
  378. // Do not add self -- even if someone else returns it
  379. if (member == RR->identity.address())
  380. return;
  381. std::vector<MulticastGroupMember>::iterator m(std::lower_bound(gs.members.begin(),gs.members.end(),member));
  382. if (m != gs.members.end()) {
  383. if (m->address == member) {
  384. m->timestamp = now;
  385. return;
  386. }
  387. gs.members.insert(m,MulticastGroupMember(member,now));
  388. } else {
  389. gs.members.push_back(MulticastGroupMember(member,now));
  390. }
  391. for(std::list<OutboundMulticast>::iterator tx(gs.txQueue.begin());tx!=gs.txQueue.end();) {
  392. if (tx->atLimit())
  393. gs.txQueue.erase(tx++);
  394. else {
  395. tx->sendIfNew(RR,tPtr,member);
  396. if (tx->atLimit())
  397. gs.txQueue.erase(tx++);
  398. else ++tx;
  399. }
  400. }
  401. }
  402. } // namespace ZeroTier