linux – Round Robin Usage of Multiple IPs for Outgoing Connections on Single Interface

linuxlinux-networkingload balancingnetworking

my issue:
I have a process creating more than 10k TCP-Connection to the same destination IP&Port per second. After a short period of time, no new connections can be created any more, as there are no more source ports available.

I already tried to mitigate this by setting net.ipv4.ip_local_port_range and net.ipv4.tcp_fin_timeout but it did not solve the issue.

Now my thought was: If I could tell the kernel to round robin source IP-Addresses, then I could easily add more IPs to my one interface and therefore have a higher limit of outgoing connections.

I tried adding multiple routes to the same destination via the same interface but specify different source IPs:

ip route add default via 10.1.1.1 dev eth0 src 10.1.1.10
ip route add default via 10.1.1.1 dev eth0 src 10.1.1.11

But i get RTNETLINK answers: File exists.

Am I on the right track here?
Is adding another source IP "they way to go" in those cases?

Is there a way to round robin / load balance the usage of source IPs through the kernel somehow?

My hope was, that in case I manage to place two routes with the same weight but different src-addresses towards the same network, the kernel would round robin that.

(Background: I am running HAproxy on that machine and have to load balance more than 10k connections towards a single backend server)

Best Answer

Not easily via the kernel, no.

I am running HAproxy on that machine and have to load balance more than 10k connections towards a single backend server

You can tell haproxy to use a specific source IP address for outgoing connections to a server, for example:

  server app1_s1 10.0.1.1:80 source 10.1.1.10
  server app1_s2 10.0.1.1:80 source 10.1.1.11
  server app2_s1 10.0.1.2:80 source 10.1.1.10
  server app2_s2 10.0.1.2:80 source 10.1.1.11