Link.hpp 5.7 KB

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