BondController.cpp 6.6 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 "BondController.hpp"
  14. #include "Peer.hpp"
  15. namespace ZeroTier {
  16. int BondController::_minReqPathMonitorInterval;
  17. uint8_t BondController::_defaultBondingPolicy;
  18. BondController::BondController(const RuntimeEnvironment *renv) :
  19. RR(renv)
  20. {
  21. bondStartTime = RR->node->now();
  22. }
  23. bool BondController::slaveAllowed(std::string &policyAlias, SharedPtr<Slave> slave)
  24. {
  25. bool foundInDefinitions = false;
  26. if (_slaveDefinitions.count(policyAlias)) {
  27. auto it = _slaveDefinitions[policyAlias].begin();
  28. while (it != _slaveDefinitions[policyAlias].end()) {
  29. if (slave->ifname() == (*it)->ifname()) {
  30. foundInDefinitions = true;
  31. break;
  32. }
  33. ++it;
  34. }
  35. }
  36. return _slaveDefinitions[policyAlias].empty() || foundInDefinitions;
  37. }
  38. void BondController::addCustomSlave(std::string& policyAlias, SharedPtr<Slave> slave)
  39. {
  40. Mutex::Lock _l(_slaves_m);
  41. _slaveDefinitions[policyAlias].push_back(slave);
  42. auto search = _interfaceToSlaveMap[policyAlias].find(slave->ifname());
  43. if (search == _interfaceToSlaveMap[policyAlias].end()) {
  44. slave->setAsUserSpecified(true);
  45. _interfaceToSlaveMap[policyAlias].insert(std::pair<std::string, SharedPtr<Slave>>(slave->ifname(), slave));
  46. } else {
  47. fprintf(stderr, "slave already exists=%s\n", slave->ifname().c_str());
  48. // Slave is already defined, overlay user settings
  49. }
  50. }
  51. bool BondController::addCustomPolicy(const SharedPtr<Bond>& newBond)
  52. {
  53. Mutex::Lock _l(_bonds_m);
  54. if (!_bondPolicyTemplates.count(newBond->policyAlias())) {
  55. _bondPolicyTemplates[newBond->policyAlias()] = newBond;
  56. return true;
  57. }
  58. return false;
  59. }
  60. bool BondController::assignBondingPolicyToPeer(int64_t identity, const std::string& policyAlias)
  61. {
  62. Mutex::Lock _l(_bonds_m);
  63. if (!_policyTemplateAssignments.count(identity)) {
  64. _policyTemplateAssignments[identity] = policyAlias;
  65. return true;
  66. }
  67. return false;
  68. }
  69. SharedPtr<Bond> BondController::createTransportTriggeredBond(const RuntimeEnvironment *renv, const SharedPtr<Peer>& peer)
  70. {
  71. fprintf(stderr, "createTransportTriggeredBond\n");
  72. Mutex::Lock _l(_bonds_m);
  73. int64_t identity = peer->identity().address().toInt();
  74. Bond *bond = nullptr;
  75. if (!_bonds.count(identity)) {
  76. std::string policyAlias;
  77. int _defaultBondingPolicy = defaultBondingPolicy();
  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, cannot re-register. exiting\n", identity); exit(0); // TODO: Remove
  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 (_interfaceToSlaveMap.count(bond->policyAlias())) {
  109. std::map<std::string, SharedPtr<Slave> >::iterator it = _interfaceToSlaveMap[bond->policyAlias()].begin();
  110. while (it != _interfaceToSlaveMap[bond->policyAlias()].end()) {
  111. if (it->second->isUserSpecified()) {
  112. bond->_userHasSpecifiedSlaves = true;
  113. }
  114. if (it->second->isUserSpecified() && it->second->primary()) {
  115. bond->_userHasSpecifiedPrimarySlave = 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->_userHasSpecifiedSlaveSpeeds = true;
  122. }
  123. ++it;
  124. }
  125. }
  126. return bond;
  127. }
  128. return SharedPtr<Bond>();
  129. }
  130. SharedPtr<Slave> BondController::getSlaveBySocket(const std::string& policyAlias, uint64_t localSocket)
  131. {
  132. Mutex::Lock _l(_slaves_m);
  133. char ifname[16];
  134. _phy->getIfName((PhySocket *) ((uintptr_t)localSocket), ifname, 16);
  135. std::string ifnameStr(ifname);
  136. auto search = _interfaceToSlaveMap[policyAlias].find(ifnameStr);
  137. if (search == _interfaceToSlaveMap[policyAlias].end()) {
  138. SharedPtr<Slave> s = new Slave(ifnameStr, 0, 0, 0, 0, 0, true, ZT_MULTIPATH_SLAVE_MODE_SPARE, "", 0.0);
  139. _interfaceToSlaveMap[policyAlias].insert(std::pair<std::string,SharedPtr<Slave> >(ifnameStr, s));
  140. return s;
  141. }
  142. else {
  143. return search->second;
  144. }
  145. }
  146. SharedPtr<Slave> BondController::getSlaveByName(const std::string& policyAlias, const std::string& ifname)
  147. {
  148. Mutex::Lock _l(_slaves_m);
  149. auto search = _interfaceToSlaveMap[policyAlias].find(ifname);
  150. if (search != _interfaceToSlaveMap[policyAlias].end()) {
  151. return search->second;
  152. }
  153. return SharedPtr<Slave>();
  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(_slaves_m);
  163. if (_interfaceToSlaveMap.empty()) {
  164. return true; // no restrictions
  165. }
  166. std::map<std::string, std::map<std::string, SharedPtr<Slave> > >::iterator policyItr = _interfaceToSlaveMap.begin();
  167. while (policyItr != _interfaceToSlaveMap.end()) {
  168. std::map<std::string, SharedPtr<Slave> >::iterator slaveItr = policyItr->second.begin();
  169. while (slaveItr != policyItr->second.end()) {
  170. if (slaveItr->first == ifname) {
  171. return true;
  172. }
  173. ++slaveItr;
  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