Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: improve max-connection limits code #28464

Merged
merged 4 commits into from Nov 7, 2023

Conversation

mzumsande
Copy link
Contributor

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

@DrahtBot
Copy link
Contributor

DrahtBot commented Sep 12, 2023

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Code Coverage

For detailed information about the code coverage, see the test coverage report.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK naumenkogs, amitiuttarwar, achow101

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #27600 (net: Add new permission forceinbound to evict a random unprotected connection if all slots are otherwise full by pinheadmz)
  • #23352 (test: Extend stale_tip_peer_management test by maflcko)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

src/net.h Outdated
/**
* Maximum number of automatic connections permitted, excluding manual
* connections but including inbounds. May be changed by the user and is
* potentially limited by the number of available file descriptors.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

96904e8

nit: Perhaps I'd be more generic saying something-something operating system limits

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've mentioned both reasons now (the generic and the specific one).

@naumenkogs
Copy link
Member

ACK 96904e8

nice catching the negative number. I haven't found a way to exploit this bug.

amitiuttarwar and others added 4 commits October 3, 2023 13:52
Currently the logic is fragmented between init and connman. Encapsulating this
logic within connman allows for less mental overhead and easier reuse in tests.

Co-authored-by: Martin Zumsande <mzumsande@gmail.com>
Extract the logic for calculating & maintaining inbound connection limits to be
a member within connman for consistency with other maximum connection limits.

Note that we now limit m_max_inbound to 0 and don't call
AttemptToEvictConnection() when we don't have any inbounds.
Previously, nMaxInbound could become negative if the user ran with a low
-maxconnections, which didn't break any logic but didn't make sense.

Co-authored-by: Martin Zumsande <mzumsande@gmail.com>
-BEGIN VERIFY SCRIPT-
sed -i 's/nMaxConnections/m_max_automatic_connections/g' src/net.h src/net.cpp
sed -i 's/\.nMaxConnections/\.m_max_automatic_connections/g' src/init.cpp src/test/denialofservice_tests.cpp
sed -i 's/nMaxFeeler/m_max_feeler/g' src/net.h
sed -i 's/nMaxAddnode/m_max_addnode/g' src/net.h src/net.cpp
sed -i 's/m_max_outbound\([^_]\)/m_max_automatic_outbound\1/g' src/net.h src/net.cpp
-END VERIFY SCRIPT-

Co-authored-by: Martin Zumsande <mzumsande@gmail.com>
Co-authored-by: Martin Zumsande <mzumsande@gmail.com>
@mzumsande
Copy link
Contributor Author

96904e8 to df69b22: Rebased, and changed a comment slightly.

@amitiuttarwar
Copy link
Contributor

amitiuttarwar commented Oct 18, 2023

co-author review ACK df69b22

I think the windows failure is the known issue from #28509 that should be fixed post #28567

@@ -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("-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("-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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

df69b22

have you considered changing the named argument? -maxconnections -> -maxautoconnections.
It should in that case be backward-compatible (still allow legacy -maxconnections)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot about this comment - I hadn't considered it, and I think I would prefer maxautoconnections, but changing it (even with keeping an alias) seems like too much effort for too little benefit for my taste .

@naumenkogs
Copy link
Member

ACK df69b22

@DrahtBot DrahtBot requested review from amitiuttarwar and removed request for amitiuttarwar October 18, 2023 08:18
@achow101
Copy link
Member

achow101 commented Nov 7, 2023

ACK df69b22

@achow101 achow101 merged commit 82ea4e7 into bitcoin:master Nov 7, 2023
15 of 16 checks passed
@mzumsande mzumsande deleted the 202308_refactor_limits branch March 20, 2024 19:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants