Skip to content

Commit 82ea4e7

Browse files
committed
Merge bitcoin#28464: net: improve max-connection limits code
df69b22 doc: improve documentation around connection limit maximums (Amiti Uttarwar) adc171e scripted-diff: Rename connection limit variables (Amiti Uttarwar) e9fd9c0 net: add m_max_inbound to connman (Amiti Uttarwar) c25e0e0 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 bitcoin#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 df69b22 naumenkogs: ACK df69b22 achow101: ACK df69b22 Tree-SHA512: 913d56136bc1df739978de50db67302f88bac2a9d34748ae96763288d97093e998fc0f94f9b6eff12867712d7e86225af6128f4170bf2b5b8ab76f024870a22c
2 parents 962ea5c + df69b22 commit 82ea4e7

File tree

4 files changed

+34
-35
lines changed

4 files changed

+34
-35
lines changed

src/init.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ void SetupServerArgs(ArgsManager& argsman)
489489
argsman.AddArg("-forcednsseed", strprintf("Always query for peer addresses via DNS lookup (default: %u)", DEFAULT_FORCEDNSSEED), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
490490
argsman.AddArg("-listen", strprintf("Accept connections from outside (default: %u if no -proxy, -connect or -maxconnections=0)", DEFAULT_LISTEN), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
491491
argsman.AddArg("-listenonion", strprintf("Automatically create Tor onion service (default: %d)", DEFAULT_LISTEN_ONION), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
492-
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);
492+
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);
493493
argsman.AddArg("-maxreceivebuffer=<n>", strprintf("Maximum per-connection receive buffer, <n>*1000 bytes (default: %u)", DEFAULT_MAXRECEIVEBUFFER), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
494494
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);
495495
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)
17511751

17521752
CConnman::Options connOptions;
17531753
connOptions.nLocalServices = nLocalServices;
1754-
connOptions.nMaxConnections = nMaxConnections;
1755-
connOptions.m_max_outbound_full_relay = std::min(MAX_OUTBOUND_FULL_RELAY_CONNECTIONS, connOptions.nMaxConnections);
1756-
connOptions.m_max_outbound_block_relay = std::min(MAX_BLOCK_RELAY_ONLY_CONNECTIONS, connOptions.nMaxConnections-connOptions.m_max_outbound_full_relay);
1757-
connOptions.nMaxAddnode = MAX_ADDNODE_CONNECTIONS;
1758-
connOptions.nMaxFeeler = MAX_FEELER_CONNECTIONS;
1754+
connOptions.m_max_automatic_connections = nMaxConnections;
17591755
connOptions.uiInterface = &uiInterface;
17601756
connOptions.m_banman = node.banman.get();
17611757
connOptions.m_msgproc = node.peerman.get();

