Merge bitcoin/bitcoin#28464: net: improve max-connection limits code

df69b22f2e doc: improve documentation around connection limit maximums (Amiti Uttarwar)
adc171edf4 scripted-diff: Rename connection limit variables (Amiti Uttarwar)
e9fd9c0225 net: add m_max_inbound to connman (Amiti Uttarwar)
c25e0e0555 net, refactor: move calculations for connection type limits into connman (Amiti Uttarwar)

Pull request description:

  This is joint work with amitiuttarwar.

  This has the first few commits of #28463. It is not strictly a prerequisite for that, but has changes that in our opinion make sense on their own.
  It improves the handling of maximum numbers for different connection types (that are set during init and don’t change after) by:
  * moving all calculations into one place, `CConnMan::Init()`. Before, they were dispersed between `Init`, `CConnman::Init` and other parts of `CConnman`, resulting in some duplicated test code.
  * removing the possibility of having a negative maximum of inbound connections, which is hard to argue about
  * renaming of variables and doc improvements

ACKs for top commit:
  amitiuttarwar:
    co-author review ACK df69b22f2e
  naumenkogs:
    ACK df69b22f2e
  achow101:
    ACK df69b22f2e

Tree-SHA512: 913d56136bc1df739978de50db67302f88bac2a9d34748ae96763288d97093e998fc0f94f9b6eff12867712d7e86225af6128f4170bf2b5b8ab76f024870a22c
This commit is contained in:
Andrew Chow 2023-11-07 16:53:37 -05:00
commit 82ea4e787c
No known key found for this signature in database
GPG key ID: 17565732E08E5E41
4 changed files with 34 additions and 35 deletions

View file

