BondController.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Copyright (c)2013-2020 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: 2024-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 "Constants.hpp"
  14. #include "BondController.hpp"
  15. #include "Peer.hpp"
  16. namespace ZeroTier {
  17. int BondController::_minReqPathMonitorInterval;
  18. uint8_t BondController::_defaultBondingPolicy;
  19. BondController::BondController(const RuntimeEnvironment *renv) :
  20. RR(renv)
  21. {
  22. bondStartTime = RR->node->now();
  23. _defaultBondingPolicy = ZT_BONDING_POLICY_NONE;
  24. }
  25. bool BondController::linkAllowed(std::string &policyAlias, SharedPtr<Link> link)
  26. {
  27. bool foundInDefinitions = false;
  28. if (_linkDefinitions.count(policyAlias)) {
  29. auto it = _linkDefinitions[policyAlias].begin();
  30. while (it != _linkDefinitions[policyAlias].end()) {
  31. if (link->ifname() == (*it)->ifname()) {
  32. foundInDefinitions = true;
  33. break;
  34. }
  35. ++it;
  36. }
  37. }
  38. return _linkDefinitions[policyAlias].empty() || foundInDefinitions;
  39. }
  40. void BondController::addCustomLink(std::string& policyAlias, SharedPtr<Link> link)
  41. {
  42. Mutex::Lock _l(_links_m);
  43. _linkDefinitions[policyAlias].push_back(link);
  44. auto search = _interfaceToLinkMap[policyAlias].find(link->ifname());
  45. if (search == _interfaceToLinkMap[policyAlias].end()) {
  46. link->setAsUserSpecified(true);
  47. _interfaceToLinkMap[policyAlias].insert(std::pair<std::string, SharedPtr<Link>>(link->ifname(), link));
  48. } else {
  49. //fprintf(stderr, "link already exists=%s\n", link->ifname().c_str());
  50. // Link is already defined, overlay user settings
  51. }
  52. }
  53. bool BondController::addCustomPolicy(const SharedPtr<Bond>& newBond)
  54. {
  55. Mutex::Lock _l(_bonds_m);
  56. if (!_bondPolicyTemplates.count(newBond->policyAlias())) {
  57. _bondPolicyTemplates[newBond->policyAlias()] = newBond;
  58. return true;
  59. }
  60. return false;
  61. }
  62. bool BondController::assignBondingPolicyToPeer(int64_t identity, const std::string& policyAlias)
  63. {
  64. Mutex::Lock _l(_bonds_m);
  65. if (!_policyTemplateAssignments.count(identity)) {
  66. _policyTemplateAssignments[identity] = policyAlias;
  67. return true;
  68. }
  69. return false;
  70. }
  71. SharedPtr<Bond> BondController::createTransportTriggeredBond(const RuntimeEnvironment *renv, const SharedPtr<Peer>& peer)
  72. {
  73. Mutex::Lock _l(_bonds_m);
  74. int64_t identity = peer->identity().address().toInt();
  75. Bond *bond = nullptr;
  76. if (!_bonds.count(identity)) {
  77. std::string policyAlias;
  78. //fprintf(stderr, "new bond, registering for %llx\n", identity);
  79. if (!_policyTemplateAssignments.count(identity)) {
  80. if (_defaultBondingPolicy) {
  81. //fprintf(stderr, " no assignment, using default (%d)\n", _defaultBondingPolicy);
  82. bond = new Bond(renv, _defaultBondingPolicy, peer);
  83. }
  84. if (!_defaultBondingPolicy && _defaultBondingPolicyStr.length()) {
  85. //fprintf(stderr, " no assignment, using default custom (%s)\n", _defaultBondingPolicyStr.c_str());
  86. bond = new Bond(renv, _bondPolicyTemplates[_defaultBondingPolicyStr].ptr(), peer);
  87. }
  88. }
  89. else {
  90. //fprintf(stderr, " assignment found for %llx, using it as a template (%s)\n", identity,_policyTemplateAssignments[identity].c_str());
  91. if (!_bondPolicyTemplates[_policyTemplateAssignments[identity]]) {
  92. //fprintf(stderr, "unable to locate template (%s), ignoring assignment for (%llx), using defaults\n", _policyTemplateAssignments[identity].c_str(), identity);
  93. bond = new Bond(renv, _defaultBondingPolicy, peer);
  94. }
  95. else {
  96. bond = new Bond(renv, _bondPolicyTemplates[_policyTemplateAssignments[identity]].ptr(), peer);
  97. }
  98. }
  99. }
  100. else {
  101. //fprintf(stderr, "bond already exists for %llx.\n", identity);
  102. }
  103. if (bond) {
  104. _bonds[identity] = bond;
  105. /**
  106. * Determine if user has specified anything that could affect the bonding policy's decisions
  107. */
  108. if (_interfaceToLinkMap.count(bond->policyAlias())) {
  109. std::map<std::string, SharedPtr<Link> >::iterator it = _interfaceToLinkMap[bond->policyAlias()].begin();
  110. while (it != _interfaceToLinkMap[bond->policyAlias()].end()) {
  111. if (it->second->isUserSpecified()) {
  112. bond->_userHasSpecifiedLinks = true;
  113. }
  114. if (it->second->isUserSpecified() && it->second->primary()) {
  115. bond->_userHasSpecifiedPrimaryLink = true;
  116. }
  117. if (it->second->isUserSpecified() && it->second->userHasSpecifiedFailoverInstructions()) {
  118. bond->_userHasSpecifiedFailoverInstructions = true;
  119. }
  120. if (it->second->isUserSpecified() && (it->second->speed() > 0)) {
  121. bond->_userHasSpecifiedLinkSpeeds = true;
  122. }
  123. ++it;
  124. }
  125. }
  126. return bond;
  127. }
  128. return SharedPtr<Bond>();
  129. }
  130. SharedPtr<Link> BondController::getLinkBySocket(const std::string& policyAlias, uint64_t localSocket)
  131. {
  132. Mutex::Lock _l(_links_m);
  133. char ifname[16];
  134. _phy->getIfName((PhySocket *) ((uintptr_t)localSocket), ifname, 16);
  135. std::string ifnameStr(ifname);
  136. auto search = _interfaceToLinkMap[policyAlias].find(ifnameStr);
  137. if (search == _interfaceToLinkMap[policyAlias].end()) {
  138. SharedPtr<Link> s = new Link(ifnameStr, 0, 0, 0, 0, 0, true, ZT_MULTIPATH_SLAVE_MODE_SPARE, "", 0.0);
  139. _interfaceToLinkMap[policyAlias].insert(std::pair<std::string,SharedPtr<Link> >(ifnameStr, s));
  140. return s;
  141. }
  142. else {
  143. return search->second;
  144. }
  145. }
  146. SharedPtr<Link> BondController::getLinkByName(const std::string& policyAlias, const std::string& ifname)
  147. {
  148. Mutex::Lock _l(_links_m);
  149. auto search = _interfaceToLinkMap[policyAlias].find(ifname);
  150. if (search != _interfaceToLinkMap[policyAlias].end()) {
  151. return search->second;
  152. }
  153. return SharedPtr<Link>();
  154. }
  155. bool BondController::allowedToBind(const std::string& ifname)
  156. {
  157. return true;
  158. /*
  159. if (!_defaultBondingPolicy) {
  160. return true; // no restrictions
  161. }
  162. Mutex::Lock _l(_links_m);
  163. if (_interfaceToLinkMap.empty()) {
  164. return true; // no restrictions
  165. }
  166. std::map<std::string, std::map<std::string, SharedPtr<Link> > >::iterator policyItr = _interfaceToLinkMap.begin();
  167. while (policyItr != _interfaceToLinkMap.end()) {
  168. std::map<std::string, SharedPtr<Link> >::iterator linkItr = policyItr->second.begin();
  169. while (linkItr != policyItr->second.end()) {
  170. if (linkItr->first == ifname) {
  171. return true;
  172. }
  173. ++linkItr;
  174. }
  175. ++policyItr;
  176. }
  177. return false;
  178. */
  179. }
  180. void BondController::processBackgroundTasks(void *tPtr, const int64_t now)
  181. {
  182. Mutex::Lock _l(_bonds_m);
  183. std::map<int64_t,SharedPtr<Bond> >::iterator bondItr = _bonds.begin();
  184. while (bondItr != _bonds.end()) {
  185. bondItr->second->processBackgroundTasks(tPtr, now);
  186. ++bondItr;
  187. }
  188. }
  189. } // namespace ZeroTier