]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: cfgdiag: adjust diag on servers
authorAmaury Denoyelle <adenoyelle@haproxy.com>
Fri, 14 Nov 2025 08:40:07 +0000 (09:40 +0100)
committerAmaury Denoyelle <adenoyelle@haproxy.com>
Fri, 14 Nov 2025 09:00:26 +0000 (10:00 +0100)
Adjust code dealing with diagnostics performed on server. The objective
is to extract the check on duplicate cookies in a dedicated function
outside of the proxies/servers loop.

This does not have any noticeable impact. This patch is merely a code
improvment to implement easily new future diagnostics on servers.

src/cfgdiag.c

index f8e4a9e082c1a75d3f68140c2c30194c4147a154..c3b05e5d6c37f34fca9ab7c814c88551b2532218 100644 (file)
@@ -8,6 +8,10 @@
 #include <haproxy/proxy.h>
 #include <haproxy/server.h>
 
+struct cookie_entry {
+       struct ebpt_node node;
+};
+
 /* Use this function to emit diagnostic.
  * This can be used as a shortcut to set value pointed by <ret> to 1 at the
  * same time.
@@ -41,43 +45,42 @@ static inline void *diag_alloc(size_t size)
  * value. Backup servers are not taken into account as it can be quite common to
  * share cookie values in this case.
  */
-static void check_server_cookies(int *ret)
+static void srv_diag_cookies(int *ret, struct server *srv, struct eb_root *cookies_tree)
 {
-       struct cookie_entry {
-               struct ebpt_node node;
-       };
+       struct ebpt_node *cookie_node;
 
-       struct proxy  *px;
-       struct server *srv;
+       /* do not take into account backup servers */
+       if (!srv->cookie || (srv->flags & SRV_F_BACKUP))
+               return;
+
+       cookie_node = ebis_lookup(cookies_tree, srv->cookie);
+       if (cookie_node) {
+               diag_warning(ret, "parsing [%s:%d] : 'server %s' : same cookie value is set for a previous non-backup server in the same backend, it may break connection persistence\n",
+                            srv->conf.file, srv->conf.line, srv->id);
+       }
+       else {
+               cookie_node = diag_alloc(sizeof(*cookie_node));
+               cookie_node->key = srv->cookie;
+               ebis_insert(cookies_tree, cookie_node);
+       }
+}
 
+/* Perform a series of diagnostics on every servers from the configuration. */
+static void run_servers_diag(int *ret)
+{
        struct eb_root cookies_tree = EB_ROOT_UNIQUE;
        struct ebpt_node *cookie_node;
-       struct cookie_entry *cookie_entry;
-       struct ebpt_node *node;
+       struct proxy  *px;
+       struct server *srv;
 
        for (px = proxies_list; px; px = px->next) {
-               for (srv = px->srv; srv; srv = srv->next) {
-                       /* do not take into account backup servers */
-                       if (!srv->cookie || (srv->flags & SRV_F_BACKUP))
-                               continue;
-
-                       cookie_node = ebis_lookup(&cookies_tree, srv->cookie);
-                       if (cookie_node) {
-                               diag_warning(ret, "parsing [%s:%d] : 'server %s' : same cookie value is set for a previous non-backup server in the same backend, it may break connection persistence\n",
-                                            srv->conf.file, srv->conf.line, srv->id);
-                               continue;
-                       }
-
-                       cookie_entry = diag_alloc(sizeof(*cookie_entry));
-                       cookie_entry->node.key = srv->cookie;
-                       ebis_insert(&cookies_tree, &cookie_entry->node);
-               }
+               for (srv = px->srv; srv; srv = srv->next)
+                       srv_diag_cookies(ret, srv, &cookies_tree);
 
-               /* clear the tree and free its entries */
-               while ((node = ebpt_first(&cookies_tree))) {
-                       cookie_entry = ebpt_entry(node, struct cookie_entry, node);
-                       eb_delete(&node->node);
-                       free(cookie_entry);
+               /* clear the cookies tree before passing to the next proxy */
+               while ((cookie_node = ebpt_first(&cookies_tree))) {
+                       ebpt_delete(cookie_node);
+                       free(cookie_node);
                }
        }
 }
@@ -91,7 +94,7 @@ int cfg_run_diagnostics()
 {
        int ret = 0;
 
-       check_server_cookies(&ret);
+       run_servers_diag(&ret);
 
        return ret;
 }