BondController.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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::slaveAllowed(std::string &policyAlias, SharedPtr<Slave> slave)
  26. {
  27. bool foundInDefinitions = false;
  28. if (_slaveDefinitions.count(policyAlias)) {
  29. auto it = _slaveDefinitions[policyAlias].begin();
  30. while (it != _slaveDefinitions[policyAlias].end()) {
  31. if (slave->ifname() == (*it)->ifname()) {
  32. foundInDefinitions = true;
  33. break;
  34. }
  35. ++it;
  36. }
  37. }
  38. return _slaveDefinitions[policyAlias].empty() || foundInDefinitions;
  39. }
  40. void BondController::addCustomSlave(std::string& policyAlias, SharedPtr<Slave> slave)
  41. {
  42. Mutex::Lock _l(_slaves_m);
  43. _slaveDefinitions[policyAlias].push_back(slave);
  44. auto search = _interfaceToSlaveMap[policyAlias].find(slave->ifname());
  45. if (search == _interfaceToSlaveMap[policyAlias].end()) {
  46. slave->setAsUserSpecified(true);
  47. _interfaceToSlaveMap[policyAlias].insert(std::pair<std::string, SharedPtr<Slave>>(slave->ifname(), slave));
  48. } else {
  49. fprintf(stderr, "slave already exists=%s\n", slave->ifname().c_str());
  50. // Slave 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. fprintf(stderr, "createTransportTriggeredBond\n");
  74. Mutex::Lock _l(_bonds_m);
  75. int64_t identity = peer->identity().address().toInt();
  76. Bond *bond = nullptr;
  77. if (!_bonds.count(identity)) {
  78. std::string policyAlias;
  79. fprintf(stderr, "new bond, registering for %llx\n", identity);
  80. if (!_policyTemplateAssignments.count(identity)) {
  81. if (_defaultBondingPolicy) {
  82. fprintf(stderr, " no assignment, using default (%d)\n", _defaultBondingPolicy);
  83. bond = new Bond(renv, _defaultBondingPolicy, peer);
  84. }
  85. if (!_defaultBondingPolicy && _defaultBondingPolicyStr.length()) {
  86. fprintf(stderr, " no assignment, using default custom (%s)\n", _defaultBondingPolicyStr.c_str());
  87. bond = new Bond(renv, _bondPolicyTemplates[_defaultBondingPolicyStr].ptr(), peer);
  88. }
  89. }
  90. else {
  91. fprintf(stderr, " assignment found for %llx, using it as a template (%s)\n", identity,_policyTemplateAssignments[identity].c_str());
  92. if (!_bondPolicyTemplates[_policyTemplateAssignments[identity]]) {
  93. fprintf(stderr, "unable to locate template (%s), ignoring assignment for (%llx), using defaults\n", _policyTemplateAssignments[identity].c_str(), identity);
  94. bond = new Bond(renv, _defaultBondingPolicy, peer);
  95. }
  96. else {
  97. bond = new Bond(renv, _bondPolicyTemplates[_policyTemplateAssignments[identity]].ptr(), peer);
  98. }
  99. }
  100. }
  101. else {
  102. fprintf(stderr, "bond already exists for %llx.\n", identity);
  103. }
  104. if (bond) {
  105. _bonds[identity] = bond;
  106. /**
  107. * Determine if user has specified anything that could affect the bonding policy's decisions
  108. */
  109. if (_interfaceToSlaveMap.count(bond->policyAlias())) {
  110. std::map<std::string, SharedPtr<Slave> >::iterator it = _interfaceToSlaveMap[bond->policyAlias()].begin();
  111. while (it != _interfaceToSlaveMap[bond->policyAlias()].end()) {
  112. if (it->second->isUserSpecified()) {
  113. bond->_userHasSpecifiedSlaves = true;
  114. }
  115. if (it->second->isUserSpecified() && it->second->primary()) {
  116. bond->_userHasSpecifiedPrimarySlave = true;
  117. }
  118. if (it->second->isUserSpecified() && it->second->userHasSpecifiedFailoverInstructions()) {
  119. bond->_userHasSpecifiedFailoverInstructions = true;
  120. }
  121. if (it->second->isUserSpecified() && (it->second->speed() > 0)) {
  122. bond->_userHasSpecifiedSlaveSpeeds = true;
  123. }
  124. ++it;
  125. }
  126. }
  127. return bond;
  128. }
  129. return SharedPtr<Bond>();
  130. }
  131. SharedPtr<Slave> BondController::getSlaveBySocket(const std::string& policyAlias, uint64_t localSocket)
  132. {
  133. Mutex::Lock _l(_slaves_m);
  134. char ifname[16];
  135. _phy->getIfName((PhySocket *) ((uintptr_t)localSocket), ifname, 16);
  136. std::string ifnameStr(ifname);
  137. auto search = _interfaceToSlaveMap[policyAlias].find(ifnameStr);
  138. if (search == _interfaceToSlaveMap[policyAlias].end()) {
  139. SharedPtr<Slave> s = new Slave(ifnameStr, 0, 0, 0, 0, 0, true, ZT_MULTIPATH_SLAVE_MODE_SPARE, "", 0.0);
  140. _interfaceToSlaveMap[policyAlias].insert(std::pair<std::string,SharedPtr<Slave> >(ifnameStr, s));
  141. return s;
  142. }
  143. else {
  144. return search->second;
  145. }
  146. }
  147. SharedPtr<Slave> BondController::getSlaveByName(const std::string& policyAlias, const std::string& ifname)
  148. {
  149. Mutex::Lock _l(_slaves_m);
  150. auto search = _interfaceToSlaveMap[policyAlias].find(ifname);
  151. if (search != _interfaceToSlaveMap[policyAlias].end()) {
  152. return search->second;
  153. }
  154. return SharedPtr<Slave>();
  155. }
  156. bool BondController::allowedToBind(const std::string& ifname)
  157. {
  158. return true;
  159. /*
  160. if (!_defaultBondingPolicy) {
  161. return true; // no restrictions
  162. }
  163. Mutex::Lock _l(_slaves_m);
  164. if (_interfaceToSlaveMap.empty()) {
  165. return true; // no restrictions
  166. }
  167. std::map<std::string, std::map<std::string, SharedPtr<Slave> > >::iterator policyItr = _interfaceToSlaveMap.begin();
  168. while (policyItr != _interfaceToSlaveMap.end()) {
  169. std::map<std::string, SharedPtr<Slave> >::iterator slaveItr = policyItr->second.begin();
  170. while (slaveItr != policyItr->second.end()) {
  171. if (slaveItr->first == ifname) {
  172. return true;
  173. }
  174. ++slaveItr;
  175. }
  176. ++policyItr;
  177. }
  178. return false;
  179. */
  180. }
  181. void BondController::processBackgroundTasks(void *tPtr, const int64_t now)
  182. {
  183. Mutex::Lock _l(_bonds_m);
  184. std::map<int64_t,SharedPtr<Bond> >::iterator bondItr = _bonds.begin();
  185. while (bondItr != _bonds.end()) {
  186. bondItr->second->processBackgroundTasks(tPtr, now);
  187. ++bondItr;
  188. }
  189. }
  190. } // namespace ZeroTier