]> git.ipfire.org Git - thirdparty/kernel/linux.git/commit
inet: add ip_local_port_step_width sysctl to improve port usage distribution
authorFernando Fernandez Mancera <fmancera@suse.de>
Mon, 9 Mar 2026 02:39:45 +0000 (03:39 +0100)
committerJakub Kicinski <kuba@kernel.org>
Wed, 11 Mar 2026 01:59:39 +0000 (18:59 -0700)
commit7da62262ec96a4b345d207b6bcd2ddf5231b7f7d
tree90ceca3b452b376c2c57496778829de62be18260
parentae95cbaedb754633b9c0d35712a024b16a70962e
inet: add ip_local_port_step_width sysctl to improve port usage distribution

With the current port selection algorithm, ports after a reserved port
range or long time used port are used more often than others [1]. This
causes an uneven port usage distribution. 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, leading to connectivity problems between applications on cloud
environments.

The real issue here is that these firewalls cannot cope with
standards-compliant port reuse. This is a workaround for such situations
and an improvement on the distribution of ports selected.

The proposed solution is to implement a variant of RFC 6056 Algorithm 5.
The step size is selected randomly on every connect() call ensuring it
is a coprime with respect to the size of the range of ports we want to
scan. This way, we can ensure that all ports within the range are
scanned before returning an error. To enable this algorithm, the user
must configure the new sysctl option "net.ipv4.ip_local_port_step_width".

In addition, on graphs generated we can observe that the distribution of
source ports is more even with the proposed approach. [2]

[1] https://0xffsoftware.com/port_graph_current_alg.html

[2] https://0xffsoftware.com/port_graph_random_step_alg.html

Reviewed-by: Eric Dumazet <edumazet@google.com>
Signed-off-by: Fernando Fernandez Mancera <fmancera@suse.de>
Link: https://patch.msgid.link/20260309023946.5473-2-fmancera@suse.de
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
Documentation/networking/ip-sysctl.rst
Documentation/networking/net_cachelines/netns_ipv4_sysctl.rst
include/net/netns/ipv4.h
net/ipv4/inet_hashtables.c
net/ipv4/sysctl_net_ipv4.c