Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increasing limit of outgoing TCP connections on MacOS

Short version of question is: How to tune\configure macOS (Mojave 10.14.3) settings to allow more then 10k outgoing TCP connections per process and more then 16k connections in total.

Details:
I'm trying to make MacBookPro (16Gb RAM, Core i7) usable for stress-testing tcp server. Server itself hosted on separate pc, so right now the question is about outgoing connections only.

Below advices already processed and helped me significantly increase initial os limits.
1) I used [launchctl] ("Too many open files" when executing gatling on Mac) to increase maxfiles limit to 1 million.
2) I used sysctl to set\check kern.maxfiles limits. Actually (as I understand) this is the same as #1.
3) I played with ulimit. Actually I didn't notice any effect of this tool on my OS. But any way...

So now I MacOS can establish ~10k connections per process and 16k total connections in the system.

For simplicity my tool just open TCP connections in a infinite loop and waits.

try
{
  while (true)
  {
    CreateAndConnectSocket(); //add socket to list
    ++connectedSockets;
  }
}
catch(Exception e)
{
  LogWrite("Connected sockets:" + connectedSockets);
  LogWrite(e);
  WaitForAnyKey();
}

Then I follow below steps.

1) Launch server on separate PC. 2) Open two terminals on mac.
3) Execute in first terminal window:

$ sudo launchctl limit maxfiles 1048576 1048600   
$ ulimit -S -n 1048576   

4) Verify that changes applied in first terminal:

$ ulimit -S -n  
1048576  
$ launchctl limit maxfiles  
    maxfiles    1048576        1048600      
$ sysctl kern.maxfilesperproc  
kern.maxfilesperproc: 1048576
$ sysctl kern.maxfiles  
kern.maxfiles: 1048600  

5) Launch "ulimit -S -n 1048576" in second terminal (Not sure that ulimit is required at all.)
6) Verify that all changes applied in second terminal window (same as #4). 7) Launch "test client" in first terminal.
8) Launch "test client" in second terminal.

Result:
After step 7 in first terminal I can see, that tool opened 10k connections (10202 to be precise) and fell down with exception "Too many open files in system". Have no idea why opened files is an issue with 1 million limit.
After step 8 in second terminal I can see that tool opened 6k connectoins and fell down with exception "Can't assign requested address".

While sockets remain opened (tools wait for key press), no other connections can be created in the system - browsers can't establish connections to google.com, etc.

And ofcourse tcp server remains accessible from another PCs.

Since I was able to tune "Windows 10 Home" for higher connection numbers, I believe that MacOS can be tuned too.

like image 717
Pavlo K Avatar asked Mar 26 '19 21:03

Pavlo K


Video Answer


1 Answers

16383 TCP connections (from the same IP to the same port) is the limit imposed by default in MacOs (at least in Mojave).

This limit is defined by the ephemeral port range:

$ sudo sysctl net.inet.ip.portrange

net.inet.ip.portrange.lowfirst: 1023
net.inet.ip.portrange.lowlast: 600
net.inet.ip.portrange.first: 49152
net.inet.ip.portrange.last: 65535
net.inet.ip.portrange.hifirst: 49152
net.inet.ip.portrange.hilast: 65535

By default the range starts from 49152 (net.inet.ip.portrange.first) and ends to 65535 (net.inet.ip.portrange.last). That is, 65535 - 49152 = 16383.

You can make the ephemeral port range starting from 32768:

sudo sysctl -w net.inet.ip.portrange.first=32768

This way you double the available ephemeral ports (65535 - 32768 = 32767).

like image 100
Luca Fagioli Avatar answered Nov 15 '22 07:11

Luca Fagioli