Skip to content

lib/connect: defer port selection until connect() time#6295

Closed
crrodriguez wants to merge 1 commit into
curl:masterfrom
crrodriguez:IP_BIND_ADDRESS_NO_PORT
Closed

lib/connect: defer port selection until connect() time#6295
crrodriguez wants to merge 1 commit into
curl:masterfrom
crrodriguez:IP_BIND_ADDRESS_NO_PORT

Conversation

@crrodriguez

Copy link
Copy Markdown
Contributor

If supported, defer port selection until connect() time
if --interface is given and source port is 0.

Reproducer:

  • start fast webserver on port 80

  • starve system of ephemeral ports
    $ sysctl net.ipv4.ip_local_port_range="60990 60999"

  • start a curl/libcurl "crawler"
    $curl --keepalive --parallel --parallel-immediate --head --interface
    127.0.0.2 "http://127.0.0.[1-254]/file[001-002].txt"

current result:
(possible some successful data)
curl: (45) bind failed with errno 98: Address already in use

result after patch:
(complete success or few connections failing, higlhy depending on load)

Fail only when all the possible 4-tuple combinations are exhausted, which is
impossible to do when port is selected at bind() time becuse the kernel
does not know if socket will be listen()'ed on or connect'ed yet.

If supported, defer port selection until connect() time
if --interface is given and source port is 0.

Reproducer:

* start fast webserver on port 80
* starve system of ephemeral ports
$  sysctl net.ipv4.ip_local_port_range="60990 60999"

* start a curl/libcurl "crawler"
$curl --keepalive --parallel --parallel-immediate --head --interface
127.0.0.2 "http://127.0.0.[1-254]/file[001-002].txt"

current result:
(possible some successful data)
curl: (45) bind failed with errno 98: Address already in use

result after patch:
(complete success or few connections failing, higlhy depending on load)

Fail only when all the possible 4-tuple combinations are exhausted, which is
impossible to do when port is selected at bind() time becuse the kernel
does not know if socket will be listen()'ed on or connect'ed  yet.
@crrodriguez crrodriguez force-pushed the IP_BIND_ADDRESS_NO_PORT branch from 076e5c5 to 861c522 Compare December 9, 2020 20:07
@crrodriguez

Copy link
Copy Markdown
Contributor Author

@bagder bagder self-assigned this Dec 9, 2020
@crrodriguez

crrodriguez commented Dec 10, 2020

Copy link
Copy Markdown
Contributor Author

More agressive test to prove source ports are now shared as long 4-tuple is unique

curl --keepalive --parallel --parallel-immediate --head --interface 127.0.0.2 "http://127.0.[0-254].[1-254]/file[001-002].txt"

@bagder

bagder commented Dec 10, 2020

Copy link
Copy Markdown
Member

Thanks!

@bagder bagder closed this in 25b4e15 Dec 10, 2020
@crrodriguez crrodriguez deleted the IP_BIND_ADDRESS_NO_PORT branch December 10, 2020 12:29
@crrodriguez crrodriguez restored the IP_BIND_ADDRESS_NO_PORT branch December 17, 2020 14:31
@crrodriguez crrodriguez deleted the IP_BIND_ADDRESS_NO_PORT branch December 17, 2020 14:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

2 participants