inet: add ip_retry_random_port sysctl to reduce sequential port retries
Subsystems (4)
RFC PATCH 1/1 inet: add ip_retry_random_port sysctl to reduce sequential port retries
With the current port selection algorithm, ports after a reserved port or long time used port are used more often than others. This combines with cloud environments blocking connections between the application server and the database server if there was a previous connection with the same source port. This leads to connectivity problems between applications on cloud environments.
The situation is that a source tuple is usable again after being closed for a maximum lifetime segment of two minutes while in the firewall it's still noted as existing for 60 minutes or longer. So in case that the port is reused for the same target tuple before the firewall cleans up, the connection will fail due to firewall interference which itself will reset the activity timeout in its own table. We understand the real issue here is that these firewalls cannot cope with standards-compliant port reuse. But this is a workaround for such situations and an improvement on the distribution of ports selected.
The proposed solution is instead of incrementing the port number, performing a re-selection of a new random port within the remaining range. This solution is configured via sysctl new option "net.ipv4.ip_retry_random_port".
The test run consists of two processes, a client and a server, and loops connect to the server sending some bytes back. The results we got are promising:
Executed test: Current algorithm
ephemeral port range: 9000-65499
simulated selections: 10000000
retries during simulation: 14197718
longest retry sequence: 5202
Executed test: Proposed modified algorithm
ephemeral port range: 9000-65499
simulated selections: 10000000
retries during simulation: 3976671
longest retry sequence: 12
In addition, on graphs generated we can observe that the distribution of source ports is more even with the proposed patch.
--- .../networking/net_cachelines/netns_ipv4_sysctl.rst | 1 + include/net/netns/ipv4.h | 1 + net/ipv4/inet_hashtables.c | 7 ++++++- net/ipv4/sysctl_net_ipv4.c | 7 +++++++ 4 files changed, 15 insertions(+), 1 deletion(-)
Documentation/networking/net_cachelines/netns_ipv4_sysctl.rst +1 -0
@@ -47,6 +47,7 @@ u8 sysctl_tcp_ecn u8 sysctl_tcp_ecn_fallback u8 sysctl_ip_default_ttl ip4_dst_hoplimit/ip_select_ttl u8 sysctl_ip_no_pmtu_disc +u8 sysctl_ip_retry_random_port u8 sysctl_ip_fwd_use_pmtu read_mostly ip_dst_mtu_maybe_forward/ip_skb_dst_mtu u8 sysctl_ip_fwd_update_priority ip_forward u8 sysctl_ip_nonlocal_bind
include/net/netns/ipv4.h +1 -0
@@ -156,6 +156,7 @@ struct netns_ipv4 {u8 sysctl_ip_default_ttl;u8 sysctl_ip_no_pmtu_disc; +u8 sysctl_ip_retry_random_port;u8 sysctl_ip_fwd_update_priority;u8 sysctl_ip_nonlocal_bind;u8 sysctl_ip_autobind_reuse;
net/ipv4/inet_hashtables.c +6 -1
@@ -1088,8 +1088,13 @@ int __inet_hash_connect(struct inet_timewait_death_row *death_row,for (i =0 ; i < remaining; i += step, port += step) {if (unlikely (port >= high)) port -= remaining; -if (inet_is_local_reserved_port (net, port)) +if (inet_is_local_reserved_port (net, port)) { +if (net->ipv4.sysctl_ip_retry_random_port) { + port = low +get_random_u32_below (remaining); + port = ((port &1 ) == step) ? port : (port -1 ); + }continue ; + } head = &hinfo->bhash[inet_bhashfn (net, port, hinfo->bhash_size)];rcu_read_lock ();
net/ipv4/sysctl_net_ipv4.c +7 -0
@@ -822,6 +822,13 @@ static struct ctl_table ipv4_net_table[] = { .mode = 0644, .proc_handler = ipv4_local_port_range, }, + { + .procname = "ip_retry_random_port", + .maxlen = sizeof(u8), + .data = &init_net.ipv4.sysctl_ip_retry_random_port, + .mode = 0644, + .proc_handler = proc_dou8vec_minmax, + }, { .procname = "ip_local_reserved_ports", .data = &init_net.ipv4.sysctl_local_reserved_ports,
Signature · 1 line
2.52.0
Quoted 41 lines · Fernando Fernandez Mancera
With the current port selection algorithm, ports after a reserved port or long time used port are used more often than others. This combines with cloud environments blocking connections between the application server and the database server if there was a previous connection with the same source port. This leads to connectivity problems between applications on cloud environments.
The situation is that a source tuple is usable again after being closed for a maximum lifetime segment of two minutes while in the firewall it's still noted as existing for 60 minutes or longer. So in case that the port is reused for the same target tuple before the firewall cleans up, the connection will fail due to firewall interference which itself will reset the activity timeout in its own table. We understand the real issue here is that these firewalls cannot cope with standards-compliant port reuse. But this is a workaround for such situations and an improvement on the distribution of ports selected.
The proposed solution is instead of incrementing the port number, performing a re-selection of a new random port within the remaining range. This solution is configured via sysctl new option "net.ipv4.ip_retry_random_port".
The test run consists of two processes, a client and a server, and loops connect to the server sending some bytes back. The results we got are promising:
Executed test: Current algorithm
ephemeral port range: 9000-65499
simulated selections: 10000000
retries during simulation: 14197718
longest retry sequence: 5202Executed test: Proposed modified algorithm
ephemeral port range: 9000-65499
simulated selections: 10000000
retries during simulation: 3976671
longest retry sequence: 12In addition, on graphs generated we can observe that the distribution of source ports is more even with the proposed patch.
Quoted 9 lines
Signed-off-by Fernando Fernandez Mancera <[email protected]>Tested-by Thorsten Toepper <[email protected]>--- .../networking/net_cachelines/netns_ipv4_sysctl.rst | 1 + include/net/netns/ipv4.h | 1 + net/ipv4/inet_hashtables.c | 7 ++++++- net/ipv4/sysctl_net_ipv4.c | 7 +++++++ 4 files changed, 15 insertions(+), 1 deletion(-)
I just noticed I didn't add the following diffs to the patch. Please keep them on mind and sorry for the inconvenience.
Documentation/networking/ip-sysctl.rst +11 -0
@@ -1610,6 +1610,17 @@ ip_local_reserved_ports - list of comma separated ranges Default: Empty +ip_retry_random_port - BOOLEAN + Randomize the selection of a new port if a reserved port is hit during + automatic port selection instead of incrementing the port number. + + Possible values: + + - 0 (disabled) + - 1 (enabled) + + Default: 0 (disabled) + ip_unprivileged_port_start - INTEGER This is a per-namespace sysctl. It defines the first unprivileged port in the network namespace. Privileged ports
net/ipv4/sysctl_net_ipv4.c +2 -0
@@ -828,6 +828,8 @@ static struct ctl_table ipv4_net_table[] = { .data = &init_net.ipv4.sysctl_ip_retry_random_port, .mode = 0644, .proc_handler = proc_dou8vec_minmax, + .extra1 = SYSCTL_ZERO, + .extra2 = SYSCTL_ONE, }, { .procname = "ip_local_reserved_ports",
Quoted 79 lines · Fernando Fernandez Mancera
With the current port selection algorithm, ports after a reserved port or long time used port are used more often than others. This combines with cloud environments blocking connections between the application server and the database server if there was a previous connection with the same source port. This leads to connectivity problems between applications on cloud environments.
The situation is that a source tuple is usable again after being closed for a maximum lifetime segment of two minutes while in the firewall it's still noted as existing for 60 minutes or longer. So in case that the port is reused for the same target tuple before the firewall cleans up, the connection will fail due to firewall interference which itself will reset the activity timeout in its own table. We understand the real issue here is that these firewalls cannot cope with standards-compliant port reuse. But this is a workaround for such situations and an improvement on the distribution of ports selected.
The proposed solution is instead of incrementing the port number, performing a re-selection of a new random port within the remaining range. This solution is configured via sysctl new option "net.ipv4.ip_retry_random_port".
The test run consists of two processes, a client and a server, and loops connect to the server sending some bytes back. The results we got are promising:
Executed test: Current algorithm
ephemeral port range: 9000-65499
simulated selections: 10000000
retries during simulation: 14197718
longest retry sequence: 5202Executed test: Proposed modified algorithm
ephemeral port range: 9000-65499
simulated selections: 10000000
retries during simulation: 3976671
longest retry sequence: 12In addition, on graphs generated we can observe that the distribution of source ports is more even with the proposed patch.
Signed-off-by Fernando Fernandez Mancera <[email protected]>Tested-by Thorsten Toepper <[email protected]>--- .../networking/net_cachelines/netns_ipv4_sysctl.rst | 1 + include/net/netns/ipv4.h | 1 + net/ipv4/inet_hashtables.c | 7 ++++++- net/ipv4/sysctl_net_ipv4.c | 7 +++++++ 4 files changed, 15 insertions(+), 1 deletion(-)Documentation/networking/net_cachelines/netns_ipv4_sysctl.rst +1
-0@@ -47,6 +47,7 @@ u8 sysctl_tcp_ecn u8 sysctl_tcp_ecn_fallback u8 sysctl_ip_default_ttl ip4_dst_hoplimit/ip_select_ttl u8 sysctl_ip_no_pmtu_disc +u8 sysctl_ip_retry_random_port u8 sysctl_ip_fwd_use_pmtu read_mostly ip_dst_mtu_maybe_forward/ip_skb_dst_mtu u8 sysctl_ip_fwd_update_priority ip_forward u8 sysctl_ip_nonlocal_bindinclude/net/netns/ipv4.h +1
-0@@ -156,6 +156,7 @@ struct netns_ipv4 {u8 sysctl_ip_default_ttl;u8 sysctl_ip_no_pmtu_disc; +u8 sysctl_ip_retry_random_port;u8 sysctl_ip_fwd_update_priority;u8 sysctl_ip_nonlocal_bind;u8 sysctl_ip_autobind_reuse;net/ipv4/inet_hashtables.c +0
-0
net/ipv4/inet_hashtables.c int __inet_hash_connect(struct inet_timewait_death_row *death_row, · line 1094
Quoted 8 lines
net/ipv4/inet_hashtables.c +4
-1@@ -1088,8 +1088,13 @@ int __inet_hash_connect(struct inet_timewait_death_row *death_row,for (i =0 ; i < remaining; i += step, port += step) {if (unlikely (port >= high)) port -= remaining; -if (inet_is_local_reserved_port (net, port)) +if (inet_is_local_reserved_port (net, port)) { +if (net->ipv4.sysctl_ip_retry_random_port) { + port = low +get_random_u32_below (remaining); + port = ((port &1 ) == step) ? port : (port -1 );
The AI bot did a good observation (https://netdev-ai.bots.linux.dev/ai-review.html?id=c1544ebc-4c9d-45c5-bce9-784764102912). I think this would be better as it will keep the random scan within the same parity when needed.
net/ipv4/inet_hashtables.c +5 -2
@@ -1090,8 +1090,11 @@ int __inet_hash_connect(struct inet_timewait_death_row *death_row, port -= remaining;if (inet_is_local_reserved_port (net, port)) {if (net->ipv4.sysctl_ip_retry_random_port) { - port = low +get_random_u32_below (remaining); - port = ((port &1 ) == step) ? port : (port -1 ); +u32 candidate = low +get_random_u32_below (remaining); + +if (step ==2 && (candidate &1 ) != (port &1 )) + candidate++; + port = candidate; }continue ; }
Quoted 24 lines
net/ipv4/inet_hashtables.c +2
-0+ }continue ; + } head = &hinfo->bhash[inet_bhashfn (net, port, hinfo->bhash_size)];rcu_read_lock ();net/ipv4/sysctl_net_ipv4.c +7
-0@@ -822,6 +822,13 @@ static struct ctl_table ipv4_net_table[] = { .mode = 0644, .proc_handler = ipv4_local_port_range, }, + { + .procname = "ip_retry_random_port", + .maxlen = sizeof(u8), + .data = &init_net.ipv4.sysctl_ip_retry_random_port, + .mode = 0644, + .proc_handler = proc_dou8vec_minmax, + }, { .procname = "ip_local_reserved_ports", .data = &init_net.ipv4.sysctl_local_reserved_ports,
Quoted 80 lines · Fernando Fernandez Mancera
With the current port selection algorithm, ports after a reserved port or long time used port are used more often than others. This combines with cloud environments blocking connections between the application server and the database server if there was a previous connection with the same source port. This leads to connectivity problems between applications on cloud environments.
The situation is that a source tuple is usable again after being closed for a maximum lifetime segment of two minutes while in the firewall it's still noted as existing for 60 minutes or longer. So in case that the port is reused for the same target tuple before the firewall cleans up, the connection will fail due to firewall interference which itself will reset the activity timeout in its own table. We understand the real issue here is that these firewalls cannot cope with standards-compliant port reuse. But this is a workaround for such situations and an improvement on the distribution of ports selected.
The proposed solution is instead of incrementing the port number, performing a re-selection of a new random port within the remaining range. This solution is configured via sysctl new option "net.ipv4.ip_retry_random_port".
The test run consists of two processes, a client and a server, and loops connect to the server sending some bytes back. The results we got are promising:
Executed test: Current algorithm
ephemeral port range: 9000-65499
simulated selections: 10000000
retries during simulation: 14197718
longest retry sequence: 5202Executed test: Proposed modified algorithm
ephemeral port range: 9000-65499
simulated selections: 10000000
retries during simulation: 3976671
longest retry sequence: 12In addition, on graphs generated we can observe that the distribution of source ports is more even with the proposed patch.
Signed-off-by Fernando Fernandez Mancera <[email protected]>Tested-by Thorsten Toepper <[email protected]>--- .../networking/net_cachelines/netns_ipv4_sysctl.rst | 1 + include/net/netns/ipv4.h | 1 + net/ipv4/inet_hashtables.c | 7 ++++++- net/ipv4/sysctl_net_ipv4.c | 7 +++++++ 4 files changed, 15 insertions(+), 1 deletion(-)Documentation/networking/net_cachelines/netns_ipv4_sysctl.rst +1
-0@@ -47,6 +47,7 @@ u8 sysctl_tcp_ecn u8 sysctl_tcp_ecn_fallback u8 sysctl_ip_default_ttl ip4_dst_hoplimit/ip_select_ttl u8 sysctl_ip_no_pmtu_disc +u8 sysctl_ip_retry_random_port u8 sysctl_ip_fwd_use_pmtu read_mostly ip_dst_mtu_maybe_forward/ip_skb_dst_mtu u8 sysctl_ip_fwd_update_priority ip_forward u8 sysctl_ip_nonlocal_bindinclude/net/netns/ipv4.h +1
-0@@ -156,6 +156,7 @@ struct netns_ipv4 {u8 sysctl_ip_default_ttl;u8 sysctl_ip_no_pmtu_disc; +u8 sysctl_ip_retry_random_port;u8 sysctl_ip_fwd_update_priority;u8 sysctl_ip_nonlocal_bind;u8 sysctl_ip_autobind_reuse;net/ipv4/inet_hashtables.c +0
-0
net/ipv4/inet_hashtables.c int __inet_hash_connect(struct inet_timewait_death_row *death_row, · line 1095
Quoted 9 lines
net/ipv4/inet_hashtables.c +5
-1@@ -1088,8 +1088,13 @@ int __inet_hash_connect(struct inet_timewait_death_row *death_row,for (i =0 ; i < remaining; i += step, port += step) {if (unlikely (port >= high)) port -= remaining; -if (inet_is_local_reserved_port (net, port)) +if (inet_is_local_reserved_port (net, port)) { +if (net->ipv4.sysctl_ip_retry_random_port) { + port = low +get_random_u32_below (remaining); + port = ((port &1 ) == step) ? port : (port -1 ); + }
What happens when almost all ephemeral ports are in use, and hundreds of ports are reserved ?
Choosing a random value each time we meet a reserved port is going to be quite expensive,
and we might return an error from this function even if there are many available ports.
Perhaps randomly select @step one time at the beginning of this function so that @step/2 and @remaining/2
are relatively prime numbers.
Quoted 95 lines · Eric Dumazet
Quoted 80 lines · Fernando Fernandez Mancera
With the current port selection algorithm, ports after a reserved port or long time used port are used more often than others. This combines with cloud environments blocking connections between the application server and the database server if there was a previous connection with the same source port. This leads to connectivity problems between applications on cloud environments.
The situation is that a source tuple is usable again after being closed for a maximum lifetime segment of two minutes while in the firewall it's still noted as existing for 60 minutes or longer. So in case that the port is reused for the same target tuple before the firewall cleans up, the connection will fail due to firewall interference which itself will reset the activity timeout in its own table. We understand the real issue here is that these firewalls cannot cope with standards-compliant port reuse. But this is a workaround for such situations and an improvement on the distribution of ports selected.
The proposed solution is instead of incrementing the port number, performing a re-selection of a new random port within the remaining range. This solution is configured via sysctl new option "net.ipv4.ip_retry_random_port".
The test run consists of two processes, a client and a server, and loops connect to the server sending some bytes back. The results we got are promising:
Executed test: Current algorithm
ephemeral port range: 9000-65499
simulated selections: 10000000
retries during simulation: 14197718
longest retry sequence: 5202Executed test: Proposed modified algorithm
ephemeral port range: 9000-65499
simulated selections: 10000000
retries during simulation: 3976671
longest retry sequence: 12In addition, on graphs generated we can observe that the distribution of source ports is more even with the proposed patch.
Signed-off-by Fernando Fernandez Mancera <[email protected]>Tested-by Thorsten Toepper <[email protected]>--- .../networking/net_cachelines/netns_ipv4_sysctl.rst | 1 + include/net/netns/ipv4.h | 1 + net/ipv4/inet_hashtables.c | 7 ++++++- net/ipv4/sysctl_net_ipv4.c | 7 +++++++ 4 files changed, 15 insertions(+), 1 deletion(-)Documentation/networking/net_cachelines/netns_ipv4_sysctl.rst +1
-0@@ -47,6 +47,7 @@ u8 sysctl_tcp_ecn u8 sysctl_tcp_ecn_fallback u8 sysctl_ip_default_ttl ip4_dst_hoplimit/ip_select_ttl u8 sysctl_ip_no_pmtu_disc +u8 sysctl_ip_retry_random_port u8 sysctl_ip_fwd_use_pmtu read_mostly ip_dst_mtu_maybe_forward/ip_skb_dst_mtu u8 sysctl_ip_fwd_update_priority ip_forward u8 sysctl_ip_nonlocal_bindinclude/net/netns/ipv4.h +1
-0@@ -156,6 +156,7 @@ struct netns_ipv4 {u8 sysctl_ip_default_ttl;u8 sysctl_ip_no_pmtu_disc; +u8 sysctl_ip_retry_random_port;u8 sysctl_ip_fwd_update_priority;u8 sysctl_ip_nonlocal_bind;u8 sysctl_ip_autobind_reuse;net/ipv4/inet_hashtables.c +0
-0net/ipv4/inet_hashtables.c int __inet_hash_connect(struct inet_timewait_death_row *death_row, · line 1095
Quoted 9 lines
net/ipv4/inet_hashtables.c +5
-1@@ -1088,8 +1088,13 @@ int __inet_hash_connect(struct inet_timewait_death_row *death_row,for (i =0 ; i < remaining; i += step, port += step) {if (unlikely (port >= high)) port -= remaining; -if (inet_is_local_reserved_port (net, port)) +if (inet_is_local_reserved_port (net, port)) { +if (net->ipv4.sysctl_ip_retry_random_port) { + port = low +get_random_u32_below (remaining); + port = ((port &1 ) == step) ? port : (port -1 ); + }What happens when almost all ephemeral ports are in use, and hundreds of ports are reserved ?
Quoted 9 lines
Choosing a random value each time we meet a reserved port is going to be quite expensive,
and we might return an error from this function even if there are many available ports.Perhaps randomly select @step one time at the beginning of this function so that @step/2 and @remaining/2
are relatively prime numbers.
That actually makes sense. It would ensure all ports are visited before returning an error. Let me test this out.
Thank you Eric,
Fernando.
Quoted 104 lines · Fernando Fernandez Mancera
Quoted 103 lines · Eric Dumazet
Quoted 82 lines · Fernando Fernandez Mancera
With the current port selection algorithm, ports after a reserved port or long time used port are used more often than others. This combines with cloud environments blocking connections between the application server and the database server if there was a previous connection with the same source port. This leads to connectivity problems between applications on cloud environments.
The situation is that a source tuple is usable again after being closed for a maximum lifetime segment of two minutes while in the firewall it's still noted as existing for 60 minutes or longer. So in case that the port is reused for the same target tuple before the firewall cleans up, the connection will fail due to firewall interference which itself will reset the activity timeout in its own table. We understand the real issue here is that these firewalls cannot cope with standards-compliant port reuse. But this is a workaround for such situations and an improvement on the distribution of ports selected.
The proposed solution is instead of incrementing the port number, performing a re-selection of a new random port within the remaining range. This solution is configured via sysctl new option "net.ipv4.ip_retry_random_port".
The test run consists of two processes, a client and a server, and loops connect to the server sending some bytes back. The results we got are promising:
Executed test: Current algorithm
ephemeral port range: 9000-65499
simulated selections: 10000000
retries during simulation: 14197718
longest retry sequence: 5202Executed test: Proposed modified algorithm
ephemeral port range: 9000-65499
simulated selections: 10000000
retries during simulation: 3976671
longest retry sequence: 12In addition, on graphs generated we can observe that the distribution of source ports is more even with the proposed patch.
Signed-off-by Fernando Fernandez Mancera <[email protected]>Tested-by Thorsten Toepper <[email protected]>---
.../networking/net_cachelines/netns_ipv4_sysctl.rst | 1 + include/net/netns/ipv4.h | 1 + net/ipv4/inet_hashtables.c | 7 ++++++- net/ipv4/sysctl_net_ipv4.c | 7 +++++++4 files changed, 15 insertions(+), 1 deletion(-)Documentation/networking/net_cachelines/netns_ipv4_sysctl.rst +0
-0@@ -47,6 +47,7 @@ u8 sysctl_tcp_ecnu8
sysctl_tcp_ecn_fallback
u8 sysctl_ip_default_ttl ip4_dst_hoplimit/ip_select_ttl
u8sysctl_ip_no_pmtu_disc+u8sysctl_ip_retry_random_port
u8sysctl_ip_fwd_use_pmturead_mostly ip_dst_mtu_maybe_forward/ip_skb_dst_mtu
u8 sysctl_ip_fwd_update_priorityip_forward
u8sysctl_ip_nonlocal_bindinclude/net/netns/ipv4.h +1
-0@@ -156,6 +156,7 @@ struct netns_ipv4 { u8 sysctl_ip_default_ttl; u8 sysctl_ip_no_pmtu_disc; + u8 sysctl_ip_retry_random_port; u8 sysctl_ip_fwd_update_priority; u8 sysctl_ip_nonlocal_bind; u8 sysctl_ip_autobind_reuse;net/ipv4/inet_hashtables.c +0
-0@@ -1088,8 +1088,13 @@ int __inet_hash_connect(struct inet_timewait_death_row *death_row, for (i = 0; i < remaining; i += step, port += step) {net/ipv4/inet_hashtables.c int __inet_hash_connect(struct · line 1098
Quoted 10 lines
net/ipv4/inet_hashtables.c +5
-1if (unlikely(port >= high)) port -= remaining; - if (inet_is_local_reserved_port(net, port)) + if (inet_is_local_reserved_port(net, port)) { + if (net->ipv4.sysctl_ip_retry_random_port) { + port = low +get_random_u32_below (remaining); + port = ((port & 1) == step) ? port : (port -1 ); + }What happens when almost all ephemeral ports are in use, and hundreds of ports are reserved ?
Choosing a random value each time we meet a reserved port is going to be quite expensive,
and we might return an error from this function even if there are many available ports.
Quoted 7 lines
Quoted 4 lines
Perhaps randomly select @step one time at the beginning of this function so that @step/2 and @remaining/2
are relatively prime numbers.That actually makes sense. It would ensure all ports are visited before returning an error. Let me test this out.
It makes sense. I have tested this approach and we got a more even distribution of source ports when having thousands of reserved ports. No difference at all when not using reserved ports.
Please, you can find the distribution graph with the current algorithm [1] and with the random step algorithm [2].
While I understand that this approach is introducing a call to get_random_u32_below() on every connect, I am wondering if it makes sense to replace the existing algorithm with this variant. What do you think?
Please, notice the implementation below. I plan to send an official v1 once net-next is open. In addition, I am rewriting the commit message as I find the current one confusing.
[1] https://0xffsoftware.com/port_graph_current_alg.html
[2] https://0xffsoftware.com/port_graph_random_step_alg.html
Thanks,
Fernando.
net/ipv4/inet_hashtables.c +15 -2
@@ -16,6 +16,7 @@#include <linux/wait.h> #include <linux/vmalloc.h> #include <linux/memblock.h> +#include <linux/gcd.h> #include <net/addrconf.h> #include <net/inet_connection_sock.h>
@@ -1046,11 +1047,11 @@ int __inet_hash_connect(struct inet_timewait_death_row *death_row,struct net *net =sock_net (sk);struct inet_bind2_bucket *tb2;struct inet_bind_bucket *tb; +int step, scan_step, l3mdev;bool tb_created = false;u32 remaining, offset;int ret, i, low, high;bool local_ports; -int step, l3mdev;u32 index;if (port) {
@@ -1065,6 +1066,7 @@ int __inet_hash_connect(struct inet_timewait_death_row *death_row, local_ports =inet_sk_get_local_port_range (sk, &low, &high); step = local_ports ?1 :2 ; + scan_step = step; high++;/* [32768, 60999] -> [32768, 61000[ */ remaining = high - low;
@@ -1083,9 +1085,20 @@ int __inet_hash_connect(struct inet_timewait_death_row *death_row,*/ if (!local_ports) offset &= ~1U ; +if (net->ipv4.sysctl_ip_retry_random_port) { +u32 range = (step ==1 ) ? remaining : (remaining /2 ); + + scan_step =1 +get_random_u32_below (range -1 ); +while (gcd (scan_step, range) !=1 ) { + scan_step++; +if (unlikely (scan_step >= range)) + scan_step =1 ; + } + scan_step *= step; + } other_parity_scan: port = low + offset; -for (i =0 ; i < remaining; i += step, port += step) { +for (i =0 ; i < remaining; i += step, port += scan_step) {if (unlikely (port >= high)) port -= remaining;if (inet_is_local_reserved_port (net, port))
Quoted 3 lines
Thank you Eric,
Fernando.
Quoted 7 lines · Fernando Fernandez Mancera
It makes sense. I have tested this approach and we got a more even distribution of source ports when having thousands of reserved ports. No difference at all when not using reserved ports.
Quoted 7 lines
Please, you can find the distribution graph with the current algorithm
[1] and with the random step algorithm [2].While I understand that this approach is introducing a call to
get_random_u32_below()on every connect, I am wondering if it makes sense to replace the existing algorithm with this variant. What do you think?
I would ask RFC 6056 experts like Fernando Gont what they think.
Note that if we use random at each connect(), we defeat one of the principles of ephemeral port selection : try very hard to avoid 4-tuple collision.
Quoted 8 lines
Please, notice the implementation below. I plan to send an official v1 once net-next is open. In addition, I am rewriting the commit message as I find the current one confusing.
[1] https://0xffsoftware.com/port_graph_current_alg.html
[2] https://0xffsoftware.com/port_graph_random_step_alg.html
Quoted 17 lines · Eric Dumazet
Quoted 7 lines · Fernando Fernandez Mancera
It makes sense. I have tested this approach and we got a more even distribution of source ports when having thousands of reserved ports. No difference at all when not using reserved ports.
Quoted 7 lines
Please, you can find the distribution graph with the current algorithm
[1] and with the random step algorithm [2].While I understand that this approach is introducing a call to
get_random_u32_below()on every connect, I am wondering if it makes sense to replace the existing algorithm with this variant. What do you think?
Quoted 5 lines
I would ask RFC 6056 experts like Fernando Gont what they think.
Note that if we use random at each
connect(), we defeat one of the principles of ephemeral port selection : try very hard to avoid 4-tuple collision.
Right. I will reach out to him and get his opinion. I have plenty of time before net-next open again. I am also collecting some metrics regarding the 4-tuple collision frequency.
Quoted 9 lines
Quoted 8 lines
Please, notice the implementation below. I plan to send an official v1 once net-next is open. In addition, I am rewriting the commit message as I find the current one confusing.
[1] https://0xffsoftware.com/port_graph_current_alg.html
[2] https://0xffsoftware.com/port_graph_random_step_alg.html
Quoted 21 lines · Fernando Fernandez Mancera
Quoted 17 lines · Eric Dumazet
Quoted 11 lines · On Fri, Feb 6, 2026 at 5:28 PM Fernando Fernandez Mancera [email protected]…
It makes sense. I have tested this approach and we got a more even distribution of source ports when having thousands of reserved ports. No difference at all when not using reserved ports.
Please, you can find the distribution graph with the current algorithm
[1] and with the random step algorithm [2].While I understand that this approach is introducing a call to
get_random_u32_below()on every connect, I am wondering if it makes sense to replace the existing algorithm with this variant. What do you think?I would ask RFC 6056 experts like Fernando Gont what they think.
Quoted 8 lines
Quoted 2 lines
Note that if we use random at each
connect(), we defeat one of the principles of ephemeral port selection : try very hard to avoid 4-tuple collision.Right. I will reach out to him and get his opinion. I have plenty of time before net-next open again. I am also collecting some metrics regarding the 4-tuple collision frequency.
We have had this problem in AWS for a long time. The patch works on our system. What is needed for it to be included in the next Linux release?
Please bring this to the stable versions.
Quoted 9 lines
Quoted 7 lines
Quoted 7 lines
Please, notice the implementation below. I plan to send an official v1 once net-next is open. In addition, I am rewriting the commit message as I find the current one confusing.
[1] https://0xffsoftware.com/port_graph_current_alg.html
[2] https://0xffsoftware.com/port_graph_random_step_alg.html
Quoted 27 lines · On 2/9/26 2:53 PM, [email protected] wrote:
Quoted 25 lines · Fernando Fernandez Mancera
Quoted 19 lines · Eric Dumazet
Quoted 11 lines · On Fri, Feb 6, 2026 at 5:28 PM Fernando Fernandez Mancera [email protected]…
It makes sense. I have tested this approach and we got a more even distribution of source ports when having thousands of reserved ports. No difference at all when not using reserved ports.
Please, you can find the distribution graph with the current algorithm
[1] and with the random step algorithm [2].While I understand that this approach is introducing a call to
get_random_u32_below()on every connect, I am wondering if it makes sense to replace the existing algorithm with this variant. What do you think?I would ask RFC 6056 experts like Fernando Gont what they think.
Note that if we use random at each
connect(), we defeat one of the principles of ephemeral port selection : try very hard to avoid 4-tuple collision.
Quoted 7 lines
Quoted 4 lines
Right. I will reach out to him and get his opinion. I have plenty of time before net-next open again. I am also collecting some metrics regarding the 4-tuple collision frequency.
We have had this problem in AWS for a long time. The patch works on our system. What is needed for it to be included in the next Linux release?
This just an RFC, I discourage using it in production yet. An official v1 will be sent once net-next is open and there it needs to be reviewed and approved by the maintainers.
Quoted 2 lines
Please bring this to the stable versions.
I don't think that will happen. This is an improvement not a "fix" per definition. Anyway, you could ask for a backport to your vendor/distribution.
Thanks,
Fernando.
Quoted 9 lines
Quoted 9 lines
Quoted 7 lines
Quoted 7 lines
Please, notice the implementation below. I plan to send an official v1 once net-next is open. In addition, I am rewriting the commit message as I find the current one confusing.
[1] https://0xffsoftware.com/port_graph_current_alg.html
[2] https://0xffsoftware.com/port_graph_random_step_alg.html