Multicaster.cpp 14 KB

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