<div dir="ltr"><div></div><div>I wrote some tests[1] which showed behaviour I did not expect. IP_BIND_ADDRESS_NO_PORT seems to work as it should, but calling bind without it enabled turns out to be even worse than I thought.<br></div><div>This is what I think is happening: A successful bind() on a socket without IP_BIND_ADDRESS_NO_PORT enabled, with or without an explicit port configured, makes the assigned (or supplied) port unavailable for new connect()s (on different sockets), no matter the destination. I.e if you exhaust the entire net.ipv4.ip_local_port_range with bind() (no matter what IP you bind to!), connect() will stop working - no matter what IP you attempt to connect to. You can work around this by manually doing a bind() (with or without an explicit port, but without IP_BIND_ADDRESS_NO_PORT) on the socket before connect(). <br></div><div><br></div><div>$ uname -a</div><div>Linux laptop 5.15.0-56-generic #62-Ubuntu SMP Tue Nov 22 19:54:14 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux<br></div><div># sysctl -w net.ipv4.ip_local_port_range="40000 40100"</div><div>$ cd server && cargo run &</div><div>Version used: <a href="https://github.com/AndersTrier/IP_BIND_ADDRESS_NO_PORT_tests/blob/e74b09f680bb01a0078fe7e043e786c111103647/connect.py">https://github.com/AndersTrier/IP_BIND_ADDRESS_NO_PORT_tests/blob/e74b09f680bb01a0078fe7e043e786c111103647/connect.py</a></div><div>$ ../connect.py <br>Raised RLIMIT_NOFILE softlimit from 1024 to 200000<br>Select test (1-6): 2<br>#### Test 2 ####<br>Error on bind: [Errno 98] Address already in use<br>Made 101 connections. Expected to be around 101.<br>Select test (1-6): 1<br>#### Test 1 ####<br>Error on connect: [Errno 99] Cannot assign requested address<br>Made 0 connections. Expected to be around 101.<br>Select test (1-6): 3<br>#### Test 3 ####<br>Error on bind: [Errno 98] Address already in use<br>Made 200 connections. Expected to be around 202.</div><div><br></div><div>What blows my mind is that after running test2, you cannot connect to anything without manually doing a bind() beforehand (as shown by test1 and test3 above)! This also means that after running test2, software like ssh stops working:</div><div>$ ssh -v <a href="http://mirrors.dotsrc.org">mirrors.dotsrc.org</a><br>[...]<br>debug1: connect to address 130.225.254.116 port 22: Cannot assign requested address<br></div><div><br></div><div>When using IP_BIND_ADDRESS_NO_PORT, we don't have this problem (1 5 6 can be run in any order):</div><div>$ ./connect.py<br>Raised RLIMIT_NOFILE softlimit from 1024 to 200000<br>Select test (1-6): 5<br>#### Test 5 ####<br>Error on connect: [Errno 99] Cannot assign requested address<br>Made 90 connections. Expected to be around 101.<br>Select test (1-6): 6<br>#### Test 6 ####<br>Error on connect: [Errno 99] Cannot assign requested address<br>Made 180 connections. Expected to be around 202.<br>Select test (1-6): 1<br>#### Test 1 ####<br>Error on connect: [Errno 99] Cannot assign requested address<br>Made 90 connections. Expected to be around 101.</div><div><br></div><div>> Removing the IP_BIND_ADDRESS_NO_PORT option from Haproxy and<br><div>
> *doing nothing else* is sufficient to resolve the problem.</div></div><div>Maybe there are other processes on the same host which calls bind() without IP_BIND_ADDRESS_NO_PORT, and blocks the ports? E.g OutboundBindAddress or similar in torrc?</div><div><br></div><div>[1] <a href="https://github.com/AndersTrier/IP_BIND_ADDRESS_NO_PORT_tests">https://github.com/AndersTrier/IP_BIND_ADDRESS_NO_PORT_tests</a></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, Dec 10, 2022 at 7:15 PM Anders Trier Olesen <<a href="mailto:anders.trier.olesen@gmail.com">anders.trier.olesen@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">> 
I urge you to run an experient yourself, if these observations are not<br><div>
> what you expect. I was surprised, as well.</div><div>Very interesting. I'll run some tests.<br></div><div><br></div><div>We do agree that IP_BIND_ADDRESS_NO_PORT should fix OPs' problem, right? With it enabled, there's no path to inet_csk_bind_conflict which is where OPs CPU spend too much time.</div><div><br></div><div>- Anders<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, Dec 10, 2022 at 4:23 PM David Fifield <<a href="mailto:david@bamsoftware.com" target="_blank">david@bamsoftware.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Sat, Dec 10, 2022 at 09:59:14AM +0100, Anders Trier Olesen wrote:<br>
> IP_BIND_ADDRESS_NO_PORT did not fix your somewhat similar problem in your<br>
> Haproxy setup, because all the connections are to the same dst tuple <ip, port><br>
> (i.e 127.0.0.1:ExtORPort).<br>
> The connect() system call is looking for a unique 5-tuple <protocol, srcip,<br>
> srcport, dstip, dstport>. In the Haproxy setup, the only free variable is<br>
> srcport <tcp, 127.0.0.1, srcport, 127.0.0.1, ExtORPort>, so toggling<br>
> IP_BIND_ADDRESS_NO_PORT makes no difference.<br>
<br>
No—that is what I thought too, at first, but experimentally it is not<br>
the case. Removing the IP_BIND_ADDRESS_NO_PORT option from Haproxy and<br>
*doing nothing else* is sufficient to resolve the problem. Haproxy ends<br>
up binding to the same address it would have bound to with<br>
IP_BIND_ADDRESS_NO_PORT, and there are the same number of 5-tuples to<br>
the same endpoints, but the EADDRNOTAVAIL errors stop. It is<br>
counterintuitive and unexpected, which why I took the trouble to write<br>
it up.<br>
<br>
As I wrote at #40201, there are divergent code paths for connect in the<br>
kernel when the port is already bound versus when it is not bound. It's<br>
not as simple as filling in blanks in a 5-tuple in otherwise identical<br>
code paths.<br>
<br>
Anyway, it is not true that all connections go to the same (IP, port).<br>
(There would be no need to use a load balancer if that were the case.)<br>
At the time, we were running 12 tor processes with 12 different<br>
ExtORPorts (each ExtORPort on a different IP address, even: 127.0.3.1,<br>
127.0.3.2, etc.). We started to have EADDRNOTAVAIL problems at around<br>
3000 connections per ExtORPort, which is far too few to have exhausted<br>
the 5-tuple space. Please check the discussion at #40201 again, because<br>
I documented this detail there.<br>
<br>
I urge you to run an experient yourself, if these observations are not<br>
what you expect. I was surprised, as well.<br>
_______________________________________________<br>
tor-relays mailing list<br>
<a href="mailto:tor-relays@lists.torproject.org" target="_blank">tor-relays@lists.torproject.org</a><br>
<a href="https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-relays" rel="noreferrer" target="_blank">https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-relays</a><br>
</blockquote></div>
</blockquote></div>