Slave.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. #ifndef ZT_SLAVE_HPP
  14. #define ZT_SLAVE_HPP
  15. #include <string>
  16. #include "../node/AtomicCounter.hpp"
  17. namespace ZeroTier {
  18. class Slave
  19. {
  20. friend class SharedPtr<Slave>;
  21. public:
  22. Slave() {}
  23. /**
  24. *
  25. * @param ifnameStr
  26. * @param ipvPref
  27. * @param speed
  28. * @param enabled
  29. * @param mode
  30. * @param failoverToSlaveStr
  31. * @param userSpecifiedAlloc
  32. */
  33. Slave(std::string& ifnameStr,
  34. uint8_t ipvPref,
  35. uint32_t speed,
  36. uint32_t slaveMonitorInterval,
  37. uint32_t upDelay,
  38. uint32_t downDelay,
  39. bool enabled,
  40. uint8_t mode,
  41. std::string failoverToSlaveStr,
  42. float userSpecifiedAlloc) :
  43. _ifnameStr(ifnameStr),
  44. _ipvPref(ipvPref),
  45. _speed(speed),
  46. _relativeSpeed(0),
  47. _slaveMonitorInterval(slaveMonitorInterval),
  48. _upDelay(upDelay),
  49. _downDelay(downDelay),
  50. _enabled(enabled),
  51. _mode(mode),
  52. _failoverToSlaveStr(failoverToSlaveStr),
  53. _userSpecifiedAlloc(userSpecifiedAlloc),
  54. _isUserSpecified(false)
  55. {}
  56. /**
  57. * @return The string representation of this slave's underlying interface's system name.
  58. */
  59. inline std::string ifname() { return _ifnameStr; }
  60. /**
  61. * @return Whether this slave is designated as a primary.
  62. */
  63. inline bool primary() { return _mode == ZT_MULTIPATH_SLAVE_MODE_PRIMARY; }
  64. /**
  65. * @return Whether this slave is designated as a spare.
  66. */
  67. inline bool spare() { return _mode == ZT_MULTIPATH_SLAVE_MODE_SPARE; }
  68. /**
  69. * @return The name of the slave interface that should be used in the event of a failure.
  70. */
  71. inline std::string failoverToSlave() { return _failoverToSlaveStr; }
  72. /**
  73. * @return Whether this slave interface was specified by the user or auto-detected.
  74. */
  75. inline bool isUserSpecified() { return _isUserSpecified; }
  76. /**
  77. * Signify that this slave was specified by the user and not the result of auto-detection.
  78. *
  79. * @param isUserSpecified
  80. */
  81. inline void setAsUserSpecified(bool isUserSpecified) { _isUserSpecified = isUserSpecified; }
  82. /**
  83. * @return Whether or not the user has specified failover instructions.
  84. */
  85. inline bool userHasSpecifiedFailoverInstructions() { return _failoverToSlaveStr.length(); }
  86. /**
  87. * @return The speed of the slave relative to others in the bond.
  88. */
  89. inline uint8_t relativeSpeed() { return _relativeSpeed; }
  90. /**
  91. * Sets the speed of the slave relative to others in the bond.
  92. *
  93. * @param relativeSpeed The speed relative to the rest of the slave interfaces.
  94. */
  95. inline void setRelativeSpeed(uint8_t relativeSpeed) { _relativeSpeed = relativeSpeed; }
  96. /**
  97. * Sets the speed of the slave relative to others in the bond.
  98. *
  99. * @param relativeSpeed
  100. */
  101. inline void setMonitorInterval(uint32_t interval) { _slaveMonitorInterval = interval; }
  102. /**
  103. * @return The absolute speed of the slave interface (as specified by the user.)
  104. */
  105. inline uint32_t monitorInterval() { return _slaveMonitorInterval; }
  106. /**
  107. * @return The absolute speed of the slave interface (as specified by the user.)
  108. */
  109. inline uint32_t speed() { return _speed; }
  110. /**
  111. * @return The address preference for this slave interface (as specified by the user.)
  112. */
  113. inline uint8_t ipvPref() { return _ipvPref; }
  114. /**
  115. * @return The mode (e.g. primary/spare) for this slave interface (as specified by the user.)
  116. */
  117. inline uint8_t mode() { return _mode; }
  118. /**
  119. * @return The upDelay parameter for all paths on this slave interface.
  120. */
  121. inline uint32_t upDelay() { return _upDelay; }
  122. /**
  123. * @return The downDelay parameter for all paths on this slave interface.
  124. */
  125. inline uint32_t downDelay() { return _downDelay; }
  126. /**
  127. * @return Whether this slave is enabled or disabled
  128. */
  129. inline uint8_t enabled() { return _enabled; }
  130. private:
  131. /**
  132. * String representation of underlying interface's system name
  133. */
  134. std::string _ifnameStr;
  135. /**
  136. * What preference (if any) a user has for IP protocol version used in
  137. * path aggregations. Preference is expressed in the order of the digits:
  138. *
  139. * 0: no preference
  140. * 4: IPv4 only
  141. * 6: IPv6 only
  142. * 46: IPv4 over IPv6
  143. * 64: IPv6 over IPv4
  144. */
  145. uint8_t _ipvPref;
  146. /**
  147. * User-specified speed of this slave/link
  148. */
  149. uint32_t _speed;
  150. /**
  151. * Speed relative to other specified slaves/links (computed by Bond)
  152. */
  153. uint8_t _relativeSpeed;
  154. /**
  155. * User-specified interval for monitoring paths on this specific slave
  156. * instead of using the more generic interval specified for the entire
  157. * bond.
  158. */
  159. uint32_t _slaveMonitorInterval;
  160. /**
  161. * How long before a path is considered to be usable after coming online. (when using policies that
  162. * support fail-over events).
  163. */
  164. uint32_t _upDelay;
  165. /**
  166. * How long before a path is considered to be dead (when using policies that
  167. * support fail-over events).
  168. */
  169. uint32_t _downDelay;
  170. /**
  171. * Whether this slave is enabled, or (disabled (possibly bad config))
  172. */
  173. uint8_t _enabled;
  174. /**
  175. * Whether this slave is designated as a primary, a spare, or no preference.
  176. */
  177. uint8_t _mode;
  178. /**
  179. * The specific name of the interface to be used in the event that this
  180. * slave fails.
  181. */
  182. std::string _failoverToSlaveStr;
  183. /**
  184. * User-specified allocation
  185. */
  186. float _userSpecifiedAlloc;
  187. /**
  188. * Whether or not this slave was created as a result of manual user specification. This is
  189. * important to know because certain policy decisions are dependent on whether the user
  190. * intents to use a specific set of interfaces.
  191. */
  192. bool _isUserSpecified;
  193. AtomicCounter __refCount;
  194. };
  195. } // namespace ZeroTier
  196. #endif