Bug 220101 - LibAlias: allocate NAT ports using exhaustive search
Summary: LibAlias: allocate NAT ports using exhaustive search
Status: Open
Alias: None
Product: Base System
Classification: Unclassified
Component: kern (show other bugs)
Version: CURRENT
Hardware: Any Any
: --- Affects Some People
Assignee: freebsd-net (Nobody)
URL:
Keywords: needs-qa
Depends on:
Blocks:
 
Reported: 2017-06-18 09:03 UTC by Damjan Jovanovic
Modified: 2022-12-10 13:48 UTC (History)
2 users (show)

See Also:


Attachments
LibAlias: allocate NAT ports using exhaustive search (6.85 KB, patch)
2017-06-18 09:03 UTC, Damjan Jovanovic
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
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.