[Dnsmasq-discuss] [PATCH 3/3] Use the dhcp-host index for the duplicate IPv4 address check.

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


Each dhcp-host with an IPv4 address was checked against every config
already read, which is O(n^2) when reading a dhcp-hostsfile: with 18000
IPv4 dhcp-hosts this was most of the remaining time of a SIGHUP.

Add IPv4 addresses to the dhcp-host index, and add configs to it as they
are read instead of rebuilding it, so that reading the file stays linear.
The index is still discarded when configs are removed, or after
dhcp_update_configs(), which can change their addresses.
---
 src/dhcp-common.c | 82 ++++++++++++++++++++++++++++++++++++++---------
 src/dnsmasq.h     |  2 ++
 src/option.c      |  6 ++--
 3 files changed, 70 insertions(+), 20 deletions(-)

diff --git a/src/dhcp-common.c b/src/dhcp-common.c
index 74a0cb6..01ad33b 100644
--- a/src/dhcp-common.c
+++ b/src/dhcp-common.c
@@ -366,25 +366,26 @@ 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. */
+/* Index of daemon->dhcp_conf by exact (non-wildcard) hardware address and
+   by IPv4 address, so that find_config() and the duplicate address check
+   for dhcp-host don't scan every config. Those scans made each SIGHUP
+   O(leases * dhcp-hosts) and O(dhcp-hosts ^ 2), which takes tens of seconds
+   with tens of thousands of hosts.
+
+   The index is built on first use. dhcp_config_added() adds a config which
+   has just been put at the head of daemon->dhcp_conf, and
+   dhcp_config_changed() throws the index away: it must be called when
+   configs are removed from the list or have their hardware or IPv4
+   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 = NULL, **config_index_addr = 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 unsigned int config_index_mask, config_index_used, config_index_size;
 static int config_index_valid = 0, config_index_clid, config_index_wildcard;
 
 static unsigned int config_hash(const unsigned char *data, int len)
@@ -400,7 +401,7 @@ static unsigned int config_hash(const unsigned char *data, int len)
 static unsigned int config_entries(struct dhcp_config *config)
 {
   struct hwaddr_config *conf_addr;
-  unsigned int n = 0;
+  unsigned int n = (config->flags & CONFIG_ADDR) ? 1 : 0;
 
   for (conf_addr = config->hwaddr; conf_addr; conf_addr = conf_addr->next)
     if (conf_addr->wildcard_mask == 0)
@@ -428,13 +429,22 @@ static void config_index_link(struct dhcp_config *config, struct config_index_en
 	e->next = *bucket;
 	*bucket = e++;
       }
+
+  if (config->flags & CONFIG_ADDR)
+    {
+      bucket = &config_index_addr[config_hash((unsigned char *)&config->addr, INADDRSZ) & config_index_mask];
+      e->config = config;
+      e->next = *bucket;
+      *bucket = e;
+    }
 }
 
 void dhcp_config_changed(void)
 {
   free(config_index);
+  free(config_index_addr);
   free(config_index_entries);
-  config_index = NULL;
+  config_index = config_index_addr = NULL;
   config_index_entries = NULL;
   config_index_valid = 0;
 }
@@ -455,14 +465,16 @@ static void config_index_build(struct dhcp_config *configs)
       count++;
     }
 
+  /* Leave room to add as many entries again before rebuilding. */
   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));
+  config_index_addr = whine_malloc(size * sizeof(struct config_index_entry *));
+  config_index_entries = whine_malloc(size * sizeof(struct config_index_entry));
 
-  if (!list || !config_index || !config_index_entries)
+  if (!list || !config_index || !config_index_addr || !config_index_entries)
     {
       free(list);
       dhcp_config_changed();
@@ -471,6 +483,7 @@ static void config_index_build(struct dhcp_config *configs)
     }
 
   config_index_mask = size - 1;