@ -489,7 +489,7 @@ void SetupServerArgs(ArgsManager& argsman)
argsman.AddArg("-forcednsseed", strprintf("Always query for peer addresses via DNS lookup (default: %u)", DEFAULT_FORCEDNSSEED), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION); argsman.AddArg("-forcednsseed", strprintf("Always query for peer addresses via DNS lookup (default: %u)", DEFAULT_FORCEDNSSEED), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
argsman.AddArg("-listen", strprintf("Accept connections from outside (default: %u if no -proxy, -connect or -maxconnections=0)", DEFAULT_LISTEN), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION); argsman.AddArg("-listen", strprintf("Accept connections from outside (default: %u if no -proxy, -connect or -maxconnections=0)", DEFAULT_LISTEN), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
argsman.AddArg("-listenonion", strprintf("Automatically create Tor onion service (default: %d)", DEFAULT_LISTEN_ONION), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION); argsman.AddArg("-listenonion", strprintf("Automatically create Tor onion service (default: %d)", DEFAULT_LISTEN_ONION), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
argsman.AddArg("-maxconnections=<n>", strprintf("Maintain at most <n> connections to peers (default: %u). This limit does not apply to connections manually added via -addnode or the addnode RPC, which have a separate limit of %u.", DEFAULT_MAX_PEER_CONNECTIONS, MAX_ADDNODE_CONNECTIONS), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION); argsman.AddArg("-maxconnections=<n>", strprintf("Maintain at most <n> automatic connections to peers (default: %u). This limit does not apply to connections manually added via -addnode or the addnode RPC, which have a separate limit of %u.", DEFAULT_MAX_PEER_CONNECTIONS, MAX_ADDNODE_CONNECTIONS), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
argsman.AddArg("-maxreceivebuffer=<n>", strprintf("Maximum per-connection receive buffer, <n>*1000 bytes (default: %u)", DEFAULT_MAXRECEIVEBUFFER), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION); argsman.AddArg("-maxreceivebuffer=<n>", strprintf("Maximum per-connection receive buffer, <n>*1000 bytes (default: %u)", DEFAULT_MAXRECEIVEBUFFER), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
argsman.AddArg("-maxsendbuffer=<n>", strprintf("Maximum per-connection memory usage for the send buffer, <n>*1000 bytes (default: %u)", DEFAULT_MAXSENDBUFFER), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION); argsman.AddArg("-maxsendbuffer=<n>", strprintf("Maximum per-connection memory usage for the send buffer, <n>*1000 bytes (default: %u)", DEFAULT_MAXSENDBUFFER), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
argsman.AddArg("-maxtimeadjustment", strprintf("Maximum allowed median peer time offset adjustment. Local perspective of time may be influenced by outbound peers forward or backward by this amount (default: %u seconds).", DEFAULT_MAX_TIME_ADJUSTMENT), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION); argsman.AddArg("-maxtimeadjustment", strprintf("Maximum allowed median peer time offset adjustment. Local perspective of time may be influenced by outbound peers forward or backward by this amount (default: %u seconds).", DEFAULT_MAX_TIME_ADJUSTMENT), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
@ -1751,11 +1751,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
CConnman::Options connOptions; CConnman::Options connOptions;
connOptions.nLocalServices = nLocalServices; connOptions.nLocalServices = nLocalServices;
connOptions.nMaxConnections = nMaxConnections; connOptions.m_max_automatic_connections = nMaxConnections;
connOptions.m_max_outbound_full_relay = std::min(MAX_OUTBOUND_FULL_RELAY_CONNECTIONS, connOptions.nMaxConnections);
connOptions.m_max_outbound_block_relay = std::min(MAX_BLOCK_RELAY_ONLY_CONNECTIONS, connOptions.nMaxConnections-connOptions.m_max_outbound_full_relay);
connOptions.nMaxAddnode = MAX_ADDNODE_CONNECTIONS;
connOptions.nMaxFeeler = MAX_FEELER_CONNECTIONS;
connOptions.uiInterface = &uiInterface; connOptions.uiInterface = &uiInterface;
connOptions.m_banman = node.banman.get(); connOptions.m_banman = node.banman.get();
connOptions.m_msgproc = node.peerman.get(); connOptions.m_msgproc = node.peerman.get();

View file

@ -1730,7 +1730,6 @@ void CConnman::CreateNodeFromAcceptedSocket(std::unique_ptr<Sock>&& sock,
const CAddress& addr) const CAddress& addr)
{ {
int nInbound = 0; int nInbound = 0;
int nMaxInbound = nMaxConnections - m_max_outbound;
AddWhitelistPermissionFlags(permission_flags, addr); AddWhitelistPermissionFlags(permission_flags, addr);
if (NetPermissions::HasFlag(permission_flags, NetPermissionFlags::Implicit)) { if (NetPermissions::HasFlag(permission_flags, NetPermissionFlags::Implicit)) {
@ -1776,13 +1775,13 @@ void CConnman::CreateNodeFromAcceptedSocket(std::unique_ptr<Sock>&& sock,
// Only accept connections from discouraged peers if our inbound slots aren't (almost) full. // Only accept connections from discouraged peers if our inbound slots aren't (almost) full.
bool discouraged = m_banman && m_banman->IsDiscouraged(addr); bool discouraged = m_banman && m_banman->IsDiscouraged(addr);
if (!NetPermissions::HasFlag(permission_flags, NetPermissionFlags::NoBan) && nInbound + 1 >= nMaxInbound && discouraged) if (!NetPermissions::HasFlag(permission_flags, NetPermissionFlags::NoBan) && nInbound + 1 >= m_max_inbound && discouraged)
{ {
LogPrint(BCLog::NET, "connection from %s dropped (discouraged)\n", addr.ToStringAddrPort()); LogPrint(BCLog::NET, "connection from %s dropped (discouraged)\n", addr.ToStringAddrPort());
return; return;
} }
if (nInbound >= nMaxInbound) if (nInbound >= m_max_inbound)
{ {
if (!AttemptToEvictConnection()) { if (!AttemptToEvictConnection()) {
// No connection to evict, disconnect the new connection // No connection to evict, disconnect the new connection
@ -2733,7 +2732,7 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
// different netgroups in ipv4/ipv6 networks + all peers in Tor/I2P/CJDNS networks. // different netgroups in ipv4/ipv6 networks + all peers in Tor/I2P/CJDNS networks.
// Don't record addrman failure attempts when node is offline. This can be identified since all local // Don't record addrman failure attempts when node is offline. This can be identified since all local
// network connections (if any) belong in the same netgroup, and the size of `outbound_ipv46_peer_netgroups` would only be 1. // network connections (if any) belong in the same netgroup, and the size of `outbound_ipv46_peer_netgroups` would only be 1.
const bool count_failures{((int)outbound_ipv46_peer_netgroups.size() + outbound_privacy_network_peers) >= std::min(nMaxConnections - 1, 2)}; const bool count_failures{((int)outbound_ipv46_peer_netgroups.size() + outbound_privacy_network_peers) >= std::min(m_max_automatic_connections - 1, 2)};
// Use BIP324 transport when both us and them have NODE_V2_P2P set. // Use BIP324 transport when both us and them have NODE_V2_P2P set.
const bool use_v2transport(addrConnect.nServices & GetLocalServices() & NODE_P2P_V2); const bool use_v2transport(addrConnect.nServices & GetLocalServices() & NODE_P2P_V2);
OpenNetworkConnection(addrConnect, count_failures, std::move(grant), /*strDest=*/nullptr, conn_type, use_v2transport); OpenNetworkConnection(addrConnect, count_failures, std::move(grant), /*strDest=*/nullptr, conn_type, use_v2transport);
@ -3208,11 +3207,11 @@ bool CConnman::Start(CScheduler& scheduler, const Options& connOptions)
if (semOutbound == nullptr) { if (semOutbound == nullptr) {
// initialize semaphore // initialize semaphore
semOutbound = std::make_unique<CSemaphore>(std::min(m_max_outbound, nMaxConnections)); semOutbound = std::make_unique<CSemaphore>(std::min(m_max_automatic_outbound, m_max_automatic_connections));
} }
if (semAddnode == nullptr) { if (semAddnode == nullptr) {
// initialize semaphore // initialize semaphore
semAddnode = std::make_unique<CSemaphore>(nMaxAddnode); semAddnode = std::make_unique<CSemaphore>(m_max_addnode);
} }
// //
@ -3293,13 +3292,13 @@ void CConnman::Interrupt()
g_socks5_interrupt(); g_socks5_interrupt();
if (semOutbound) { if (semOutbound) {
for (int i=0; i<m_max_outbound; i++) { for (int i=0; i<m_max_automatic_outbound; i++) {
semOutbound->post(); semOutbound->post();
} }
} }
if (semAddnode) { if (semAddnode) {
for (int i=0; i<nMaxAddnode; i++) { for (int i=0; i<m_max_addnode; i++) {
semAddnode->post(); semAddnode->post();
} }
} }

View file

@ -1045,11 +1045,7 @@ public:
struct Options struct Options
{ {
ServiceFlags nLocalServices = NODE_NONE; ServiceFlags nLocalServices = NODE_NONE;
int nMaxConnections = 0; int m_max_automatic_connections = 0;
int m_max_outbound_full_relay = 0;
int m_max_outbound_block_relay = 0;
int nMaxAddnode = 0;
int nMaxFeeler = 0;
CClientUIInterface* uiInterface = nullptr; CClientUIInterface* uiInterface = nullptr;
NetEventsInterface* m_msgproc = nullptr; NetEventsInterface* m_msgproc = nullptr;
BanMan* m_banman = nullptr; BanMan* m_banman = nullptr;
@ -1076,13 +1072,12 @@ public:
AssertLockNotHeld(m_total_bytes_sent_mutex); AssertLockNotHeld(m_total_bytes_sent_mutex);
nLocalServices = connOptions.nLocalServices; nLocalServices = connOptions.nLocalServices;
nMaxConnections = connOptions.nMaxConnections; m_max_automatic_connections = connOptions.m_max_automatic_connections;
m_max_outbound_full_relay = std::min(connOptions.m_max_outbound_full_relay, connOptions.nMaxConnections); m_max_outbound_full_relay = std::min(MAX_OUTBOUND_FULL_RELAY_CONNECTIONS, m_max_automatic_connections);
m_max_outbound_block_relay = connOptions.m_max_outbound_block_relay; m_max_outbound_block_relay = std::min(MAX_BLOCK_RELAY_ONLY_CONNECTIONS, m_max_automatic_connections - m_max_outbound_full_relay);
m_max_automatic_outbound = m_max_outbound_full_relay + m_max_outbound_block_relay + m_max_feeler;
m_max_inbound = std::max(0, m_max_automatic_connections - m_max_automatic_outbound);
m_use_addrman_outgoing = connOptions.m_use_addrman_outgoing; m_use_addrman_outgoing = connOptions.m_use_addrman_outgoing;
nMaxAddnode = connOptions.nMaxAddnode;
nMaxFeeler = connOptions.nMaxFeeler;
m_max_outbound = m_max_outbound_full_relay + m_max_outbound_block_relay + nMaxFeeler;
m_client_interface = connOptions.uiInterface; m_client_interface = connOptions.uiInterface;
m_banman = connOptions.m_banman; m_banman = connOptions.m_banman;
m_msgproc = connOptions.m_msgproc; m_msgproc = connOptions.m_msgproc;
@ -1466,7 +1461,18 @@ private:
std::unique_ptr<CSemaphore> semOutbound; std::unique_ptr<CSemaphore> semOutbound;
std::unique_ptr<CSemaphore> semAddnode; std::unique_ptr<CSemaphore> semAddnode;
int nMaxConnections;
/**
* Maximum number of automatic connections permitted, excluding manual
* connections but including inbounds. May be changed by the user and is
* potentially limited by the operating system (number of file descriptors).
*/
int m_max_automatic_connections;
/*
* Maximum number of peers by connection type. Might vary from defaults
* based on -maxconnections init value.
*/
// How many full-relay (tx, block, addr) outbound peers we want // How many full-relay (tx, block, addr) outbound peers we want
int m_max_outbound_full_relay; int m_max_outbound_full_relay;
@ -1475,9 +1481,11 @@ private:
// We do not relay tx or addr messages with these peers // We do not relay tx or addr messages with these peers
int m_max_outbound_block_relay; int m_max_outbound_block_relay;
int nMaxAddnode; int m_max_addnode{MAX_ADDNODE_CONNECTIONS};
int nMaxFeeler; int m_max_feeler{MAX_FEELER_CONNECTIONS};
int m_max_outbound; int m_max_automatic_outbound;
int m_max_inbound;
bool m_use_addrman_outgoing; bool m_use_addrman_outgoing;
CClientUIInterface* m_client_interface; CClientUIInterface* m_client_interface;
NetEventsInterface* m_msgproc; NetEventsInterface* m_msgproc;

View file

@ -147,9 +147,7 @@ BOOST_AUTO_TEST_CASE(stale_tip_peer_management)
constexpr int max_outbound_full_relay = MAX_OUTBOUND_FULL_RELAY_CONNECTIONS; constexpr int max_outbound_full_relay = MAX_OUTBOUND_FULL_RELAY_CONNECTIONS;
CConnman::Options options; CConnman::Options options;
options.nMaxConnections = DEFAULT_MAX_PEER_CONNECTIONS; options.m_max_automatic_connections = DEFAULT_MAX_PEER_CONNECTIONS;
options.m_max_outbound_full_relay = max_outbound_full_relay;
options.nMaxFeeler = MAX_FEELER_CONNECTIONS;
const auto time_init{GetTime<std::chrono::seconds>()}; const auto time_init{GetTime<std::chrono::seconds>()};
SetMockTime(time_init); SetMockTime(time_init);
@ -248,9 +246,7 @@ BOOST_AUTO_TEST_CASE(block_relay_only_eviction)
constexpr int max_outbound_block_relay{MAX_BLOCK_RELAY_ONLY_CONNECTIONS}; constexpr int max_outbound_block_relay{MAX_BLOCK_RELAY_ONLY_CONNECTIONS};
constexpr int64_t MINIMUM_CONNECT_TIME{30}; constexpr int64_t MINIMUM_CONNECT_TIME{30};
CConnman::Options options; CConnman::Options options;
options.nMaxConnections = DEFAULT_MAX_PEER_CONNECTIONS; options.m_max_automatic_connections = DEFAULT_MAX_PEER_CONNECTIONS;
options.m_max_outbound_full_relay = MAX_OUTBOUND_FULL_RELAY_CONNECTIONS;
options.m_max_outbound_block_relay = max_outbound_block_relay;
connman->Init(options); connman->Init(options);
std::vector<CNode*> vNodes; std::vector<CNode*> vNodes;