Rely on the kernel for port allocation #498
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The testing infrastructure requires that a
substrate
binary is present for individual testing.However, having a different
polkadot
binary running resulted in failing tests.Root Cause
The function
next_open_port()
insubxt
crate produced 3 port numbers by inspectinga range of ports.
To validate that a given port is opened the
TcpListener::bind(("0.0.0.0", 0))
bind call is executed.However, the address "0.0.0.0" is not representative of the localhost, but of a non-routable unknown address.
This observation is validated using the
polkadot
binary which starts by default on127.0.0.1:9944
. AnyTcpListener::bind(("0.0.0.0", 0))
will result in success even with the binary started.Port Allocation
While at it, remove the scanning logic and rely entirely on the Kernel to provide 3 available ports.
The port allocation is performed by the inet_csk_get_port() function.
When providing a zero port, asking for allocation, the code path goes to inet_csk_find_open_port().
A port scanning is performed that uses a spinlock for concurrent access with the addition of checking for bind conflicts.
The kernel should provide 3 different ports if they are available.
Testing Done
Started 100 threads, each requesting for 100 new ports. Multiple runs of the binary presented no duplicates.
However, when requesting more ports than available (ie from 1000 threads), the ports are reutilized and values eventually wrap from
net.inet.ip.portrange.last
tonet.inet.ip.portrange.first
.Closes #497.