+  config_index_size = size;
   config_index_used = 0;
 
   /* Link from the end of the list, so that each hash chain keeps the order
@@ -487,6 +500,25 @@ static void config_index_build(struct dhcp_config *configs)
   free(list);
 }
 
+void dhcp_config_added(struct dhcp_config *config)
+{
+  unsigned int n;
+
+  if (!config_index_valid || !config_index_entries)
+    return;
+
+  if (config != daemon->dhcp_conf || config->next != config_index_list ||
+      config_index_used + (n = config_entries(config)) > config_index_size)
+    {
+      dhcp_config_changed();
+      return;
+    }
+
+  config_index_link(config, &config_index_entries[config_index_used]);
+  config_index_used += n;
+  config_index_list = config;
+}
+
 static int config_index_ok(void)
 {
   if (!config_index_valid || config_index_list != daemon->dhcp_conf)
@@ -495,6 +527,21 @@ static int config_index_ok(void)
   return config_index_entries != NULL;
 }
 
+/* Same as config_find_by_address(daemon->dhcp_conf, addr) */
+struct dhcp_config *dhcp_config_find_by_address(struct in_addr addr)
+{
+  struct config_index_entry *e;
+
+  if (!config_index_ok())
+    return config_find_by_address(daemon->dhcp_conf, addr);
+
+  for (e = config_index_addr[config_hash((unsigned char *)&addr, INADDRSZ) & config_index_mask]; e; e = e->next)
+    if ((e->config->flags & CONFIG_ADDR) && e->config->addr.s_addr == addr.s_addr)
+      return e->config;
+
+  return NULL;
+}
+
 static struct dhcp_config *find_config_match(struct dhcp_config *configs,
 					     struct dhcp_context *context,
 					     unsigned char *clid, int clid_len,
@@ -693,6 +740,9 @@ void dhcp_update_configs(struct dhcp_config *configs)
       goto again;
     }
 #endif
+
+  /* Addresses of configs may have changed. */
+  dhcp_config_changed();
 }
 
 #ifdef HAVE_LINUX_NETWORK 
diff --git a/src/dnsmasq.h b/src/dnsmasq.h
index e8ea4ad..8d76055 100644
--- a/src/dnsmasq.h
+++ b/src/dnsmasq.h
@@ -1870,6 +1870,8 @@ struct dhcp_config *find_config(struct dhcp_config *configs,
 				struct dhcp_netid *filter);
 int config_has_mac(struct dhcp_config *config, unsigned char *hwaddr, int len, int type);
 void dhcp_config_changed(void);
+void dhcp_config_added(struct dhcp_config *config);
+struct dhcp_config *dhcp_config_find_by_address(struct in_addr addr);
 #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 410a3ac..35a359c 100644
--- a/src/option.c
+++ b/src/option.c
@@ -4167,16 +4167,13 @@ static int one_opt(int option, char *arg, char *errstr, char *gen_err, int comma
 	      }
 	    else if (strchr(arg, '.') && (inet_pton(AF_INET, arg, &in) > 0))
 	      {
-		struct dhcp_config *configs;
-		
 		new->addr = in;
 		new->flags |= CONFIG_ADDR;
 		
 		/* If the same IP appears in more than one host config, then DISCOVER
 		   for one of the hosts will get the address, but REQUEST will be NAKed,
 		   since the address is reserved by the other one -> protocol loop. */
-		for (configs = daemon->dhcp_conf; configs; configs = configs->next) 
-		  if ((configs->flags & CONFIG_ADDR) && configs->addr.s_addr == in.s_addr)
+		if (dhcp_config_find_by_address(in))
 		    {
 		      inet_ntop(AF_INET, &in, daemon->addrbuff, ADDRSTRLEN);
 		      sprintf(errstr, _("duplicate dhcp-host IP address %s"),
@@ -4268,6 +4265,7 @@ static int one_opt(int option, char *arg, char *errstr, char *gen_err, int comma
 	  }
 
 	daemon->dhcp_conf = new;
+	dhcp_config_added(new);
 	break;
       }
       
-- 
2.47.0




More information about the Dnsmasq-discuss mailing list