[Dnsmasq-discuss] [PATCH 2/3] Index dhcp-host configs by hardware address in find_config().
Axel Jacquet
axel.jacquet at infomaniak.com
Wed Nov 25 09:50:13 UTC 2026
find_config() scanned every dhcp-host config, sometimes more than once,
for each lookup. lease_update_from_configs() does a lookup for each lease
after every SIGHUP, so that was O(leases * dhcp-hosts): with 32000 leases
and 36000 dhcp-host entries (an OpenStack Neutron network with many
subnets), this was 7s of the 8s of CPU used by each SIGHUP after the
previous patch, during which no DHCP or DNS request is answered. Each
DHCP request did the same scan too.
Keep a hash index of exact hardware addresses, built on first use and
discarded by dhcp_config_changed() when the list of configs changes.
Hash chains keep the order of the list, so the result is the same as the
linear scan. The scans of client-id and wildcard configs are skipped when
there are none. The old code is still used if the index can't be allocated.
---
src/dhcp-common.c | 149 ++++++++++++++++++++++++++++++++++++++++++++--
src/dhcp.c | 1 +
src/dnsmasq.h | 1 +
src/option.c | 2 +
4 files changed, 149 insertions(+), 4 deletions(-)
diff --git a/src/dhcp-common.c b/src/dhcp-common.c
index fa92381..74a0cb6 100644
--- a/src/dhcp-common.c
+++ b/src/dhcp-common.c
@@ -366,6 +366,135 @@ static int is_config_in_context(struct dhcp_context *context, struct dhcp_config
return 0;
}
+/* Index of daemon->dhcp_conf by exact (non-wildcard) hardware address, so
+ that find_config() doesn't scan every config. That scan made each SIGHUP
+ O(leases * dhcp-hosts), which takes tens of seconds with tens of thousands
+ of hosts.
+
+ The index is built on first use, and rebuilt when a config is put at the
+ head of daemon->dhcp_conf. dhcp_config_changed() throws it away: it must
+ be called when configs are removed from the list or have their hardware
+ addresses changed. If the index can't be allocated, linear scans are
+ used. */
+struct config_index_entry {
+ struct dhcp_config *config;
+ struct config_index_entry *next;
+};
+
+static struct config_index_entry **config_index = NULL;
+static struct config_index_entry *config_index_entries = NULL;
+static struct dhcp_config *config_index_list = NULL;
+static unsigned int config_index_mask, config_index_used;
+static int config_index_valid = 0, config_index_clid, config_index_wildcard;
+
+static unsigned int config_hash(const unsigned char *data, int len)
+{
+ unsigned int h = 2166136261u; /* FNV-1a */
+
+ while (len-- > 0)
+ h = (h ^ *data++) * 16777619u;
+
+ return h;
+}
+
+static unsigned int config_entries(struct dhcp_config *config)
+{
+ struct hwaddr_config *conf_addr;
+ unsigned int n = 0;
+
+ for (conf_addr = config->hwaddr; conf_addr; conf_addr = conf_addr->next)
+ if (conf_addr->wildcard_mask == 0)
+ n++;
+
+ return n;
+}
+
+/* Put the entries of config at the head of their hash chains. */
+static void config_index_link(struct dhcp_config *config, struct config_index_entry *e)
+{
+ struct hwaddr_config *conf_addr;
+ struct config_index_entry **bucket;
+
+ if (config->flags & CONFIG_CLID)
+ config_index_clid = 1;
+
+ for (conf_addr = config->hwaddr; conf_addr; conf_addr = conf_addr->next)
+ if (conf_addr->wildcard_mask != 0)
+ config_index_wildcard = 1;
+ else
+ {
+ bucket = &config_index[config_hash(conf_addr->hwaddr, conf_addr->hwaddr_len) & config_index_mask];
+ e->config = config;
+ e->next = *bucket;
+ *bucket = e++;
+ }
+}
+
+void dhcp_config_changed(void)
+{
+ free(config_index);
+ free(config_index_entries);
+ config_index = NULL;
+ config_index_entries = NULL;
+ config_index_valid = 0;
+}
+
+static void config_index_build(struct dhcp_config *configs)
+{
+ struct dhcp_config *config, **list;
+ unsigned int n = 0, count = 0, i, size = 16;
+
+ dhcp_config_changed();
+ config_index_valid = 1;
+ config_index_list = configs;
+ config_index_clid = config_index_wildcard = 0;
+
+ for (config = configs; config; config = config->next)
+ {
+ n += config_entries(config);
+ count++;
+ }
+
+ while (size < 2 * n)
+ size <<= 1;
+
+ list = whine_malloc((count + 1) * sizeof(struct dhcp_config *));
+ config_index = whine_malloc(size * sizeof(struct config_index_entry *));
+ config_index_entries = whine_malloc((n + 1) * sizeof(struct config_index_entry));
+
+ if (!list || !config_index || !config_index_entries)
+ {
+ free(list);
+ dhcp_config_changed();
+ config_index_valid = 1; /* use linear scans until the list changes */
+ return;
+ }
+
+ config_index_mask = size - 1;
+ config_index_used = 0;
+
+ /* Link from the end of the list, so that each hash chain keeps the order
+ of the list: the first match in the list wins. */
+ for (i = 0, config = configs; config; config = config->next)
+ list[i++] = config;
+
+ while (i-- != 0)
+ {
+ config_index_link(list[i], &config_index_entries[config_index_used]);
+ config_index_used += config_entries(list[i]);
+ }
+
+ free(list);
+}
+
+static int config_index_ok(void)
+{
+ if (!config_index_valid || config_index_list != daemon->dhcp_conf)
+ config_index_build(daemon->dhcp_conf);
+
+ return config_index_entries != NULL;
+}
+
static struct dhcp_config *find_config_match(struct dhcp_config *configs,
struct dhcp_context *context,
unsigned char *clid, int clid_len,
@@ -376,8 +505,11 @@ static struct dhcp_config *find_config_match(struct dhcp_config *configs,
int count, new;
struct dhcp_config *config, *candidate;
struct hwaddr_config *conf_addr;
+ int indexed;
+
+ indexed = (configs == daemon->dhcp_conf && config_index_ok());
- if (clid)
+ if (clid && (!indexed || config_index_clid))
for (config = configs; config; config = config->next)
if (config->flags & CONFIG_CLID)
{
@@ -399,7 +531,17 @@ static struct dhcp_config *find_config_match(struct dhcp_config *configs,
}
- if (hwaddr)
+ if (hwaddr && indexed)
+ {
+ struct config_index_entry *e;
+
+ for (e = config_index[config_hash(hwaddr, hw_len) & config_index_mask]; e; e = e->next)
+ if (config_has_mac(e->config, hwaddr, hw_len, hw_type) &&
+ is_config_in_context(context, e->config) &&
+ match_netid(e->config->filter, tags, tag_not_needed))
+ return e->config;
+ }
+ else if (hwaddr)
for (config = configs; config; config = config->next)
if (config_has_mac(config, hwaddr, hw_len, hw_type) &&
is_config_in_context(context, config) &&
@@ -415,7 +557,7 @@ static struct dhcp_config *find_config_match(struct dhcp_config *configs,
return config;
- if (!hwaddr)
+ if (!hwaddr || (indexed && !config_index_wildcard))
return NULL;
/* use match with fewest wildcard octets */
@@ -551,7 +693,6 @@ void dhcp_update_configs(struct dhcp_config *configs)
goto again;
}
#endif
-
}
#ifdef HAVE_LINUX_NETWORK
diff --git a/src/dhcp.c b/src/dhcp.c
index 6546741..b8de016 100644
--- a/src/dhcp.c
+++ b/src/dhcp.c
@@ -1079,6 +1079,7 @@ void dhcp_read_ethers(void)
}
fclose(f);
+ dhcp_config_changed();
my_syslog(MS_DHCP | LOG_INFO, _("read %s - %d addresses"), ETHERSFILE, count);
}
diff --git a/src/dnsmasq.h b/src/dnsmasq.h
index 5a1527a..e8ea4ad 100644
--- a/src/dnsmasq.h
+++ b/src/dnsmasq.h
@@ -1869,6 +1869,7 @@ struct dhcp_config *find_config(struct dhcp_config *configs,
int hw_type, char *hostname,
struct dhcp_netid *filter);
int config_has_mac(struct dhcp_config *config, unsigned char *hwaddr, int len, int type);
+void dhcp_config_changed(void);
#ifdef HAVE_LINUX_NETWORK
char *whichdevice(void);
int bind_dhcp_devices(char *bound_device);
diff --git a/src/option.c b/src/option.c
index 012b14a..410a3ac 100644
--- a/src/option.c
+++ b/src/option.c
@@ -5913,6 +5913,8 @@ static void clear_dynamic_conf(void)
else
up = &configs->next;
}
+
+ dhcp_config_changed();
}
static void clear_dhcp_opt(struct dhcp_opt **dhcp_opts)
--
2.47.0
More information about the Dnsmasq-discuss
mailing list