BondController.hpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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: 2025-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. #ifndef ZT_BONDCONTROLLER_HPP
  14. #define ZT_BONDCONTROLLER_HPP
  15. #include <map>
  16. #include <vector>
  17. #include "SharedPtr.hpp"
  18. #include "../osdep/Phy.hpp"
  19. #include "../osdep/Link.hpp"
  20. namespace ZeroTier {
  21. class RuntimeEnvironment;
  22. class Bond;
  23. class Peer;
  24. class BondController
  25. {
  26. friend class Bond;
  27. public:
  28. BondController(const RuntimeEnvironment *renv);
  29. /**
  30. * @return Whether this link is permitted to become a member of a bond.
  31. */
  32. bool linkAllowed(std::string &policyAlias, SharedPtr<Link> link);
  33. /**
  34. * @return The minimum interval required to poll the active bonds to fulfill all active monitoring timing requirements.
  35. */
  36. int minReqPathMonitorInterval() { return _minReqPathMonitorInterval; }
  37. /**
  38. * @param minReqPathMonitorInterval The minimum interval required to poll the active bonds to fulfill all active monitoring timing requirements.
  39. */
  40. static void setMinReqPathMonitorInterval(int minReqPathMonitorInterval) { _minReqPathMonitorInterval = minReqPathMonitorInterval; }
  41. /**
  42. * @return Whether the bonding layer is currently set up to be used.
  43. */
  44. bool inUse() { return !_bondPolicyTemplates.empty() || _defaultBondingPolicy; }
  45. /**
  46. * @param basePolicyName Bonding policy name (See ZeroTierOne.h)
  47. * @return The bonding policy code for a given human-readable bonding policy name
  48. */
  49. static int getPolicyCodeByStr(const std::string& basePolicyName)
  50. {
  51. if (basePolicyName == "active-backup") { return 1; }
  52. if (basePolicyName == "broadcast") { return 2; }
  53. if (basePolicyName == "balance-rr") { return 3; }
  54. if (basePolicyName == "balance-xor") { return 4; }
  55. if (basePolicyName == "balance-aware") { return 5; }
  56. return 0; // "none"
  57. }
  58. /**
  59. * @param policy Bonding policy code (See ZeroTierOne.h)
  60. * @return The human-readable name for the given bonding policy code
  61. */
  62. static std::string getPolicyStrByCode(int policy)
  63. {
  64. if (policy == 1) { return "active-backup"; }
  65. if (policy == 2) { return "broadcast"; }
  66. if (policy == 3) { return "balance-rr"; }
  67. if (policy == 4) { return "balance-xor"; }
  68. if (policy == 5) { return "balance-aware"; }
  69. return "none";
  70. }
  71. /**
  72. * Sets the default bonding policy for new or undefined bonds.
  73. *
  74. * @param bp Bonding policy
  75. */
  76. void setBondingLayerDefaultPolicy(uint8_t bp) { _defaultBondingPolicy = bp; }
  77. /**
  78. * Sets the default (custom) bonding policy for new or undefined bonds.
  79. *
  80. * @param alias Human-readable string alias for bonding policy
  81. */
  82. void setBondingLayerDefaultPolicyStr(std::string alias) { _defaultBondingPolicyStr = alias; }
  83. /**
  84. * @return The default bonding policy
  85. */
  86. static int defaultBondingPolicy() { return _defaultBondingPolicy; }
  87. /**
  88. * Add a user-defined link to a given bonding policy.
  89. *
  90. * @param policyAlias User-defined custom name for variant of bonding policy
  91. * @param link Pointer to new link definition
  92. */
  93. void addCustomLink(std::string& policyAlias, SharedPtr<Link> link);
  94. /**
  95. * Add a user-defined bonding policy that is based on one of the standard types.
  96. *
  97. * @param newBond Pointer to custom Bond object
  98. * @return Whether a uniquely-named custom policy was successfully added
  99. */
  100. bool addCustomPolicy(const SharedPtr<Bond>& newBond);
  101. /**
  102. * Assigns a specific bonding policy
  103. *
  104. * @param identity
  105. * @param policyAlias
  106. * @return
  107. */
  108. bool assignBondingPolicyToPeer(int64_t identity, const std::string& policyAlias);
  109. /**
  110. * Get pointer to bond by a given peer ID
  111. *
  112. * @param peer Remote peer ID
  113. * @return A pointer to the Bond
  114. */
  115. SharedPtr<Bond> getBondByPeerId(int64_t identity);
  116. /**
  117. * Add a new bond to the bond controller.
  118. *
  119. * @param renv Runtime environment
  120. * @param peer Remote peer that this bond services
  121. * @return A pointer to the newly created Bond
  122. */
  123. SharedPtr<Bond> createTransportTriggeredBond(const RuntimeEnvironment *renv, const SharedPtr<Peer>& peer);
  124. /**
  125. * Periodically perform maintenance tasks for the bonding layer.
  126. *
  127. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  128. * @param now Current time
  129. */
  130. void processBackgroundTasks(void *tPtr, int64_t now);
  131. /**
  132. * Gets a reference to a physical link definition given a policy alias and a local socket.
  133. *
  134. * @param policyAlias Policy in use
  135. * @param localSocket Local source socket
  136. * @return Physical link definition
  137. */
  138. SharedPtr<Link> getLinkBySocket(const std::string& policyAlias, uint64_t localSocket);
  139. /**
  140. * Gets a reference to a physical link definition given its human-readable system name.
  141. *
  142. * @param policyAlias Policy in use
  143. * @param ifname Alphanumeric human-readable name
  144. * @return Physical link definition
  145. */
  146. SharedPtr<Link> getLinkByName(const std::string& policyAlias, const std::string& ifname);
  147. /**
  148. * @param ifname Name of interface that we want to know if we can bind to
  149. */
  150. bool allowedToBind(const std::string& ifname);
  151. uint64_t getBondStartTime() { return bondStartTime; }
  152. private:
  153. Phy<BondController *> *_phy;
  154. const RuntimeEnvironment *RR;
  155. Mutex _bonds_m;
  156. Mutex _links_m;
  157. /**
  158. * The last time that the bond controller updated the set of bonds.
  159. */
  160. uint64_t _lastBackgroundBondControlTaskCheck;
  161. /**
  162. * The minimum monitoring interval among all paths in this bond.
  163. */
  164. static int _minReqPathMonitorInterval;
  165. /**
  166. * The default bonding policy used for new bonds unless otherwise specified.
  167. */
  168. static uint8_t _defaultBondingPolicy;
  169. /**
  170. * The default bonding policy used for new bonds unless otherwise specified.
  171. */
  172. std::string _defaultBondingPolicyStr;
  173. /**
  174. * All currently active bonds.
  175. */
  176. std::map<int64_t,SharedPtr<Bond> > _bonds;
  177. /**
  178. * Map of peers to custom bonding policies
  179. */
  180. std::map<int64_t,std::string> _policyTemplateAssignments;
  181. /**
  182. * User-defined bonding policies (can be assigned to a peer)
  183. */
  184. std::map<std::string,SharedPtr<Bond> > _bondPolicyTemplates;
  185. /**
  186. * Set of links defined for a given bonding policy
  187. */
  188. std::map<std::string,std::vector<SharedPtr<Link> > > _linkDefinitions;
  189. /**
  190. * Set of link objects mapped to their physical interfaces
  191. */
  192. std::map<std::string, std::map<std::string, SharedPtr<Link> > > _interfaceToLinkMap;
  193. // TODO: Remove
  194. uint64_t bondStartTime;
  195. };
  196. } // namespace ZeroTier
  197. #endif