BondController.hpp 6.6 KB

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