ZeroTierOne/node/BondController.hpp

231 lines
6.3 KiB
C++
Raw Normal View History

2020-05-12 08:35:48 +00:00
/*
* Copyright (c)2013-2020 ZeroTier, Inc.
*
* Use of this software is governed by the Business Source License included
* in the LICENSE.TXT file in the project's root directory.
*
2020-08-20 19:51:39 +00:00
* Change Date: 2025-01-01
2020-05-12 08:35:48 +00:00
*
* On the date above, in accordance with the Business Source License, use
* of this software will be governed by version 2.0 of the Apache License.
*/
/****/
#ifndef ZT_BONDCONTROLLER_HPP
#define ZT_BONDCONTROLLER_HPP
#include <map>
#include <vector>
#include "SharedPtr.hpp"
#include "../osdep/Phy.hpp"
2020-06-17 21:54:13 +00:00
#include "../osdep/Link.hpp"
2020-05-12 08:35:48 +00:00
namespace ZeroTier {
class RuntimeEnvironment;
class Bond;
class Peer;
class BondController
{
friend class Bond;
public:
BondController(const RuntimeEnvironment *renv);
/**
2020-06-17 21:54:13 +00:00
* @return Whether this link is permitted to become a member of a bond.
2020-05-12 08:35:48 +00:00
*/
2020-06-17 21:54:13 +00:00
bool linkAllowed(std::string &policyAlias, SharedPtr<Link> link);
2020-05-12 08:35:48 +00:00
/**
* @return The minimum interval required to poll the active bonds to fulfill all active monitoring timing requirements.
*/
int minReqPathMonitorInterval() { return _minReqPathMonitorInterval; }
/**
* @param minReqPathMonitorInterval The minimum interval required to poll the active bonds to fulfill all active monitoring timing requirements.
2020-05-12 08:35:48 +00:00
*/
static void setMinReqPathMonitorInterval(int minReqPathMonitorInterval) { _minReqPathMonitorInterval = minReqPathMonitorInterval; }
/**
* @return Whether the bonding layer is currently set up to be used.
*/
bool inUse() { return !_bondPolicyTemplates.empty() || _defaultBondingPolicy; }
2020-05-15 03:09:25 +00:00
/**
* @param basePolicyName Bonding policy name (See ZeroTierOne.h)
* @return The bonding policy code for a given human-readable bonding policy name
*/
2020-05-12 08:35:48 +00:00
static int getPolicyCodeByStr(const std::string& basePolicyName)
{
if (basePolicyName == "active-backup") { return 1; }
if (basePolicyName == "broadcast") { return 2; }
if (basePolicyName == "balance-rr") { return 3; }
if (basePolicyName == "balance-xor") { return 4; }
if (basePolicyName == "balance-aware") { return 5; }
return 0; // "none"
}
/**
* @param policy Bonding policy code (See ZeroTierOne.h)
* @return The human-readable name for the given bonding policy code
*/
static std::string getPolicyStrByCode(int policy)
{
if (policy == 1) { return "active-backup"; }
if (policy == 2) { return "broadcast"; }
if (policy == 3) { return "balance-rr"; }
if (policy == 4) { return "balance-xor"; }
if (policy == 5) { return "balance-aware"; }
return "none";
}
2020-05-15 03:09:25 +00:00
/**
* Sets the default bonding policy for new or undefined bonds.
2020-05-12 08:35:48 +00:00
*
2020-05-15 03:09:25 +00:00
* @param bp Bonding policy
*/
2020-05-12 08:35:48 +00:00
void setBondingLayerDefaultPolicy(uint8_t bp) { _defaultBondingPolicy = bp; }
2020-05-15 03:09:25 +00:00
/**
* Sets the default (custom) bonding policy for new or undefined bonds.
2020-05-12 08:35:48 +00:00
*
2020-05-15 03:09:25 +00:00
* @param alias Human-readable string alias for bonding policy
*/
2020-05-12 08:35:48 +00:00
void setBondingLayerDefaultPolicyStr(std::string alias) { _defaultBondingPolicyStr = alias; }
/**
* @return The default bonding policy
*/
static int defaultBondingPolicy() { return _defaultBondingPolicy; }
/**
2020-06-17 21:54:13 +00:00
* Add a user-defined link to a given bonding policy.
2020-05-12 08:35:48 +00:00
*
* @param policyAlias User-defined custom name for variant of bonding policy
2020-06-17 21:54:13 +00:00
* @param link Pointer to new link definition
2020-05-12 08:35:48 +00:00
*/
2020-06-17 21:54:13 +00:00
void addCustomLink(std::string& policyAlias, SharedPtr<Link> link);
2020-05-12 08:35:48 +00:00
/**
* Add a user-defined bonding policy that is based on one of the standard types.
*
* @param newBond Pointer to custom Bond object
* @return Whether a uniquely-named custom policy was successfully added
*/
bool addCustomPolicy(const SharedPtr<Bond>& newBond);
/**
2020-05-15 03:09:25 +00:00
* Assigns a specific bonding policy
2020-05-12 08:35:48 +00:00
*
* @param identity
* @param policyAlias
* @return
*/
bool assignBondingPolicyToPeer(int64_t identity, const std::string& policyAlias);
/**
* Add a new bond to the bond controller.
*
* @param renv Runtime environment
* @param peer Remote peer that this bond services
* @return A pointer to the newly created Bond
*/
SharedPtr<Bond> createTransportTriggeredBond(const RuntimeEnvironment *renv, const SharedPtr<Peer>& peer);
/**
* Periodically perform maintenance tasks for the bonding layer.
*
* @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
* @param now Current time
*/
void processBackgroundTasks(void *tPtr, int64_t now);
/**
2020-06-17 21:54:13 +00:00
* Gets a reference to a physical link definition given a policy alias and a local socket.
2020-05-12 08:35:48 +00:00
*
* @param policyAlias Policy in use
* @param localSocket Local source socket
2020-06-17 21:54:13 +00:00
* @return Physical link definition
2020-05-12 08:35:48 +00:00
*/
2020-06-17 21:54:13 +00:00
SharedPtr<Link> getLinkBySocket(const std::string& policyAlias, uint64_t localSocket);
2020-05-12 08:35:48 +00:00
/**
2020-06-17 21:54:13 +00:00
* Gets a reference to a physical link definition given its human-readable system name.
2020-05-12 08:35:48 +00:00
*
* @param policyAlias Policy in use
* @param ifname Alphanumeric human-readable name
2020-06-17 21:54:13 +00:00
* @return Physical link definition
2020-05-12 08:35:48 +00:00
*/
2020-06-17 21:54:13 +00:00
SharedPtr<Link> getLinkByName(const std::string& policyAlias, const std::string& ifname);
2020-05-12 08:35:48 +00:00
/**
* @param ifname Name of interface that we want to know if we can bind to
*/
bool allowedToBind(const std::string& ifname);
uint64_t getBondStartTime() { return bondStartTime; }
private:
Phy<BondController *> *_phy;
const RuntimeEnvironment *RR;
Mutex _bonds_m;
2020-06-17 21:54:13 +00:00
Mutex _links_m;
2020-05-12 08:35:48 +00:00
/**
* The last time that the bond controller updated the set of bonds.
*/
uint64_t _lastBackgroundBondControlTaskCheck;
/**
* The minimum monitoring interval among all paths in this bond.
*/
static int _minReqPathMonitorInterval;
/**
* The default bonding policy used for new bonds unless otherwise specified.
*/
static uint8_t _defaultBondingPolicy;
/**
* The default bonding policy used for new bonds unless otherwise specified.
*/
std::string _defaultBondingPolicyStr;
/**
* All currently active bonds.
*/
std::map<int64_t,SharedPtr<Bond> > _bonds;
/**
* Map of peers to custom bonding policies
*/
std::map<int64_t,std::string> _policyTemplateAssignments;
/**
* User-defined bonding policies (can be assigned to a peer)
*/
std::map<std::string,SharedPtr<Bond> > _bondPolicyTemplates;
/**
2020-06-17 21:54:13 +00:00
* Set of links defined for a given bonding policy
2020-05-12 08:35:48 +00:00
*/
2020-06-17 21:54:13 +00:00
std::map<std::string,std::vector<SharedPtr<Link> > > _linkDefinitions;
2020-05-12 08:35:48 +00:00
/**
2020-06-17 21:54:13 +00:00
* Set of link objects mapped to their physical interfaces
2020-05-12 08:35:48 +00:00
*/
2020-06-17 21:54:13 +00:00
std::map<std::string, std::map<std::string, SharedPtr<Link> > > _interfaceToLinkMap;
2020-05-12 08:35:48 +00:00
// TODO: Remove
uint64_t bondStartTime;
};
} // namespace ZeroTier
#endif