Bond.hpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  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_BOND_HPP
  14. #define ZT_BOND_HPP
  15. #include "Flow.hpp"
  16. #include "Packet.hpp"
  17. #include "Path.hpp"
  18. #include "Peer.hpp"
  19. #include <list>
  20. #include <map>
  21. namespace ZeroTier {
  22. class RuntimeEnvironment;
  23. class Link;
  24. class Peer;
  25. class Bond {
  26. friend class SharedPtr<Bond>;
  27. friend class Peer;
  28. friend class BondController;
  29. struct PathQualityComparator {
  30. bool operator()(const SharedPtr<Path>& a, const SharedPtr<Path>& b)
  31. {
  32. if (a->_failoverScore == b->_failoverScore) {
  33. return a < b;
  34. }
  35. return a->_failoverScore > b->_failoverScore;
  36. }
  37. };
  38. public:
  39. // TODO: Remove
  40. bool _header;
  41. int64_t _lastLogTS;
  42. int64_t _lastPrintTS;
  43. void dumpInfo(const int64_t now);
  44. bool relevant();
  45. SharedPtr<Link> getLink(const SharedPtr<Path>& path);
  46. /**
  47. * Constructor. Creates a bond based off of ZT defaults
  48. *
  49. * @param renv Runtime environment
  50. * @param policy Bonding policy
  51. * @param peer
  52. */
  53. Bond(const RuntimeEnvironment* renv, int policy, const SharedPtr<Peer>& peer);
  54. /**
  55. * Constructor. For use when user intends to manually specify parameters
  56. *
  57. * @param basePolicy
  58. * @param policyAlias
  59. * @param peer
  60. */
  61. Bond(const RuntimeEnvironment* renv, std::string& basePolicy, std::string& policyAlias, const SharedPtr<Peer>& peer);
  62. /**
  63. * Constructor. Creates a bond based off of a user-defined bond template
  64. *
  65. * @param renv Runtime environment
  66. * @param original
  67. * @param peer
  68. */
  69. Bond(const RuntimeEnvironment* renv, SharedPtr<Bond> originalBond, const SharedPtr<Peer>& peer);
  70. /**
  71. * @return The human-readable name of the bonding policy
  72. */
  73. std::string policyAlias()
  74. {
  75. return _policyAlias;
  76. }
  77. /**
  78. * Inform the bond about the path that its peer (owning object) just learned about.
  79. * If the path is allowed to be used, it will be inducted into the bond on a trial
  80. * period where link statistics will be collected to judge its quality.
  81. *
  82. * @param path Newly-learned Path which should now be handled by the Bond
  83. * @param now Current time
  84. */
  85. void nominatePath(const SharedPtr<Path>& path, int64_t now);
  86. /**
  87. * Propagate and memoize often-used bonding preferences for each path
  88. */
  89. void applyUserPrefs();
  90. /**
  91. * Check path states and perform bond rebuilds if needed.
  92. *
  93. * @param now Current time
  94. * @param rebuild Whether or not the bond should be reconstructed.
  95. */
  96. void curateBond(const int64_t now, bool rebuild);
  97. /**
  98. * Periodically perform statistical summaries of quality metrics for all paths.
  99. *
  100. * @param now Current time
  101. */
  102. void estimatePathQuality(int64_t now);
  103. /**
  104. * Record an invalid incoming packet. This packet failed
  105. * MAC/compression/cipher checks and will now contribute to a
  106. * Packet Error Ratio (PER).
  107. *
  108. * @param path Path over which packet was received
  109. */
  110. void recordIncomingInvalidPacket(const SharedPtr<Path>& path);
  111. /**
  112. * Record statistics on outbound an packet.
  113. *
  114. * @param path Path over which packet is being sent
  115. * @param packetId Packet ID
  116. * @param payloadLength Packet data length
  117. * @param verb Packet verb
  118. * @param flowId Flow ID
  119. * @param now Current time
  120. */
  121. void recordOutgoingPacket(const SharedPtr<Path>& path, uint64_t packetId, uint16_t payloadLength, Packet::Verb verb, int32_t flowId, int64_t now);
  122. /**
  123. * Process the contents of an inbound VERB_QOS_MEASUREMENT to gather path quality observations.
  124. *
  125. * @param now Current time
  126. * @param count Number of records
  127. * @param rx_id table of packet IDs
  128. * @param rx_ts table of holding times
  129. */
  130. void receivedQoS(const SharedPtr<Path>& path, int64_t now, int count, uint64_t* rx_id, uint16_t* rx_ts);
  131. /**
  132. * Process the contents of an inbound VERB_ACK to gather path quality observations.
  133. *
  134. * @param path Path over which packet was received
  135. * @param now Current time
  136. * @param ackedBytes Number of bytes ACKed by this VERB_ACK
  137. */
  138. void receivedAck(const SharedPtr<Path>& path, int64_t now, int32_t ackedBytes);
  139. /**
  140. * Generate the contents of a VERB_QOS_MEASUREMENT packet.
  141. *
  142. * @param now Current time
  143. * @param qosBuffer destination buffer
  144. * @return Size of payload
  145. */
  146. int32_t generateQoSPacket(const SharedPtr<Path>& path, int64_t now, char* qosBuffer);
  147. /**
  148. * Record statistics for an inbound packet.
  149. *
  150. * @param path Path over which packet was received
  151. * @param packetId Packet ID
  152. * @param payloadLength Packet data length
  153. * @param verb Packet verb
  154. * @param flowId Flow ID
  155. * @param now Current time
  156. */
  157. void recordIncomingPacket(const SharedPtr<Path>& path, uint64_t packetId, uint16_t payloadLength, Packet::Verb verb, int32_t flowId, int64_t now);
  158. /**
  159. * Determines the most appropriate path for packet and flow egress. This decision is made by
  160. * the underlying bonding policy as well as QoS-related statistical observations of path quality.
  161. *
  162. * @param now Current time
  163. * @param flowId Flow ID
  164. * @return Pointer to suggested Path
  165. */
  166. SharedPtr<Path> getAppropriatePath(int64_t now, int32_t flowId);
  167. /**
  168. * Creates a new flow record
  169. *
  170. * @param path Path over which flow shall be handled
  171. * @param flowId Flow ID
  172. * @param entropy A byte of entropy to be used by the bonding algorithm
  173. * @param now Current time
  174. * @return Pointer to newly-created Flow
  175. */
  176. SharedPtr<Flow> createFlow(const SharedPtr<Path>& path, int32_t flowId, unsigned char entropy, int64_t now);
  177. /**
  178. * Removes flow records that are past a certain age limit.
  179. *
  180. * @param age Age threshold to be forgotten
  181. * @param oldest Whether only the oldest shall be forgotten
  182. * @param now Current time
  183. */
  184. void forgetFlowsWhenNecessary(uint64_t age, bool oldest, int64_t now);
  185. /**
  186. * Assigns a new flow to a bonded path
  187. *
  188. * @param flow Flow to be assigned
  189. * @param now Current time
  190. */
  191. bool assignFlowToBondedPath(SharedPtr<Flow>& flow, int64_t now);
  192. /**
  193. * Determine whether a path change should occur given the remote peer's reported utility and our
  194. * local peer's known utility. This has the effect of assigning inbound and outbound traffic to
  195. * the same path.
  196. *
  197. * @param now Current time
  198. * @param path Path over which the negotiation request was received
  199. * @param remoteUtility How much utility the remote peer claims to gain by using the declared path
  200. */
  201. void processIncomingPathNegotiationRequest(uint64_t now, SharedPtr<Path>& path, int16_t remoteUtility);
  202. /**
  203. * Determine state of path synchronization and whether a negotiation request
  204. * shall be sent to the peer.
  205. *
  206. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  207. * @param now Current time
  208. */
  209. void pathNegotiationCheck(void* tPtr, const int64_t now);
  210. /**
  211. * Sends a VERB_ACK to the remote peer.
  212. *
  213. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  214. * @param path Path over which packet should be sent
  215. * @param localSocket Local source socket
  216. * @param atAddress
  217. * @param now Current time
  218. */
  219. void sendACK(void* tPtr, const SharedPtr<Path>& path, int64_t localSocket, const InetAddress& atAddress, int64_t now);
  220. /**
  221. * Sends a VERB_QOS_MEASUREMENT to the remote peer.
  222. *
  223. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  224. * @param path Path over which packet should be sent
  225. * @param localSocket Local source socket
  226. * @param atAddress
  227. * @param now Current time
  228. */
  229. void sendQOS_MEASUREMENT(void* tPtr, const SharedPtr<Path>& path, int64_t localSocket, const InetAddress& atAddress, int64_t now);
  230. /**
  231. * Sends a VERB_PATH_NEGOTIATION_REQUEST to the remote peer.
  232. *
  233. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  234. * @param path Path over which packet should be sent
  235. */
  236. void sendPATH_NEGOTIATION_REQUEST(void* tPtr, const SharedPtr<Path>& path);
  237. /**
  238. *
  239. * @param now Current time
  240. */
  241. void processBalanceTasks(int64_t now);
  242. /**
  243. * Perform periodic tasks unique to active-backup
  244. *
  245. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  246. * @param now Current time
  247. */
  248. void processActiveBackupTasks(void* tPtr, int64_t now);
  249. /**
  250. * Switches the active link in an active-backup scenario to the next best during
  251. * a failover event.
  252. *
  253. * @param now Current time
  254. */
  255. void dequeueNextActiveBackupPath(uint64_t now);
  256. /**
  257. * Set bond parameters to reasonable defaults, these may later be overwritten by
  258. * user-specified parameters.
  259. *
  260. * @param policy Bonding policy
  261. * @param templateBond
  262. */
  263. void setReasonableDefaults(int policy, SharedPtr<Bond> templateBond, bool useTemplate);
  264. /**
  265. * Check and assign user-specified quality weights to this bond.
  266. *
  267. * @param weights Set of user-specified weights
  268. * @param len Length of weight vector
  269. */
  270. void setUserQualityWeights(float weights[], int len);
  271. /**
  272. * @param latencyInMilliseconds Maximum acceptable latency.
  273. */
  274. void setMaxAcceptableLatency(int16_t latencyInMilliseconds)
  275. {
  276. _maxAcceptableLatency = latencyInMilliseconds;
  277. }
  278. /**
  279. * @param latencyInMilliseconds Maximum acceptable (mean) latency.
  280. */
  281. void setMaxAcceptableMeanLatency(int16_t latencyInMilliseconds)
  282. {
  283. _maxAcceptableMeanLatency = latencyInMilliseconds;
  284. }
  285. /**
  286. * @param latencyVarianceInMilliseconds Maximum acceptable packet delay variance (jitter).
  287. */
  288. void setMaxAcceptablePacketDelayVariance(int16_t latencyVarianceInMilliseconds)
  289. {
  290. _maxAcceptablePacketDelayVariance = latencyVarianceInMilliseconds;
  291. }
  292. /**
  293. * @param lossRatio Maximum acceptable packet loss ratio (PLR).
  294. */
  295. void setMaxAcceptablePacketLossRatio(float lossRatio)
  296. {
  297. _maxAcceptablePacketLossRatio = lossRatio;
  298. }
  299. /**
  300. * @param errorRatio Maximum acceptable packet error ratio (PER).
  301. */
  302. void setMaxAcceptablePacketErrorRatio(float errorRatio)
  303. {
  304. _maxAcceptablePacketErrorRatio = errorRatio;
  305. }
  306. /**
  307. * @param errorRatio Maximum acceptable packet error ratio (PER).
  308. */
  309. void setMinAcceptableAllocation(float minAlloc)
  310. {
  311. _minAcceptableAllocation = (uint8_t)(minAlloc * 255);
  312. }
  313. /**
  314. * @return Whether the user has defined links for use on this bond
  315. */
  316. inline bool userHasSpecifiedLinks()
  317. {
  318. return _userHasSpecifiedLinks;
  319. }
  320. /**
  321. * @return Whether the user has defined a set of failover link(s) for this bond
  322. */
  323. inline bool userHasSpecifiedFailoverInstructions()
  324. {
  325. return _userHasSpecifiedFailoverInstructions;
  326. };
  327. /**
  328. * @return Whether the user has specified a primary link
  329. */
  330. inline bool userHasSpecifiedPrimaryLink()
  331. {
  332. return _userHasSpecifiedPrimaryLink;
  333. }
  334. /**
  335. * @return Whether the user has specified link speeds
  336. */
  337. inline bool userHasSpecifiedLinkSpeeds()
  338. {
  339. return _userHasSpecifiedLinkSpeeds;
  340. }
  341. /**
  342. * Periodically perform maintenance tasks for each active bond.
  343. *
  344. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  345. * @param now Current time
  346. */
  347. void processBackgroundTasks(void* tPtr, int64_t now);
  348. /**
  349. * Rate limit gate for VERB_ACK
  350. *
  351. * @param now Current time
  352. * @return Whether the incoming packet should be rate-gated
  353. */
  354. inline bool rateGateACK(const int64_t now)
  355. {
  356. _ackCutoffCount++;
  357. int numToDrain = _lastAckRateCheck ? (now - _lastAckRateCheck) / ZT_ACK_DRAINAGE_DIVISOR : _ackCutoffCount;
  358. _lastAckRateCheck = now;
  359. if (_ackCutoffCount > numToDrain) {
  360. _ackCutoffCount -= numToDrain;
  361. }
  362. else {
  363. _ackCutoffCount = 0;
  364. }
  365. return (_ackCutoffCount < ZT_ACK_CUTOFF_LIMIT);
  366. }
  367. /**
  368. * Rate limit gate for VERB_QOS_MEASUREMENT
  369. *
  370. * @param now Current time
  371. * @return Whether the incoming packet should be rate-gated
  372. */
  373. inline bool rateGateQoS(const int64_t now)
  374. {
  375. _qosCutoffCount++;
  376. int numToDrain = (now - _lastQoSRateCheck) / ZT_QOS_DRAINAGE_DIVISOR;
  377. _lastQoSRateCheck = now;
  378. if (_qosCutoffCount > numToDrain) {
  379. _qosCutoffCount -= numToDrain;
  380. }
  381. else {
  382. _qosCutoffCount = 0;
  383. }
  384. return (_qosCutoffCount < ZT_QOS_CUTOFF_LIMIT);
  385. }
  386. /**
  387. * Rate limit gate for VERB_PATH_NEGOTIATION_REQUEST
  388. *
  389. * @param now Current time
  390. * @return Whether the incoming packet should be rate-gated
  391. */
  392. inline bool rateGatePathNegotiation(const int64_t now)
  393. {
  394. if ((now - _lastPathNegotiationReceived) <= ZT_PATH_NEGOTIATION_CUTOFF_TIME)
  395. ++_pathNegotiationCutoffCount;
  396. else
  397. _pathNegotiationCutoffCount = 0;
  398. _lastPathNegotiationReceived = now;
  399. return (_pathNegotiationCutoffCount < ZT_PATH_NEGOTIATION_CUTOFF_LIMIT);
  400. }
  401. /**
  402. * @param interval Maximum amount of time user expects a failover to take on this bond.
  403. */
  404. inline void setFailoverInterval(uint32_t interval)
  405. {
  406. _failoverInterval = interval;
  407. }
  408. /**
  409. * @param interval Maximum amount of time user expects a failover to take on this bond.
  410. */
  411. inline uint32_t getFailoverInterval()
  412. {
  413. return _failoverInterval;
  414. }
  415. /**
  416. * @param strategy Strategy that the bond uses to re-assign protocol flows.
  417. */
  418. inline void setFlowRebalanceStrategy(uint32_t strategy)
  419. {
  420. _flowRebalanceStrategy = strategy;
  421. }
  422. /**
  423. * @param strategy Strategy that the bond uses to prob for path aliveness and quality
  424. */
  425. inline void setLinkMonitorStrategy(uint8_t strategy)
  426. {
  427. _linkMonitorStrategy = strategy;
  428. }
  429. /**
  430. * @param abOverflowEnabled Whether "overflow" mode is enabled for this active-backup bond
  431. */
  432. inline void setOverflowMode(bool abOverflowEnabled)
  433. {
  434. _abOverflowEnabled = abOverflowEnabled;
  435. }
  436. /**
  437. * @return the current up delay parameter
  438. */
  439. inline uint16_t getUpDelay()
  440. {
  441. return _upDelay;
  442. }
  443. /**
  444. * @param upDelay Length of time before a newly-discovered path is admitted to the bond
  445. */
  446. inline void setUpDelay(int upDelay)
  447. {
  448. if (upDelay >= 0) {
  449. _upDelay = upDelay;
  450. }
  451. }
  452. /**
  453. * @return Length of time before a newly-failed path is removed from the bond
  454. */
  455. inline uint16_t getDownDelay()
  456. {
  457. return _downDelay;
  458. }
  459. /**
  460. * @param downDelay Length of time before a newly-failed path is removed from the bond
  461. */
  462. inline void setDownDelay(int downDelay)
  463. {
  464. if (downDelay >= 0) {
  465. _downDelay = downDelay;
  466. }
  467. }
  468. /**
  469. * @return the current monitoring interval for the bond (can be overridden with intervals specific to certain links.)
  470. */
  471. inline uint16_t getBondMonitorInterval()
  472. {
  473. return _bondMonitorInterval;
  474. }
  475. /**
  476. * Set the current monitoring interval for the bond (can be overridden with intervals specific to certain links.)
  477. *
  478. * @param monitorInterval How often gratuitous VERB_HELLO(s) are sent to remote peer.
  479. */
  480. inline void setBondMonitorInterval(uint16_t interval)
  481. {
  482. _bondMonitorInterval = interval;
  483. }
  484. /**
  485. * @param policy Bonding policy for this bond
  486. */
  487. inline void setPolicy(uint8_t policy)
  488. {
  489. _bondingPolicy = policy;
  490. }
  491. /**
  492. * @return the current bonding policy
  493. */
  494. inline uint8_t getPolicy()
  495. {
  496. return _bondingPolicy;
  497. }
  498. /**
  499. * @return the health status of the bond
  500. */
  501. inline bool isHealthy()
  502. {
  503. return _isHealthy;
  504. }
  505. /**
  506. * @return the number of links comprising this bond which are considered alive
  507. */
  508. inline uint8_t getNumAliveLinks()
  509. {
  510. return _numAliveLinks;
  511. };
  512. /**
  513. * @return the number of links comprising this bond
  514. */
  515. inline uint8_t getNumTotalLinks()
  516. {
  517. return _numTotalLinks;
  518. }
  519. /**
  520. *
  521. * @param allowFlowHashing
  522. */
  523. inline void setFlowHashing(bool allowFlowHashing)
  524. {
  525. _allowFlowHashing = allowFlowHashing;
  526. }
  527. /**
  528. * @return Whether flow-hashing is currently enabled for this bond.
  529. */
  530. bool flowHashingEnabled()
  531. {
  532. return _allowFlowHashing;
  533. }
  534. /**
  535. *
  536. * @param packetsPerLink
  537. */
  538. inline void setPacketsPerLink(int packetsPerLink)
  539. {
  540. _packetsPerLink = packetsPerLink;
  541. }
  542. /**
  543. * @return Number of packets to be sent on each interface in a balance-rr bond
  544. */
  545. inline int getPacketsPerLink()
  546. {
  547. return _packetsPerLink;
  548. }
  549. /**
  550. *
  551. * @param linkSelectMethod
  552. */
  553. inline void setLinkSelectMethod(uint8_t method)
  554. {
  555. _abLinkSelectMethod = method;
  556. }
  557. /**
  558. *
  559. * @return
  560. */
  561. inline uint8_t getLinkSelectMethod()
  562. {
  563. return _abLinkSelectMethod;
  564. }
  565. /**
  566. *
  567. * @param allowPathNegotiation
  568. */
  569. inline void setAllowPathNegotiation(bool allowPathNegotiation)
  570. {
  571. _allowPathNegotiation = allowPathNegotiation;
  572. }
  573. /**
  574. *
  575. * @return
  576. */
  577. inline bool allowPathNegotiation()
  578. {
  579. return _allowPathNegotiation;
  580. }
  581. /**
  582. * Forcibly rotates the currently active link used in an active-backup bond to the next link in the failover queue
  583. *
  584. * @return True if this operation succeeded, false if otherwise
  585. */
  586. bool abForciblyRotateLink();
  587. SharedPtr<Peer> getPeer()
  588. {
  589. return _peer;
  590. }
  591. private:
  592. const RuntimeEnvironment* RR;
  593. AtomicCounter __refCount;
  594. /**
  595. * Custom name given by the user to this bond type.
  596. */
  597. std::string _policyAlias;
  598. /**
  599. * Paths that this bond has been made aware of but that are not necessarily
  600. * part of the bond proper.
  601. */
  602. SharedPtr<Path> _paths[ZT_MAX_PEER_NETWORK_PATHS];
  603. /**
  604. * Set of indices corresponding to paths currently included in the bond proper. This
  605. * may only be updated during a call to curateBond(). The reason for this is so that
  606. * we can simplify the high frequency packet egress logic.
  607. */
  608. int _bondedIdx[ZT_MAX_PEER_NETWORK_PATHS];
  609. /**
  610. * Number of paths currently included in the _bondedIdx set.
  611. */
  612. int _numBondedPaths;
  613. /**
  614. * Flows hashed according to port and protocol
  615. */
  616. std::map<int32_t, SharedPtr<Flow> > _flows;
  617. float _qualityWeights[ZT_QOS_WEIGHT_SIZE]; // How much each factor contributes to the "quality" score of a path.
  618. uint8_t _bondingPolicy;
  619. uint32_t _upDelay;
  620. uint32_t _downDelay;
  621. // active-backup
  622. SharedPtr<Path> _abPath; // current active path
  623. std::list<SharedPtr<Path> > _abFailoverQueue;
  624. uint8_t _abLinkSelectMethod; // link re-selection policy for the primary link in active-backup
  625. bool _abOverflowEnabled;
  626. // balance-rr
  627. uint8_t _rrIdx; // index to path currently in use during Round Robin operation
  628. uint16_t _rrPacketsSentOnCurrLink; // number of packets sent on this link since the most recent path switch.
  629. /**
  630. * How many packets will be sent on a path before moving to the next path
  631. * in the round-robin sequence. A value of zero will cause a random path
  632. * selection for each outgoing packet.
  633. */
  634. int _packetsPerLink;
  635. // balance-aware
  636. uint64_t _totalBondUnderload;
  637. uint8_t _flowRebalanceStrategy;
  638. // dynamic link monitoring
  639. uint8_t _linkMonitorStrategy;
  640. uint32_t _dynamicPathMonitorInterval;
  641. // path negotiation
  642. int16_t _localUtility;
  643. SharedPtr<Path> negotiatedPath;
  644. uint8_t _numSentPathNegotiationRequests;
  645. unsigned int _pathNegotiationCutoffCount;
  646. bool _allowPathNegotiation;
  647. /**
  648. * Timers and intervals
  649. */
  650. uint32_t _failoverInterval;
  651. uint32_t _qosSendInterval;
  652. uint32_t _ackSendInterval;
  653. uint32_t throughputMeasurementInterval;
  654. uint32_t _qualityEstimationInterval;
  655. /**
  656. * Acceptable quality thresholds
  657. */
  658. float _maxAcceptablePacketLossRatio;
  659. float _maxAcceptablePacketErrorRatio;
  660. uint16_t _maxAcceptableLatency;
  661. uint16_t _maxAcceptableMeanLatency;
  662. uint16_t _maxAcceptablePacketDelayVariance;
  663. uint8_t _minAcceptableAllocation;
  664. /**
  665. * Link state reporting
  666. */
  667. bool _isHealthy;
  668. uint8_t _numAliveLinks;
  669. uint8_t _numTotalLinks;
  670. /**
  671. * Default initial punishment inflicted on misbehaving paths. Punishment slowly
  672. * drains linearly. For each eligibility change the remaining punishment is doubled.
  673. */
  674. uint32_t _defaultPathRefractoryPeriod;
  675. /**
  676. * Whether the current bonding policy requires computation of path statistics
  677. */
  678. bool _shouldCollectPathStatistics;
  679. /**
  680. * Free byte of entropy that is updated on every packet egress event.
  681. */
  682. unsigned char _freeRandomByte;
  683. /**
  684. * Remote peer that this bond services
  685. */
  686. SharedPtr<Peer> _peer;
  687. /**
  688. * Rate-limit cutoffs
  689. */
  690. uint16_t _qosCutoffCount;
  691. uint16_t _ackCutoffCount;
  692. /**
  693. * Recent event timestamps
  694. */
  695. uint64_t _lastAckRateCheck;
  696. uint64_t _lastQoSRateCheck;
  697. uint64_t _lastQualityEstimation;
  698. uint64_t _lastCheckUserPreferences;
  699. uint64_t _lastBackgroundTaskCheck;
  700. uint64_t _lastBondStatusLog;
  701. uint64_t _lastPathNegotiationReceived;
  702. uint64_t _lastPathNegotiationCheck;
  703. uint64_t _lastSentPathNegotiationRequest;
  704. uint64_t _lastFlowStatReset;
  705. uint64_t _lastFlowExpirationCheck;
  706. uint64_t _lastFlowRebalance;
  707. uint64_t _lastFrame;
  708. uint64_t _lastActiveBackupPathChange;
  709. Mutex _paths_m;
  710. Mutex _flows_m;
  711. /**
  712. * Whether the user has specified links for this bond.
  713. */
  714. bool _userHasSpecifiedLinks;
  715. /**
  716. * Whether the user has specified a primary link for this bond.
  717. */
  718. bool _userHasSpecifiedPrimaryLink;
  719. /**
  720. * Whether the user has specified failover instructions for this bond.
  721. */
  722. bool _userHasSpecifiedFailoverInstructions;
  723. /**
  724. * Whether the user has specified links speeds for this bond.
  725. */
  726. bool _userHasSpecifiedLinkSpeeds;
  727. /**
  728. * How frequently (in ms) a VERB_ECHO is sent to a peer to verify that a
  729. * path is still active. A value of zero (0) will disable active path
  730. * monitoring; as result, all monitoring will be a function of traffic.
  731. */
  732. uint16_t _bondMonitorInterval;
  733. /**
  734. * Whether or not flow hashing is allowed.
  735. */
  736. bool _allowFlowHashing;
  737. };
  738. } // namespace ZeroTier
  739. #endif