Bug 220101

Summary: LibAlias: allocate NAT ports using exhaustive search
Product: Base System Reporter: Damjan Jovanovic <damjan.jov>
Component: kernAssignee: freebsd-net (Nobody) <net>
Status: Open ---    
Severity: Affects Some People CC: emaste, grahamperrin
Priority: --- Keywords: needs-qa
Version: CURRENT   
Hardware: Any   
OS: Any   
Attachments:
Description Flags
LibAlias: allocate NAT ports using exhaustive search none

Description Damjan Jovanovic 2017-06-18 09:03:44 UTC
Created attachment 183594 [details]
LibAlias: allocate NAT ports using exhaustive search

Currently libalias allocates NAT ports from a pool of 32768 ports (from ALIAS_PORT_BASE to ALIAS_PORT_BASE + ALIAS_PORT_MASK), by randomly trying GET_NEW_PORT_MAX_ATTEMPTS (20) ports and returning the first unused one, otherwise failing.

On a heavily loaded setup, as the number of ports in use approaches 32768, requests for ports will increasingly fail even though there are free ports. With 30000 ports in use, 17.12% of requests fail, with 31000 ports in use, 32.98% of requests fail, with 32000 ports in use, 62.23% of requests fail.

This behavior is erratic, and both PF and IPFILTER allocate ports by exhaustively searching the entire port range from a random starting point (eg. pf_get_sport() in sys/netpfil/pf/pf_lb.c), which is guaranteed to find a port if any are available. The attached patch changes libalias to do the same.