src/net.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1730,7 +1730,6 @@ void CConnman::CreateNodeFromAcceptedSocket(std::unique_ptr<Sock>&& sock,
17301730
const CAddress& addr)
17311731
{
17321732
int nInbound = 0;
1733-
int nMaxInbound = nMaxConnections - m_max_outbound;
17341733

17351734
AddWhitelistPermissionFlags(permission_flags, addr);
17361735
if (NetPermissions::HasFlag(permission_flags, NetPermissionFlags::Implicit)) {
@@ -1776,13 +1775,13 @@ void CConnman::CreateNodeFromAcceptedSocket(std::unique_ptr<Sock>&& sock,
17761775

17771776
// Only accept connections from discouraged peers if our inbound slots aren't (almost) full.
17781777
bool discouraged = m_banman && m_banman->IsDiscouraged(addr);
1779-
if (!NetPermissions::HasFlag(permission_flags, NetPermissionFlags::NoBan) && nInbound + 1 >= nMaxInbound && discouraged)
1778+
if (!NetPermissions::HasFlag(permission_flags, NetPermissionFlags::NoBan) && nInbound + 1 >= m_max_inbound && discouraged)
17801779
{
17811780
LogPrint(BCLog::NET, "connection from %s dropped (discouraged)\n", addr.ToStringAddrPort());
17821781
return;
17831782
}
17841783

1785-
if (nInbound >= nMaxInbound)
1784+
if (nInbound >= m_max_inbound)
17861785
{
17871786
if (!AttemptToEvictConnection()) {
17881787
// No connection to evict, disconnect the new connection
@@ -2733,7 +2732,7 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
27332732
// different netgroups in ipv4/ipv6 networks + all peers in Tor/I2P/CJDNS networks.
27342733
// Don't record addrman failure attempts when node is offline. This can be identified since all local
27352734
// network connections (if any) belong in the same netgroup, and the size of `outbound_ipv46_peer_netgroups` would only be 1.
2736-
const bool count_failures{((int)outbound_ipv46_peer_netgroups.size() + outbound_privacy_network_peers) >= std::min(nMaxConnections - 1, 2)};
2735+
const bool count_failures{((int)outbound_ipv46_peer_netgroups.size() + outbound_privacy_network_peers) >= std::min(m_max_automatic_connections - 1, 2)};
27372736
// Use BIP324 transport when both us and them have NODE_V2_P2P set.
27382737
const bool use_v2transport(addrConnect.nServices & GetLocalServices() & NODE_P2P_V2);
27392738
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)
32083207

32093208
if (semOutbound == nullptr) {
32103209
// initialize semaphore
3211-
semOutbound = std::make_unique<CSemaphore>(std::min(m_max_outbound, nMaxConnections));
3210+
semOutbound = std::make_unique<CSemaphore>(std::min(m_max_automatic_outbound, m_max_automatic_connections));
32123211
}
32133212
if (semAddnode == nullptr) {
32143213
// initialize semaphore
3215-
semAddnode = std::make_unique<CSemaphore>(nMaxAddnode);
3214+
semAddnode = std::make_unique<CSemaphore>(m_max_addnode);
32163215
}
32173216

32183217
//
@@ -3293,13 +3292,13 @@ void CConnman::Interrupt()
32933292
g_socks5_interrupt();
32943293

32953294
if (semOutbound) {
3296-
for (int i=0; i<m_max_outbound; i++) {
3295+
for (int i=0; i<m_max_automatic_outbound; i++) {
32973296
semOutbound->post();
32983297
}
32993298
}
33003299

33013300
if (semAddnode) {
3302-
for (int i=0; i<nMaxAddnode; i++) {
3301+
for (int i=0; i<m_max_addnode; i++) {
33033302
semAddnode->post();
33043303
}
33053304
}

src/net.h

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,11 +1045,7 @@ class CConnman
10451045
struct Options
10461046
{
10471047
ServiceFlags nLocalServices = NODE_NONE;
1048-
int nMaxConnections = 0;
1049-
int m_max_outbound_full_relay = 0;
1050-
int m_max_outbound_block_relay = 0;
1051-
int nMaxAddnode = 0;
1052-
int nMaxFeeler = 0;
1048+
int m_max_automatic_connections = 0;
10531049
CClientUIInterface* uiInterface = nullptr;
10541050
NetEventsInterface* m_msgproc = nullptr;
10551051
BanMan* m_banman = nullptr;
@@ -1076,13 +1072,12 @@ class CConnman
10761072
AssertLockNotHeld(m_total_bytes_sent_mutex);
10771073

10781074
nLocalServices = connOptions.nLocalServices;
1079-
nMaxConnections = connOptions.nMaxConnections;
1080-
m_max_outbound_full_relay = std::min(connOptions.m_max_outbound_full_relay, connOptions.nMaxConnections);
1081-
m_max_outbound_block_relay = connOptions.m_max_outbound_block_relay;
1075+
m_max_automatic_connections = connOptions.m_max_automatic_connections;
1076+
m_max_outbound_full_relay = std::min(MAX_OUTBOUND_FULL_RELAY_CONNECTIONS, m_max_automatic_connections);
1077+
m_max_outbound_block_relay = std::min(MAX_BLOCK_RELAY_ONLY_CONNECTIONS, m_max_automatic_connections - m_max_outbound_full_relay);
1078+
m_max_automatic_outbound = m_max_outbound_full_relay + m_max_outbound_block_relay + m_max_feeler;
1079+
m_max_inbound = std::max(0, m_max_automatic_connections - m_max_automatic_outbound);
10821080
m_use_addrman_outgoing = connOptions.m_use_addrman_outgoing;
1083-
nMaxAddnode = connOptions.nMaxAddnode;
1084-
nMaxFeeler = connOptions.nMaxFeeler;
1085-
m_max_outbound = m_max_outbound_full_relay + m_max_outbound_block_relay + nMaxFeeler;
10861081
m_client_interface = connOptions.uiInterface;
10871082
m_banman = connOptions.m_banman;
10881083
m_msgproc = connOptions.m_msgproc;
@@ -1466,7 +1461,18 @@ class CConnman
14661461

14671462
std::unique_ptr<CSemaphore> semOutbound;
14681463
std::unique_ptr<CSemaphore> semAddnode;
1469-
int nMaxConnections;
1464+
1465+
/**
1466+
* Maximum number of automatic connections permitted, excluding manual
1467+
* connections but including inbounds. May be changed by the user and is
1468+
* potentially limited by the operating system (number of file descriptors).
1469+
*/
1470+
int m_max_automatic_connections;
1471+
1472+
/*
1473+
* Maximum number of peers by connection type. Might vary from defaults
1474+
* based on -maxconnections init value.
1475+
*/
14701476

14711477
// How many full-relay (tx, block, addr) outbound peers we want
14721478
int m_max_outbound_full_relay;
@@ -1475,9 +1481,11 @@ class CConnman
14751481
// We do not relay tx or addr messages with these peers
14761482
int m_max_outbound_block_relay;
14771483

1478-
int nMaxAddnode;
1479-
int nMaxFeeler;
1480-
int m_max_outbound;
1484+
int m_max_addnode{MAX_ADDNODE_CONNECTIONS};
1485+
int m_max_feeler{MAX_FEELER_CONNECTIONS};
1486+
int m_max_automatic_outbound;
1487+
int m_max_inbound;
1488+
14811489
bool m_use_addrman_outgoing;
14821490
CClientUIInterface* m_client_interface;
14831491
NetEventsInterface* m_msgproc;

src/test/denialofservice_tests.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,7 @@ BOOST_AUTO_TEST_CASE(stale_tip_peer_management)
147147

148148
constexpr int max_outbound_full_relay = MAX_OUTBOUND_FULL_RELAY_CONNECTIONS;
149149
CConnman::Options options;
150-
options.nMaxConnections = DEFAULT_MAX_PEER_CONNECTIONS;
151-
options.m_max_outbound_full_relay = max_outbound_full_relay;
152-
options.nMaxFeeler = MAX_FEELER_CONNECTIONS;
150+
options.m_max_automatic_connections = DEFAULT_MAX_PEER_CONNECTIONS;
153151

154152
const auto time_init{GetTime<std::chrono::seconds>()};
155153
SetMockTime(time_init);
@@ -248,9 +246,7 @@ BOOST_AUTO_TEST_CASE(block_relay_only_eviction)
248246
constexpr int max_outbound_block_relay{MAX_BLOCK_RELAY_ONLY_CONNECTIONS};
249247
constexpr int64_t MINIMUM_CONNECT_TIME{30};
250248
CConnman::Options options;
251-
options.nMaxConnections = DEFAULT_MAX_PEER_CONNECTIONS;
252-
options.m_max_outbound_full_relay = MAX_OUTBOUND_FULL_RELAY_CONNECTIONS;
253-
options.m_max_outbound_block_relay = max_outbound_block_relay;
249+
options.m_max_automatic_connections = DEFAULT_MAX_PEER_CONNECTIONS;
254250

255251
connman->Init(options);
256252
std::vector<CNode*> vNodes;

0 commit comments

Comments
 (0)