Add Bonds, Slaves, and Flows

This commit is contained in:
Joseph Henry 2020-05-12 01:35:48 -07:00
commit a50e8e9878
31 changed files with 4898 additions and 1966 deletions

View file

@ -1,10 +1,10 @@
/*
* Copyright (c)2019 ZeroTier, Inc.
* 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.
*
* Change Date: 2023-01-01
* Change Date: 2024-01-01
*
* 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.
@ -347,6 +347,23 @@ public:
}
}
// Generate set of unique interface names (used for formation of logical slave set in multipath code)
for(std::map<InetAddress,std::string>::const_iterator ii(localIfAddrs.begin());ii!=localIfAddrs.end();++ii) {
slaveIfNames.insert(ii->second);
}
for (std::set<std::string>::iterator si(slaveIfNames.begin());si!=slaveIfNames.end();si++) {
bool bFoundMatch = false;
for(std::map<InetAddress,std::string>::const_iterator ii(localIfAddrs.begin());ii!=localIfAddrs.end();++ii) {
if (ii->second == *si) {
bFoundMatch = true;
break;
}
}
if (!bFoundMatch) {
slaveIfNames.erase(si);
}
}
// Create new bindings for those not already bound
for(std::map<InetAddress,std::string>::const_iterator ii(localIfAddrs.begin());ii!=localIfAddrs.end();++ii) {
unsigned int bi = 0;
@ -444,7 +461,15 @@ public:
return false;
}
inline std::set<std::string> getSlaveInterfaceNames()
{
Mutex::Lock _l(_lock);
return slaveIfNames;
}
private:
std::set<std::string> slaveIfNames;
_Binding _bindings[ZT_BINDER_MAX_BINDINGS];
std::atomic<unsigned int> _bindingCount;
Mutex _lock;

View file

@ -55,8 +55,6 @@ LinuxNetLink::LinuxNetLink()
{
// set socket timeout to 1 sec so we're not permablocking recv() calls
_setSocketTimeout(_fd, 1);
int yes=1;
setsockopt(_fd,SOL_SOCKET,SO_REUSEADDR,(char*)&yes,sizeof(yes));
_la.nl_family = AF_NETLINK;
_la.nl_pid = 0; //getpid()+1;
@ -430,8 +428,6 @@ void LinuxNetLink::_linkDeleted(struct nlmsghdr *nlp)
void LinuxNetLink::_requestIPv4Routes()
{
int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
int yes=1;
setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,(char*)&yes,sizeof(yes));
if (fd == -1) {
fprintf(stderr, "Error opening RTNETLINK socket: %s\n", strerror(errno));
return;
@ -485,8 +481,6 @@ void LinuxNetLink::_requestIPv4Routes()
void LinuxNetLink::_requestIPv6Routes()
{
int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
int yes=1;
setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,(char*)&yes,sizeof(yes));
if (fd == -1) {
fprintf(stderr, "Error opening RTNETLINK socket: %s\n", strerror(errno));
return;
@ -540,8 +534,6 @@ void LinuxNetLink::_requestIPv6Routes()
void LinuxNetLink::_requestInterfaceList()
{
int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
int yes=1;
setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,(char*)&yes,sizeof(yes));
if (fd == -1) {
fprintf(stderr, "Error opening RTNETLINK socket: %s\n", strerror(errno));
return;
@ -595,8 +587,6 @@ void LinuxNetLink::addRoute(const InetAddress &target, const InetAddress &via, c
if (!target) return;
int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
int yes=1;
setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,(char*)&yes,sizeof(yes));
if (fd == -1) {
fprintf(stderr, "Error opening RTNETLINK socket: %s\n", strerror(errno));
return;
@ -713,8 +703,6 @@ void LinuxNetLink::delRoute(const InetAddress &target, const InetAddress &via, c
if (!target) return;
int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
int yes=1;
setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,(char*)&yes,sizeof(yes));
if (fd == -1) {
fprintf(stderr, "Error opening RTNETLINK socket: %s\n", strerror(errno));
return;
@ -828,8 +816,6 @@ void LinuxNetLink::delRoute(const InetAddress &target, const InetAddress &via, c
void LinuxNetLink::addAddress(const InetAddress &addr, const char *iface)
{
int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
int yes=1;
setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,(char*)&yes,sizeof(yes));
if (fd == -1) {
fprintf(stderr, "Error opening RTNETLINK socket: %s\n", strerror(errno));
return;
@ -948,8 +934,6 @@ void LinuxNetLink::addAddress(const InetAddress &addr, const char *iface)
void LinuxNetLink::removeAddress(const InetAddress &addr, const char *iface)
{
int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
int yes=1;
setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,(char*)&yes,sizeof(yes));
if (fd == -1) {
fprintf(stderr, "Error opening RTNETLINK socket: %s\n", strerror(errno));
return;

View file

@ -1,10 +1,10 @@
/*
* Copyright (c)2019 ZeroTier, Inc.
* 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.
*
* Change Date: 2023-01-01
* Change Date: 2024-01-01
*
* 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.
@ -459,6 +459,22 @@ uint64_t OSUtils::jsonInt(const nlohmann::json &jv,const uint64_t dfl)
return dfl;
}
double OSUtils::jsonDouble(const nlohmann::json &jv,const double dfl)
{
try {
if (jv.is_number()) {
return (double)jv;
}
else if (jv.is_string()) {
std::string s = jv;
return Utils::strToDouble(s.c_str());
} else if (jv.is_boolean()) {
return (double)jv;
}
} catch ( ... ) {}
return dfl;
}
uint64_t OSUtils::jsonIntHex(const nlohmann::json &jv,const uint64_t dfl)
{
try {

View file

@ -1,10 +1,10 @@
/*
* Copyright (c)2019 ZeroTier, Inc.
* 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.
*
* Change Date: 2023-01-01
* Change Date: 2024-01-01
*
* 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.
@ -277,6 +277,7 @@ public:
static nlohmann::json jsonParse(const std::string &buf);
static std::string jsonDump(const nlohmann::json &j,int indentation = 1);
static uint64_t jsonInt(const nlohmann::json &jv,const uint64_t dfl);
static double jsonDouble(const nlohmann::json &jv,const double dfl);
static uint64_t jsonIntHex(const nlohmann::json &jv,const uint64_t dfl);
static bool jsonBool(const nlohmann::json &jv,const bool dfl);
static std::string jsonString(const nlohmann::json &jv,const char *dfl);

View file

@ -1,10 +1,10 @@
/*
* Copyright (c)2019 ZeroTier, Inc.
* 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.
*
* Change Date: 2023-01-01
* Change Date: 2024-01-01
*
* 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.
@ -261,46 +261,6 @@ public:
}
}
/**
* Whether or not the socket object is in a closed state
*
* @param s Socket object
* @return true if socket is closed, false if otherwise
*/
inline bool isClosed(PhySocket *s)
{
PhySocketImpl *sws = (reinterpret_cast<PhySocketImpl *>(s));
return sws->type == ZT_PHY_SOCKET_CLOSED;
}
/**
* Get state of socket object
*
* @param s Socket object
* @return State of socket
*/
inline int getState(PhySocket *s)
{
PhySocketImpl *sws = (reinterpret_cast<PhySocketImpl *>(s));
return sws->type;
}
/**
* In the event that this socket is erased, we need a way to convey to the multipath logic
* that this path is no longer valid.
*
* @param s Socket object
* @return Whether the state of this socket is within an acceptable range of values
*/
inline bool isValidState(PhySocket *s)
{
if (s) {
PhySocketImpl *sws = (reinterpret_cast<PhySocketImpl *>(s));
return sws->type >= ZT_PHY_SOCKET_CLOSED && sws->type <= ZT_PHY_SOCKET_UNIX_LISTEN;
}
return false;
}
/**
* Cause poll() to stop waiting immediately
*

238
osdep/Slave.hpp Normal file
View file

@ -0,0 +1,238 @@
/*
* 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.
*
* Change Date: 2024-01-01
*
* 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_SLAVE_HPP
#define ZT_SLAVE_HPP
#include <string>
#include "../node/AtomicCounter.hpp"
namespace ZeroTier {
class Slave
{
friend class SharedPtr<Slave>;
public:
Slave() {}
/**
*
* @param ifnameStr
* @param ipvPref
* @param speed
* @param enabled
* @param mode
* @param failoverToSlaveStr
* @param userSpecifiedAlloc
*/
Slave(std::string& ifnameStr,
uint8_t ipvPref,
uint32_t speed,
uint32_t slaveMonitorInterval,
uint32_t upDelay,
uint32_t downDelay,
bool enabled,
uint8_t mode,
std::string failoverToSlaveStr,
float userSpecifiedAlloc) :
_ifnameStr(ifnameStr),
_ipvPref(ipvPref),
_speed(speed),
_relativeSpeed(0),
_slaveMonitorInterval(slaveMonitorInterval),
_upDelay(upDelay),
_downDelay(downDelay),
_enabled(enabled),
_mode(mode),
_failoverToSlaveStr(failoverToSlaveStr),
_userSpecifiedAlloc(userSpecifiedAlloc),
_isUserSpecified(false)
{}
/**
* @return The string representation of this slave's underlying interface's system name.
*/
inline std::string ifname() { return _ifnameStr; }
/**
* @return Whether this slave is designated as a primary.
*/
inline bool primary() { return _mode == ZT_MULTIPATH_SLAVE_MODE_PRIMARY; }
/**
* @return Whether this slave is designated as a spare.
*/
inline bool spare() { return _mode == ZT_MULTIPATH_SLAVE_MODE_SPARE; }
/**
* @return The name of the slave interface that should be used in the event of a failure.
*/
inline std::string failoverToSlave() { return _failoverToSlaveStr; }
/**
* @return Whether this slave interface was specified by the user or auto-detected.
*/
inline bool isUserSpecified() { return _isUserSpecified; }
/**
* Signify that this slave was specified by the user and not the result of auto-detection.
*
* @param isUserSpecified
*/
inline void setAsUserSpecified(bool isUserSpecified) { _isUserSpecified = isUserSpecified; }
/**
* @return Whether or not the user has specified failover instructions.
*/
inline bool userHasSpecifiedFailoverInstructions() { return _failoverToSlaveStr.length(); }
/**
* @return The speed of the slave relative to others in the bond.
*/
inline uint8_t relativeSpeed() { return _relativeSpeed; }
/**
* Sets the speed of the slave relative to others in the bond.
*
* @param relativeSpeed The speed relative to the rest of the slave interfaces.
*/
inline void setRelativeSpeed(uint8_t relativeSpeed) { _relativeSpeed = relativeSpeed; }
/**
* Sets the speed of the slave relative to others in the bond.
*
* @param relativeSpeed
*/
inline void setMonitorInterval(uint32_t interval) { _slaveMonitorInterval = interval; }
/**
* @return The absolute speed of the slave interface (as specified by the user.)
*/
inline uint32_t monitorInterval() { return _slaveMonitorInterval; }
/**
* @return The absolute speed of the slave interface (as specified by the user.)
*/
inline uint32_t speed() { return _speed; }
/**
* @return The address preference for this slave interface (as specified by the user.)
*/
inline uint8_t ipvPref() { return _ipvPref; }
/**
* @return The mode (e.g. primary/spare) for this slave interface (as specified by the user.)
*/
inline uint8_t mode() { return _mode; }
/**
* @return The upDelay parameter for all paths on this slave interface.
*/
inline uint32_t upDelay() { return _upDelay; }
/**
* @return The downDelay parameter for all paths on this slave interface.
*/
inline uint32_t downDelay() { return _downDelay; }
/**
* @return Whether this slave is enabled or disabled
*/
inline uint8_t enabled() { return _enabled; }
private:
/**
* String representation of underlying interface's system name
*/
std::string _ifnameStr;
/**
* What preference (if any) a user has for IP protocol version used in
* path aggregations. Preference is expressed in the order of the digits:
*
* 0: no preference
* 4: IPv4 only
* 6: IPv6 only
* 46: IPv4 over IPv6
* 64: IPv6 over IPv4
*/
uint8_t _ipvPref;
/**
* User-specified speed of this slave/link
*/
uint32_t _speed;
/**
* Speed relative to other specified slaves/links (computed by Bond)
*/
uint8_t _relativeSpeed;
/**
* User-specified interval for monitoring paths on this specific slave
* instead of using the more generic interval specified for the entire
* bond.
*/
uint32_t _slaveMonitorInterval;
/**
* How long before a path is considered to be usable after coming online. (when using policies that
* support fail-over events).
*/
uint32_t _upDelay;
/**
* How long before a path is considered to be dead (when using policies that
* support fail-over events).
*/
uint32_t _downDelay;
/**
* Whether this slave is enabled, or (disabled (possibly bad config))
*/
uint8_t _enabled;
/**
* Whether this slave is designated as a primary, a spare, or no preference.
*/
uint8_t _mode;
/**
* The specific name of the interface to be used in the event that this
* slave fails.
*/
std::string _failoverToSlaveStr;
/**
* User-specified allocation
*/
float _userSpecifiedAlloc;
/**
* Whether or not this slave was created as a result of manual user specification. This is
* important to know because certain policy decisions are dependent on whether the user
* intents to use a specific set of interfaces.
*/
bool _isUserSpecified;
AtomicCounter __refCount;
};
} // namespace ZeroTier
#endif