[Dnsmasq-discuss] [PATCH 0/3] Make SIGHUP and DHCP lookups scale with many dhcp-hosts and leases

Axel Jacquet axel.jacquet at infomaniak.com
Wed Nov 25 09:50:11 UTC 2026


Hello Simon, hello all,

This series removes three quadratic loops that make dnsmasq unresponsive
for a long time on each SIGHUP when it serves tens of thousands of
dhcp-hosts and leases.

Where this comes from
---------------------

We run an OpenStack public cloud. OpenStack Neutron starts one dnsmasq per
network and sends it a SIGHUP each time a port of the network is created,
updated or deleted, after rewriting its files. One of our provider
networks has:

  - 100 subnets (82 IPv4 /24, 18 IPv6 /64 dhcpv6-stateful), 18000 ports
  - a dhcp-hostsfile of 36600 lines (one per address, IPv4 and IPv6)
  - an addn-hosts file of 73000 names
  - a lease file of about 35000 leases

On that network dnsmasq 2.90 gets 13 to 23 SIGHUPs per hour, and each one
keeps it busy for about 38s, during which no DHCP or DNS request is
answered: dnsmasq is deaf for 8 to 15 minutes per hour. New instances wait
minutes for their first lease and their boot times out, and the kernel
drops DHCP packets while the socket queue is full.

Profiling a SIGHUP showed three loops:

  1. rerun_scripts() marks every lease as changed, and do_script_run()
     handles one lease per call and restarts from the head of the list:
     O(leases^2), even when there is no lease script or DBus to notify.

  2. lease_update_from_configs() calls find_config() for each lease, and
     find_config() scans every dhcp-host config: O(leases * dhcp-hosts).
     Each DHCP request pays for that scan too.

  3. Reading the dhcp-hostsfile checks each IPv4 address against every
     config already read for duplicates: O(dhcp-hosts^2).

Patch 1 does the do_script_run() work in a single pass when nothing
consumes the events. Patch 2 adds a hash index of dhcp-host configs by
exact hardware address, used by find_config(). Patch 3 adds IPv4
addresses to that index for the duplicate check. The index keeps the
order of the config list, so the first match is the same as with the
linear scans. When it can't be allocated, the old code is used.

Results
-------

Lab on current master (v2.93-44-g10fbc39), synthetic data with the same
shape as the network above (36000 dhcp-hosts, 32000 leases, the same
command line as Neutron), -O2, Linux arm64:

                                    master   +1       +1+2     +1+2+3
  CPU time of one SIGHUP            17.5s    8.4s     1.11s    0.17s
  startup                           32.1s    13.5s    6.3s     5.4s
  DHCP latency during a reload p50  18.0s    9.1s     1.17s    0.23s
  idle DHCP latency p50             4.7ms    5.0ms    1.4ms    1.2ms
  idle DHCP throughput (sequential) 199/s    180/s    687/s    789/s
  RSS                               28.7MB   28.7MB   30.8MB   33.9MB

The extra memory is the index, about 150 bytes per dhcp-host entry.

Checks
------

  - DHCPOFFERs for 2000 clients and DNS answers for 200 names, after
    two SIGHUPs, are identical with and without the patches.
  - Corner cases give the same offers and log messages: several MACs for
    one host, the same MAC on two lines, duplicate IPs, a v6-only and a
    v4 line for one MAC, a wildcard MAC with "ignore" next to an exact
    one, unknown tags, set:, id:, dhcp-host on the command line.
  - With --dhcp-script, a SIGHUP still runs the script once per lease
    ("old" event).
  - The series and each patch alone build with -DNO_DHCP, -DNO_DHCP6,
    -DNO_SCRIPT and -DNO_DHCP -DNO_DHCP6, without new warnings
    (-Wall -W).
  - The same tests run clean under AddressSanitizer and UBSan.

The rest of the startup time is mostly lease_set_hostname() when the
lease file is read, which is also quadratic. I left it out of this series
to keep it small, and can send a follow-up if this approach is fine with
you.

Thanks,
Axel

Axel Jacquet (3):
  Avoid O(n^2) lease scan in do_script_run() when nothing consumes lease
    events.
  Index dhcp-host configs by hardware address in find_config().
  Use the dhcp-host index for the duplicate IPv4 address check.

 src/dhcp-common.c | 197 +++++++++++++++++++++++++++++++++++++++++++++-
 src/dhcp.c        |   1 +
 src/dnsmasq.h     |   3 +
 src/lease.c       |  27 +++++--
 src/option.c      |   8 +-
 5 files changed, 224 insertions(+), 12 deletions(-)

-- 
2.47.0




More information about the Dnsmasq-discuss mailing list