Multicaster.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2014 ZeroTier Networks LLC
  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. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #include <algorithm>
  28. #include "Constants.hpp"
  29. #include "SharedPtr.hpp"
  30. #include "Multicaster.hpp"
  31. #include "Topology.hpp"
  32. #include "Switch.hpp"
  33. #include "Packet.hpp"
  34. #include "Peer.hpp"
  35. #include "CertificateOfMembership.hpp"
  36. #include "RuntimeEnvironment.hpp"
  37. namespace ZeroTier {
  38. Multicaster::Multicaster()
  39. {
  40. }
  41. Multicaster::~Multicaster()
  42. {
  43. }
  44. void Multicaster::send(const RuntimeEnvironment *RR,uint64_t nwid,const CertificateOfMembership *com,unsigned int limit,uint64_t now,const MulticastGroup &mg,const MAC &src,unsigned int etherType,const void *data,unsigned int len)
  45. {
  46. Mutex::Lock _l(_groups_m);
  47. MulticastGroupStatus &gs = _groups[mg];
  48. if (gs.members.size() >= limit) {
  49. // If we already have enough members, just send and we're done -- no need for TX queue
  50. OutboundMulticast out;
  51. out.init(now,RR->identity.address(),nwid,ZT_MULTICAST_DEFAULT_IMPLICIT_GATHER,src,mg,etherType,data,len);
  52. unsigned int count = 0;
  53. for(std::vector<MulticastGroupMember>::const_reverse_iterator m(gs.members.rbegin());m!=gs.members.rend();++m) {
  54. out.sendOnly(*(RR->sw),m->address);
  55. if (++count >= limit)
  56. break;
  57. }
  58. } else {
  59. // If we don't already have enough members, send to the ones we have and then gather (if allowed within gather rate limit delay)
  60. gs.txQueue.push_back(OutboundMulticast());
  61. OutboundMulticast &out = gs.txQueue.back();
  62. out.init(now,RR->identity.address(),nwid,ZT_MULTICAST_DEFAULT_IMPLICIT_GATHER,src,mg,etherType,data,len);
  63. for(std::vector<MulticastGroupMember>::const_reverse_iterator m(gs.members.rbegin());m!=gs.members.rend();++m)
  64. out.sendAndLog(*(RR->sw),m->address);
  65. if ((now - gs.lastExplicitGather) >= ZT_MULTICAST_GATHER_DELAY) {
  66. gs.lastExplicitGather = now;
  67. // Explicitly gather -- right now we only do this from supernodes since they
  68. // know all multicast group memberships. In the future this might be more
  69. // distributed somehow.
  70. SharedPtr<Peer> sn(RR->topology->getBestSupernode());
  71. if (sn) {
  72. Packet outp(sn->address(),RR->identity.address(),Packet::VERB_MULTICAST_GATHER);
  73. outp.append(nwid);
  74. outp.append((uint8_t)((com) ? 0x01: 0x00));
  75. mg.mac().appendTo(outp);
  76. outp.append((uint32_t)mg.adi());
  77. outp.append((uint32_t)((limit - (unsigned int)gs.members.size()) + 1)); // +1 just means we'll have an extra in the queue if available
  78. if (com) com->serialize(outp);
  79. outp.armor(sn->key(),true);
  80. sn->send(RR,outp.data(),outp.size(),now);
  81. }
  82. }
  83. }
  84. }
  85. void Multicaster::clean(const RuntimeEnvironment *RR,uint64_t now,unsigned int limit)
  86. {
  87. Mutex::Lock _l(_groups_m);
  88. for(std::map< MulticastGroup,MulticastGroupStatus >::iterator mm(_groups.begin());mm!=_groups.end();) {
  89. // Remove expired outgoing multicasts from multicast TX queue
  90. for(std::list<OutboundMulticast>::iterator tx(mm->second.txQueue.begin());tx!=mm->second.txQueue.end();) {
  91. if ((tx->expired(now))||(tx->sentToCount() >= limit))
  92. mm->second.txQueue.erase(tx++);
  93. else ++tx;
  94. }
  95. // Remove expired members from membership list, and update rank
  96. // so that remaining members can be sorted in ascending order of
  97. // transmit priority.
  98. std::vector<MulticastGroupMember>::iterator reader(mm->second.members.begin());
  99. std::vector<MulticastGroupMember>::iterator writer(mm->second.members.begin());
  100. unsigned int count = 0;
  101. while (reader != mm->second.members.end()) {
  102. if ((now - reader->timestamp) < ZT_MULTICAST_LIKE_EXPIRE) {
  103. *writer = *reader;
  104. /* We rank in ascending order of most recent relevant activity. For peers we've learned
  105. * about by direct LIKEs, we do this in order of their own activity. For indirectly
  106. * acquired peers we do this minus a constant to place these categorically below directly
  107. * learned peers. For peers with no active Peer record, we use the time we last learned
  108. * about them minus one day (a large constant) to put these at the bottom of the list.
  109. * List is sorted in ascending order of rank and multicasts are sent last-to-first. */
  110. if (writer->learnedFrom) {
  111. SharedPtr<Peer> p(RR->topology->getPeer(writer->learnedFrom));
  112. if (p)
  113. writer->rank = p->lastUnicastFrame() - ZT_MULTICAST_LIKE_EXPIRE;
  114. else writer->rank = writer->timestamp - (86400000 + ZT_MULTICAST_LIKE_EXPIRE);
  115. } else {
  116. SharedPtr<Peer> p(RR->topology->getPeer(writer->address));
  117. if (p)
  118. writer->rank = p->lastUnicastFrame();
  119. else writer->rank = writer->timestamp - 86400000;
  120. }
  121. ++writer;
  122. ++count;
  123. }
  124. ++reader;
  125. }
  126. if (count) {
  127. // There are remaining members, so re-sort them by rank and resize the vector
  128. std::sort(mm->second.members.begin(),writer); // sorts in ascending order of rank
  129. mm->second.members.resize(count); // trim off the ones we cut, after writer
  130. ++mm;
  131. } else if (mm->second.txQueue.empty()) {
  132. // There are no remaining members and no pending multicasts, so erase the entry
  133. _groups.erase(mm++);
  134. } else ++mm;
  135. }
  136. }
  137. void Multicaster::_add(const RuntimeEnvironment *RR,uint64_t now,MulticastGroupStatus &gs,const Address &learnedFrom,const Address &member)
  138. {
  139. // assumes _groups_m is locked
  140. // Update timestamp and learnedFrom if existing
  141. for(std::vector<MulticastGroupMember>::iterator m(gs.members.begin());m!=gs.members.end();++m) {
  142. if (m->address == member) {
  143. if (m->learnedFrom)
  144. m->learnedFrom = learnedFrom; // only update with indirect learnedFrom if we've never directly learned from this peer
  145. m->timestamp = now;
  146. return;
  147. }
  148. }
  149. // If not existing, add to end of list (highest priority) -- these will
  150. // be resorted on next clean(). In the future we might want to insert
  151. // this somewhere else but we'll try this for now.
  152. gs.members.push_back(MulticastGroupMember(member,learnedFrom,now));
  153. }
  154. } // namespace ZeroTier